| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- /// A light switch style control that is either on or off.
- ///
- /// This corresponds to the ``ToggleStyle/switch`` toggle style.
- struct ToggleSwitch: ElementaryView, View {
- /// Whether the switch is active or not.
- private var active: Binding<Bool>
- /// Creates a switch.
- ///
- /// - Parameter active: Whether the switch is active or not.
- public init(isOn active: Binding<Bool>) {
- self.active = active
- }
- func asWidget<Backend: BaseAppBackend>(backend: Backend) -> Backend.Widget {
- return backend.createSwitch()
- }
- func computeLayout<Backend: BaseAppBackend>(
- _ widget: Backend.Widget,
- proposedSize: ProposedViewSize,
- environment: EnvironmentValues,
- backend: Backend
- ) -> ViewLayoutResult {
- let size = ViewSize(backend.naturalSize(of: widget))
- return ViewLayoutResult.leafView(size: size)
- }
- func commit<Backend: BaseAppBackend>(
- _ widget: Backend.Widget,
- layout: ViewLayoutResult,
- environment: EnvironmentValues,
- backend: Backend
- ) {
- backend.updateSwitch(widget, environment: environment) { newActiveState in
- if active.wrappedValue != newActiveState {
- active.wrappedValue = newActiveState
- }
- }
- backend.setState(ofSwitch: widget, to: active.wrappedValue)
- backend.setSize(of: widget, to: layout.size.vector)
- }
- }
|