Skip to main content

Button

Struct Button 

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

Button widget.

A clickable button with various display styles.

Implementations§

Source§

impl<'a> Button<'a>

Source

pub fn new(label: &'a str, state: &'a ButtonState) -> Self

Create a new button.

§Arguments
  • label - The button text
  • state - Reference to the button state
Source

pub fn icon(self, icon: &'a str) -> Self

Set an icon to display before the label.

Source

pub fn style(self, style: ButtonStyle) -> Self

Set the button style.

Source

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

Set the button variant.

Source

pub fn focus_id(self, id: FocusId) -> Self

Set the focus ID.

Source

pub fn alignment(self, alignment: Alignment) -> Self

Set the text alignment.

Source

pub fn min_width(&self) -> u16

Calculate minimum width for this button.

Source

pub fn min_height(&self) -> u16

Calculate minimum height for this button.

Source

pub fn render_stateful( self, area: Rect, buf: &mut Buffer, ) -> ClickRegion<ButtonAction>

Render the button and return the click region.

This method renders the button and returns a ClickRegion that you must register with a ClickRegionRegistry to enable mouse click support.

For a more convenient API, consider using render_with_registry() which handles both rendering and registration in one call.

§Example
use ratatui_interact::components::{Button, ButtonState};
use ratatui_interact::traits::ClickRegionRegistry;
use ratatui::layout::Rect;
use ratatui::buffer::Buffer;

let state = ButtonState::enabled();
let button = Button::new("OK", &state);
let area = Rect::new(0, 0, 10, 1);
let mut buf = Buffer::empty(Rect::new(0, 0, 20, 5));
let mut registry: ClickRegionRegistry<usize> = ClickRegionRegistry::new();

let region = button.render_stateful(area, &mut buf);
registry.register(region.area, 0);
Source

pub fn render_with_registry<D: Clone>( self, area: Rect, buf: &mut Buffer, registry: &mut ClickRegionRegistry<D>, data: D, )

Render the button and automatically register its click region.

This is a convenience method that combines render_stateful() with registry registration. Use this when you have a ClickRegionRegistry and want to avoid the two-step render + register pattern.

§Arguments
  • area - The area to render the button in
  • buf - The buffer to render to
  • registry - The click region registry to register with
  • data - The data to associate with this button’s click region
§Example
use ratatui_interact::components::{Button, ButtonState};
use ratatui_interact::traits::ClickRegionRegistry;
use ratatui::layout::Rect;
use ratatui::buffer::Buffer;

let state = ButtonState::enabled();
let mut registry: ClickRegionRegistry<usize> = ClickRegionRegistry::new();

// Clear before each render cycle
registry.clear();

let button = Button::new("OK", &state);
let area = Rect::new(0, 0, 10, 1);
let mut buf = Buffer::empty(Rect::new(0, 0, 20, 5));

// Render and register in one call
button.render_with_registry(area, &mut buf, &mut registry, 0);

// Later, check for clicks
if let Some(&idx) = registry.handle_click(5, 0) {
    println!("Button {} clicked!", idx);
}

Trait Implementations§

Source§

impl Widget for Button<'_>

Source§

fn render(self, area: Rect, buf: &mut Buffer)

Draws the current state of the widget in the given buffer. That is the only method required to implement a custom widget.

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