Add support for toggles

This commit is contained in:
david-swift 2023-12-19 22:20:00 +01:00
parent 85ca28d32f
commit 3a0897cbb5
4 changed files with 126 additions and 0 deletions

View File

@ -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)

View File

@ -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.

View File

@ -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<Bool>) {
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<Bool>) {
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)
}
}

View File

@ -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 |