pub struct Button<'a> { /* private fields */ }Expand description
Button widget.
A clickable button with various display styles.
Implementations§
Source§impl<'a> Button<'a>
impl<'a> Button<'a>
Sourcepub fn new(label: &'a str, state: &'a ButtonState) -> Self
pub fn new(label: &'a str, state: &'a ButtonState) -> Self
Sourcepub fn style(self, style: ButtonStyle) -> Self
pub fn style(self, style: ButtonStyle) -> Self
Set the button style.
Sourcepub fn variant(self, variant: ButtonVariant) -> Self
pub fn variant(self, variant: ButtonVariant) -> Self
Set the button variant.
Sourcepub fn min_height(&self) -> u16
pub fn min_height(&self) -> u16
Calculate minimum height for this button.
Sourcepub fn render_stateful(
self,
area: Rect,
buf: &mut Buffer,
) -> ClickRegion<ButtonAction>
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);Sourcepub fn render_with_registry<D: Clone>(
self,
area: Rect,
buf: &mut Buffer,
registry: &mut ClickRegionRegistry<D>,
data: D,
)
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 inbuf- The buffer to render toregistry- The click region registry to register withdata- 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§
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> 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
Mutably borrows from an owned value. Read more
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>
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 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>
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