//! ## Label //! //! label component use std::time::UNIX_EPOCH; use tuirealm::command::{Cmd, CmdResult}; use tuirealm::event::{Key, KeyEvent}; use tuirealm::props::{Alignment, Color, Style, TextModifiers}; use tuirealm::ratatui::layout::Rect; use tuirealm::ratatui::widgets::Paragraph; use tuirealm::{AttrValue, Attribute, Component, Event, Frame, MockComponent, Props, State}; use super::{Msg, UserEvent}; /// Simple label component; just renders a text /// NOTE: since I need just one label, I'm not going to use different object; I will directly implement Component for Label. /// This is not ideal actually and in a real app you should differentiate Mock Components from Application Components. pub struct Label { props: Props, } impl Default for Label { fn default() -> Self { Self { props: Props::default(), } } } impl MockComponent for Label { fn view(&mut self, frame: &mut Frame, area: Rect) { // Check if visible if self.props.get_or(Attribute::Display, AttrValue::Flag(true)) == AttrValue::Flag(true) { // Get properties let text = self .props .get_or(Attribute::Text, AttrValue::String(String::default())) .unwrap_string(); let alignment = self .props .get_or(Attribute::TextAlign, AttrValue::Alignment(Alignment::Left)) .unwrap_alignment(); let foreground = self .props .get_or(Attribute::Foreground, AttrValue::Color(Color::Reset)) .unwrap_color(); let background = self .props .get_or(Attribute::Background, AttrValue::Color(Color::Reset)) .unwrap_color(); let modifiers = self .props .get_or( Attribute::TextProps, AttrValue::TextModifiers(TextModifiers::empty()), ) .unwrap_text_modifiers(); frame.render_widget( Paragraph::new(text) .style( Style::default() .fg(foreground) .bg(background) .add_modifier(modifiers), ) .alignment(alignment), area, ); } } fn query(&self, attr: Attribute) -> Option { self.props.get(attr) } fn attr(&mut self, attr: Attribute, value: AttrValue) { self.props.set(attr, value); } fn state(&self) -> State { State::None } fn perform(&mut self, _: Cmd) -> CmdResult { CmdResult::None } } impl Component for Label { fn on(&mut self, ev: Event) -> Option { // Does nothing match ev { Event::Keyboard(KeyEvent { code: Key::Esc, .. }) => Some(Msg::AppClose), Event::User(UserEvent::GotData(time)) => { // set text self.attr( Attribute::Text, AttrValue::String( time.duration_since(UNIX_EPOCH) .unwrap() .as_secs() .to_string(), ), ); Some(Msg::None) } _ => None, } } }