pub trait Widget {
// Required method
fn render(self, area: Rect, buf: &mut Buffer)
where Self: Sized;
}
Expand description
A Widget
is a type that can be drawn on a Buffer
in a given Rect
.
Prior to Ratatui 0.26.0, widgets generally were created for each frame as they were consumed during rendering. This meant that they were not meant to be stored but used as commands to draw common figures in the UI.
Starting with Ratatui 0.26.0, we added a new WidgetRef
trait and implemented this on all the
internal widgets. This allows you to store a reference to a widget and render it later. It also
allows you to render boxed widgets. This is useful when you want to store a collection of
widgets with different types. You can then iterate over the collection and render each widget.
The Widget
trait can still be implemented, however, it is recommended to implement WidgetRef
and add an implementation of Widget
that calls WidgetRef::render_ref
. This pattern should be
used where backwards compatibility is required (all the internal widgets use this approach).
A blanket implementation of Widget
for &W
where W
implements WidgetRef
is provided.
Widget is also implemented for &str
and String
types.
§Examples
use ratatui::{backend::TestBackend, prelude::*, widgets::*};
terminal.draw(|frame| {
frame.render_widget(Clear, frame.size());
});
It’s common to render widgets inside other widgets:
use ratatui::{prelude::*, widgets::*};
struct MyWidget;
impl Widget for MyWidget {
fn render(self, area: Rect, buf: &mut Buffer) {
Line::raw("Hello").render(area, buf);
}
}
Required Methods§
Implementations on Foreign Types§
source§impl Widget for &str
impl Widget for &str
Renders a string slice as a widget.
This implementation allows a string slice (&str
) to act as a widget, meaning it can be drawn
onto a Buffer
in a specified Rect
. The slice represents a static string which can be
rendered by reference, thereby avoiding the need for string cloning or ownership transfer when
drawing the text to the screen.
source§impl Widget for String
impl Widget for String
Implementors§
impl Widget for Line<'_>
impl Widget for Span<'_>
impl Widget for Text<'_>
impl Widget for Block<'_>
impl Widget for BarChart<'_>
impl Widget for Chart<'_>
impl Widget for Clear
impl Widget for Gauge<'_>
impl Widget for LineGauge<'_>
impl Widget for List<'_>
impl Widget for Paragraph<'_>
impl Widget for Sparkline<'_>
impl Widget for Table<'_>
impl Widget for Tabs<'_>
impl<DS: DateStyler> Widget for Monthly<'_, DS>
widget-calendar
only.