Move over to datarow

This commit is contained in:
ClementTsang 2021-12-25 04:00:04 -05:00
parent 519ea23b21
commit 3e5dbb75fb
12 changed files with 81 additions and 48 deletions

View File

@ -238,7 +238,7 @@ impl Application for AppState {
self.terminator.load(SeqCst) self.terminator.load(SeqCst)
} }
fn view<'b>(&mut self, ctx: &mut ViewContext<'_>) -> Element<'static, Self::Message> { fn view<'b>(&mut self, ctx: &mut ViewContext<'_>) -> Element<Self::Message> {
use crate::tuine::FlexElement; use crate::tuine::FlexElement;
use crate::tuine::TempTable; use crate::tuine::TempTable;
use crate::tuine::TextTable; use crate::tuine::TextTable;

View File

@ -21,7 +21,7 @@ pub trait Application: Sized {
/// always returning false. /// always returning false.
fn is_terminated(&self) -> bool; fn is_terminated(&self) -> bool;
fn view<'b>(&mut self, ctx: &mut ViewContext<'_>) -> Element<'static, Self::Message>; fn view<'b>(&mut self, ctx: &mut ViewContext<'_>) -> Element<Self::Message>;
/// To run upon stopping the application. /// To run upon stopping the application.
fn destructor(&mut self) {} fn destructor(&mut self) {}

View File

@ -8,16 +8,16 @@ use crate::tuine::{
/// ///
/// Inspired by Flutter's [Container class](https://api.flutter.dev/flutter/widgets/Container-class.html). /// Inspired by Flutter's [Container class](https://api.flutter.dev/flutter/widgets/Container-class.html).
#[derive(Default)] #[derive(Default)]
pub struct Container<'a, Message> { pub struct Container<Message> {
width: Option<u16>, width: Option<u16>,
height: Option<u16>, height: Option<u16>,
child: Option<Box<Element<'a, Message>>>, child: Option<Box<Element<Message>>>,
} }
impl<'a, Message> Container<'a, Message> { impl<Message> Container<Message> {
pub fn with_child<C>(child: C) -> Self pub fn with_child<C>(child: C) -> Self
where where
C: Into<Element<'a, Message>>, C: Into<Element<Message>>,
{ {
Self { Self {
width: None, width: None,
@ -28,7 +28,7 @@ impl<'a, Message> Container<'a, Message> {
pub fn child<C>(mut self, child: Option<C>) -> Self pub fn child<C>(mut self, child: Option<C>) -> Self
where where
C: Into<Element<'a, Message>>, C: Into<Element<Message>>,
{ {
self.child = child.map(|c| Box::new(c.into())); self.child = child.map(|c| Box::new(c.into()));
self self
@ -45,7 +45,7 @@ impl<'a, Message> Container<'a, Message> {
} }
} }
impl<'a, Message> TmpComponent<Message> for Container<'a, Message> { impl<Message> TmpComponent<Message> for Container<Message> {
fn draw<B>( fn draw<B>(
&mut self, state_ctx: &mut StateContext<'_>, draw_ctx: &DrawContext<'_>, &mut self, state_ctx: &mut StateContext<'_>, draw_ctx: &DrawContext<'_>,
frame: &mut Frame<'_, B>, frame: &mut Frame<'_, B>,

View File

@ -6,28 +6,28 @@ use crate::tuine::{
use super::Axis; use super::Axis;
pub struct FlexElement<'a, Message> { pub struct FlexElement<Message> {
/// Represents a ratio with other [`FlexElement`]s on how far to expand. /// Represents a ratio with other [`FlexElement`]s on how far to expand.
pub flex: u16, pub flex: u16,
element: Element<'a, Message>, element: Element<Message>,
} }
impl<'a, Message> FlexElement<'a, Message> { impl<Message> FlexElement<Message> {
pub fn new<I: Into<Element<'a, Message>>>(element: I) -> Self { pub fn new<I: Into<Element<Message>>>(element: I) -> Self {
Self { Self {
flex: 1, flex: 1,
element: element.into(), element: element.into(),
} }
} }
pub fn with_flex<I: Into<Element<'a, Message>>>(element: I, flex: u16) -> Self { pub fn with_flex<I: Into<Element<Message>>>(element: I, flex: u16) -> Self {
Self { Self {
flex, flex,
element: element.into(), element: element.into(),
} }
} }
pub fn with_no_flex<I: Into<Element<'a, Message>>>(element: I) -> Self { pub fn with_no_flex<I: Into<Element<Message>>>(element: I) -> Self {
Self { Self {
flex: 0, flex: 0,
element: element.into(), element: element.into(),
@ -86,8 +86,8 @@ impl<'a, Message> FlexElement<'a, Message> {
} }
} }
impl<'a, Message> From<Element<'a, Message>> for FlexElement<'a, Message> { impl<Message> From<Element<Message>> for FlexElement<Message> {
fn from(element: Element<'a, Message>) -> Self { fn from(element: Element<Message>) -> Self {
Self { flex: 0, element } Self { flex: 0, element }
} }
} }

View File

@ -16,12 +16,12 @@ pub enum Axis {
Vertical, Vertical,
} }
pub struct Flex<'a, Message> { pub struct Flex<Message> {
children: Vec<FlexElement<'a, Message>>, children: Vec<FlexElement<Message>>,
alignment: Axis, alignment: Axis,
} }
impl<'a, Message> Flex<'a, Message> { impl<Message> Flex<Message> {
pub fn new(alignment: Axis) -> Self { pub fn new(alignment: Axis) -> Self {
Self { Self {
children: vec![], children: vec![],
@ -40,7 +40,7 @@ impl<'a, Message> Flex<'a, Message> {
/// Creates a new [`Flex`] with a horizontal alignment with the given children. /// Creates a new [`Flex`] with a horizontal alignment with the given children.
pub fn row_with_children<C>(children: Vec<C>) -> Self pub fn row_with_children<C>(children: Vec<C>) -> Self
where where
C: Into<FlexElement<'a, Message>>, C: Into<FlexElement<Message>>,
{ {
Self { Self {
children: children.into_iter().map(Into::into).collect(), children: children.into_iter().map(Into::into).collect(),
@ -59,7 +59,7 @@ impl<'a, Message> Flex<'a, Message> {
/// Creates a new [`Flex`] with a vertical alignment with the given children. /// Creates a new [`Flex`] with a vertical alignment with the given children.
pub fn column_with_children<C>(children: Vec<C>) -> Self pub fn column_with_children<C>(children: Vec<C>) -> Self
where where
C: Into<FlexElement<'a, Message>>, C: Into<FlexElement<Message>>,
{ {
Self { Self {
children: children.into_iter().map(Into::into).collect(), children: children.into_iter().map(Into::into).collect(),
@ -69,7 +69,7 @@ impl<'a, Message> Flex<'a, Message> {
pub fn with_child<E>(mut self, child: E) -> Self pub fn with_child<E>(mut self, child: E) -> Self
where where
E: Into<Element<'a, Message>>, E: Into<Element<Message>>,
{ {
self.children.push(FlexElement::with_no_flex(child.into())); self.children.push(FlexElement::with_no_flex(child.into()));
self self
@ -77,7 +77,7 @@ impl<'a, Message> Flex<'a, Message> {
pub fn with_flex_child<E>(mut self, child: E, flex: u16) -> Self pub fn with_flex_child<E>(mut self, child: E, flex: u16) -> Self
where where
E: Into<Element<'a, Message>>, E: Into<Element<Message>>,
{ {
self.children self.children
.push(FlexElement::with_flex(child.into(), flex)); .push(FlexElement::with_flex(child.into(), flex));
@ -85,7 +85,7 @@ impl<'a, Message> Flex<'a, Message> {
} }
} }
impl<'a, Message> TmpComponent<Message> for Flex<'a, Message> { impl<Message> TmpComponent<Message> for Flex<Message> {
fn draw<B>( fn draw<B>(
&mut self, state_ctx: &mut StateContext<'_>, draw_ctx: &DrawContext<'_>, &mut self, state_ctx: &mut StateContext<'_>, draw_ctx: &DrawContext<'_>,
frame: &mut Frame<'_, B>, frame: &mut Frame<'_, B>,

View File

@ -19,6 +19,7 @@ impl Numeric for u8 {}
impl Numeric for usize {} impl Numeric for usize {}
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
#[derive(Clone, Copy)]
#[enum_dispatch(Numeric)] #[enum_dispatch(Numeric)]
pub enum Number { pub enum Number {
f64, f64,
@ -54,6 +55,7 @@ impl Display for Number {
} }
} }
#[derive(Clone)]
pub enum DataCell { pub enum DataCell {
NumberCell(Number), NumberCell(Number),
String(Cow<'static, str>), String(Cow<'static, str>),

View File

@ -1,15 +1,30 @@
use tui::style::Style; use tui::{style::Style, widgets::Row};
use super::DataCell; use super::DataCell;
#[derive(Clone)]
pub struct DataRow { pub struct DataRow {
cells: Vec<DataCell>, pub cells: Vec<DataCell>,
style: Option<Style>, pub style: Option<Style>,
} }
impl DataRow { impl DataRow {
pub fn new(cells: Vec<DataCell>) -> Self {
Self { cells, style: None }
}
pub fn style(mut self, style: Option<Style>) -> Self { pub fn style(mut self, style: Option<Style>) -> Self {
self.style = style; self.style = style;
self self
} }
} }
impl From<DataRow> for Row<'_> {
fn from(row: DataRow) -> Self {
if let Some(style) = row.style {
Row::new(row.cells).style(style)
} else {
Row::new(row.cells)
}
}
}

View File

@ -34,13 +34,13 @@ pub struct StyleSheet {
} }
/// A sortable, scrollable table for text data. /// A sortable, scrollable table for text data.
pub struct TextTable<'a, Message> { pub struct TextTable<Message> {
key: Key, key: Key,
column_widths: Vec<u16>, column_widths: Vec<u16>,
columns: Vec<TextColumn>, columns: Vec<TextColumn>,
show_gap: bool, show_gap: bool,
show_selected_entry: bool, show_selected_entry: bool,
rows: Vec<Row<'a>>, rows: Vec<DataRow>,
style_sheet: StyleSheet, style_sheet: StyleSheet,
sortable: bool, sortable: bool,
table_gap: u16, table_gap: u16,
@ -48,7 +48,7 @@ pub struct TextTable<'a, Message> {
on_selected_click: Option<Box<dyn Fn(usize) -> Message>>, on_selected_click: Option<Box<dyn Fn(usize) -> Message>>,
} }
impl<'a, Message> TextTable<'a, Message> { impl<Message> TextTable<Message> {
#[track_caller] #[track_caller]
pub fn new<S: Into<Cow<'static, str>>>(ctx: &mut ViewContext<'_>, columns: Vec<S>) -> Self { pub fn new<S: Into<Cow<'static, str>>>(ctx: &mut ViewContext<'_>, columns: Vec<S>) -> Self {
Self { Self {
@ -72,7 +72,13 @@ impl<'a, Message> TextTable<'a, Message> {
/// Sets the row to display in the table. /// Sets the row to display in the table.
/// ///
/// Defaults to displaying no data if not set. /// Defaults to displaying no data if not set.
pub fn rows(mut self, rows: Vec<Row<'a>>) -> Self { pub fn rows(mut self, rows: Vec<DataRow>) -> Self {
self.rows = rows;
if self.sortable {
self.sort_data();
}
self self
} }
@ -98,6 +104,11 @@ impl<'a, Message> TextTable<'a, Message> {
/// Defaults to `false` if not set. /// Defaults to `false` if not set.
pub fn sortable(mut self, sortable: bool) -> Self { pub fn sortable(mut self, sortable: bool) -> Self {
self.sortable = sortable; self.sortable = sortable;
if self.sortable {
self.sort_data();
}
self self
} }
@ -120,6 +131,10 @@ impl<'a, Message> TextTable<'a, Message> {
self self
} }
fn sort_data(&mut self) {
self.rows.sort_by(|a, b| todo!());
}
fn update_column_widths(&mut self, bounds: Rect) { fn update_column_widths(&mut self, bounds: Rect) {
let total_width = bounds.width; let total_width = bounds.width;
let mut width_remaining = bounds.width; let mut width_remaining = bounds.width;
@ -169,7 +184,7 @@ impl<'a, Message> TextTable<'a, Message> {
} }
} }
impl<'a, Message> TmpComponent<Message> for TextTable<'a, Message> { impl<Message> TmpComponent<Message> for TextTable<Message> {
fn draw<B>( fn draw<B>(
&mut self, state_ctx: &mut StateContext<'_>, draw_ctx: &DrawContext<'_>, &mut self, state_ctx: &mut StateContext<'_>, draw_ctx: &DrawContext<'_>,
frame: &mut Frame<'_, B>, frame: &mut Frame<'_, B>,
@ -205,7 +220,10 @@ impl<'a, Message> TmpComponent<Message> for TextTable<'a, Message> {
let start = state.display_start_index(rect, scrollable_height as usize); let start = state.display_start_index(rect, scrollable_height as usize);
let end = min(state.num_items(), start + scrollable_height as usize); let end = min(state.num_items(), start + scrollable_height as usize);
self.rows[start..end].to_vec() self.rows.drain(start..end).into_iter().map(|row| {
let r: Row<'_> = row.into();
r
})
}; };
// Now build up our headers... // Now build up our headers...

View File

@ -1,11 +1,11 @@
use crate::tuine::{Shortcut, TextTable, TmpComponent, ViewContext}; use crate::tuine::{Shortcut, TextTable, TmpComponent, ViewContext};
/// A [`TempTable`] is a text table that is meant to display temperature data. /// A [`TempTable`] is a text table that is meant to display temperature data.
pub struct TempTable<'a, Message> { pub struct TempTable<Message> {
inner: Shortcut<Message, TextTable<'a, Message>>, inner: Shortcut<Message, TextTable<Message>>,
} }
impl<'a, Message> TempTable<'a, Message> { impl<Message> TempTable<Message> {
#[track_caller] #[track_caller]
pub fn new(ctx: &mut ViewContext<'_>) -> Self { pub fn new(ctx: &mut ViewContext<'_>) -> Self {
Self { Self {
@ -14,7 +14,7 @@ impl<'a, Message> TempTable<'a, Message> {
} }
} }
impl<'a, Message> TmpComponent<Message> for TempTable<'a, Message> { impl<Message> TmpComponent<Message> for TempTable<Message> {
fn draw<Backend>( fn draw<Backend>(
&mut self, state_ctx: &mut crate::tuine::StateContext<'_>, &mut self, state_ctx: &mut crate::tuine::StateContext<'_>,
draw_ctx: &crate::tuine::DrawContext<'_>, frame: &mut tui::Frame<'_, Backend>, draw_ctx: &crate::tuine::DrawContext<'_>, frame: &mut tui::Frame<'_, Backend>,

View File

@ -8,16 +8,16 @@ use super::{
/// An [`Element`] is an instantiated [`Component`]. /// An [`Element`] is an instantiated [`Component`].
#[enum_dispatch(TmpComponent<Message>)] #[enum_dispatch(TmpComponent<Message>)]
pub enum Element<'a, Message, C = Empty> pub enum Element<Message, C = Empty>
where where
C: TmpComponent<Message>, C: TmpComponent<Message>,
{ {
Block, Block,
Carousel, Carousel,
Container(Container<'a, Message>), Container(Container<Message>),
Flex(Flex<'a, Message>), Flex(Flex<Message>),
Shortcut(Shortcut<Message, C>), Shortcut(Shortcut<Message, C>),
TextTable(TextTable<'a, Message>), TextTable(TextTable<Message>),
Empty, Empty,
TempTable(TempTable<'a, Message>), TempTable(TempTable<Message>),
} }

View File

@ -2,7 +2,7 @@ use tui::layout::Rect;
use crate::tuine::{Bounds, Element, LayoutNode, TmpComponent}; use crate::tuine::{Bounds, Element, LayoutNode, TmpComponent};
pub fn build_layout_tree<Message>(rect: Rect, root: &Element<'_, Message>) -> LayoutNode { pub fn build_layout_tree<Message>(rect: Rect, root: &Element<Message>) -> LayoutNode {
let mut root_layout_node = LayoutNode::from_rect(rect); let mut root_layout_node = LayoutNode::from_rect(rect);
let bounds = Bounds { let bounds = Bounds {
min_width: 0, min_width: 0,

View File

@ -75,7 +75,7 @@ where
/// Handles a [`Event`]. /// Handles a [`Event`].
fn on_event<A>( fn on_event<A>(
application: &mut A, user_interface: &mut Element<'_, A::Message>, app_data: &mut AppData, application: &mut A, user_interface: &mut Element<A::Message>, app_data: &mut AppData,
layout: &mut LayoutNode, event: Event, layout: &mut LayoutNode, event: Event,
) where ) where
A: Application + 'static, A: Application + 'static,
@ -100,9 +100,7 @@ fn on_event<A>(
} }
/// Creates a new [`Element`] representing the root of the user interface. /// Creates a new [`Element`] representing the root of the user interface.
fn new_user_interface<A>( fn new_user_interface<A>(application: &mut A, app_data: &mut AppData) -> Element<A::Message>
application: &mut A, app_data: &mut AppData,
) -> Element<'static, A::Message>
where where
A: Application + 'static, A: Application + 'static,
{ {
@ -112,7 +110,7 @@ where
/// Updates the layout, and draws the given user interface. /// Updates the layout, and draws the given user interface.
fn draw<M, B>( fn draw<M, B>(
user_interface: &mut Element<'_, M>, terminal: &mut Terminal<B>, app_data: &mut AppData, user_interface: &mut Element<M>, terminal: &mut Terminal<B>, app_data: &mut AppData,
layout: &mut LayoutNode, layout: &mut LayoutNode,
) -> anyhow::Result<()> ) -> anyhow::Result<()>
where where