pub struct Button<'a> { /* private fields */ }Expand description
A filled, centered label, styled by a Response the caller already resolved via
Interaction::interact.
Button is pure presentation, not a new source of truth: it never calls interact itself and
has no Id type parameter, unlike Interaction<Id>. The app still owns the Interaction<Id>
context and decides the button’s id/Sense: the same division of labor as
every other widget here (state lives outside; the widget only reads it), applied to the
interact module’s own doctest pattern (“draw the button, using response.hovered()/
focused() to pick a style”) instead of leaving every call site to hand-roll it:
use retroglyph_core::{Grid, Rect};
use retroglyph_widgets::{Button, Interaction, Sense, Surface, Widget};
#[derive(Clone, Copy, PartialEq, Eq)]
enum Id {
Save,
}
let mut grid = Grid::new(20, 10);
let mut interaction = Interaction::<Id>::new();
interaction.begin_frame();
let area = Rect::new(0, 0, 10, 1);
let response = interaction.interact(area, Id::Save, Sense::click());
Button::new("Save", response).render(area, &mut Surface::new(&mut grid, area, 0));
interaction.end_frame();Precedence when more than one Response flag is set at once:
pressed > hovered >
focused > the default style: matching the conventional
:active > :hover > :focus ordering, so a press always reads as pressed even while
still hovered, and a keyboard-focused-but-not-hovered button still shows something distinct
from idle.
style, hovered_style, pressed_style, and focused_style each default to a fixed
palette; set them with Button::style/Button::hovered_style/Button::pressed_style/
Button::focused_style.
Implementations§
Source§impl<'a> Button<'a>
impl<'a> Button<'a>
Sourcepub fn new(label: &'a str, response: Response) -> Self
pub fn new(label: &'a str, response: Response) -> Self
A button labeled label, styled from response.
Sourcepub const fn hovered_style(self, style: Style) -> Self
pub const fn hovered_style(self, style: Style) -> Self
Set the style used while Response::hovered is true.
Sourcepub const fn pressed_style(self, style: Style) -> Self
pub const fn pressed_style(self, style: Style) -> Self
Set the style used while Response::pressed is true.
Sourcepub const fn focused_style(self, style: Style) -> Self
pub const fn focused_style(self, style: Style) -> Self
Set the style used while Response::focused is true (and neither pressed nor
hovered).
Sourcepub fn theme(self, theme: Theme) -> Self
pub fn theme(self, theme: Theme) -> Self
Applies theme’s named roles to all four of this button’s states: idle becomes
theme.fg on theme.panel_bg; hovered/pressed swap in theme.hover_bg/theme.press_bg
for the background; focused becomes theme.accent on theme.panel_bg. The same mapping
09_widgets_dashboard’s “Ping” button hand-threads today.
Call before any manual _style override you want to keep.
Sourcepub fn theme_on(self, theme: Theme, bg: Color) -> Self
pub fn theme_on(self, theme: Theme, bg: Color) -> Self
Same as Button::theme, but the idle and focused states are drawn on bg instead of
theme.panel_bg (hovered_style/pressed_style still use theme.hover_bg/
theme.press_bg, unaffected by bg): for a button drawn directly on a backdrop other
than a themed super::Panel/super::Modal’s fill. Button::theme is exactly
theme_on(theme, theme.panel_bg).