Skip to content

Commit 7664cc9

Browse files
committed
Update SettingToggle - impl. edgePadding setter, fix. icon padding issue via nillable horizontalPadding _ref aheze#42
This update fixes SettingToggle implementing Icons among other functionality (ref. aheze#42) - impl: edgePadding setter (optional) via env_var, otherwise uses horizontalPadding (default) - fixes: Icon padding issue (ref via nillable horizontalPadding (ref. [aheze/Setting/#2614139662](aheze#38 (comment))), covering aheze#38, aheze#37 - impl: SettingToggle extension supporting `.icon(...)` methods
1 parent 9f64b15 commit 7664cc9

1 file changed

Lines changed: 28 additions & 6 deletions

File tree

Sources/Views/SettingToggle.swift

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public struct SettingToggle: View, Setting {
1515
public var icon: SettingIcon?
1616
public var horizontalSpacing = CGFloat(12)
1717
public var verticalPadding = CGFloat(14)
18-
public var horizontalPadding = CGFloat(16)
18+
public var horizontalPadding: CGFloat? = nil
1919
public var onChange: ((Bool) -> Void)? // Add onChange closure
2020

2121
public init(
@@ -25,7 +25,7 @@ public struct SettingToggle: View, Setting {
2525
isOn: Binding<Bool>,
2626
horizontalSpacing: CGFloat = CGFloat(12),
2727
verticalPadding: CGFloat = CGFloat(14),
28-
horizontalPadding: CGFloat = CGFloat(16),
28+
horizontalPadding: CGFloat? = nil,
2929
onChange: ((Bool) -> Void)? = nil // Initialize onChange closure
3030
) {
3131
self.id = id
@@ -52,21 +52,23 @@ public struct SettingToggle: View, Setting {
5252
}
5353

5454
struct SettingToggleView: View {
55+
@Environment(\.edgePadding) var edgePadding
56+
5557
let icon: SettingIcon?
5658
let title: String
5759
@Binding var isOn: Bool
5860

5961
var horizontalSpacing = CGFloat(12)
6062
var verticalPadding = CGFloat(14)
61-
var horizontalPadding = CGFloat(16)
63+
var horizontalPadding: CGFloat? = nil
6264
var onChange: ((Bool) -> Void)? // Receive onChange closure
6365

6466
var body: some View {
6567
HStack(spacing: horizontalSpacing) {
6668
if let icon {
67-
SettingIconView(icon: icon)
69+
SettingIconView(icon: icon)
6870
}
69-
71+
7072
Text(title)
7173
.fixedSize(horizontal: false, vertical: true)
7274
.frame(maxWidth: .infinity, alignment: .leading)
@@ -78,7 +80,27 @@ struct SettingToggleView: View {
7880
onChange?(newValue) // Call onChange closure
7981
})
8082
}
81-
.padding(.horizontal, horizontalPadding)
83+
.padding(.horizontal, horizontalPadding ?? edgePadding)
8284
.accessibilityElement(children: .combine)
8385
}
8486
}
87+
88+
public extension SettingToggle {
89+
func icon(_ icon: String, color: Color = .blue) -> SettingToggle {
90+
var toggle = self
91+
toggle.icon = .system(icon: icon, backgroundColor: color)
92+
return toggle
93+
}
94+
95+
func icon(_ icon: String, foregroundColor: Color = .white, backgroundColor: Color = .blue) -> SettingToggle {
96+
var toggle = self
97+
toggle.icon = .system(icon: icon, foregroundColor: foregroundColor, backgroundColor: backgroundColor)
98+
return toggle
99+
}
100+
101+
func icon(icon: SettingIcon) -> SettingToggle {
102+
var toggle = self
103+
toggle.icon = icon
104+
return toggle
105+
}
106+
}

0 commit comments

Comments
 (0)