diff --git a/Documentation/Reference/README.md b/Documentation/Reference/README.md index c66696f..bbaec07 100644 --- a/Documentation/Reference/README.md +++ b/Documentation/Reference/README.md @@ -40,6 +40,7 @@ - [Submenu](structs/Submenu.md) - [Text](structs/Text.md) - [ToastOverlay](structs/ToastOverlay.md) +- [Toggle](structs/Toggle.md) - [ToolbarView](structs/ToolbarView.md) - [VStack](structs/VStack.md) - [Window](structs/Window.md) diff --git a/Documentation/Reference/structs/Toggle.md b/Documentation/Reference/structs/Toggle.md new file mode 100644 index 0000000..35cac4b --- /dev/null +++ b/Documentation/Reference/structs/Toggle.md @@ -0,0 +1,52 @@ +**STRUCT** + +# `Toggle` + +A toggle button widget. + +## Properties +### `label` + +The button's label. + +### `icon` + +The button's icon. + +### `isOn` + +Whether the toggle is on. + +## Methods +### `init(_:icon:isOn:)` + +Initialize a toggle button. +- Parameters: + - label: The button's label. + - icon: The button's icon. + - isOn: Whether the toggle is on. + +### `init(_:isOn:)` + +Initialize a toggle button. +- Parameters: + - label: The buttons label. + - isOn: Whether the toggle is on. + +### `update(_:modifiers:)` + +Update a toggle button's view storage. +- Parameters: + - storage: The view storage. + - modifiers: Modify views before being updated. + +### `container(modifiers:)` + +Get a button's view storage. +- Parameter modifiers: Modify views before being updated. +- Returns: The button's view storage. + +### `updateState(toggle:)` + +Update the toggle's state. +- Parameter toggle: The toggle. diff --git a/Sources/Adwaita/View/Toggle.swift b/Sources/Adwaita/View/Toggle.swift new file mode 100644 index 0000000..ac7571e --- /dev/null +++ b/Sources/Adwaita/View/Toggle.swift @@ -0,0 +1,72 @@ +// +// Toggle.swift +// Adwaita +// +// Created by david-swift on 19.12.23. +// + +import Libadwaita + +/// A toggle button widget. +public struct Toggle: Widget { + + /// The button's label. + var label: String? + /// The button's icon. + var icon: Icon? + /// Whether the toggle is on. + @Binding var isOn: Bool + + // swiftlint:disable function_default_parameter_at_end + /// Initialize a toggle button. + /// - Parameters: + /// - label: The button's label. + /// - icon: The button's icon. + /// - isOn: Whether the toggle is on. + public init(_ label: String? = nil, icon: Icon, isOn: Binding) { + self.label = label + self.icon = icon + self._isOn = isOn + } + // swiftlint:enable function_default_parameter_at_end + + /// Initialize a toggle button. + /// - Parameters: + /// - label: The buttons label. + /// - isOn: Whether the toggle is on. + public init(_ label: String, isOn: Binding) { + self.label = label + self._isOn = isOn + } + + /// Update a toggle button's view storage. + /// - Parameters: + /// - storage: The view storage. + /// - modifiers: Modify views before being updated. + public func update(_ storage: ViewStorage, modifiers: [(View) -> View]) { + if let toggle = storage.view as? Libadwaita.ToggleButton { + updateState(toggle: toggle) + } + } + + /// Get a button's view storage. + /// - Parameter modifiers: Modify views before being updated. + /// - Returns: The button's view storage. + public func container(modifiers: [(View) -> View]) -> ViewStorage { + let toggle: Libadwaita.ToggleButton = .init(label ?? "") + updateState(toggle: toggle) + return .init(toggle) + } + + /// Update the toggle's state. + /// - Parameter toggle: The toggle. + func updateState(toggle: Libadwaita.ToggleButton) { + if let icon { + toggle.setLabel(icon: icon, label: label) + } else if let label { + toggle.setLabel(label) + } + toggle.setActive(isOn) + } + +} diff --git a/user-manual/Information/Widgets.md b/user-manual/Information/Widgets.md index 47f0c2c..e944418 100644 --- a/user-manual/Information/Widgets.md +++ b/user-manual/Information/Widgets.md @@ -10,6 +10,7 @@ This is an overview of the available widgets and other components in _Adwaita_. | Text | A widget for displaying a small amount of text. | GtkLabel | | VStack | A widget which arranges child widgets into a single column. | GtkBox | | HStack | A widget which arranges child widgets into a single row. | GtkBox | +| Toggle | A button with two possible states, on and off. | GtkToggleButton | | List | A widget which arranges child widgets vertically into rows. | GtkListBox | | Menu | A widget showing a button that toggles the appearance of a menu. | GtkMenuButton | | NavigationSplitView | A widget presenting sidebar and content side by side. | AdwNavigationSplitView |