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

Source

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.

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 const fn disabled_style(self, style: Style) -> Self

Set the style used while Response::disabled is true, regardless of any other Response flag.

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

Source§

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

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

impl<Id> InteractiveWidget<Id> for Button<'_>

Source§

type State = ()

State that outlives one render call, e.g. a selection index or scroll offset. () for widgets that have none.
Source§

fn sense(&self) -> Sense

What this widget needs from the pointer and keyboard, fixed by the widget: a call site cannot register it with a Sense its presentation doesn’t match.
Source§

fn render( &self, surface: &mut Surface<'_>, (): &mut Self::State, response: Response<Id>, )

Draw into surface, styled by response, which the caller already resolved (via Interaction::interact) for surface.area() and sense.
Source§

impl Widget for Button<'_>

Source§

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”.

Source§

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> 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.
§

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

§

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

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

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

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