Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions intuitive/src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@

pub mod children;
pub mod stack;
pub mod text;

mod experimental_components;
#[doc_cfg::doc_cfg(feature = "experimental")]
Expand All @@ -203,7 +204,6 @@ mod centered;
mod embed;
mod empty;
mod section;
mod text;

pub use self::{
any::Any,
Expand All @@ -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;

Expand Down
27 changes: 27 additions & 0 deletions intuitive/src/components/text/alignment.rs
Original file line number Diff line number Diff line change
@@ -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<Alignment> for TuiAlignment {
fn from(alignment: Alignment) -> Self {
match alignment {
Alignment::Left => Self::Left,
Alignment::Center => Self::Center,
Alignment::Right => Self::Right,
}
}
}
49 changes: 49 additions & 0 deletions intuitive/src/components/text/component.rs
Original file line number Diff line number Diff line change
@@ -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::<Vec<TuiSpans>>(self.lines.0.iter().cloned().map(TuiSpans::from).collect()).alignment(self.alignment.into());

frame.render_widget(widget, rect);
}
}
47 changes: 4 additions & 43 deletions intuitive/src/components/text/mod.rs
Original file line number Diff line number Diff line change
@@ -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::<Vec<TuiSpans>>(self.lines.0.iter().cloned().map(TuiSpans::from).collect());

frame.render_widget(widget, rect);
}
}
pub use alignment::Alignment;