Fix macOS toggle style

This commit is contained in:
世界
2024-09-16 14:22:36 +08:00
parent 66a33e3398
commit a8ea162606
6 changed files with 77 additions and 131 deletions
@@ -51,13 +51,35 @@ public func FormItem(_ title: String, @ViewBuilder content: () -> some View) ->
#endif
}
public func FormSection(@ViewBuilder content: () -> some View, @ViewBuilder footer: () -> some View) -> some View {
Section {
content()
} footer: {
footer()
.frame(maxWidth: .infinity, alignment: .leading)
}
public func FormToggle(_ titleKey: LocalizedStringKey, _ subtitleKey: LocalizedStringKey, _ isOn: Binding<Bool>, _ action: @escaping (_ newValue: Bool) async -> Void) -> some View {
#if os(macOS)
Toggle(isOn: isOn) {
VStack(alignment: .leading) {
Text(titleKey)
Spacer()
Text(subtitleKey)
.font(.subheadline)
.foregroundStyle(.secondary)
}
}
.onChangeCompat(of: isOn.wrappedValue) { newValue in
Task {
await action(newValue)
}
}
#else
Section {
Toggle(titleKey, isOn: isOn)
.onChangeCompat(of: isOn.wrappedValue) { newValue in
Task {
await action(newValue)
}
}
} footer: {
Text(subtitleKey)
.frame(maxWidth: .infinity, alignment: .leading)
}
#endif
}
public func FormButton(action: @escaping () -> Void, @ViewBuilder label: () -> some View) -> some View {