diff options
author | ClementTsang <34804052+ClementTsang@users.noreply.github.com> | 2023-06-02 03:34:25 -0400 |
---|---|---|
committer | ClementTsang <34804052+ClementTsang@users.noreply.github.com> | 2023-06-03 11:31:44 -0400 |
commit | 6555d2d7514077fdff37fa55214382afb81ebe7e (patch) | |
tree | 3394ae7bc22d262ce9a556eb75d3dff975d0752a | |
parent | 4a95b09b46279dc713419ac690419acb4c4a4716 (diff) |
other: refactor BottomWidget
-rw-r--r-- | src/app/layout_manager.rs | 140 | ||||
-rw-r--r-- | src/canvas.rs | 18 | ||||
-rw-r--r-- | src/options/layout_options.rs | 34 |
3 files changed, 84 insertions, 108 deletions
diff --git a/src/app/layout_manager.rs b/src/app/layout_manager.rs index dacebc31..4ac882a0 100644 --- a/src/app/layout_manager.rs +++ b/src/app/layout_manager.rs @@ -40,7 +40,6 @@ impl BottomLayout { // Now we need to create the correct mapping for moving from a specific // widget to another - let mut layout_mapping: BTreeMap<LineSegment, ColumnMappings> = BTreeMap::new(); let mut total_height = 0; for row in &self.rows { @@ -533,45 +532,38 @@ impl BottomLayout { pub fn init_basic_default(use_battery: bool) -> Self { let table_widgets = if use_battery { - let disk_widget = BottomWidget::new(BottomWidgetType::Disk, 4) - .canvas_handle_width(true) + let disk_widget = BottomWidget::new_handled(BottomWidgetType::Disk, 4) .up_neighbour(Some(100)) .left_neighbour(Some(8)) .right_neighbour(Some(DEFAULT_WIDGET_ID + 2)); - let proc_sort = BottomWidget::new(BottomWidgetType::ProcSort, DEFAULT_WIDGET_ID + 2) - .canvas_handle_width(true) - .up_neighbour(Some(100)) - .down_neighbour(Some(DEFAULT_WIDGET_ID + 1)) - .left_neighbour(Some(4)) - .right_neighbour(Some(DEFAULT_WIDGET_ID)) - .width_ratio(1) - .parent_reflector(Some((WidgetDirection::Right, 2))); + let proc_sort = + BottomWidget::new_handled(BottomWidgetType::ProcSort, DEFAULT_WIDGET_ID + 2) + .up_neighbour(Some(100)) + .down_neighbour(Some(DEFAULT_WIDGET_ID + 1)) + .left_neighbour(Some(4)) + .right_neighbour(Some(DEFAULT_WIDGET_ID)) + .parent_reflector(Some((WidgetDirection::Right, 2))); - let proc = BottomWidget::new(BottomWidgetType::Proc, DEFAULT_WIDGET_ID) - .canvas_handle_width(true) + let proc = BottomWidget::new_handled(BottomWidgetType::Proc, DEFAULT_WIDGET_ID) .up_neighbour(Some(100)) .down_neighbour(Some(DEFAULT_WIDGET_ID + 1)) .left_neighbour(Some(DEFAULT_WIDGET_ID + 2)) - .right_neighbour(Some(7)) - .width_ratio(2); + .right_neighbour(Some(7)); let proc_search = - BottomWidget::new(BottomWidgetType::ProcSearch, DEFAULT_WIDGET_ID + 1) - .canvas_handle_width(true) + BottomWidget::new_handled(BottomWidgetType::ProcSearch, DEFAULT_WIDGET_ID + 1) .up_neighbour(Some(DEFAULT_WIDGET_ID)) .left_neighbour(Some(4)) .right_neighbour(Some(7)) .parent_reflector(Some((WidgetDirection::Up, 1))); - let temp = BottomWidget::new(BottomWidgetType::Temp, 7) - .canvas_handle_width(true) + let temp = BottomWidget::new_handled(BottomWidgetType::Temp, 7) .up_neighbour(Some(100)) .left_neighbour(Some(DEFAULT_WIDGET_ID)) .right_neighbour(Some(8)); - let battery = BottomWidget::new(BottomWidgetType::Battery, 8) - .canvas_handle_width(true) + let battery = BottomWidget::new_handled(BottomWidgetType::Battery, 8) .up_neighbour(Some(100)) .left_neighbour(Some(7)) .right_neighbour(Some(4)); @@ -598,37 +590,33 @@ impl BottomLayout { .canvas_handle_width(true), ] } else { - let disk = BottomWidget::new(BottomWidgetType::Disk, 4) - .canvas_handle_width(true) + let disk = BottomWidget::new_handled(BottomWidgetType::Disk, 4) .up_neighbour(Some(100)) .left_neighbour(Some(7)) .right_neighbour(Some(DEFAULT_WIDGET_ID + 2)); - let proc_sort = BottomWidget::new(BottomWidgetType::ProcSort, DEFAULT_WIDGET_ID + 2) - .canvas_handle_width(true) - .up_neighbour(Some(100)) - .down_neighbour(Some(DEFAULT_WIDGET_ID + 1)) - .left_neighbour(Some(4)) - .right_neighbour(Some(DEFAULT_WIDGET_ID)) - .parent_reflector(Some((WidgetDirection::Right, 2))); + let proc_sort = + BottomWidget::new_handled(BottomWidgetType::ProcSort, DEFAULT_WIDGET_ID + 2) + .up_neighbour(Some(100)) + .down_neighbour(Some(DEFAULT_WIDGET_ID + 1)) + .left_neighbour(Some(4)) + .right_neighbour(Some(DEFAULT_WIDGET_ID)) + .parent_reflector(Some((WidgetDirection::Right, 2))); - let proc = BottomWidget::new(BottomWidgetType::Proc, DEFAULT_WIDGET_ID) - .canvas_handle_width(true) + let proc = BottomWidget::new_handled(BottomWidgetType::Proc, DEFAULT_WIDGET_ID) .up_neighbour(Some(100)) .down_neighbour(Some(DEFAULT_WIDGET_ID + 1)) .left_neighbour(Some(DEFAULT_WIDGET_ID + 2)) .right_neighbour(Some(7)); let proc_search = - BottomWidget::new(BottomWidgetType::ProcSearch, DEFAULT_WIDGET_ID + 1) - .canvas_handle_width(true) + BottomWidget::new_handled(BottomWidgetType::ProcSearch, DEFAULT_WIDGET_ID + 1) .up_neighbour(Some(DEFAULT_WIDGET_ID)) .left_neighbour(Some(4)) .right_neighbour(Some(7)) .parent_reflector(Some((WidgetDirection::Up, 1))); - let temp = BottomWidget::new(BottomWidgetType::Temp, 7) - .canvas_handle_width(true) + let temp = BottomWidget::new_handled(BottomWidgetType::Temp, 7) .up_neighbour(Some(100)) .left_neighbour(Some(DEFAULT_WIDGET_ID)) .right_neighbour(Some(4)); @@ -650,25 +638,20 @@ impl BottomLayout { ] }; - let cpu = BottomWidget::new(BottomWidgetType::BasicCpu, 1) - .canvas_handle_width(true) - .down_neighbour(Some(2)); + let cpu = BottomWidget::new_handled(BottomWidgetType::BasicCpu, 1).down_neighbour(Some(2)); - let mem = BottomWidget::new(BottomWidgetType::BasicMem, 2) - .canvas_handle_width(true) + let mem = BottomWidget::new_handled(BottomWidgetType::BasicMem, 2) .up_neighbour(Some(1)) .down_neighbour(Some(100)) .right_neighbour(Some(3)); - let net = BottomWidget::new(BottomWidgetType::BasicNet, 3) - .canvas_handle_width(true) + let net = BottomWidget::new_handled(BottomWidgetType::BasicNet, 3) .up_neighbour(Some(1)) .down_neighbour(Some(100)) .left_neighbour(Some(2)); - let table = BottomWidget::new(BottomWidgetType::BasicTables, 100) - .canvas_handle_width(true) - .up_neighbour(Some(2)); + let table = + BottomWidget::new_handled(BottomWidgetType::BasicTables, 100).up_neighbour(Some(2)); BottomLayout { total_row_height_ratio: 3, @@ -695,6 +678,7 @@ impl BottomLayout { } } +#[derive(Debug, Clone)] pub(crate) enum BottomLayoutNode { /// A container type, containing more [`BottomLayoutNode`] children. Container(BottomContainer), @@ -704,15 +688,17 @@ pub(crate) enum BottomLayoutNode { } /// A "container" that contains more [`BottomLayoutNode`]s. +#[derive(Debug, Clone)] pub(crate) struct BottomContainer { /// The children elements. pub(crate) children: Vec<BottomLayoutNode>, /// How the container should be sized. - pub(crate) growth_type: BottomElementSizing, + pub(crate) sizing: ElementSizing, } /// The direction in which children in a [`BottomContainer`] will be laid out. +#[derive(Debug, Clone, Copy)] pub(crate) enum BottomContainerType { /// Lay out all children horizontally. Row, @@ -722,7 +708,10 @@ pub(crate) enum BottomContainerType { } /// How the element sizing should be determined. -pub(crate) enum BottomElementSizing { +/// +/// FIXME: This should honestly be matched tighter to the canvas system; we currently have two very similar ways of doing things! +#[derive(Debug, Clone, Copy)] +pub(crate) enum ElementSizing { /// Denotes that the canvas should follow the given ratio of `lhs:rhs` to determine spacing for the element. Ratio { lhs: u32, rhs: u32 }, @@ -731,7 +720,10 @@ pub(crate) enum BottomElementSizing { FlexGrow, /// Denotes that the canvas can do whatever it likes to determine spacing for the element. - CanvasHandles, + CanvasHandled, + + /// Denotes that the element should take up 100% of the space. + Fill, } /// Represents a single row in the layout. @@ -883,21 +875,28 @@ impl WidgetDirection { } /// Represents a single widget. -#[derive(Debug, Default, Clone)] +#[derive(Debug, Clone)] pub struct BottomWidget { + /// The widget "type". pub widget_type: BottomWidgetType, + + /// The ID of this widget. pub widget_id: u64, - pub width_ratio: u32, + + /// How the widget should be sized by the canvas. + pub sizing: ElementSizing, + + /// The widget ID to go to when moving to the left. pub left_neighbour: Option<u64>, + + /// The widget ID to go to when moving to the right. pub right_neighbour: Option<u64>, - pub up_neighbour: Option<u64>, - pub down_neighbour: Option<u64>, - /// If set to true, the canvas will override any ratios. - pub canvas_handle_width: bool, + /// The widget ID to go to when moving up. + pub up_neighbour: Option<u64>, - /// Whether we want this widget to take up all available room (and ignore any ratios). - pub flex_grow: bool, + /// The widget ID to go to when moving down. + pub down_neighbour: Option<u64>, /// The value is the direction to bounce, as well as the parent offset. pub parent_reflector: Option<(WidgetDirection, u64)>, @@ -910,26 +909,29 @@ pub struct BottomWidget { } impl BottomWidget { - pub(crate) fn new(widget_type: BottomWidgetType, widget_id: u64) -> Self { + pub(crate) fn new( + widget_type: BottomWidgetType, widget_id: u64, sizing: ElementSizing, + ) -> Self { Self { widget_type, widget_id, - width_ratio: 1, + sizing, left_neighbour: None, right_neighbour: None, up_neighbour: None, down_neighbour: None, - canvas_handle_width: false, - flex_grow: false, parent_reflector: None, top_left_corner: None, bottom_right_corner: None, } } - pub(crate) fn width_ratio(mut self, width_ratio: u32) -> Self { - self.width_ratio = width_ratio; - self + pub(crate) fn new_fill(widget_type: BottomWidgetType, widget_id: u64) -> Self { + Self::new(widget_type, widget_id, ElementSizing::Fill) + } + + pub(crate) fn new_handled(widget_type: BottomWidgetType, widget_id: u64) -> Self { + Self::new(widget_type, widget_id, ElementSizing::CanvasHandled) } pub(crate) fn left_neighbour(mut self, left_neighbour: Option<u64>) -> Self { @@ -952,16 +954,6 @@ impl BottomWidget { self } - pub(crate) fn canvas_handle_width(mut self, canvas_handle_width: bool) -> Self { - self.canvas_handle_width = canvas_handle_width; - self - } - - pub(crate) fn flex_grow(mut self, flex_grow: bool) -> Self { - self.flex_grow = flex_grow; - self - } - pub(crate) fn parent_reflector( mut self, parent_reflector: Option<(WidgetDirection, u64)>, ) -> Self { diff --git a/src/canvas.rs b/src/canvas.rs index efc7383d..896418a5 100644 --- a/src/canvas.rs +++ b/src/canvas.rs @@ -13,7 +13,7 @@ use tui::{ use crate::{ app::{ self, - layout_manager::{BottomColRow, BottomLayout, BottomWidgetType}, + layout_manager::{BottomColRow, BottomLayout, BottomWidgetType, ElementSizing}, App, }, constants::*, @@ -131,16 +131,12 @@ impl Painter { let mut new_new_new_widget_constraints = Vec::new(); col_row.children.iter().for_each(|widget| { - if widget.canvas_handle_width { - new_new_new_widget_constraints.push(LayoutConstraint::CanvasHandled); - } else if widget.flex_grow { - new_new_new_widget_constraints.push(LayoutConstraint::Grow); - } else { - new_new_new_widget_constraints.push(LayoutConstraint::Ratio( - widget.width_ratio, - col_row.total_widget_ratio, - )); - } + new_new_new_widget_constraints.push(match widget.sizing { + ElementSizing::Ratio { lhs, rhs } => LayoutConstraint::Ratio(lhs, rhs), + ElementSizing::FlexGrow => LayoutConstraint::Grow, + ElementSizing::CanvasHandled => LayoutConstraint::CanvasHandled, + ElementSizing::Fill => LayoutConstraint::Ratio(1, 1), + }); }); new_new_widget_constraints.push(new_new_new_widget_constraints); }); diff --git a/src/options/layout_options.rs b/src/options/layout_options.rs index 54a07f12..ff1e11dd 100644 --- a/src/options/layout_options.rs +++ b/src/options/layout_options.rs @@ -19,41 +19,30 @@ fn new_cpu(left_legend: bool, iter_id: &mut u64) -> BottomColRow { if left_legend { BottomColRow::new(vec![ - BottomWidget::new(BottomWidgetType::CpuLegend, legend_id) - .width_ratio(3) - .canvas_handle_width(true) + BottomWidget::new_handled(BottomWidgetType::CpuLegend, legend_id) .parent_reflector(Some((WidgetDirection::Right, 1))), - BottomWidget::new(BottomWidgetType::Cpu, cpu_id) - .width_ratio(17) - .flex_grow(true), + BottomWidget::new_handled(BottomWidgetType::Cpu, cpu_id), ]) } else { BottomColRow::new(vec![ - BottomWidget::new(BottomWidgetType::Cpu, cpu_id) - .width_ratio(17) - .flex_grow(true), - BottomWidget::new(BottomWidgetType::CpuLegend, legend_id) - .width_ratio(3) - .canvas_handle_width(true) + BottomWidget::new_handled(BottomWidgetType::Cpu, cpu_id), + BottomWidget::new_handled(BottomWidgetType::CpuLegend, legend_id) .parent_reflector(Some((WidgetDirection::Left, 1))), ]) } - .total_widget_ratio(20) } fn new_proc_sort(sort_id: u64) -> BottomWidget { - BottomWidget::new(BottomWidgetType::ProcSort, sort_id) - .canvas_handle_width(true) + BottomWidget::new_handled(BottomWidgetType::ProcSort, sort_id) .parent_reflector(Some((WidgetDirection::Right, 2))) - .width_ratio(1) } fn new_proc(proc_id: u64) -> BottomWidget { - BottomWidget::new(BottomWidgetType::Proc, proc_id).width_ratio(2) + BottomWidget::new_fill(BottomWidgetType::Proc, proc_id) } fn new_proc_search(search_id: u64) -> BottomWidget { - BottomWidget::new(BottomWidgetType::ProcSearch, search_id) + BottomWidget::new_fill(BottomWidgetType::ProcSearch, search_id) .parent_reflector(Some((WidgetDirection::Up, 1))) } @@ -119,10 +108,9 @@ impl Row { .total_col_row_ratio(2) .col_width_ratio(width_ratio) } - _ => BottomCol::new(vec![BottomColRow::new(vec![BottomWidget::new( - widget_type, - *iter_id, - )])]) + _ => BottomCol::new(vec![BottomColRow::new(vec![ + BottomWidget::new_fill(widget_type, *iter_id), + ])]) .col_width_ratio(width_ratio), }); } @@ -185,7 +173,7 @@ impl Row { ); } _ => col_row_children.push( - BottomColRow::new(vec![BottomWidget::new( + BottomColRow::new(vec![BottomWidget::new_fill( widget_type, *iter_id, )]) |