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>
impl<M> Button<M>
Sourcepub fn new(label: impl Into<String>) -> Self
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.
Sourcepub fn on_press(self, on_press: impl Fn() -> M + 'static) -> Self
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.
Sourcepub const fn disabled(self, disabled: bool) -> Self
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)).
Sourcepub fn variant(self, variant: ButtonVariant) -> Self
pub fn variant(self, variant: ButtonVariant) -> Self
Set the visual variant. See ButtonVariant.
Sourcepub fn outline(self) -> Self
pub fn outline(self) -> Self
Shorthand for ButtonVariant::Outline.
Sourcepub fn secondary(self) -> Self
pub fn secondary(self) -> Self
Shorthand for ButtonVariant::Secondary.
Sourcepub fn ghost(self) -> Self
pub fn ghost(self) -> Self
Shorthand for ButtonVariant::Ghost.
Sourcepub fn destructive(self) -> Self
pub fn destructive(self) -> Self
Shorthand for ButtonVariant::Destructive.
Sourcepub const fn size(self, size: ButtonSize) -> Self
pub const fn size(self, size: ButtonSize) -> Self
Set the height, one row or three. See ButtonSize.
Sourcepub fn style(self, style: impl Fn(&Theme) -> ButtonStyle + 'static) -> Self
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
});Trait Implementations§
Source§impl<S, M> Component<S, M> for Button<M>
impl<S, M> Component<S, M> for Button<M>
Source§fn declare(&mut self, _ctx: &mut DeclareCtx<'_, S, M>)
fn declare(&mut self, _ctx: &mut DeclareCtx<'_, S, M>)
handle_event will need to read
back. Read moreSource§fn paint(&mut self, ctx: &mut PaintCtx<'_, S>)
fn paint(&mut self, ctx: &mut PaintCtx<'_, S>)
ctx carries the paint surface, area, app state,
theme, and interaction state. Read moreSource§fn handle_event(
&mut self,
event: &Event,
_state: &S,
_ctx: &mut EventCtx<'_>,
) -> EventResult<M>
fn handle_event( &mut self, event: &Event, _state: &S, _ctx: &mut EventCtx<'_>, ) -> EventResult<M>
Source§fn scope_options(&self) -> ScopeOptions
fn scope_options(&self) -> ScopeOptions
declare, so it cannot depend on paint.Source§fn interaction_area(&self, area: Rect) -> Rect
fn interaction_area(&self, area: Rect) -> Rect
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>
impl<M> Unpin for Button<M>
impl<M> UnsafeUnpin for Button<M>
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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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