portico/Sources/Portico/Extensions/Button+Extras.swift

181 lines
No EOL
8.1 KiB
Swift

import Foundation
public extension Button {
/// Creates a ``Button`` with a plain string label. Applied once at mount.
@_disfavoredOverload
init<S: StringProtocol>(_ label: S, _ onClick: @escaping () -> Void) {
self.init(label: String(label))
self = self.onClicked(onClick)
}
/// Creates a ``Button`` from a string literal whose interpolated segments are
/// re-evaluated when the `@State` they read changes, pushing the new value into
/// the widget in-place.
init(_ label: Portico.InterpolatedText, _ onClick: @escaping () -> Void) {
self.init(label: label)
self = self.onClicked(onClick)
}
/// Creates a ``Button`` that tracks a ``Binding<String>``.
init(_ label: Portico.Binding<String>, _ onClick: @escaping () -> Void) {
self.init(label: label)
self = self.onClicked(onClick)
}
/// Creates a ``Button`` whose label is re-evaluated inside a ``DependencyTracker``
/// whenever `@State` values read by the closure change.
init(_ label: @escaping () -> String, _ onClick: @escaping () -> Void) {
self.init(label: label)
self = self.onClicked(onClick)
}
}
// MARK: - Style classes
import Gtk
extension WidgetView where Target: Gtk.Button {
/// Applies the `.suggested-action` style class.
///
/// Accent colors; denotes the important/affirmative action. Combines with ``circular`` or ``pill``. Keeps its raised appearance inside a toolbar, and ``linked`` styling does not work correctly with it.
///
/// - Parameter enabled: Whether the style class is applied.
/// - Returns: A copy of this view with the modifier applied.
public func suggestedAction(_ enabled: Bool = true) -> Self {
styleClass("suggested-action", enabled)
}
/// Applies the `.suggested-action` style class.
///
/// Accent colors; denotes the important/affirmative action. Combines with ``circular`` or ``pill``. Keeps its raised appearance inside a toolbar, and ``linked`` styling does not work correctly with it.
///
/// - Parameter enabled: A binding that controls whether the class is applied.
/// - Returns: A copy of this view with the modifier applied.
public func suggestedAction(_ enabled: Binding<Bool>) -> Self {
styleClass("suggested-action", enabled)
}
/// Applies the `.destructive-action` style class.
///
/// Destructive colors, warning the user about damaging consequences. Combines with ``flat``, ``circular`` or ``pill``. Keeps its default appearance inside a toolbar.
///
/// - Parameter enabled: Whether the style class is applied.
/// - Returns: A copy of this view with the modifier applied.
public func destructiveAction(_ enabled: Bool = true) -> Self {
styleClass("destructive-action", enabled)
}
/// Applies the `.destructive-action` style class.
///
/// Destructive colors, warning the user about damaging consequences. Combines with ``flat``, ``circular`` or ``pill``. Keeps its default appearance inside a toolbar.
///
/// - Parameter enabled: A binding that controls whether the class is applied.
/// - Returns: A copy of this view with the modifier applied.
public func destructiveAction(_ enabled: Binding<Bool>) -> Self {
styleClass("destructive-action", enabled)
}
/// Applies the `.flat` style class.
///
/// Flat appearance, looking like a label or icon until hovered. Buttons inside toolbars and similar widgets are flat by default. Combines with ``circular`` or ``pill``. Equivalent to `hasFrame(false)` on `Gtk.Button` / `Gtk.MenuButton`.
///
/// - Parameter enabled: Whether the style class is applied.
/// - Returns: A copy of this view with the modifier applied.
public func flat(_ enabled: Bool = true) -> Self {
styleClass("flat", enabled)
}
/// Applies the `.flat` style class.
///
/// Flat appearance, looking like a label or icon until hovered. Buttons inside toolbars and similar widgets are flat by default. Combines with ``circular`` or ``pill``. Equivalent to `hasFrame(false)` on `Gtk.Button` / `Gtk.MenuButton`.
///
/// - Parameter enabled: A binding that controls whether the class is applied.
/// - Returns: A copy of this view with the modifier applied.
public func flat(_ enabled: Binding<Bool>) -> Self {
styleClass("flat", enabled)
}
/// Applies the `.raised` style class.
///
/// Regular appearance instead of the flat one; only useful inside toolbars and similar widgets, where `hasFrame(true)` cannot force a raised look. Combines with ``circular`` or ``pill``.
///
/// - Parameter enabled: Whether the style class is applied.
/// - Returns: A copy of this view with the modifier applied.
public func raised(_ enabled: Bool = true) -> Self {
styleClass("raised", enabled)
}
/// Applies the `.raised` style class.
///
/// Regular appearance instead of the flat one; only useful inside toolbars and similar widgets, where `hasFrame(true)` cannot force a raised look. Combines with ``circular`` or ``pill``.
///
/// - Parameter enabled: A binding that controls whether the class is applied.
/// - Returns: A copy of this view with the modifier applied.
public func raised(_ enabled: Binding<Bool>) -> Self {
styleClass("raised", enabled)
}
/// Applies the `.circular` style class.
///
/// Round button, for icons or 1-2 character labels. Combines with ``suggestedAction``, ``destructiveAction``, ``flat``, ``raised``, ``opaque``, or ``osd``.
///
/// - Parameter enabled: Whether the style class is applied.
/// - Returns: A copy of this view with the modifier applied.
public func circular(_ enabled: Bool = true) -> Self {
styleClass("circular", enabled)
}
/// Applies the `.circular` style class.
///
/// Round button, for icons or 1-2 character labels. Combines with ``suggestedAction``, ``destructiveAction``, ``flat``, ``raised``, ``opaque``, or ``osd``.
///
/// - Parameter enabled: A binding that controls whether the class is applied.
/// - Returns: A copy of this view with the modifier applied.
public func circular(_ enabled: Binding<Bool>) -> Self {
styleClass("circular", enabled)
}
/// Applies the `.pill` style class.
///
/// Pill-shaped button for important standalone actions, e.g. inside an `Adw.StatusPage`. Combines with the same set as ``circular``.
///
/// - Parameter enabled: Whether the style class is applied.
/// - Returns: A copy of this view with the modifier applied.
public func pill(_ enabled: Bool = true) -> Self {
styleClass("pill", enabled)
}
/// Applies the `.pill` style class.
///
/// Pill-shaped button for important standalone actions, e.g. inside an `Adw.StatusPage`. Combines with the same set as ``circular``.
///
/// - Parameter enabled: A binding that controls whether the class is applied.
/// - Returns: A copy of this view with the modifier applied.
public func pill(_ enabled: Binding<Bool>) -> Self {
styleClass("pill", enabled)
}
/// Applies the `.opaque` style class.
///
/// Opaque background intended to be paired with custom `background-color`/`color` overrides.
///
/// - Parameter enabled: Whether the style class is applied.
/// - Returns: A copy of this view with the modifier applied.
@available(*, deprecated, message: "Deprecated since libadwaita 1.6. Use suggestedAction() and override the accent color instead.")
public func opaque(_ enabled: Bool = true) -> Self {
styleClass("opaque", enabled)
}
/// Applies the `.opaque` style class.
///
/// Opaque background intended to be paired with custom `background-color`/`color` overrides.
///
/// - Parameter enabled: A binding that controls whether the class is applied.
/// - Returns: A copy of this view with the modifier applied.
@available(*, deprecated, message: "Deprecated since libadwaita 1.6. Use suggestedAction() and override the accent color instead.")
public func opaque(_ enabled: Binding<Bool>) -> Self {
styleClass("opaque", enabled)
}
}