/// 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 /// Creates a switch. /// /// - Parameter active: Whether the switch is active or not. public init(isOn active: Binding) { self.active = active } func asWidget(backend: Backend) -> Backend.Widget { return backend.createSwitch() } func computeLayout( _ widget: Backend.Widget, proposedSize: ProposedViewSize, environment: EnvironmentValues, backend: Backend ) -> ViewLayoutResult { let size = ViewSize(backend.naturalSize(of: widget)) return ViewLayoutResult.leafView(size: size) } func commit( _ 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) } }