From ea5a5354fbd19fedca71826bc9dd2781426a16b5 Mon Sep 17 00:00:00 2001 From: Ahnaf Mahmud <44692189+infinitepower18@users.noreply.github.com> Date: Tue, 13 Aug 2024 20:30:21 +0100 Subject: [PATCH 1/9] Allow zoom using scroll wheel --- Sources/CropImage/UnderlyingImageView.swift | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Sources/CropImage/UnderlyingImageView.swift b/Sources/CropImage/UnderlyingImageView.swift index e49de8a..382b290 100644 --- a/Sources/CropImage/UnderlyingImageView.swift +++ b/Sources/CropImage/UnderlyingImageView.swift @@ -81,6 +81,15 @@ struct UnderlyingImageView: View { scale = min(widthScale, heightScale) } + private func setupScrollMonitor() { + #if os(macOS) + NSEvent.addLocalMonitorForEvents(matching: .scrollWheel) {event in + scale = scale + event.scrollingDeltaY/1000 + return event + } + #endif + } + var imageView: Image { #if os(macOS) Image(nsImage: image) @@ -94,6 +103,9 @@ struct UnderlyingImageView: View { .gesture(dragGesture) .gesture(magnificationgesture) .gesture(rotationGesture) + .onAppear { + setupScrollMonitor() + } } var dragGesture: some Gesture { From 672ec51d3ee1dc6c8a9c62ce318a9004da192dd9 Mon Sep 17 00:00:00 2001 From: infinitepower18 <44692189+infinitepower18@users.noreply.github.com> Date: Tue, 13 Aug 2024 21:02:07 +0100 Subject: [PATCH 2/9] only allow when hovered --- Sources/CropImage/UnderlyingImageView.swift | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Sources/CropImage/UnderlyingImageView.swift b/Sources/CropImage/UnderlyingImageView.swift index 382b290..2c87225 100644 --- a/Sources/CropImage/UnderlyingImageView.swift +++ b/Sources/CropImage/UnderlyingImageView.swift @@ -25,6 +25,9 @@ struct UnderlyingImageView: View { @State private var tempOffset: CGSize = .zero @State private var tempScale: CGFloat = 1 @State private var tempRotation: Angle = .zero + #if os(macOS) + @State private var isHovering: Bool = false + #endif // When rotated odd multiples of 90 degrees, we need to switch width and height of the image in calculations. var isRotatedOddMultiplesOf90Deg: Bool { @@ -84,7 +87,9 @@ struct UnderlyingImageView: View { private func setupScrollMonitor() { #if os(macOS) NSEvent.addLocalMonitorForEvents(matching: .scrollWheel) {event in - scale = scale + event.scrollingDeltaY/1000 + if isHovering { + scale = scale + event.scrollingDeltaY/1000 + } return event } #endif @@ -160,6 +165,11 @@ struct UnderlyingImageView: View { .onChange(of: rotation) { _ in adjustToFulfillTargetFrame() } + #if os(macOS) + .onHover { hovering in + isHovering = hovering + } + #endif } } From 246b20d07998f7bbe60f8cf7f22944a57dc915bc Mon Sep 17 00:00:00 2001 From: infinitepower18 <44692189+infinitepower18@users.noreply.github.com> Date: Wed, 14 Aug 2024 22:24:08 +0100 Subject: [PATCH 3/9] Disable spring animation for scroll zoom due to performance issue --- Sources/CropImage/UnderlyingImageView.swift | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Sources/CropImage/UnderlyingImageView.swift b/Sources/CropImage/UnderlyingImageView.swift index 2c87225..6d7ab11 100644 --- a/Sources/CropImage/UnderlyingImageView.swift +++ b/Sources/CropImage/UnderlyingImageView.swift @@ -25,6 +25,7 @@ struct UnderlyingImageView: View { @State private var tempOffset: CGSize = .zero @State private var tempScale: CGFloat = 1 @State private var tempRotation: Angle = .zero + @State private var scrolling: Bool = false #if os(macOS) @State private var isHovering: Bool = false #endif @@ -69,9 +70,15 @@ struct UnderlyingImageView: View { clampedOffset.height = clampedOffset.height.clamped(to: yOffsetBounds(at: clampedScale)) if clampedScale != scale || clampedOffset != offset { - withAnimation(.interactiveSpring()) { + if scrolling { scale = clampedScale offset = clampedOffset + scrolling = false + } else { + withAnimation(.interactiveSpring()) { + scale = clampedScale + offset = clampedOffset + } } } } @@ -88,6 +95,7 @@ struct UnderlyingImageView: View { #if os(macOS) NSEvent.addLocalMonitorForEvents(matching: .scrollWheel) {event in if isHovering { + scrolling = true scale = scale + event.scrollingDeltaY/1000 } return event From 6b33bcdbe4c1caf2cebc1cc2ff526e903a5f830b Mon Sep 17 00:00:00 2001 From: Shibo Lyu Date: Wed, 18 Dec 2024 15:49:02 +0800 Subject: [PATCH 4/9] fix(macos): remove monitor after use --- Sources/CropImage/UnderlyingImageView.swift | 36 +++++++++++++-------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/Sources/CropImage/UnderlyingImageView.swift b/Sources/CropImage/UnderlyingImageView.swift index 6d7ab11..29f8543 100644 --- a/Sources/CropImage/UnderlyingImageView.swift +++ b/Sources/CropImage/UnderlyingImageView.swift @@ -26,9 +26,10 @@ struct UnderlyingImageView: View { @State private var tempScale: CGFloat = 1 @State private var tempRotation: Angle = .zero @State private var scrolling: Bool = false - #if os(macOS) - @State private var isHovering: Bool = false - #endif +#if os(macOS) + @State private var hovering: Bool = false + @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. var isRotatedOddMultiplesOf90Deg: Bool { @@ -91,17 +92,23 @@ struct UnderlyingImageView: View { scale = min(widthScale, heightScale) } +#if os(macOS) private func setupScrollMonitor() { - #if os(macOS) - NSEvent.addLocalMonitorForEvents(matching: .scrollWheel) {event in - if isHovering { + scrollMonitor = NSEvent.addLocalMonitorForEvents(matching: .scrollWheel) { event in + if hovering { scrolling = true - scale = scale + event.scrollingDeltaY/1000 + scale = scale + event.scrollingDeltaY / 1000 } return event } - #endif } + + private func removeScrollMonitor() { + if let scrollMonitor { + NSEvent.removeMonitor(scrollMonitor) + } + } +#endif var imageView: Image { #if os(macOS) @@ -116,9 +123,14 @@ struct UnderlyingImageView: View { .gesture(dragGesture) .gesture(magnificationgesture) .gesture(rotationGesture) +#if os(macOS) .onAppear { setupScrollMonitor() } + .onDisappear { + removeScrollMonitor() + } +#endif } var dragGesture: some Gesture { @@ -173,11 +185,9 @@ struct UnderlyingImageView: View { .onChange(of: rotation) { _ in adjustToFulfillTargetFrame() } - #if os(macOS) - .onHover { hovering in - isHovering = hovering - } - #endif +#if os(macOS) + .onHover { hovering = $0 } +#endif } } From 54861801e6530cd9b82c6263ca5eeeb099979c44 Mon Sep 17 00:00:00 2001 From: Shibo Lyu Date: Wed, 18 Dec 2024 16:06:40 +0800 Subject: [PATCH 5/9] chore: convert to preview macro --- Sources/CropImage/CropImageView.swift | 24 +++++++++---------- Sources/CropImage/DefaultCutHoleShape.swift | 26 ++++++++++----------- Sources/CropImage/DefaultCutHoleView.swift | 13 +++++------ Sources/CropImage/UnderlyingImageView.swift | 12 ++++------ 4 files changed, 34 insertions(+), 41 deletions(-) diff --git a/Sources/CropImage/CropImageView.swift b/Sources/CropImage/CropImageView.swift index 2d6d558..88e523f 100644 --- a/Sources/CropImage/CropImageView.swift +++ b/Sources/CropImage/CropImageView.swift @@ -225,15 +225,15 @@ public struct CropImageView: View { } } -struct CropImageView_Previews: PreviewProvider { +#Preview { struct PreviewView: View { @State private var targetSize: CGSize = .init(width: 100, height: 100) @State private var result: Result? = nil - + var body: some View { VStack { CropImageView( - image: .init(contentsOfFile: "/Users/laosb/Downloads/png.png")!, + image: .init(contentsOf: URL(string: "file:///System/Library/Desktop%20Pictures/Hello%20Metallic%20Blue.heic")!)!, targetSize: targetSize ) { result = $0 @@ -262,18 +262,16 @@ struct CropImageView_Previews: PreviewProvider { } } header: { Text("Result") } } - #if os(macOS) +#if os(macOS) .formStyle(.grouped) - #endif +#endif } } } - - static var previews: some View { - PreviewView() - #if os(macOS) - .frame(width: 500) - .frame(minHeight: 600) - #endif - } + + return PreviewView() +#if os(macOS) + .frame(width: 500) + .frame(minHeight: 600) +#endif } diff --git a/Sources/CropImage/DefaultCutHoleShape.swift b/Sources/CropImage/DefaultCutHoleShape.swift index 3a8c93e..9b55369 100644 --- a/Sources/CropImage/DefaultCutHoleShape.swift +++ b/Sources/CropImage/DefaultCutHoleShape.swift @@ -44,20 +44,18 @@ struct DefaultCutHoleShape: Shape { } } -struct DefaultCutHoleShape_Previews: PreviewProvider { - static var previews: some View { - VStack { - DefaultCutHoleShape(size: .init(width: 100, height: 100)) - .fill(style: FillStyle(eoFill: true)) - .foregroundColor(.black.opacity(0.6)) - } - .previewDisplayName("Default") - VStack { - DefaultCutHoleShape(size: .init(width: 100, height: 100), isCircular: true) - .fill(style: FillStyle(eoFill: true)) - .foregroundColor(.black.opacity(0.6)) - } - .previewDisplayName("Circular") +#Preview("Default") { + VStack { + DefaultCutHoleShape(size: .init(width: 100, height: 100)) + .fill(style: FillStyle(eoFill: true)) + .foregroundColor(.black.opacity(0.6)) } } +#Preview("Circular") { + VStack { + DefaultCutHoleShape(size: .init(width: 100, height: 100), isCircular: true) + .fill(style: FillStyle(eoFill: true)) + .foregroundColor(.black.opacity(0.6)) + } +} diff --git a/Sources/CropImage/DefaultCutHoleView.swift b/Sources/CropImage/DefaultCutHoleView.swift index 9323e7d..e9e8664 100644 --- a/Sources/CropImage/DefaultCutHoleView.swift +++ b/Sources/CropImage/DefaultCutHoleView.swift @@ -61,11 +61,10 @@ public struct DefaultCutHoleView: View { } } -struct DefaultCutHoleView_Previews: PreviewProvider { - static var previews: some View { - DefaultCutHoleView(targetSize: .init(width: 100, height: 100)) - .previewDisplayName("Default") - DefaultCutHoleView(targetSize: .init(width: 100, height: 100), isCircular: true) - .previewDisplayName("Circular") - } +#Preview("Default") { + DefaultCutHoleView(targetSize: .init(width: 100, height: 100)) +} + +#Preview("Circular") { + DefaultCutHoleView(targetSize: .init(width: 100, height: 100), isCircular: true) } diff --git a/Sources/CropImage/UnderlyingImageView.swift b/Sources/CropImage/UnderlyingImageView.swift index 29f8543..9d1ee3b 100644 --- a/Sources/CropImage/UnderlyingImageView.swift +++ b/Sources/CropImage/UnderlyingImageView.swift @@ -191,18 +191,18 @@ struct UnderlyingImageView: View { } } -struct MoveAndScalableImageView_Previews: PreviewProvider { +#Preview { struct PreviewView: View { @State private var offset: CGSize = .zero @State private var scale: CGFloat = 1 @State private var rotation: Angle = .zero - + var body: some View { UnderlyingImageView( offset: $offset, scale: $scale, rotation: $rotation, - image: .init(contentsOfFile: "/Users/laosb/Downloads/png.png")!, + image: .init(contentsOf: URL(string: "file:///System/Library/Desktop%20Pictures/Hello%20Metallic%20Blue.heic")!)!, viewSize: .init(width: 200, height: 100), targetSize: .init(width: 100, height: 100), fulfillTargetFrame: true @@ -210,8 +210,6 @@ struct MoveAndScalableImageView_Previews: PreviewProvider { .frame(width: 200, height: 100) } } - - static var previews: some View { - PreviewView() - } + + return PreviewView() } From a9672b8a33ed7b28993cae0e749b7b5964a0ee19 Mon Sep 17 00:00:00 2001 From: Shibo Lyu Date: Wed, 18 Dec 2024 16:11:02 +0800 Subject: [PATCH 6/9] feat: supports Swift 6 --- Package.swift | 3 ++- Sources/CropImage/CropImageView.swift | 14 +++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Package.swift b/Package.swift index f88e851..5fa902a 100644 --- a/Package.swift +++ b/Package.swift @@ -26,5 +26,6 @@ let package = Package( .target( name: "CropImage", dependencies: []) - ] + ], + swiftLanguageVersions: [.version("6"), .v5] ) diff --git a/Sources/CropImage/CropImageView.swift b/Sources/CropImage/CropImageView.swift index 88e523f..4bed6f2 100644 --- a/Sources/CropImage/CropImageView.swift +++ b/Sources/CropImage/CropImageView.swift @@ -19,7 +19,7 @@ public struct CropImageView: View { /// - scale: The scale binding of the image. /// - rotation: The rotation binding of the image. /// - crop: An async function to trigger crop action. Result will be delivered via ``onCrop``. - public typealias ControlClosure = ( + public typealias ControlClosure = ( _ offset: Binding, _ scale: Binding, _ rotation: Binding, @@ -30,7 +30,7 @@ public struct CropImageView: View { /// /// - Parameters: /// - targetSize: The size of the cut hole. - public typealias CutHoleClosure = (_ targetSize: CGSize) -> CutHole + public typealias CutHoleClosure = (_ targetSize: CGSize) -> CutHole /// Errors that could happen during the cropping process. public enum CropError: Error { @@ -66,8 +66,8 @@ public struct CropImageView: View { /// /// The error should be a ``CropError``. public var onCrop: (Result) -> Void - var controls: ControlClosure - var cutHole: CutHoleClosure + var controls: ControlClosure + var cutHole: CutHoleClosure /// Create a ``CropImageView`` with a custom controls view and a custom cut hole. public init( image: PlatformImage, @@ -75,8 +75,8 @@ public struct CropImageView: View { targetScale: CGFloat = 1, fulfillTargetFrame: Bool = true, onCrop: @escaping (Result) -> Void, - @ViewBuilder controls: @escaping ControlClosure, - @ViewBuilder cutHole: @escaping CutHoleClosure + @ViewBuilder controls: @escaping ControlClosure, + @ViewBuilder cutHole: @escaping CutHoleClosure ) { self.image = image self.targetSize = targetSize @@ -92,7 +92,7 @@ public struct CropImageView: View { targetScale: CGFloat = 1, fulfillTargetFrame: Bool = true, onCrop: @escaping (Result) -> Void, - @ViewBuilder controls: @escaping ControlClosure + @ViewBuilder controls: @escaping ControlClosure ) where CutHole == DefaultCutHoleView { self.image = image self.targetSize = targetSize From 7042102108a0c260d18c2869a3c8fa0d602c9a99 Mon Sep 17 00:00:00 2001 From: Shibo Lyu Date: Wed, 18 Dec 2024 16:49:24 +0800 Subject: [PATCH 7/9] fix: build on non-macOS. --- Sources/CropImage/CropImageView.swift | 2 +- Sources/CropImage/PlatformImage.swift | 7 +++++++ Sources/CropImage/UnderlyingImageView.swift | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Sources/CropImage/CropImageView.swift b/Sources/CropImage/CropImageView.swift index 4bed6f2..9cd2389 100644 --- a/Sources/CropImage/CropImageView.swift +++ b/Sources/CropImage/CropImageView.swift @@ -233,7 +233,7 @@ public struct CropImageView: View { var body: some View { VStack { CropImageView( - image: .init(contentsOf: URL(string: "file:///System/Library/Desktop%20Pictures/Hello%20Metallic%20Blue.heic")!)!, + image: .previewImage, targetSize: targetSize ) { result = $0 diff --git a/Sources/CropImage/PlatformImage.swift b/Sources/CropImage/PlatformImage.swift index 831e93b..7963809 100644 --- a/Sources/CropImage/PlatformImage.swift +++ b/Sources/CropImage/PlatformImage.swift @@ -13,10 +13,17 @@ import AppKit /// /// On macOS, it's `NSImage` and on iOS/visionOS it's `UIImage`. public typealias PlatformImage = NSImage +extension PlatformImage { + static let previewImage: PlatformImage = .init(contentsOf: URL(string: "file:///System/Library/Desktop%20Pictures/Hello%20Metallic%20Blue.heic")!)! +} #else import UIKit /// The image object type, aliased to each platform. /// /// On macOS, it's `NSImage` and on iOS/visionOS it's `UIImage`. public typealias PlatformImage = UIImage +extension PlatformImage { + // This doesn't really work, but at least passes build. + static let previewImage: PlatformImage = .init(contentsOfFile: "/System/Library/Desktop Pictures/Hello Metallic Blue.heic")! +} #endif diff --git a/Sources/CropImage/UnderlyingImageView.swift b/Sources/CropImage/UnderlyingImageView.swift index 9d1ee3b..882ade4 100644 --- a/Sources/CropImage/UnderlyingImageView.swift +++ b/Sources/CropImage/UnderlyingImageView.swift @@ -202,7 +202,7 @@ struct UnderlyingImageView: View { offset: $offset, scale: $scale, rotation: $rotation, - image: .init(contentsOf: URL(string: "file:///System/Library/Desktop%20Pictures/Hello%20Metallic%20Blue.heic")!)!, + image: .previewImage, viewSize: .init(width: 200, height: 100), targetSize: .init(width: 100, height: 100), fulfillTargetFrame: true From 481f59cf412f41641d4b737d8fbb51433031ce03 Mon Sep 17 00:00:00 2001 From: Shibo Lyu Date: Fri, 20 Dec 2024 14:54:32 +0800 Subject: [PATCH 8/9] fix: macos build fixes #4 --- Sources/CropImage/PlatformImage.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/CropImage/PlatformImage.swift b/Sources/CropImage/PlatformImage.swift index 7963809..ab243b5 100644 --- a/Sources/CropImage/PlatformImage.swift +++ b/Sources/CropImage/PlatformImage.swift @@ -14,7 +14,7 @@ import AppKit /// On macOS, it's `NSImage` and on iOS/visionOS it's `UIImage`. public typealias PlatformImage = NSImage extension PlatformImage { - static let previewImage: PlatformImage = .init(contentsOf: URL(string: "file:///System/Library/Desktop%20Pictures/Hello%20Metallic%20Blue.heic")!)! + @MainActor static let previewImage: PlatformImage = .init(contentsOf: URL(string: "file:///System/Library/Desktop%20Pictures/Hello%20Metallic%20Blue.heic")!)! } #else import UIKit From 9a8b486f5529768600a885d3e53cde19867a7098 Mon Sep 17 00:00:00 2001 From: Shibo Lyu Date: Tue, 24 Dec 2024 14:18:08 +0800 Subject: [PATCH 9/9] Create FUNDING.yml --- .github/FUNDING.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .github/FUNDING.yml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..b1de7a2 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,2 @@ +github: [laosb] +buy_me_a_coffee: laosb