Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add adjustmentKind to CropView.State #235

Merged
merged 2 commits into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions Dev/Sources/SwiftUIDemo/DemoCropView2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ struct DemoCropView2: View {
@StateObject var editingStack: EditingStack
@State var resultImage: ResultImage?
@State var angle: EditingCrop.AdjustmentAngle = .zero
@State var baselineAngle: EditingCrop.AdjustmentAngle = .zero
@State var isDragging: Bool = false

init(
editingStack: @escaping () -> EditingStack
Expand Down Expand Up @@ -51,9 +53,18 @@ struct DemoCropView2: View {
cropOutsideOverlay: { kind in
Rectangle()
.fill(kind == nil ? Color.white : Color.white.opacity(0.6))
},
stateHandler: { state in
state.ifChanged(\.adjustmentKind).do { kind in
if kind.isEmpty {
isDragging = false
} else {
isDragging = true
}
}
}
)
.adjustmentAngle(angle)
.adjustmentAngle(angle + baselineAngle)
.croppingAspectRatio(.init(width: 1, height: 1.4))
.frame(height: 300)
.clipped()
Expand All @@ -67,7 +78,18 @@ struct DemoCropView2: View {

Spacer()

Slider(value: $angle.degrees, in: -45.0...45.0, step: 1)
HStack {
Slider(value: $angle.degrees, in: -45.0...45.0, step: 1)
.disabled(isDragging)
Button(action: {
baselineAngle -= .degrees(90)
}, label: {
Text("Rotate")
})
.disabled(isDragging)
}
.disabled(isDragging)
.padding(24)

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ extension CropView {
var updating: () -> Void = {}
var didChange: () -> Void = {}

var didUpdateAdjustmentKind: (CropView.State.AdjustmentKind) -> Void = { _ in }

private let topLeftControlPointView = TapExpandedView(horizontal: 16, vertical: 16)
private let topRightControlPointView = TapExpandedView(horizontal: 16, vertical: 16)
private let bottomLeftControlPointView = TapExpandedView(horizontal: 16, vertical: 16)
Expand Down Expand Up @@ -340,11 +342,13 @@ extension CropView {
func willBeginScrollViewAdjustment() {
cropInsideOverlay?.didBeginAdjustment(kind: .scrollView)
cropOutsideOverlay?.didBeginAdjustment(kind: .scrollView)
didUpdateAdjustmentKind(.scrollView)
}

func didEndScrollViewAdjustment() {
cropInsideOverlay?.didEndAdjustment(kind: .scrollView)
cropOutsideOverlay?.didEndAdjustment(kind: .scrollView)
didUpdateAdjustmentKind([])
}

@inline(__always)
Expand Down Expand Up @@ -384,6 +388,7 @@ extension CropView {
willChange()
cropInsideOverlay?.didBeginAdjustment(kind: .guide)
cropOutsideOverlay?.didBeginAdjustment(kind: .guide)
didUpdateAdjustmentKind(.guide)
}

private func onGestureTrackingChanged() {
Expand All @@ -399,6 +404,7 @@ extension CropView {
didChange()
cropInsideOverlay?.didEndAdjustment(kind: .guide)
cropOutsideOverlay?.didEndAdjustment(kind: .guide)
didUpdateAdjustmentKind([])
}

private var widthConstraint: NSLayoutConstraint!
Expand Down
20 changes: 16 additions & 4 deletions Sources/BrightroomUI/Shared/Components/Crop/CropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public final class CropView: UIView, UIScrollViewDelegate {

public fileprivate(set) var frame: CGRect = .zero

public fileprivate(set) var adjustmentKind: AdjustmentKind = []

fileprivate var layoutVersion: UInt64 = 0

/**
Expand Down Expand Up @@ -270,9 +272,12 @@ public final class CropView: UIView, UIScrollViewDelegate {
scrollView.addSubview(imagePlatterView)
scrollView.delegate = self

guideView.didChange = { [weak self] in
guideView.willChange = { [weak self] in
guard let self = self else { return }
self.didChangeGuideViewWithDelay()
self.willChangeGuideView()
store.commit {
$0.adjustmentKind = .guide
}
}

guideView.updating = { [weak self] in
Expand All @@ -283,9 +288,16 @@ public final class CropView: UIView, UIScrollViewDelegate {
// updateScrollViewInset(crop: currentProposedCrop)
}

guideView.willChange = { [weak self] in
guideView.didChange = { [weak self] in
guard let self = self else { return }
self.willChangeGuideView()
self.didChangeGuideViewWithDelay()
}

guideView.didUpdateAdjustmentKind = { [weak self] kind in
guard let self else { return }
self.store.commit {
$0.adjustmentKind = kind
}
}

#if false
Expand Down
Loading