Skip to content

Commit 9e29399

Browse files
committed
Update toggleStyle to be an environment modifier
This means that toggleStyle can now be applied to whole view hierarchies at a time.
1 parent 0d74e9e commit 9e29399

File tree

9 files changed

+30
-21
lines changed

9 files changed

+30
-21
lines changed

Sources/AppKitBackend/AppKitBackend.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public final class AppKitBackend: AppBackend {
2020
public let defaultTableCellVerticalPadding = 4
2121
public let defaultPaddingAmount = 10
2222
public let requiresToggleSwitchSpacer = false
23-
public let defaultToggleStyle = ToggleStyle.button
2423
public let requiresImageUpdateOnScaleFactorChange = false
2524
public let menuImplementationStyle = MenuImplementationStyle.dynamicPopover
2625
public let canRevealFiles = true

Sources/Gtk3Backend/Gtk3Backend.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public final class Gtk3Backend: AppBackend {
3333
public let defaultPaddingAmount = 10
3434
public let requiresToggleSwitchSpacer = false
3535
public let scrollBarWidth = 0
36-
public let defaultToggleStyle = ToggleStyle.button
3736
public let requiresImageUpdateOnScaleFactorChange = true
3837
public let menuImplementationStyle = MenuImplementationStyle.dynamicPopover
3938
public let canRevealFiles = true

Sources/GtkBackend/GtkBackend.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public final class GtkBackend: AppBackend {
3232
public let defaultPaddingAmount = 10
3333
public let scrollBarWidth = 0
3434
public let requiresToggleSwitchSpacer = false
35-
public let defaultToggleStyle = ToggleStyle.button
3635
public let requiresImageUpdateOnScaleFactorChange = false
3736
public let menuImplementationStyle = MenuImplementationStyle.dynamicPopover
3837
public let canRevealFiles = true

Sources/SwiftCrossUI/Backend/AppBackend.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,6 @@ public protocol AppBackend {
7575
var scrollBarWidth: Int { get }
7676
/// If `true`, a toggle in the ``ToggleStyle/switch`` style grows to fill its parent container.
7777
var requiresToggleSwitchSpacer: Bool { get }
78-
/// The default style for toggles.
79-
var defaultToggleStyle: ToggleStyle { get }
8078
/// If `true`, all images in a window will get updated when the window's
8179
/// scale factor changes (``EnvironmentValues/windowScaleFactor``).
8280
///

Sources/SwiftCrossUI/Environment/EnvironmentValues.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ public struct EnvironmentValues {
6060
/// The style of list to use.
6161
package var listStyle: ListStyle
6262

63+
/// The style of toggle to use.
64+
public var toggleStyle: ToggleStyle
65+
6366
// Backing storage for extensible subscript
6467
private var extraValues: [ObjectIdentifier: Any]
6568

@@ -163,6 +166,7 @@ public struct EnvironmentValues {
163166
window = nil
164167
extraValues = [:]
165168
listStyle = .default
169+
toggleStyle = .button
166170
isEnabled = true
167171
}
168172

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
extension View {
2+
/// Sets the style of the toggle.
3+
public func toggleStyle(_ toggleStyle: ToggleStyle) -> some View {
4+
return EnvironmentModifier(self) { environment in
5+
return environment.with(\.toggleStyle, toggleStyle)
6+
}
7+
}
8+
}
Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
/// A control for toggling between two values (usually representing on and off).
12
public struct Toggle: View {
23
@Environment(\.backend) var backend
4+
@Environment(\.toggleStyle) var toggleStyle
35

4-
/// The style of toggle shown.
5-
var selectedToggleStyle: ToggleStyle?
66
/// The label to be shown on or beside the toggle.
77
var label: String
88
/// Whether the toggle is active or not.
@@ -15,7 +15,7 @@ public struct Toggle: View {
1515
}
1616

1717
public var body: some View {
18-
switch selectedToggleStyle ?? backend.defaultToggleStyle {
18+
switch toggleStyle.style {
1919
case .switch:
2020
HStack {
2121
Text(label)
@@ -32,16 +32,18 @@ public struct Toggle: View {
3232
}
3333
}
3434

35-
extension Toggle {
36-
/// Sets the style of the toggle.
37-
public func toggleStyle(_ selectedToggleStyle: ToggleStyle) -> Toggle {
38-
var toggle = self
39-
toggle.selectedToggleStyle = selectedToggleStyle
40-
return toggle
41-
}
42-
}
35+
/// A style of toggle.
36+
public struct ToggleStyle {
37+
package var style: Style
4338

44-
public enum ToggleStyle {
45-
case `switch`
46-
case button
39+
/// A toggle switch.
40+
public static let `switch` = Self(style: .switch)
41+
/// A toggle button. Generally looks like a regular button when off and an
42+
/// accented button when on.
43+
public static let button = Self(style: .button)
44+
45+
package enum Style {
46+
case `switch`
47+
case button
48+
}
4749
}

Sources/UIKitBackend/UIKitBackend.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public final class UIKitBackend: AppBackend {
1313
public let scrollBarWidth = 0
1414
public let defaultPaddingAmount = 15
1515
public let requiresToggleSwitchSpacer = true
16-
public let defaultToggleStyle = ToggleStyle.switch
1716
public let menuImplementationStyle = MenuImplementationStyle.menuButton
1817

1918
// TODO: When tables are supported, update these
@@ -76,6 +75,8 @@ public final class UIKitBackend: AppBackend {
7675
design: .default
7776
)
7877

78+
environment.toggleStyle = .switch
79+
7980
switch UITraitCollection.current.userInterfaceStyle {
8081
case .light:
8182
environment.colorScheme = .light

Sources/WinUIBackend/WinUIBackend.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public final class WinUIBackend: AppBackend {
3939
public let defaultTableCellVerticalPadding = 4
4040
public let defaultPaddingAmount = 10
4141
public let requiresToggleSwitchSpacer = false
42-
public let defaultToggleStyle = ToggleStyle.button
4342
public let requiresImageUpdateOnScaleFactorChange = false
4443
public let menuImplementationStyle = MenuImplementationStyle.dynamicPopover
4544
public let canRevealFiles = false

0 commit comments

Comments
 (0)