Radish alpha
r
Radicle terminal user interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
tui: Change content handling for labels
Erik Kundt committed 3 years ago
commit 2c74fb7e8f475b9eefa46d7722ccdc5cfcd8a555
parent 007aa1578ede24e1cf11006560287e17fa3d2b44
4 files changed +30 -21
modified radicle-tui/src/ui.rs
@@ -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(
modified radicle-tui/src/ui/components/container.rs
@@ -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];
modified radicle-tui/src/ui/components/label.rs
@@ -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())
    }
}
modified radicle-tui/src/ui/widget.rs
@@ -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)
    }