//! `ToggleButton`: a button that stays pressed while on. use super::face::{self, FaceLook, FaceSpec}; use crate::contract::{Bind, BindMut, Common, Component, Cx, Edit as _, EditResponse, Focus as _, MeasureCx, PaintUi, Pointer as _, Props, Role}; use crate::primitives::IconSpec; use crate::runtime::commands::CommandRef; use crate::runtime::env::Label; use crate::runtime::focus::KeyUse; use crate::style::{ComponentStyle, FocusLook, IconLook, StyleCx, SurfaceLook, TextLook, TypeRole}; use craft_behavior::{Metrics, Sense}; use craft_keymap::{Chord, Key}; use craft_theme::{Flags, Variant}; use egui::Vec2; /// The chords an armed button keeps. const ESCAPE: &[Chord] = &[Chord::plain(Key::Escape)]; /// The style of a toggle button. #[derive(Clone, Debug, PartialEq, ComponentStyle)] #[style(variants = [Variant::SECONDARY])] #[non_exhaustive] pub struct ToggleButtonStyle { #[style(surface)] surface: SurfaceLook, #[style(metrics)] metrics: Metrics, #[style(focus)] focus: FocusLook, /// The label. #[style(text)] text: TextLook, /// The leading and trailing icons. #[style(icon)] icon: IconLook, face: FaceLook, } /// A secondary-looking button that stays pressed while on: it takes the accent fill and edge, and its raised surface /// draws pressed in. A click flips the value; AccessKit reports the button toggled. An `icon_only` button looks like an /// `IconButton`: its icon on a ghost square, the accent plate while on, and the label as its accessible name. #[derive(crate::contract::Component, crate::contract::Props)] pub struct ToggleButton<'a> { #[common] common: Common, /// `None` for a button bound to a toggle command, which owns the state. value: Option>, /// A leading icon. #[prop(into)] icon: Option>, /// The leading icon while the button is on, in place of `icon`. #[prop(into)] on_icon: Option>, /// An icon at the reading end, mirrored right to left. #[prop(into)] trailing_icon: Option>, /// The type role of the label, for a path or a value in `Mono`; the label role by default. #[prop] role: Option, /// Shows only the icon, as an `IconButton` does; the label stays the accessible name. #[style_key] #[prop(flag)] icon_only: bool, /// While on, the button keeps Escape and turns itself off with it, so Escape in a dialog ends the armed mode before /// it closes the dialog. Off, the button keeps no Escape. #[prop(flag)] escape_disarms: bool, } impl<'a> ToggleButton<'a> { /// A button named `label` that edits `value`. pub fn new(label: impl Into