diff --git a/examples/basic.rs b/examples/basic.rs index 9fb0ee55..bd599f13 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -7,10 +7,11 @@ use ratatui::layout::Constraint; use radicle_tui as tui; use tui::store; -use tui::ui::rm::widget::container::{Column, Container, Header, HeaderProps}; +use tui::ui::rm::widget::container::{Container, Header, HeaderProps}; use tui::ui::rm::widget::input::{TextView, TextViewProps, TextViewState}; use tui::ui::rm::widget::window::{Page, Shortcuts, ShortcutsProps, Window, WindowProps}; use tui::ui::rm::widget::ToWidget; +use tui::ui::Column; use tui::{BoxedAny, Channel, Exit}; const CONTENT: &str = r#" diff --git a/src/ui.rs b/src/ui.rs index 5ee6a55e..10b5be7a 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -4,3 +4,89 @@ pub mod layout; pub mod rm; pub mod span; pub mod theme; + +use ratatui::layout::Constraint; +use ratatui::text::Text; + +pub const RENDER_WIDTH_XSMALL: usize = 50; +pub const RENDER_WIDTH_SMALL: usize = 70; +pub const RENDER_WIDTH_MEDIUM: usize = 150; +pub const RENDER_WIDTH_LARGE: usize = usize::MAX; + +#[derive(Clone, Debug, Default)] +pub struct ColumnView { + small: bool, + medium: bool, + large: bool, +} + +impl ColumnView { + pub fn all() -> Self { + Self { + small: true, + medium: true, + large: true, + } + } + + pub fn small(mut self) -> Self { + self.small = true; + self + } + + pub fn medium(mut self) -> Self { + self.medium = true; + self + } + + pub fn large(mut self) -> Self { + self.large = true; + self + } +} + +#[derive(Clone, Debug)] +pub struct Column<'a> { + pub text: Text<'a>, + pub width: Constraint, + pub skip: bool, + pub view: ColumnView, +} + +impl<'a> Column<'a> { + pub fn new(text: impl Into>, width: Constraint) -> Self { + Self { + text: text.into(), + width, + skip: false, + view: ColumnView::all(), + } + } + + pub fn skip(mut self, skip: bool) -> Self { + self.skip = skip; + self + } + + pub fn hide_small(mut self) -> Self { + self.view = ColumnView::default().medium().large(); + self + } + + pub fn hide_medium(mut self) -> Self { + self.view = ColumnView::default().large(); + self + } + + pub fn displayed(&self, area_width: usize) -> bool { + if area_width < RENDER_WIDTH_SMALL { + self.view.small + } else if area_width < RENDER_WIDTH_MEDIUM { + self.view.medium + } else if area_width < RENDER_WIDTH_LARGE { + self.view.large + } else { + true + } + } +} diff --git a/src/ui/im.rs b/src/ui/im.rs index 68f0734c..2a489747 100644 --- a/src/ui/im.rs +++ b/src/ui/im.rs @@ -20,9 +20,9 @@ use crate::event::Event; use crate::store::State; use crate::task::Interrupted; use crate::terminal; -use crate::ui::rm::widget::container::Column; use crate::ui::rm::widget::list::ToRow; use crate::ui::theme::Theme; +use crate::ui::Column; use crate::ui::im::widget::{HeaderedTable, Widget}; diff --git a/src/ui/im/widget.rs b/src/ui/im/widget.rs index 6bb9ea33..f8ab4bd3 100644 --- a/src/ui/im/widget.rs +++ b/src/ui/im/widget.rs @@ -9,9 +9,9 @@ use ratatui::{layout::Constraint, widgets::Paragraph}; use termion::event::Key; use crate::ui::ext::{FooterBlock, FooterBlockType, HeaderBlock}; -use crate::ui::rm::widget::container::Column; use crate::ui::rm::widget::list::ToRow; use crate::ui::theme::style; +use crate::ui::Column; use crate::ui::{layout, span}; use super::{Borders, Context, InnerResponse, Response, Ui}; diff --git a/src/ui/rm.rs b/src/ui/rm.rs index 8579a17d..93af1123 100644 --- a/src/ui/rm.rs +++ b/src/ui/rm.rs @@ -16,11 +16,6 @@ use crate::ui::rm::widget::Widget; const RENDERING_TICK_RATE: Duration = Duration::from_millis(250); const INLINE_HEIGHT: usize = 20; -pub const RENDER_WIDTH_XSMALL: usize = 50; -pub const RENDER_WIDTH_SMALL: usize = 70; -pub const RENDER_WIDTH_MEDIUM: usize = 150; -pub const RENDER_WIDTH_LARGE: usize = usize::MAX; - /// The `Frontend` runs an applications' view concurrently. It handles /// terminal events as well as state updates and renders the view accordingly. /// diff --git a/src/ui/rm/widget/container.rs b/src/ui/rm/widget/container.rs index e2f3e243..bdd348d0 100644 --- a/src/ui/rm/widget/container.rs +++ b/src/ui/rm/widget/container.rs @@ -8,88 +8,10 @@ use ratatui::widgets::{Block, BorderType, Borders, Row}; use crate::ui::ext::{FooterBlock, FooterBlockType, HeaderBlock}; use crate::ui::theme::{style, Theme}; -use crate::ui::rm::{RENDER_WIDTH_LARGE, RENDER_WIDTH_MEDIUM, RENDER_WIDTH_SMALL}; +use crate::ui::Column; use super::{PredefinedLayout, RenderProps, View, ViewProps, ViewState, Widget}; -#[derive(Clone, Debug, Default)] -pub struct ColumnView { - small: bool, - medium: bool, - large: bool, -} - -impl ColumnView { - pub fn all() -> Self { - Self { - small: true, - medium: true, - large: true, - } - } - - pub fn small(mut self) -> Self { - self.small = true; - self - } - - pub fn medium(mut self) -> Self { - self.medium = true; - self - } - - pub fn large(mut self) -> Self { - self.large = true; - self - } -} - -#[derive(Clone, Debug)] -pub struct Column<'a> { - pub text: Text<'a>, - pub width: Constraint, - pub skip: bool, - pub view: ColumnView, -} - -impl<'a> Column<'a> { - pub fn new(text: impl Into>, width: Constraint) -> Self { - Self { - text: text.into(), - width, - skip: false, - view: ColumnView::all(), - } - } - - pub fn skip(mut self, skip: bool) -> Self { - self.skip = skip; - self - } - - pub fn hide_small(mut self) -> Self { - self.view = ColumnView::default().medium().large(); - self - } - - pub fn hide_medium(mut self) -> Self { - self.view = ColumnView::default().large(); - self - } - - pub fn displayed(&self, area_width: usize) -> bool { - if area_width < RENDER_WIDTH_SMALL { - self.view.small - } else if area_width < RENDER_WIDTH_MEDIUM { - self.view.medium - } else if area_width < RENDER_WIDTH_LARGE { - self.view.large - } else { - true - } - } -} - #[derive(Clone, Debug)] pub struct HeaderProps<'a> { pub columns: Vec>, diff --git a/src/ui/rm/widget/list.rs b/src/ui/rm/widget/list.rs index 138f5af9..8f1126e9 100644 --- a/src/ui/rm/widget/list.rs +++ b/src/ui/rm/widget/list.rs @@ -18,10 +18,11 @@ use ratatui::Frame; use tui_tree_widget::{TreeItem, TreeState}; use crate::ui::theme::style; +use crate::ui::Column; use crate::ui::{layout, span}; -use super::{container::Column, RenderProps, View}; use super::{utils, ViewProps, ViewState}; +use super::{RenderProps, View}; /// Needs to be implemented for items that are supposed to be rendered in tables. pub trait ToRow {