diff --git a/intuitive/src/components/mod.rs b/intuitive/src/components/mod.rs index d2cf283..a611407 100644 --- a/intuitive/src/components/mod.rs +++ b/intuitive/src/components/mod.rs @@ -185,6 +185,7 @@ pub mod children; pub mod stack; +pub mod text; mod experimental_components; #[doc_cfg::doc_cfg(feature = "experimental")] @@ -203,7 +204,6 @@ mod centered; mod embed; mod empty; mod section; -mod text; pub use self::{ any::Any, @@ -212,7 +212,7 @@ pub use self::{ empty::Empty, section::Section, stack::{horizontal::Stack as HStack, vertical::Stack as VStack}, - text::Text, + text::component::Text, }; use crate::element::Any as AnyElement; diff --git a/intuitive/src/components/text/alignment.rs b/intuitive/src/components/text/alignment.rs new file mode 100644 index 0000000..e47c84d --- /dev/null +++ b/intuitive/src/components/text/alignment.rs @@ -0,0 +1,27 @@ +use tui::layout::Alignment as TuiAlignment; + +/// Control how the text in a [`Text`] component is aligned. +/// +/// [`Text`]: ../struct.Text.html +#[derive(Clone, Copy)] +pub enum Alignment { + Left, + Center, + Right, +} + +impl Default for Alignment { + fn default() -> Self { + Self::Left + } +} + +impl From for TuiAlignment { + fn from(alignment: Alignment) -> Self { + match alignment { + Alignment::Left => Self::Left, + Alignment::Center => Self::Center, + Alignment::Right => Self::Right, + } + } +} diff --git a/intuitive/src/components/text/component.rs b/intuitive/src/components/text/component.rs new file mode 100644 index 0000000..5b8cf0a --- /dev/null +++ b/intuitive/src/components/text/component.rs @@ -0,0 +1,49 @@ +use tui::{text::Spans as TuiSpans, widgets::Paragraph}; + +use super::Alignment; +use crate::{ + component, + element::{Any as AnyElement, Element}, + event::{KeyEvent, KeyHandler, MouseEvent, MouseHandler}, + terminal::{Frame, Rect}, + text::Lines, +}; + +/// A component that displays text. +/// +/// `Text` renders the [`Lines`] passed into it. +/// +/// [`Lines`]: ../text/struct.Lines.html +#[component(Text)] +pub fn render(alignment: Alignment, text: Lines, on_key: KeyHandler, on_mouse: MouseHandler) { + AnyElement::new(Frozen { + alignment: *alignment, + lines: text.clone(), + on_key: on_key.clone(), + on_mouse: on_mouse.clone(), + }) +} + +struct Frozen { + alignment: Alignment, + lines: Lines, + on_key: KeyHandler, + on_mouse: MouseHandler, +} + +impl Element for Frozen { + fn on_key(&self, event: KeyEvent) { + self.on_key.handle(event); + } + + fn on_mouse(&self, _rect: Rect, event: MouseEvent) { + self.on_mouse.handle(event); + } + + fn draw(&self, rect: Rect, frame: &mut Frame) { + let widget = + Paragraph::new::>(self.lines.0.iter().cloned().map(TuiSpans::from).collect()).alignment(self.alignment.into()); + + frame.render_widget(widget, rect); + } +} diff --git a/intuitive/src/components/text/mod.rs b/intuitive/src/components/text/mod.rs index 2dae214..2a66b90 100644 --- a/intuitive/src/components/text/mod.rs +++ b/intuitive/src/components/text/mod.rs @@ -1,45 +1,6 @@ -use tui::{text::Spans as TuiSpans, widgets::Paragraph}; +//! Structures relating to the `Text` component. -use crate::{ - component, - element::{Any as AnyElement, Element}, - event::{KeyEvent, KeyHandler, MouseEvent, MouseHandler}, - terminal::{Frame, Rect}, - text::Lines, -}; +mod alignment; +pub(super) mod component; -/// A component that displays text. -/// -/// `Text` renders the [`Lines`] passed into it. -/// -/// [`Lines`]: ../text/struct.Lines.html -#[component(Text)] -pub fn render(text: Lines, on_key: KeyHandler, on_mouse: MouseHandler) { - AnyElement::new(Frozen { - lines: text.clone(), - on_key: on_key.clone(), - on_mouse: on_mouse.clone(), - }) -} - -struct Frozen { - lines: Lines, - on_key: KeyHandler, - on_mouse: MouseHandler, -} - -impl Element for Frozen { - fn on_key(&self, event: KeyEvent) { - self.on_key.handle(event); - } - - fn on_mouse(&self, _rect: Rect, event: MouseEvent) { - self.on_mouse.handle(event); - } - - fn draw(&self, rect: Rect, frame: &mut Frame) { - let widget = Paragraph::new::>(self.lines.0.iter().cloned().map(TuiSpans::from).collect()); - - frame.render_widget(widget, rect); - } -} +pub use alignment::Alignment;