Skip to content

Commit

Permalink
Update InsideOverlay support SwiftUI (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii authored Feb 22, 2024
1 parent fec3654 commit 1105e3f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,55 @@ extension CropView {
@available(iOS 14, *)
open class SwiftUICropInsideOverlay<Content: View>: CropInsideOverlayBase {

private let controller: UIHostingController<Content>
private let controller: UIHostingController<Container>
private let proxy: Proxy

public init(@ViewBuilder content: @escaping (CropView.State.AdjustmentKind?) -> Content) {

self.proxy = .init()
self.controller = .init(rootView: Container(proxy: proxy, content: content))

controller.view.backgroundColor = .clear
controller.view.preservesSuperviewLayoutMargins = false

public init(controller: UIHostingController<Content>) {
self.controller = controller
super.init()
addSubview(controller.view)
AutoLayoutTools.setEdge(controller.view, self)
}

open override func didBeginAdjustment(kind: CropView.State.AdjustmentKind) {
proxy.activeKind = kind
}

open override func didEndAdjustment(kind: CropView.State.AdjustmentKind) {
proxy.activeKind = nil
}

private final class Proxy: ObservableObject {

@Published var activeKind: CropView.State.AdjustmentKind?

}

private struct Container: View {

@ObservedObject var proxy: Proxy

private let content: (CropView.State.AdjustmentKind?) -> Content

public init(
proxy: Proxy,
content: @escaping (CropView.State.AdjustmentKind?) -> Content
) {
self.content = content
self.proxy = proxy
}

var body: some View {
content(proxy.activeKind)
}
}

}

public final class RuleOfThirdsView: PixelEditorCodeBasedView {
Expand Down
15 changes: 12 additions & 3 deletions Sources/BrightroomUI/Shared/Components/Crop/CropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,18 @@ import BrightroomEngine
public final class CropView: UIView, UIScrollViewDelegate {

public struct State: Equatable {
public enum AdjustmentKind: Equatable {
case scrollView
case guide

public struct AdjustmentKind: OptionSet {

public var rawValue: Int = 0

public init(rawValue: Int) {
self.rawValue = rawValue
}

public static let scrollView = AdjustmentKind(rawValue: 1 << 0)
public static let guide = AdjustmentKind(rawValue: 1 << 1)

}

public fileprivate(set) var proposedCrop: EditingCrop?
Expand Down
34 changes: 16 additions & 18 deletions Sources/BrightroomUI/Shared/Components/Crop/SwiftUICropView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,45 +52,43 @@ public final class _PixelEditor_WrapperViewController<BodyView: UIView>: UIViewC
*/
@available(iOS 14, *)
public struct SwiftUICropView: UIViewControllerRepresentable {

public typealias UIViewControllerType = _PixelEditor_WrapperViewController<CropView>

private let cropInsideOverlay: AnyView?
private let cropInsideOverlay: ((CropView.State.AdjustmentKind?) -> AnyView)?
private let editingStack: EditingStack

private var _rotation: EditingCrop.Rotation?
private var _adjustmentAngle: EditingCrop.AdjustmentAngle?
private var _croppingAspectRatio: PixelAspectRatio?

public init(
public init<InsideOverlay: View>(
editingStack: EditingStack,
cropInsideOverlay: AnyView? = nil
@ViewBuilder cropInsideOverlay: @escaping (CropView.State.AdjustmentKind?) -> InsideOverlay
) {
self.cropInsideOverlay = cropInsideOverlay
self.editingStack = editingStack
self.cropInsideOverlay = { AnyView(cropInsideOverlay($0)) }
}


public init(
editingStack: EditingStack
) {
self.cropInsideOverlay = nil
self.editingStack = editingStack
}

public func makeUIViewController(context: Context) -> _PixelEditor_WrapperViewController<CropView> {

let view = CropView(editingStack: editingStack)

view.isAutoApplyEditingStackEnabled = true

let controller = _PixelEditor_WrapperViewController.init(bodyView: view)

if let cropInsideOverlay = cropInsideOverlay {

let hosting = UIHostingController.init(rootView: cropInsideOverlay)

hosting.view.backgroundColor = .clear
hosting.view.preservesSuperviewLayoutMargins = false

view.setCropInsideOverlay(CropView.SwiftUICropInsideOverlay(controller: hosting))

controller.addChild(hosting)
hosting.didMove(toParent: controller)
view.setCropInsideOverlay(CropView.SwiftUICropInsideOverlay(content: cropInsideOverlay))
}

return controller
}

Expand Down

0 comments on commit 1105e3f

Please sign in to comment.