yattee/Shared/Settings/MultiselectRow.swift

49 lines
1.3 KiB
Swift
Raw Normal View History

2022-08-13 14:12:45 +00:00
import SwiftUI
struct MultiselectRow: View {
let title: String
2022-08-14 17:02:36 +00:00
var selected: Bool
var disabled = false
2022-08-13 14:12:45 +00:00
var action: (Bool) -> Void
@State private var toggleChecked = false
var body: some View {
2022-11-11 18:53:52 +00:00
#if os(macOS)
Toggle(title, isOn: $toggleChecked)
.toggleStyle(.checkbox)
.onAppear {
guard !disabled else { return }
toggleChecked = selected
}
.onChange(of: toggleChecked) { new in
action(new)
}
#else
Button(action: { action(!selected) }) {
HStack {
2022-08-13 14:12:45 +00:00
Text(self.title)
Spacer()
if selected {
Image(systemName: "checkmark")
#if os(iOS)
.foregroundColor(.accentColor)
#endif
}
2022-11-11 18:53:52 +00:00
}
.contentShape(Rectangle())
2022-08-13 14:12:45 +00:00
}
2022-11-11 18:53:52 +00:00
.disabled(disabled)
#if !os(tvOS)
.buttonStyle(.plain)
#endif
2022-08-13 14:12:45 +00:00
#endif
}
}
struct MultiselectRow_Previews: PreviewProvider {
static var previews: some View {
2022-08-25 17:09:55 +00:00
MultiselectRow(title: "Title", selected: false) { _ in }
2022-08-13 14:12:45 +00:00
}
}