Trait ratatui::widgets::Widget

source ·
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§

source

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

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

Implementations on Foreign Types§

source§

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§

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

source§

impl Widget for String

Renders a String object as a widget.

This implementation enables an owned String to be treated as a widget, which can be rendered on a Buffer within the bounds of a given Rect.

source§

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

source§

impl<W: WidgetRef> Widget for &W

This allows you to render a widget by reference.

source§

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

Implementors§

source§

impl Widget for Line<'_>

source§

impl Widget for Span<'_>

source§

impl Widget for Text<'_>

source§

impl Widget for Block<'_>

source§

impl Widget for BarChart<'_>

source§

impl Widget for Chart<'_>

source§

impl Widget for Clear

source§

impl Widget for Gauge<'_>

source§

impl Widget for LineGauge<'_>

source§

impl Widget for List<'_>

source§

impl Widget for Paragraph<'_>

source§

impl Widget for Sparkline<'_>

source§

impl Widget for Table<'_>

source§

impl Widget for Tabs<'_>

source§

impl<DS: DateStyler> Widget for Monthly<'_, DS>

Available on crate feature widget-calendar only.
source§

impl<F> Widget for Canvas<'_, F>
where F: Fn(&mut Context<'_>),