tui: Change content handling for labels

Signed-off-by: Erik Kundt <erik@zirkular.io>
This commit is contained in:
Erik Kundt 2023-03-24 13:38:31 +01:00
parent 708e202dcc
commit ceb1f8e700
4 changed files with 30 additions and 21 deletions

View File

@ -4,9 +4,6 @@ pub mod state;
pub mod theme;
pub mod widget;
use tuirealm::props::Attribute;
use tuirealm::{MockComponent, StateValue};
use components::container::{GlobalListener, LabeledContainer, Tabs};
use components::context::{Shortcut, Shortcuts};
use components::label::Label;
@ -14,6 +11,9 @@ use components::list::{Property, PropertyList};
use components::workspace::Workspaces;
use widget::Widget;
use tuirealm::props::{AttrValue, Attribute};
use tuirealm::MockComponent;
pub fn global_listener() -> Widget<GlobalListener> {
Widget::new(GlobalListener::default())
}
@ -21,9 +21,11 @@ pub fn global_listener() -> Widget<GlobalListener> {
pub fn label(content: &str) -> Widget<Label> {
// TODO: Remove when size constraints are implemented
let width = content.chars().count() as u16;
let label = Label::new(StateValue::String(content.to_owned()));
Widget::new(label).height(1).width(width)
Widget::new(Label::default())
.content(AttrValue::String(content.to_string()))
.height(1)
.width(width)
}
pub fn labeled_container(

View File

@ -129,7 +129,9 @@ impl WidgetComponent for Header {
let display = properties
.get_or(Attribute::Display, AttrValue::Flag(true))
.unwrap_flag();
let spacer = Widget::new(Label::new(StateValue::String(String::default()))).to_boxed();
let spacer = Widget::new(Label::default())
.content(AttrValue::String(String::default()))
.to_boxed();
if display {
let labels: Vec<Box<dyn MockComponent>> = vec![self.content.clone().to_boxed(), spacer];

View File

@ -2,27 +2,22 @@ use tuirealm::command::{Cmd, CmdResult};
use tuirealm::props::{AttrValue, Attribute, Color, Props, Style};
use tuirealm::tui::layout::Rect;
use tuirealm::tui::text::Span;
use tuirealm::{Frame, MockComponent, State, StateValue};
use tuirealm::{Frame, MockComponent, State};
use crate::ui::widget::{Widget, WidgetComponent};
/// A label that can be styled using a foreground color and text modifiers.
/// Its height is fixed, its width depends on the length of the text it displays.
#[derive(Clone)]
pub struct Label {
content: StateValue,
}
impl Label {
pub fn new(content: StateValue) -> Self {
Self { content }
}
}
#[derive(Clone, Default)]
pub struct Label;
impl WidgetComponent for Label {
fn view(&mut self, properties: &Props, frame: &mut Frame, area: Rect) {
use tui_realm_stdlib::Label;
let content = properties
.get_or(Attribute::Content, AttrValue::String(String::default()))
.unwrap_string();
let display = properties
.get_or(Attribute::Display, AttrValue::Flag(true))
.unwrap_flag();
@ -39,11 +34,11 @@ impl WidgetComponent for Label {
.foreground(foreground)
.background(background)
.modifiers(modifiers.unwrap_text_modifiers())
.text(self.content.clone().unwrap_string()),
.text(content),
None => Label::default()
.foreground(foreground)
.background(background)
.text(self.content.clone().unwrap_string()),
.text(content),
};
label.view(frame, area);
@ -51,7 +46,7 @@ impl WidgetComponent for Label {
}
fn state(&self) -> State {
State::One(self.content.clone())
State::None
}
fn perform(&mut self, _cmd: Cmd) -> CmdResult {
@ -61,6 +56,11 @@ impl WidgetComponent for Label {
impl From<&Widget<Label>> for Span<'_> {
fn from(label: &Widget<Label>) -> Self {
Span::styled(label.content.clone().unwrap_string(), Style::default())
let content = label
.query(Attribute::Content)
.unwrap_or(AttrValue::String(String::default()))
.unwrap_string();
Span::styled(content, Style::default())
}
}

View File

@ -62,6 +62,11 @@ impl<T: WidgetComponent> Widget<T> {
self
}
pub fn content(mut self, content: AttrValue) -> Self {
self.attr(Attribute::Content, content);
self
}
pub fn to_boxed(self) -> Box<Self> {
Box::new(self)
}