Block

Struct Block 

Source
pub struct Block<W = Element> { /* private fields */ }
Expand description

A widget that wrap another widget and adds border and title. A widget that wraps another widget and adds border and title.

Block is typically used to visualize separation and organize sections. You can customize the border style, type of the border and so on.

§Example

let mut main = Block::horizontal()
    .title("Termint".fg(Color::Red))
    .border_type(BorderType::Double)
    .border_color(Color::LightGray);

let block1 = Block::vertical().title("Sub block");
main.add_child(block1, Constraint::Percent(50));
let block2 = Block::vertical().title("Another");
main.add_child(block2, Constraint::Percent(50));

let mut term = Term::new();
term.render(main)?;

Implementations§

Source§

impl<W> Block<W>
where W: Widget,

Source

pub fn new<T>(child: T) -> Self
where T: Into<Element>,

Returns a new Block wrapping the given widget, with all borders enabled and no title.

You can provide any type implementing the Widget trait.

Source

pub fn title<T>(self, title: T) -> Self
where T: Into<Box<dyn Text>>,

Sets the Text title displayed at the top of the Block.

This is typically used for section labels in your TUI.

Source

pub fn borders(self, borders: Border) -> Self

Sets the visible borders of the Block using the given Border flags.

§Example
let block1 = Block::horizontal().borders(Border::TOP | Border::BOTTOM);
let block2 = Block::horizontal().borders(borders!(TOP, BOTTOM));
Source

pub fn border_type(self, border_type: BorderType) -> Self

Sets the BorderType used to render the Block border.

Source

pub fn border_style<T>(self, style: T) -> Self
where T: Into<Style>,

Sets the style applied to Block borders.

Source

pub fn border_color(self, color: Color) -> Self

Sets the foreground color of the Block borders.

Source§

impl Block<Spacer>

Source

pub fn empty() -> Self

Returns an empty Block with no title and all borders

Source§

impl Block<Layout>

Source

pub fn vertical() -> Self

Returns a Block containing a vertical Layout as its child.

Very often you want to have a layout inside of a Block widget. This constructor makes it easier.

§Example
// Creates block with vertical layout as its child
let block1: Block<Layout> = Block::new(Layout::vertical());
// Does the same, but shorter
let block2: Block<Layout> = Block::vertical();
Source

pub fn horizontal() -> Self

Returns a Block containing a horizontal Layout as its child.

Very often you want to have a layout inside of a Block widget. This constructor makes it easier.

§Example
// Creates block with horizontal layout as its child
let block1: Block<Layout> = Block::new(Layout::horizontal());
// Does the same, but shorter
let block2: Block<Layout> = Block::horizontal();
Source

pub fn direction(self, direction: Direction) -> Self

Sets flexing Direction of the Layout.

Source

pub fn style<T>(self, style: T) -> Self
where T: Into<Style>,

Sets the base style of the Layout.

Source

pub fn bg<T>(self, bg: T) -> Self
where T: Into<Option<Color>>,

Sets base background color of the Layout.

Source

pub fn fg<T>(self, fg: T) -> Self
where T: Into<Option<Color>>,

Sets base foreground color of the Layout.

Source

pub fn padding<T>(self, padding: T) -> Self
where T: Into<Padding>,

Sets the Padding of the Layout.

Source

pub fn center(self) -> Self

Makes Layout center its content in the direction it flexes.

If the layout is flexing its children horizontally, the content will be centered horizontally. Otherwise it will be centered vertically.

Source

pub fn add_child<T, C>(&mut self, child: T, constraint: C)
where T: Into<Element>, C: Into<Constraint>,

👎Deprecated since 0.6.0: Kept for compatibility purposes; use push function instead

Adds child with its Constraint to Layout

Source

pub fn push<T, C>(&mut self, child: T, constraint: C)
where T: Into<Element>, C: Into<Constraint>,

Adds a child widget with its contraint

§Parameters
  • child: The widget to add (any type convertible to Element)
  • contraint: Widget’s contraint (any type convertible to Constraint)

Trait Implementations§

Source§

impl<W: Debug> Debug for Block<W>

Source§

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

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

impl<W> From<Block<W>> for Box<dyn Widget>
where W: Widget + 'static,

Source§

fn from(value: Block<W>) -> Self

Converts to this type from the input type.
Source§

impl<W> From<Block<W>> for Element
where W: Widget + 'static,

Source§

fn from(value: Block<W>) -> Self

Converts to this type from the input type.
Source§

impl<W> Widget for Block<W>
where W: Widget,

Source§

fn render(&self, buffer: &mut Buffer, rect: Rect, cache: &mut Cache)

Renders the widget into the given Buffer within the provided Rect bounds.
Source§

fn height(&self, size: &Vec2) -> usize

Returns the height of the Widget based on the width of the given size.
Source§

fn width(&self, size: &Vec2) -> usize

Returns the width of the Widget based on the height of the given size.
Source§

fn children(&self) -> Vec<&Element>

Gets widget’s children

Auto Trait Implementations§

§

impl<W> Freeze for Block<W>

§

impl<W = Element> !RefUnwindSafe for Block<W>

§

impl<W = Element> !Send for Block<W>

§

impl<W = Element> !Sync for Block<W>

§

impl<W> Unpin for Block<W>
where W: Unpin,

§

impl<W = Element> !UnwindSafe for Block<W>

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