Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed Feb 22, 2024
1 parent 9e57fbe commit 8897bd2
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 30 deletions.
25 changes: 21 additions & 4 deletions Dev/Sources/SwiftUIDemo/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ struct ContentView: View {

@State private var fullScreenView: FullscreenIdentifiableView?

@State var stack = Mocks.makeEditingStack(image: Mocks.imageHorizontal())
@State var horizontalStack = Mocks.makeEditingStack(image: Mocks.imageHorizontal())
@State var verticalStack = Mocks.makeEditingStack(image: Mocks.imageVertical())

var body: some View {
NavigationView {
Expand All @@ -22,17 +23,33 @@ struct ContentView: View {
}
}

Section("Restoration") {
Section("Restoration Horizontal") {
Button("Crop") {
fullScreenView = .init {
DemoCropView(editingStack: { stack })
DemoCropView(editingStack: { horizontalStack })
}
}

Button("Masking") {
fullScreenView = .init {
DemoMaskingView {
stack
horizontalStack
}
}
}
}

Section("Restoration Vertical") {
Button("Crop") {
fullScreenView = .init {
DemoCropView(editingStack: { verticalStack })
}
}

Button("Masking") {
fullScreenView = .init {
DemoMaskingView {
verticalStack
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public struct PhotosCropRotating: View {
case .vertical: self = .horizontal
}
}

init(_ ratio: PixelAspectRatio) {
if ratio.width > ratio.height {
self = .horizontal
} else {
self = .vertical
}
}
}

@StateObject var editingStack: EditingStack
Expand All @@ -49,9 +57,6 @@ public struct PhotosCropRotating: View {
@State private var adjustmentAngle: EditingCrop.AdjustmentAngle?
@State private var croppingAspectRatio: PixelAspectRatio?

// TODO: use init value from original aspect ratio.
@State private var croppingAspectRationDirection: Direction = .horizontal

@State private var canReset: Bool = false
@State private var isFocusingAspectRatio: Bool = false

Expand All @@ -76,6 +81,8 @@ public struct PhotosCropRotating: View {
VStack {

HStack {

// rotation
Button(
action: {

Expand All @@ -94,7 +101,7 @@ public struct PhotosCropRotating: View {
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 22)
.foregroundStyle(rotation == .angle_0 ? .secondary : .primary)
.foregroundStyle(rotation == .angle_0 || rotation == nil ? .secondary : .primary)
}
)
.disabled(isLoading)
Expand All @@ -103,7 +110,7 @@ public struct PhotosCropRotating: View {

if editingStack.state.loadedState?.hasUncommitedChanges ?? false {
Button {
rotation = nil
rotation = .angle_0
adjustmentAngle = nil
croppingAspectRatio = nil

Expand All @@ -125,6 +132,7 @@ public struct PhotosCropRotating: View {

Spacer()

// aspect ratio
Button(
action: {
isFocusingAspectRatio.toggle()
Expand Down Expand Up @@ -278,18 +286,14 @@ public struct PhotosCropRotating: View {

let isDirectionButtonDisabled: Bool = {

guard let loaded = editingStack.state.loadedState else {
guard let _ = editingStack.state.loadedState else {
return true
}

guard let croppingAspectRatio else {
return true
}

if croppingAspectRatio == .init(loaded.imageSize) {
return true
}

if croppingAspectRatio == .square {
return true
}
Expand All @@ -303,9 +307,8 @@ public struct PhotosCropRotating: View {

// vertical
Button {
if croppingAspectRationDirection != .vertical {
if croppingAspectRatio.map({ Direction($0) }) != .vertical {
croppingAspectRatio = croppingAspectRatio?.swapped()
croppingAspectRationDirection = .vertical
}
} label: {
ZStack {
Expand All @@ -316,7 +319,7 @@ public struct PhotosCropRotating: View {
RoundedRectangle(cornerRadius: 2)
.fill(Color(white: 0.4, opacity: 1))

if isDirectionButtonDisabled == false, croppingAspectRationDirection == .vertical {
if isDirectionButtonDisabled == false, croppingAspectRatio.map({ Direction($0) }) == .vertical {
Image(systemName: "checkmark")
.resizable()
.aspectRatio(contentMode: .fit)
Expand All @@ -334,9 +337,8 @@ public struct PhotosCropRotating: View {

// horizontal
Button {
if croppingAspectRationDirection != .horizontal {
if croppingAspectRatio.map({ Direction($0) }) != .horizontal {
croppingAspectRatio = croppingAspectRatio?.swapped()
croppingAspectRationDirection = .horizontal
}
} label: {
ZStack {
Expand All @@ -347,7 +349,7 @@ public struct PhotosCropRotating: View {
RoundedRectangle(cornerRadius: 2)
.fill(Color(white: 0.4, opacity: 1))

if isDirectionButtonDisabled == false, croppingAspectRationDirection == .horizontal {
if isDirectionButtonDisabled == false, croppingAspectRatio.map({ Direction($0) }) == .horizontal {
Image(systemName: "checkmark")
.resizable()
.aspectRatio(contentMode: .fit)
Expand All @@ -370,14 +372,62 @@ public struct PhotosCropRotating: View {

Group {

AspectRationButton(
title: Text("ORIGINAL"),
isSelected: croppingAspectRatio == editingStack.state.loadedState.map { .init($0.imageSize) }
) {
guard let imageSize = editingStack.state.loadedState?.imageSize else {
return
let sourceDirection = editingStack.state.loadedState.map({ Direction(.init($0.imageSize)) })
let direction = croppingAspectRatio.map({ Direction($0) }) ?? sourceDirection

switch sourceDirection {
case .none:
EmptyView()
case .vertical:
switch direction {
case .none:
EmptyView()
case .vertical:
AspectRationButton(
title: Text("ORIGINAL"),
isSelected: croppingAspectRatio == editingStack.state.loadedState.map { .init($0.imageSize) }
) {
guard let imageSize = editingStack.state.loadedState?.imageSize else {
return
}
croppingAspectRatio = PixelAspectRatio.init(imageSize)
}
case .horizontal:
AspectRationButton(
title: Text("ORIGINAL"),
isSelected: croppingAspectRatio == editingStack.state.loadedState.map { .init($0.imageSize).swapped() }
) {
guard let imageSize = editingStack.state.loadedState?.imageSize else {
return
}
croppingAspectRatio = PixelAspectRatio.init(imageSize).swapped()
}
}
case .horizontal:
switch direction {
case .none:
EmptyView()
case .vertical:
AspectRationButton(
title: Text("ORIGINAL"),
isSelected: croppingAspectRatio == editingStack.state.loadedState.map { .init($0.imageSize).swapped() }
) {
guard let imageSize = editingStack.state.loadedState?.imageSize else {
return
}
croppingAspectRatio = PixelAspectRatio.init(imageSize).swapped()
}
case .horizontal:
AspectRationButton(
title: Text("ORIGINAL"),
isSelected: croppingAspectRatio == editingStack.state.loadedState.map { .init($0.imageSize) }
) {
guard let imageSize = editingStack.state.loadedState?.imageSize else {
return
}
croppingAspectRatio = PixelAspectRatio.init(imageSize)
}
}
croppingAspectRatio = .init(imageSize)
}

AspectRationButton(
Expand All @@ -395,15 +445,18 @@ public struct PhotosCropRotating: View {
}

ForEach(Self.horizontalRectangleApectRatios) { ratio in
switch croppingAspectRationDirection {
case .vertical:

switch direction {
case .none:
EmptyView()
case .vertical?:
AspectRationButton(
title: Text("\(Int(ratio.height)):\(Int(ratio.width))"),
isSelected: croppingAspectRatio == ratio.swapped()
) {
croppingAspectRatio = ratio.swapped()
}
case .horizontal:
case .horizontal?:
AspectRationButton(
title: Text("\(Int(ratio.width)):\(Int(ratio.height))"),
isSelected: croppingAspectRatio == ratio
Expand Down

0 comments on commit 8897bd2

Please sign in to comment.