Skip to main content

Button

Struct Button 

Source
pub struct Button<M> { /* private fields */ }
Expand description

A button that can be focused and pressed, declared with component.

After on_press is set, pressing it — Enter, Space, or a left click — returns the message built by that handler. Without a handler the button is not focusable and ignores activation keys and clicks; use ButtonWidget when only painting is needed. Right and middle clicks do nothing. Painting is delegated to ButtonWidget; this half adds focus and event handling.

A button holds no state of its own. Its label and disabledness are values you pass at declaration, which means they come from app state and there is nothing to keep in sync. Those declared values also stay in effect for event handling until the next successful render, so a click arriving between an update and a redraw is judged against the button the user actually saw.

use ratcn::Button;

let _button = Button::new("Delete")
    .destructive()
    .disabled(!state.can_delete)
    .on_press(|| Msg::Delete);

Implementations§

Source§

impl<M> Button<M>

Source

pub fn new(label: impl Into<String>) -> Self

A default-variant button labelled label.

It does nothing until on_press gives it a message to emit. Without one it is not focusable and ignores activation keys and clicks. Use ButtonWidget instead for paint-only presentation.

Source

pub fn on_press(self, on_press: impl Fn() -> M + 'static) -> Self

What to emit when the button is pressed: Enter, Space, or a left click.

Source

pub const fn disabled(self, disabled: bool) -> Self

Grey the button out and stop it responding.

A disabled button is not focusable, so Tab skips it, and it ignores events rather than consuming them. Focus already parked on it stays there. Pass the value from app state (.disabled(!state.can_save)).

Source

pub fn variant(self, variant: ButtonVariant) -> Self

Set the visual variant. See ButtonVariant.

Source

pub fn outline(self) -> Self

Shorthand for ButtonVariant::Outline.

Source

pub fn secondary(self) -> Self

Shorthand for ButtonVariant::Secondary.

Source

pub fn ghost(self) -> Self

Shorthand for ButtonVariant::Ghost.

Source

pub fn destructive(self) -> Self

Source

pub const fn size(self, size: ButtonSize) -> Self

Set the height, one row or three. See ButtonSize.

Source

pub fn style(self, style: impl Fn(&Theme) -> ButtonStyle + 'static) -> Self

Replace the ButtonStyle the theme and variant derive. Resolved from the active theme at render time, so a style built from theme follows theme switches; a fixed style ignores the argument (|_| STYLE).

use ratcn::{Button, ButtonStyle, ButtonVariant};

let _button = Button::new("Archive").on_press(|| Msg::Archive).style(|theme| {
    let mut style = ButtonStyle::from_theme(theme, ButtonVariant::Default);
    style.background = theme.accent;
    style
});
Source

pub fn width(&self) -> u16

Columns this button needs: the label in terminal cells, plus two cells of padding on each side.

Build layout constraints from the same instance you are about to declare, so the constraint and the paint cannot disagree.

Trait Implementations§

Source§

impl<S, M> Component<S, M> for Button<M>

Source§

fn declare(&mut self, _ctx: &mut DeclareCtx<'_, S, M>)

Declare the component: lay out its area, declare its descendants, and record whatever handle_event will need to read back. Read more
Source§

fn paint(&mut self, ctx: &mut PaintCtx<'_, S>)

Paint the component. ctx carries the paint surface, area, app state, theme, and interaction state. Read more
Source§

fn handle_event( &mut self, event: &Event, _state: &S, _ctx: &mut EventCtx<'_>, ) -> EventResult<M>

Offer this component an event. Read more
Source§

fn scope_options(&self) -> ScopeOptions

The scope this component opens around its descendants. Read once, before declare, so it cannot depend on paint.
Source§

fn interaction_area(&self, area: Rect) -> Rect

Return the area used for focus, hit-testing, and event routing. Read more
Source§

fn prepare(&mut self, _state: &State)

Prepare this component from the state it is being declared with. Read more
Source§

fn reveal_in_viewport( &mut self, _target: Rect, _state: &State, _ctx: &mut EventCtx<'_>, )

Bring target into view inside this component’s viewport. Read more
Source§

impl<M> Debug for Button<M>

Source§

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

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

impl<S, M> MeasuredComponent<S, M> for Button<M>

Source§

fn measure(&self) -> Size

The component’s preferred width and height in terminal cells. Read more

Auto Trait Implementations§

§

impl<M> !RefUnwindSafe for Button<M>

§

impl<M> !Send for Button<M>

§

impl<M> !Sync for Button<M>

§

impl<M> !UnwindSafe for Button<M>

§

impl<M> Freeze for Button<M>
where Option<Box<dyn Fn() -> M>>: Freeze,

§

impl<M> Unpin for Button<M>
where Option<Box<dyn Fn() -> M>>: Unpin,

§

impl<M> UnsafeUnpin for Button<M>
where Option<Box<dyn Fn() -> M>>: UnsafeUnpin,

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.