From 5d7bf9f663978f3b661f66bf750090434fe479ba Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Wed, 17 May 2023 11:33:57 +0200 Subject: [PATCH] tui: Introduce table item and table model --- radicle-tui/src/ui/theme.rs | 7 + radicle-tui/src/ui/widget/common/list.rs | 236 ++++++++++++++++------- 2 files changed, 177 insertions(+), 66 deletions(-) diff --git a/radicle-tui/src/ui/theme.rs b/radicle-tui/src/ui/theme.rs index d6bc04f2..c7bb5324 100644 --- a/radicle-tui/src/ui/theme.rs +++ b/radicle-tui/src/ui/theme.rs @@ -41,6 +41,11 @@ pub struct Icons { pub whitespace: char, } +#[derive(Debug, Clone)] +pub struct Tables { + pub spacing: u16, +} + /// The Radicle TUI theme. Will be defined in a JSON config file in the /// future. e.g.: /// { @@ -60,6 +65,7 @@ pub struct Theme { pub name: String, pub colors: Colors, pub icons: Icons, + pub tables: Tables, } pub fn default_dark() -> Theme { @@ -95,5 +101,6 @@ pub fn default_dark() -> Theme { tab_overline: '▔', whitespace: ' ', }, + tables: Tables { spacing: 2 }, } } diff --git a/radicle-tui/src/ui/widget/common/list.rs b/radicle-tui/src/ui/widget/common/list.rs index 9e1d9138..2997332a 100644 --- a/radicle-tui/src/ui/widget/common/list.rs +++ b/radicle-tui/src/ui/widget/common/list.rs @@ -1,12 +1,8 @@ -use radicle::Profile; use tuirealm::command::{Cmd, CmdResult}; -use tuirealm::props::{ - AttrValue, Attribute, BorderSides, BorderType, Color, PropPayload, PropValue, Props, Style, - TextSpan, -}; +use tuirealm::props::{AttrValue, Attribute, BorderSides, BorderType, Color, Props, Style}; use tuirealm::tui::layout::{Constraint, Direction, Layout, Rect}; use tuirealm::tui::widgets::{Block, Cell, Row, TableState}; -use tuirealm::{Frame, MockComponent, State, StateValue}; +use tuirealm::{Frame, MockComponent, State}; use crate::ui::layout; use crate::ui::theme::Theme; @@ -15,8 +11,86 @@ use crate::ui::widget::{Widget, WidgetComponent}; use super::container::Header; use super::label::Label; -pub trait List { - fn row(&self, theme: &Theme, profile: &Profile) -> Vec; +/// A generic item that can be displayed in a table with [`W`] columns. +pub trait TableItem { + /// Should return fields as table cells. + fn row(&self, theme: &Theme) -> [Cell; W]; +} + +/// Grow behavior of a table column. +/// +/// [`tui::widgets::Table`] does only support percental column widths. +/// A [`ColumnWidth`] is used to specify the grow behaviour of a table column +/// and a percental column width is calculated based on that. +#[derive(Clone, Copy, Eq, PartialEq)] +pub enum ColumnWidth { + /// A fixed-size column. + Fixed(u16), + /// A growable column. + Grow, +} + +/// A generic table model with [`W`] columns. +/// +/// [`V`] needs to implement `TableItem` in order to be displayed by the +/// table this model is used in. +#[derive(Clone)] +pub struct TableModel +where + V: TableItem, +{ + /// The table header. + header: [Widget