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: SizeAndPositionThe 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.
impl<C: 'static> WidgetBuilder<C> for StaticTextWidgetBuilder<C>
Implementations for the methods in WidgetBuilder.
Source§type RendererType = Vec<Span>
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>)>
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>
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
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
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
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
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
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
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
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
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
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
fn with_sap(self, sap: SizeAndPosition) -> Self
Sets the SizeAndPosition configuration directly.
Source§fn with_parent(self, parent: Option<usize>) -> Self
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>
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.");