Skip to content

Commit c3bc981

Browse files
committed
Made ProgressView resizability optional through .resizable()
Not resizable by default, can be changed at runtime. Does not affect ProgressBarView
1 parent 6879d40 commit c3bc981

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

Examples/Sources/ControlsExample/ControlsApp.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct ControlsApp: App {
1717
@State var flavor: String? = nil
1818
@State var enabled = true
1919
@State var progressViewSize: Int = 10
20+
@State var isProgressViewResizable = true
2021

2122
var body: some Scene {
2223
WindowGroup("ControlsApp") {
@@ -70,8 +71,10 @@ struct ControlsApp: App {
7071
Text("Value: \(text)")
7172
}
7273

73-
Slider($progressViewSize, minimum: 0, maximum: 100)
74+
Toggle("Enable ProgressView resizability", active: $isProgressViewResizable)
75+
Slider($progressViewSize, minimum: 10, maximum: 100)
7476
ProgressView()
77+
.resizable(isProgressViewResizable)
7578
.frame(width: progressViewSize, height: progressViewSize)
7679

7780
VStack {
@@ -87,6 +90,7 @@ struct ControlsApp: App {
8790
Toggle(enabled ? "Disable all" : "Enable all", active: $enabled)
8891
.padding()
8992
}
93+
9094
}.defaultSize(width: 400, height: 600)
9195
}
9296
}

Sources/SwiftCrossUI/Views/ProgressView.swift

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ public struct ProgressView<Label: View>: View {
44
private var label: Label
55
private var progress: Double?
66
private var kind: Kind
7+
private var isSpinnerResizable: Bool = false
78

89
private enum Kind {
910
case spinner
@@ -23,7 +24,7 @@ public struct ProgressView<Label: View>: View {
2324
private var progressIndicator: some View {
2425
switch kind {
2526
case .spinner:
26-
ProgressSpinnerView()
27+
ProgressSpinnerView(isResizable: isSpinnerResizable)
2728
case .bar:
2829
ProgressBarView(value: progress)
2930
}
@@ -50,6 +51,25 @@ public struct ProgressView<Label: View>: View {
5051
self.kind = .bar
5152
self.progress = value.map(Double.init)
5253
}
54+
55+
/// Used to make a copy with applied changes.
56+
private init(
57+
label: Label,
58+
_ progress: Double?,
59+
kind: Kind,
60+
isSpinnerResizable: Bool
61+
) {
62+
self.label = label
63+
self.progress = progress
64+
self.kind = kind
65+
self.isSpinnerResizable = isSpinnerResizable
66+
}
67+
68+
/// Makes the ProgressView resizable.
69+
/// Only affects `Kind.spinner`.
70+
public func resizable(_ isResizable: Bool = true) -> Self {
71+
Self(label: label, progress, kind: kind, isSpinnerResizable: isResizable)
72+
}
5373
}
5474

5575
extension ProgressView where Label == EmptyView {
@@ -101,7 +121,10 @@ extension ProgressView where Label == Text {
101121
}
102122

103123
struct ProgressSpinnerView: ElementaryView {
104-
init() {}
124+
let isResizable: Bool
125+
init(isResizable: Bool = false) {
126+
self.isResizable = isResizable
127+
}
105128

106129
func asWidget<Backend: AppBackend>(backend: Backend) -> Backend.Widget {
107130
backend.createProgressSpinner()
@@ -115,6 +138,12 @@ struct ProgressSpinnerView: ElementaryView {
115138
dryRun: Bool
116139
) -> ViewUpdateResult {
117140
let naturalSize = backend.naturalSize(of: widget)
141+
guard isResizable else {
142+
// Required to reset its size when resizability
143+
// gets changed at runtime
144+
backend.setSize(of: widget, to: naturalSize)
145+
return ViewUpdateResult.leafView(size: ViewSize(fixedSize: naturalSize))
146+
}
118147
let min = max(min(proposedSize.x, proposedSize.y), 10)
119148
let size = SIMD2(
120149
min,

0 commit comments

Comments
 (0)