Fixed centered text

This commit is contained in:
veeso
2021-03-21 12:21:50 +01:00
parent 7c9548c668
commit f2681ba0b9

View File

@@ -24,29 +24,25 @@
*/ */
// locals // locals
use super::{Canvas, Component, InputEvent, Msg, Payload, PropValue, Props, PropsBuilder}; use super::{Canvas, Component, InputEvent, Msg, Payload, Props, PropsBuilder};
use crate::utils::fmt::align_text_center; use crate::utils::fmt::align_text_center;
// ext // ext
use tui::{ use tui::{
layout::Rect, layout::Rect,
style::Style, style::Style,
text::{Span, Spans, Text as TuiText}, text::{Span, Spans, Text as TuiText},
widgets::Paragraph, widgets::{Block, BorderType, Paragraph},
}; };
// -- state // -- state
struct OwnStates { struct OwnStates {
focus: bool, focus: bool,
width: u16,
} }
impl OwnStates { impl Default for OwnStates {
fn new(width: u16) -> Self { fn default() -> Self {
Self { Self { focus: false }
focus: false,
width,
}
} }
} }
@@ -62,14 +58,9 @@ impl CText {
/// ///
/// Instantiate a new Text component /// Instantiate a new Text component
pub fn new(props: Props) -> Self { pub fn new(props: Props) -> Self {
// Get width
let width: u16 = match props.value {
PropValue::Unsigned(w) => w as u16,
_ => 0,
};
CText { CText {
props, props,
states: OwnStates::new(width), states: OwnStates::default(),
} }
} }
} }
@@ -89,7 +80,7 @@ impl Component for CText {
.iter() .iter()
.map(|x| { .map(|x| {
Span::styled( Span::styled(
align_text_center(x.content.as_str(), self.states.width), align_text_center(x.content.as_str(), area.width),
Style::default() Style::default()
.add_modifier(x.get_modifiers()) .add_modifier(x.get_modifiers())
.fg(x.fg) .fg(x.fg)
@@ -107,7 +98,15 @@ impl Component for CText {
.fg(self.props.foreground) .fg(self.props.foreground)
.bg(self.props.background), .bg(self.props.background),
); );
render.render_widget(Paragraph::new(text), area); render.render_widget(
Paragraph::new(text).block(
Block::default()
.borders(self.props.borders)
.border_style(Style::default().fg(self.props.foreground))
.border_type(BorderType::Rounded),
),
area,
);
} }
} }