Skip to main content

Button

Struct Button 

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

Source

pub fn new(label: &'a str, response: Response) -> Self

A button labeled label, styled from response.

Source

pub const fn style(self, style: Style) -> Self

Set the default (idle) style.

Source

pub const fn hovered_style(self, style: Style) -> Self

Set the style used while Response::hovered is true.

Source

pub const fn pressed_style(self, style: Style) -> Self

Set the style used while Response::pressed is true.

Source

pub const fn focused_style(self, style: Style) -> Self

Set the style used while Response::focused is true (and neither pressed nor hovered).

Source

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.

Source

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<'a> Clone for Button<'a>

Source§

fn clone(&self) -> Button<'a>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Copy for Button<'a>

Source§

impl<'a> Debug for Button<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Widget for Button<'_>

Source§

fn render(&self, area: Rect, surface: &mut Surface<'_>)

Draw this widget into area, via surface scoped to it.

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<C> WithAlpha for C
where C: Copy,

Source§

fn with_alpha_first<A>(self, alpha: A) -> AlphaFirst<A, Self>

Wraps self with alpha, storing alpha before the color in memory (see AlphaFirst).
Source§

fn with_alpha_last<A>(self, alpha: A) -> AlphaLast<A, Self>

Wraps self with alpha, storing alpha after the color in memory (see AlphaLast).