ratatui::widgets::canvas

Struct Context

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

Holds the state of the Canvas when painting to it.

This is used by the Canvas widget to draw shapes on the grid. It can be useful to think of this as similar to the Frame struct that is used to draw widgets on the terminal.

Implementations§

Source§

impl<'a> Context<'a>

Source

pub fn new( width: u16, height: u16, x_bounds: [f64; 2], y_bounds: [f64; 2], marker: Marker, ) -> Self

Create a new Context with the given width and height measured in terminal columns and rows respectively. The x and y bounds define the specific area of some coordinate system that will be drawn on the canvas. The marker defines the type of points used to draw the shapes.

Applications should not use this directly but rather use the Canvas widget. This will be created by the Canvas::paint method and passed to the closure that is used to draw on the canvas.

The x and y bounds should be specified as left/right and bottom/top respectively. For example, if you want to draw a map of the world, you might want to use the following bounds:

use ratatui::{symbols, widgets::canvas::Context};

let ctx = Context::new(
    100,
    100,
    [-180.0, 180.0],
    [-90.0, 90.0],
    symbols::Marker::Braille,
);
Source

pub fn draw<S>(&mut self, shape: &S)
where S: Shape,

Draw the given Shape in this context

Source

pub fn layer(&mut self)

Save the existing state of the grid as a layer.

Save the existing state as a layer to be rendered and reset the grid to its initial state for the next layer.

This allows the canvas to be drawn in multiple layers. This is useful if you want to draw multiple shapes on the Canvas in specific order.

Source

pub fn print<T>(&mut self, x: f64, y: f64, line: T)
where T: Into<TextLine<'a>>,

Print a Text on the Canvas at the given position.

Note that the text is always printed on top of the canvas and is not affected by the layers.

Trait Implementations§

Source§

impl<'a> Debug for Context<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, 'b> From<&'a mut Context<'b>> for Painter<'a, 'b>

Source§

fn from(context: &'a mut Context<'b>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a> Freeze for Context<'a>

§

impl<'a> !RefUnwindSafe for Context<'a>

§

impl<'a> !Send for Context<'a>

§

impl<'a> !Sync for Context<'a>

§

impl<'a> Unpin for Context<'a>

§

impl<'a> !UnwindSafe for Context<'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.