mirror of
https://github.com/laosb/CropImage.git
synced 2025-04-30 23:51:08 +00:00
fix(macos): remove monitor after use
This commit is contained in:
parent
246b20d079
commit
6b33bcdbe4
1 changed files with 23 additions and 13 deletions
|
@ -26,9 +26,10 @@ struct UnderlyingImageView: View {
|
||||||
@State private var tempScale: CGFloat = 1
|
@State private var tempScale: CGFloat = 1
|
||||||
@State private var tempRotation: Angle = .zero
|
@State private var tempRotation: Angle = .zero
|
||||||
@State private var scrolling: Bool = false
|
@State private var scrolling: Bool = false
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
@State private var isHovering: Bool = false
|
@State private var hovering: Bool = false
|
||||||
#endif
|
@State private var scrollMonitor: Any?
|
||||||
|
#endif
|
||||||
|
|
||||||
// When rotated odd multiples of 90 degrees, we need to switch width and height of the image in calculations.
|
// When rotated odd multiples of 90 degrees, we need to switch width and height of the image in calculations.
|
||||||
var isRotatedOddMultiplesOf90Deg: Bool {
|
var isRotatedOddMultiplesOf90Deg: Bool {
|
||||||
|
@ -91,17 +92,23 @@ struct UnderlyingImageView: View {
|
||||||
scale = min(widthScale, heightScale)
|
scale = min(widthScale, heightScale)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if os(macOS)
|
||||||
private func setupScrollMonitor() {
|
private func setupScrollMonitor() {
|
||||||
#if os(macOS)
|
scrollMonitor = NSEvent.addLocalMonitorForEvents(matching: .scrollWheel) { event in
|
||||||
NSEvent.addLocalMonitorForEvents(matching: .scrollWheel) {event in
|
if hovering {
|
||||||
if isHovering {
|
|
||||||
scrolling = true
|
scrolling = true
|
||||||
scale = scale + event.scrollingDeltaY/1000
|
scale = scale + event.scrollingDeltaY / 1000
|
||||||
}
|
}
|
||||||
return event
|
return event
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func removeScrollMonitor() {
|
||||||
|
if let scrollMonitor {
|
||||||
|
NSEvent.removeMonitor(scrollMonitor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
var imageView: Image {
|
var imageView: Image {
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
|
@ -116,9 +123,14 @@ struct UnderlyingImageView: View {
|
||||||
.gesture(dragGesture)
|
.gesture(dragGesture)
|
||||||
.gesture(magnificationgesture)
|
.gesture(magnificationgesture)
|
||||||
.gesture(rotationGesture)
|
.gesture(rotationGesture)
|
||||||
|
#if os(macOS)
|
||||||
.onAppear {
|
.onAppear {
|
||||||
setupScrollMonitor()
|
setupScrollMonitor()
|
||||||
}
|
}
|
||||||
|
.onDisappear {
|
||||||
|
removeScrollMonitor()
|
||||||
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
var dragGesture: some Gesture {
|
var dragGesture: some Gesture {
|
||||||
|
@ -173,11 +185,9 @@ struct UnderlyingImageView: View {
|
||||||
.onChange(of: rotation) { _ in
|
.onChange(of: rotation) { _ in
|
||||||
adjustToFulfillTargetFrame()
|
adjustToFulfillTargetFrame()
|
||||||
}
|
}
|
||||||
#if os(macOS)
|
#if os(macOS)
|
||||||
.onHover { hovering in
|
.onHover { hovering = $0 }
|
||||||
isHovering = hovering
|
#endif
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue