From 65a1ee5202193c123c8a11c7826489e5fab4efc6 Mon Sep 17 00:00:00 2001 From: ClementTsang Date: Sat, 11 Dec 2021 18:38:22 -0500 Subject: Start layout tree work --- src/tuice/component.rs | 6 +++--- src/tuice/component/base/block.rs | 4 ++-- src/tuice/component/base/carousel.rs | 4 ++-- src/tuice/component/base/column.rs | 4 ++-- src/tuice/component/base/container.rs | 6 +++--- src/tuice/component/base/row.rs | 4 ++-- src/tuice/component/base/shortcut.rs | 4 ++-- src/tuice/component/base/text_table.rs | 4 ++-- src/tuice/context.rs | 6 ------ src/tuice/draw_context.rs | 1 + src/tuice/layout/build_layout.rs | 22 ++++++++++++++++++++++ src/tuice/layout/layout_node.rs | 22 +++++++++++++--------- src/tuice/layout/mod.rs | 5 ++--- src/tuice/mod.rs | 4 ++-- 14 files changed, 58 insertions(+), 38 deletions(-) delete mode 100644 src/tuice/context.rs create mode 100644 src/tuice/draw_context.rs create mode 100644 src/tuice/layout/build_layout.rs diff --git a/src/tuice/component.rs b/src/tuice/component.rs index c359cfac..f4646ac5 100644 --- a/src/tuice/component.rs +++ b/src/tuice/component.rs @@ -6,7 +6,7 @@ pub use widget::*; use tui::{layout::Rect, Frame}; -use super::{Bounds, Context, Event, LayoutNode, Size, Status}; +use super::{Bounds, DrawContext, Event, LayoutNode, Size, Status}; /// A component displays information and can be interacted with. #[allow(unused_variables)] @@ -15,7 +15,7 @@ where Backend: tui::backend::Backend, { /// Draws the component. - fn draw(&mut self, area: Rect, context: &Context, frame: &mut Frame<'_, Backend>); + fn draw(&mut self, area: Rect, context: &DrawContext, frame: &mut Frame<'_, Backend>); /// How a component should react to an [`Event`]. /// @@ -27,7 +27,7 @@ where /// How a component should size itself and its children, given some [`Bounds`]. /// /// Defaults to returning a [`Size`] that fills up the bounds given. - fn layout(&self, bounds: Bounds) -> Size { + fn layout(&self, bounds: Bounds, node: &mut LayoutNode) -> Size { Size { width: bounds.max_width, height: bounds.max_height, diff --git a/src/tuice/component/base/block.rs b/src/tuice/component/base/block.rs index 35d3a351..e2697f1e 100644 --- a/src/tuice/component/base/block.rs +++ b/src/tuice/component/base/block.rs @@ -1,6 +1,6 @@ use tui::{backend::Backend, layout::Rect, Frame}; -use crate::tuice::{Component, Context, Event, Status}; +use crate::tuice::{Component, DrawContext, Event, Status}; pub struct Block {} @@ -8,7 +8,7 @@ impl Component for Block where B: Backend, { - fn draw(&mut self, _area: Rect, _context: &Context, _frame: &mut Frame<'_, B>) { + fn draw(&mut self, _area: Rect, _context: &DrawContext, _frame: &mut Frame<'_, B>) { todo!() } diff --git a/src/tuice/component/base/carousel.rs b/src/tuice/component/base/carousel.rs index bfdfe6ff..2169f9d0 100644 --- a/src/tuice/component/base/carousel.rs +++ b/src/tuice/component/base/carousel.rs @@ -1,6 +1,6 @@ use tui::{backend::Backend, layout::Rect, Frame}; -use crate::tuice::{Component, Context, Event, Status}; +use crate::tuice::{Component, DrawContext, Event, Status}; pub struct Carousel {} @@ -8,7 +8,7 @@ impl Component for Carousel where B: Backend, { - fn draw(&mut self, _area: Rect, _context: &Context, _frame: &mut Frame<'_, B>) { + fn draw(&mut self, _area: Rect, _context: &DrawContext, _frame: &mut Frame<'_, B>) { todo!() } diff --git a/src/tuice/component/base/column.rs b/src/tuice/component/base/column.rs index 02b084f4..3c15f893 100644 --- a/src/tuice/component/base/column.rs +++ b/src/tuice/component/base/column.rs @@ -1,6 +1,6 @@ use tui::{backend::Backend, layout::Rect, Frame}; -use crate::tuice::{Component, Context, Event, Status}; +use crate::tuice::{Component, DrawContext, Event, Status}; pub struct Column {} @@ -8,7 +8,7 @@ impl Component for Column where B: Backend, { - fn draw(&mut self, _area: Rect, _context: &Context, _frame: &mut Frame<'_, B>) { + fn draw(&mut self, _area: Rect, _context: &DrawContext, _frame: &mut Frame<'_, B>) { todo!() } diff --git a/src/tuice/component/base/container.rs b/src/tuice/component/base/container.rs index 0f1f68cd..a2c28750 100644 --- a/src/tuice/component/base/container.rs +++ b/src/tuice/component/base/container.rs @@ -1,6 +1,6 @@ use tui::{backend::Backend, layout::Rect, Frame}; -use crate::tuice::{Bounds, Component, Context, Event, LayoutNode, Length, Size, Status}; +use crate::tuice::{Bounds, Component, DrawContext, Event, Length, Size, Status, LayoutNode}; pub struct Container<'a, Message, B> where @@ -28,7 +28,7 @@ impl<'a, Message, B> Component for Container<'a, Message, B> where B: Backend, { - fn draw(&mut self, area: Rect, _context: &Context, _frame: &mut Frame<'_, B>) { + fn draw(&mut self, area: Rect, _context: &DrawContext, _frame: &mut Frame<'_, B>) { todo!() } @@ -36,7 +36,7 @@ where todo!() } - fn layout(&self, bounds: Bounds) -> Size { + fn layout(&self, bounds: Bounds, node: &mut LayoutNode) -> Size { let width = match self.width { Length::Flex => { todo!() diff --git a/src/tuice/component/base/row.rs b/src/tuice/component/base/row.rs index ebdee1c7..dcdbf5d2 100644 --- a/src/tuice/component/base/row.rs +++ b/src/tuice/component/base/row.rs @@ -1,6 +1,6 @@ use tui::{backend::Backend, layout::Rect, Frame}; -use crate::tuice::{Bounds, Component, Context, Event, Size, Status}; +use crate::tuice::{Bounds, Component, DrawContext, Event, Size, Status}; #[derive(Default)] pub struct Row<'a, Message, B> @@ -29,7 +29,7 @@ impl<'a, Message, B> Component for Row<'a, Message, B> where B: Backend, { - fn draw(&mut self, area: Rect, context: &Context, frame: &mut Frame<'_, B>) { + fn draw(&mut self, area: Rect, context: &DrawContext, frame: &mut Frame<'_, B>) { self.children.iter_mut().for_each(|child| { // TODO: This is just temp! We need layout! child.draw(area, context, frame); diff --git a/src/tuice/component/base/shortcut.rs b/src/tuice/component/base/shortcut.rs index 37a24e94..9c70d10c 100644 --- a/src/tuice/component/base/shortcut.rs +++ b/src/tuice/component/base/shortcut.rs @@ -1,6 +1,6 @@ use tui::{backend::Backend, layout::Rect, Frame}; -use crate::tuice::{Component, Context, Event, Status}; +use crate::tuice::{Component, DrawContext, Event, Status}; /// A [`Component`] to handle keyboard shortcuts and assign actions to them. /// @@ -11,7 +11,7 @@ impl Component for Shortcut where B: Backend, { - fn draw(&mut self, _area: Rect, _context: &Context, _frame: &mut Frame<'_, B>) { + fn draw(&mut self, _area: Rect, _context: &DrawContext, _frame: &mut Frame<'_, B>) { todo!() } diff --git a/src/tuice/component/base/text_table.rs b/src/tuice/component/base/text_table.rs index 597d5573..042e524c 100644 --- a/src/tuice/component/base/text_table.rs +++ b/src/tuice/component/base/text_table.rs @@ -14,7 +14,7 @@ use unicode_segmentation::UnicodeSegmentation; use crate::{ constants::TABLE_GAP_HEIGHT_LIMIT, - tuice::{Component, Context, Event, Status}, + tuice::{Component, DrawContext, Event, Status}, }; pub use self::table_column::{TextColumn, TextColumnConstraint}; @@ -179,7 +179,7 @@ impl<'a, Message, B> Component for TextTable<'a, Message> where B: Backend, { - fn draw(&mut self, area: Rect, context: &Context, frame: &mut Frame<'_, B>) { + fn draw(&mut self, area: Rect, context: &DrawContext, frame: &mut Frame<'_, B>) { self.table_gap = if !self.show_gap || (self.rows.len() + 2 > area.height.into() && area.height < TABLE_GAP_HEIGHT_LIMIT) { diff --git a/src/tuice/context.rs b/src/tuice/context.rs deleted file mode 100644 index 766e0c17..00000000 --- a/src/tuice/context.rs +++ /dev/null @@ -1,6 +0,0 @@ -use crate::app::layout_manager::LayoutNode; - -/// Internal management for drawing and the like. -pub struct Context { - layout_root: LayoutNode, -} diff --git a/src/tuice/draw_context.rs b/src/tuice/draw_context.rs new file mode 100644 index 00000000..2815d9a7 --- /dev/null +++ b/src/tuice/draw_context.rs @@ -0,0 +1 @@ +pub struct DrawContext {} diff --git a/src/tuice/layout/build_layout.rs b/src/tuice/layout/build_layout.rs new file mode 100644 index 00000000..6d8c59fb --- /dev/null +++ b/src/tuice/layout/build_layout.rs @@ -0,0 +1,22 @@ +use tui::layout::Rect; + +use crate::tuice::{Bounds, Component, LayoutNode}; + +pub fn build_layout_tree( + area: Rect, root: &Box>, +) -> LayoutNode +where + Backend: tui::backend::Backend, +{ + let mut root_layout_node = LayoutNode::from_area(area); + let bounds = Bounds { + min_width: 0, + min_height: 0, + max_width: area.width, + max_height: area.height, + }; + + root.layout(bounds, &mut root_layout_node); + + root_layout_node +} diff --git a/src/tuice/layout/layout_node.rs b/src/tuice/layout/layout_node.rs index aebaaa49..e6a5ba61 100644 --- a/src/tuice/layout/layout_node.rs +++ b/src/tuice/layout/layout_node.rs @@ -1,12 +1,16 @@ use tui::layout::Rect; -/// A node for the layout tree. -pub enum LayoutNode { - Leaf { - area: Rect, - }, - Branch { - area: Rect, - children: Vec, - }, +#[derive(Default)] +pub struct LayoutNode { + pub area: Rect, + pub children: Vec, +} + +impl LayoutNode { + pub fn from_area(area: Rect) -> Self { + Self { + area, + children: vec![], + } + } } diff --git a/src/tuice/layout/mod.rs b/src/tuice/layout/mod.rs index 903c4039..631694b0 100644 --- a/src/tuice/layout/mod.rs +++ b/src/tuice/layout/mod.rs @@ -10,6 +10,5 @@ pub use size::Size; pub mod layout_node; pub use layout_node::LayoutNode; -pub fn build_layout() -> LayoutNode { - todo!() -} +pub mod build_layout; +pub use build_layout::build_layout_tree; diff --git a/src/tuice/mod.rs b/src/tuice/mod.rs index 62ce33db..3a942e2f 100644 --- a/src/tuice/mod.rs +++ b/src/tuice/mod.rs @@ -15,5 +15,5 @@ pub use runtime::RuntimeEvent; pub mod layout; pub use layout::*; -pub mod context; -pub use context::*; +pub mod draw_context; +pub use draw_context::*; -- cgit v1.2.3