portico/Sources/Portico/Generated/Switch.swift

246 lines
11 KiB
Swift

// Generated by PorticoGen. DO NOT EDIT. See Sources/PorticoGen to make changes.
import Adw
import Gtk
import Gio
import Gdk
// PorticoGen: generateStruct | source: Gtk.Switch
/// Shows a "light switch" that has two states: on or off.
///
/// <picture>
/// <source srcset="switch-dark.png" media="(prefers-color-scheme: dark)">
/// <img alt="An example GtkSwitch" src="switch.png">
/// </picture>
///
/// The user can control which state should be active by clicking the
/// empty area, or by dragging the slider.
///
/// `GtkSwitch` can also express situations where the underlying state changes
/// with a delay. In this case, the slider position indicates the user's recent
/// change (represented by the [property`Gtk`.Switch:active] property), while the
/// trough color indicates the present underlying state (represented by the
/// [property`Gtk`.Switch:state] property).
///
/// <picture>
/// <source srcset="switch-state-dark.png" media="(prefers-color-scheme: dark)">
/// <img alt="GtkSwitch with delayed state change" src="switch-state.png">
/// </picture>
///
/// See [signal`Gtk`.Switch::state-set] for details.
///
/// # Shortcuts and Gestures
///
/// `GtkSwitch` supports pan and drag gestures to move the slider.
///
/// # CSS nodes
///
/// ```
/// switch
/// image
/// image
/// slider
/// ```
///
/// `GtkSwitch` has four css nodes, the main node with the name switch and
/// subnodes for the slider and the on and off images. Neither of them is
/// using any style classes.
///
/// # Accessibility
///
/// `GtkSwitch` uses the [enum`Gtk`.AccessibleRole.switch] role.
///
/// A Portico view that mounts a `Gtk.Switch`.
@MainActor public struct Switch: View {
private let make: (MountContext) -> Gtk.Switch
private var configure: [(Gtk.Switch, MountContext) -> Void] = []
public var body: Never { fatalError() }
// PorticoGen: generateInits(static) | source: Gtk.Switch.init()
/// Creates a new `GtkSwitch` widget.
///
/// Applied once at mount; use the `Binding` or closure overload for values that change.
/// Optional value parameters are applied only when non-`nil`; a `nil` argument leaves the widget's own default in place and cannot clear a nullable property - use the matching modifier for that.
///
/// - Parameter active: Whether the `GtkSwitch` widget is in its on or off state.
/// - Parameter state: The backend state that is controlled by the switch.
/// - Parameter onActivate: Invoked when the widget emits the `activate` signal.
/// - Parameter onStateSet: Invoked when the widget emits the `state-set` signal. The closure receives the signal's arguments in order. Its return value is forwarded to GTK as the signal's result.
public init(active: Bool? = nil, state: Bool? = nil, onActivate: (() -> Void)? = nil, onStateSet: ((Bool) -> Bool)? = nil) {
make = { _ in Gtk.Switch() }
configure.append { w, ctx in
if let active { w.setActive(isActive: active) }
if let state { w.setState(state: state) }
if let onActivate { ctx.registry.add(w.connectActivate { _ in onActivate() }) }
if let onStateSet { ctx.registry.add(w.connectStateSet { _, a0 in onStateSet(a0) }) }
}
}
}
extension Switch: WidgetView {
public typealias Target = Gtk.Switch
@_spi(Portico) public func appending(
_ step: @escaping (Gtk.Switch, MountContext) -> Void
) -> Self {
var c = self
c.configure.append(step)
return c
}
}
@_spi(Portico) extension Switch: Mountable {
@_spi(Portico) public func mount(_ ctx: MountContext) -> Gtk.Widget {
let w = make(ctx)
for step in configure { step(w, ctx) }
return w
}
}
// PorticoGen: generateModifierExtension | source: Gtk.Switch
/// Modifiers for `Gtk.Switch`, available on every Portico view whose
/// backing widget is `Gtk.Switch` or one of its subclasses.
extension WidgetView where Target: Gtk.Switch {
// PorticoGen: generateModifierExtension -> generatePropertyModifiers(static) | source: Gtk.Switch.setActive(isActive:)
/// Changes the state of `self` to the desired one.
///
/// Applied once at mount; use the `Binding` or closure overload for a value that changes.
///
/// - Parameter active: Whether the `GtkSwitch` widget is in its on or off state.
/// - Returns: A copy of this view with the modifier applied.
public func active(_ active: Bool) -> Self {
appending { w, _ in
w.setActive(isActive: active)
}
}
// PorticoGen: generateModifierExtension -> generatePropertyModifiers -> bindingModifier(twoWay) | source: Gtk.Switch.setActive(isActive:), GObject.Object.connectNotify(detail:_:), Gtk.Switch.getActive()
/// Changes the state of `self` to the desired one.
///
/// Applied at mount and re-applied on every change the binding publishes.
/// When `Bool` conforms to `Equatable` this binds in both directions: the widget's `notify` signal writes its current value back into the binding, so changes made in the UI propagate to the bound state. Each direction compares before writing, which terminates the echo after one hop. A value type that is not `Equatable` binds one way only, because the echo cannot be broken.
///
/// - Returns: A copy of this view with the modifier applied.
public func active(_ active: Portico.Binding<Bool>) -> Self {
appending { w, ctx in
Portico.bindProperty(
w, active, registry: ctx.registry, notifyDetail: "active",
read: { [w] in w.getActive() },
write: { [w] v in w.setActive(isActive: v) }
)
}
}
// PorticoGen: generateModifierExtension -> generatePropertyModifiers(closure) | source: Gtk.Switch.setActive(isActive:)
/// Changes the state of `self` to the desired one.
///
/// The closure runs inside a `DependencyTracker`, so any `@State` it reads re-runs it and pushes the new value through `Gtk.Switch.setActive(isActive:)`.
///
/// - Parameter active: Whether the `GtkSwitch` widget is in its on or off state.
/// - Returns: A copy of this view with the modifier applied.
public func active(_ active: @escaping () -> Bool) -> Self {
appending { w, ctx in
let tracker = DependencyTracker { [w] in w.setActive(isActive: active()) }
tracker.run()
ctx.registry.add(tracker)
}
}
// PorticoGen: generateModifierExtension -> generatePropertyModifiers(static) | source: Gtk.Switch.setState(state:)
/// Sets the underlying state of the `GtkSwitch`.
///
/// This function is typically called from a [signal`Gtk`.Switch::state-set]
/// signal handler in order to set up delayed state changes.
///
/// See [signal`Gtk`.Switch::state-set] for details.
///
/// Applied once at mount; use the `Binding` or closure overload for a value that changes.
///
/// - Parameter state: The backend state that is controlled by the switch.
/// - Returns: A copy of this view with the modifier applied.
public func state(_ state: Bool) -> Self {
appending { w, _ in
w.setState(state: state)
}
}
// PorticoGen: generateModifierExtension -> generatePropertyModifiers -> bindingModifier(twoWay) | source: Gtk.Switch.setState(state:), GObject.Object.connectNotify(detail:_:), Gtk.Switch.getState()
/// Sets the underlying state of the `GtkSwitch`.
///
/// This function is typically called from a [signal`Gtk`.Switch::state-set]
/// signal handler in order to set up delayed state changes.
///
/// See [signal`Gtk`.Switch::state-set] for details.
///
/// Applied at mount and re-applied on every change the binding publishes.
/// When `Bool` conforms to `Equatable` this binds in both directions: the widget's `notify` signal writes its current value back into the binding, so changes made in the UI propagate to the bound state. Each direction compares before writing, which terminates the echo after one hop. A value type that is not `Equatable` binds one way only, because the echo cannot be broken.
///
/// - Returns: A copy of this view with the modifier applied.
public func state(_ state: Portico.Binding<Bool>) -> Self {
appending { w, ctx in
Portico.bindProperty(
w, state, registry: ctx.registry, notifyDetail: "state",
read: { [w] in w.getState() },
write: { [w] v in w.setState(state: v) }
)
}
}
// PorticoGen: generateModifierExtension -> generatePropertyModifiers(closure) | source: Gtk.Switch.setState(state:)
/// Sets the underlying state of the `GtkSwitch`.
///
/// This function is typically called from a [signal`Gtk`.Switch::state-set]
/// signal handler in order to set up delayed state changes.
///
/// See [signal`Gtk`.Switch::state-set] for details.
///
/// The closure runs inside a `DependencyTracker`, so any `@State` it reads re-runs it and pushes the new value through `Gtk.Switch.setState(state:)`.
///
/// - Parameter state: The backend state that is controlled by the switch.
/// - Returns: A copy of this view with the modifier applied.
public func state(_ state: @escaping () -> Bool) -> Self {
appending { w, ctx in
let tracker = DependencyTracker { [w] in w.setState(state: state()) }
tracker.run()
ctx.registry.add(tracker)
}
}
// PorticoGen: generateModifierExtension -> generateSignalModifier | source: Gtk.Switch.connectActivate(_:)
/// Emitted to animate the switch.
///
/// Applications should never connect to this signal,
/// but use the [property`Gtk`.Switch:active] property.
///
/// - Parameter handler: Invoked when the widget emits the `activate` signal.
/// - Returns: A copy of this view with the modifier applied.
public func onActivate(_ handler: @escaping () -> Void) -> Self {
appending { w, ctx in
ctx.registry.add(w.connectActivate { _ in handler() })
}
}
// PorticoGen: generateModifierExtension -> generateSignalModifier | source: Gtk.Switch.connectStateSet(_:)
/// Emitted to change the underlying state.
///
/// The ::state-set signal is emitted when the user changes the switch
/// position. The default handler calls [method`Gtk`.Switch.set_state] with the
/// value of `state`.
///
/// To implement delayed state change, applications can connect to this
/// signal, initiate the change of the underlying state, and call
/// [method`Gtk`.Switch.set_state] when the underlying state change is
/// complete. The signal handler should return `true` to prevent the
/// default handler from running.
///
/// - Parameter handler: Invoked when the widget emits the `state-set` signal. The closure receives the signal's arguments in order. Its return value is forwarded to GTK as the signal's result.
/// - Returns: A copy of this view with the modifier applied.
public func onStateSet(_ handler: @escaping (Bool) -> Bool) -> Self {
appending { w, ctx in
ctx.registry.add(w.connectStateSet { _, a0 in handler(a0) })
}
}
}