Skip to main content

StaticTextWidgetBuilder

Struct StaticTextWidgetBuilder 

Source
pub struct StaticTextWidgetBuilder<C> {
    pub size_and_position: SizeAndPosition,
    pub render_text: Vec<Span>,
    /* private fields */
}
Expand description

Builder for creating StaticTextWidget instances with a fluent interface. Maintains configuration state until build() is called to create the actual widget. StaticTextWidgetBuilder is an example of an implementation of WidgetBuilder, where the struct doesn’t implement Widget.

Fields§

§size_and_position: SizeAndPosition

The size and position configuration for the widget.

§render_text: Vec<Span>

The custom render function for the widget, if any.

Trait Implementations§

Source§

impl<C: 'static> WidgetBuilder<C> for StaticTextWidgetBuilder<C>

Implementations for the methods in WidgetBuilder.

Source§

type RendererType = Vec<Span>

The type representing the renderer content. This is different from other widgets as it is not a closure, but rather the actual content to render.

Source§

type FunctionType = Box<dyn Fn(&mut dyn Widget<C>, &mut C, &mut App<C>, &mut Scene<C>)>

Static widgets do not respond to events, so this is a no-op that returns self.

Source§

fn build( self, display_area: &Rect, ) -> Result<(Box<dyn Widget<C>>, Window), WidgetBuilderError>

Constructs a StaticTextWidget, an implementor of Widget, given the parameters. Validates that size and position are non-zero before creating the widget. The method takes in a reference to the terminal’s current area/size.

§Example:
use term_render::widget_impls::{StaticTextWidgetBuilder, WidgetBuilder};
use term_render::render::Rect;
let (widget, window) = StaticTextWidgetBuilder::<AppData>::builder(String::new())
    .build(&Rect::default())  // replace &Rect with the actual terminal size (such as `&app.area.read()`)
    .expect("Invalid widget position or size.");
Source§

fn with_position(self, position: (u16, u16)) -> Self

Sets the widget’s fixed position (static layout). Retrains the dynamic proportions of any dynamic positioning configuration already in place.

Source§

fn with_size(self, size: (u16, u16)) -> Self

Sets the widget’s fixed position (static layout). Retrains the dynamic proportions of any dynamic size configuration already in place.

Source§

fn with_dynamic_position( self, position_offset: (i16, i16), position_area_percent: (f32, f32), ) -> Self

Configures dynamic positioning based on terminal size with a fixed offset.

Source§

fn with_dynamic_size( self, size_offset: (i16, i16), size_area_percent: (f32, f32), ) -> Self

Configures dynamic sizing based on terminal size with a fixed offset.

Source§

fn with_border(self, border: bool) -> Self

Sets whether the widget should have a border. By default, all widgets are borderless.

Source§

fn with_title(self, title: String) -> Self

Sets the widget’s title (displayed in border if enabled; invisible otherwise).

Source§

fn with_depth(self, depth: u16) -> Self

Assigns a depth to the widget. Higher values represent UI elements that are stacked further ontop of other elements. By default, the builder instance has a depth of None, which corresponds to 0 (root depth). This is fine as long as it isn’t trying to appear ontop of other elements, such as for a pop-up.

Source§

fn with_renderer(self, renderer: Self::RendererType) -> Self

This renderer is unique, as instead of providing a render closure, the user provides the actual rendered content directly. This is because static text widgets don’t need to change their content dynamically, and as such a closure is unnecessary overhead. This also means that once the widget sets the text, it will never change. The widget can change size and position, but the text will remain constant. If the text is meant to be centered, it will not retain that centering so a StaticWidget or DynamicWidget would be more appropriate.

§Example:
use term_render::widget_impls::{StaticTextWidgetBuilder, WidgetBuilder};
use term_render::render::{Rect, Colorize, ColorType};
use term_render::color;

let (widget, window) = StaticTextWidgetBuilder::<AppData>::builder(String::new())
    .with_renderer(vec![
       term_render::render::Span::from_tokens(vec![
            color!["Hello World!", Blue, Bold],
        ])
    ])
    .build(&Rect::default())
    .unwrap();
Source§

fn builder(name: String) -> Self

Generates a new builder instance with a provided unique name identifier. By default, size_and_position are no compatible and require the user to provide them using the other WidgetBuilder trait functions.

§Example:
use term_render::widget_impls::{StaticTextWidgetBuilder, WidgetBuilder};
let builder = StaticTextWidgetBuilder::<AppData>::builder(String::from("Widget Name"));
Source§

fn with_sap(self, sap: SizeAndPosition) -> Self

Sets the SizeAndPosition configuration directly.

Source§

fn with_parent(self, parent: Option<usize>) -> Self

Sets the parent widget index for this widget, if any. By default, the parent is None, indicating a root node. However, only one root node can exist at a given time in a scene graph.

Source§

fn add_to_scene( self, app: &mut App<C>, scene: &mut Scene<C>, ) -> Result<usize, WidgetErr>

Builds the widget and adds it to the provided scene, returning the new widget’s index in the scene graph. This method combines the build and scene.add_widget calls into one for convenience. If building the widget fails, an error is returned instead.

§Example:
use term_render::widget_impls::{StaticTextWidgetBuilder, WidgetBuilder};
use term_render::render::Rect;
let mut app = term_render::App::new().unwrap();
let mut scene = term_render::widget::Scene::new();
let widget_index = StaticTextWidgetBuilder::<AppData>::builder(String::from("My Widget"))
    .with_position((5, 5))
    .with_size((20, 10))
    .add_to_scene(&mut app, &mut scene)
    .expect("Failed to build and add widget to scene.");
Source§

fn with_update_handler( self, _handler: Box<dyn Fn(&mut dyn Widget<C>, &mut C, &mut App<C>, &mut Scene<C>)>, ) -> Self

Sets the widget’s update handler closure. This closure is called during event updates.

Auto Trait Implementations§

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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.