pub struct Button<'a> { /* private fields */ }Expand description
A filled, centered label, styled by a Response the caller resolves via
Interaction::interact (or, through InteractiveWidget,
has resolved automatically).
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: the same division of labor as every other widget here
(state lives outside; the widget only reads it). InteractiveWidget::sense fixes the
Sense this button needs (Sense::click), so a call
site can’t mismatch it:
use retroglyph_core::grid::{Grid, Rect};
use retroglyph_ui::{Button, InteractiveWidget, Interaction, Surface};
#[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 button = Button::new("Save");
let response = interaction.interact(area, Id::Save, InteractiveWidget::<Id>::sense(&button));
InteractiveWidget::render(&button, &mut Surface::new(&mut grid, area, 0), &mut (), response);
interaction.end_frame();Precedence when more than one Response flag is set at once:
disabled > pressed >
hovered > focused > the default style:
matching the conventional :disabled > :active > :hover > :focus ordering, so a
disabled button always reads as muted regardless of a stale hover/press, 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, focused_style, and disabled_style default to
Theme::DARK, as if Button::theme had been called; set them with
Button::style/Button::hovered_style/Button::pressed_style/
Button::focused_style/Button::disabled_style.
Implementations§
Source§impl<'a> Button<'a>
impl<'a> Button<'a>
Sourcepub fn new(label: &'a str) -> Self
pub fn new(label: &'a str) -> Self
A button labeled label, styled from Theme::DARK (as if Button::theme had been
called); set Button::theme/Button::theme_on for a different Theme or one of the
_style setters for a one-off override.
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 const fn disabled_style(self, style: Style) -> Self
pub const fn disabled_style(self, style: Style) -> Self
Set the style used while Response::disabled is true, regardless of any other
Response flag.
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).
Trait Implementations§
Source§impl<Id> InteractiveWidget<Id> for Button<'_>
impl<Id> InteractiveWidget<Id> for Button<'_>
Source§type State = ()
type State = ()
() for
widgets that have none.Source§impl Widget for Button<'_>
impl Widget for Button<'_>
Source§fn render(&self, surface: &mut Surface<'_>)
fn render(&self, surface: &mut Surface<'_>)
Draws this button in its idle style: the non-interactive counterpart to
InteractiveWidget::render, sharing the same drawing routine with
Response::default standing in for “nothing happened”.
impl<'a> Copy for Button<'a>
Auto Trait Implementations§
impl<'a> Freeze for Button<'a>
impl<'a> RefUnwindSafe for Button<'a>
impl<'a> Send for Button<'a>
impl<'a> Sync for Button<'a>
impl<'a> Unpin for Button<'a>
impl<'a> UnsafeUnpin for Button<'a>
impl<'a> UnwindSafe for Button<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<C> WithAlpha for Cwhere
C: Copy,
impl<C> WithAlpha for Cwhere
C: Copy,
§fn with_alpha_first<A>(self, alpha: A) -> AlphaFirst<A, Self>
fn with_alpha_first<A>(self, alpha: A) -> AlphaFirst<A, Self>
self with alpha, storing alpha before the color in memory
(see [AlphaFirst]).§fn with_alpha_last<A>(self, alpha: A) -> AlphaLast<A, Self>
fn with_alpha_last<A>(self, alpha: A) -> AlphaLast<A, Self>
self with alpha, storing alpha after the color in memory
(see [AlphaLast]).