refactor: remove TypedBuilder for col (#1162)

* refactor: remove TypedBuilder for col

* clean up basic
This commit is contained in:
Clement Tsang 2023-05-20 01:27:42 -04:00 committed by GitHub
parent b6f9a9a98d
commit 8a49c49267
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 218 additions and 196 deletions

View file

@ -535,185 +535,167 @@ impl BottomLayout {
pub fn init_basic_default(use_battery: bool) -> Self { pub fn init_basic_default(use_battery: bool) -> Self {
let table_widgets = if use_battery { let table_widgets = if use_battery {
let disk_widget = BottomWidget::new(BottomWidgetType::Disk, 4)
.canvas_handle_width(true)
.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 = BottomWidget::new(BottomWidgetType::Proc, DEFAULT_WIDGET_ID)
.canvas_handle_width(true)
.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);
let proc_search =
BottomWidget::new(BottomWidgetType::ProcSearch, DEFAULT_WIDGET_ID + 1)
.canvas_handle_width(true)
.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)
.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)
.up_neighbour(Some(100))
.left_neighbour(Some(7))
.right_neighbour(Some(4));
vec![ vec![
BottomCol::builder() BottomCol::new(vec![
.canvas_handle_width(true) BottomColRow::new(vec![disk_widget]).canvas_handle_height(true)
.children(vec![BottomColRow::new(vec![BottomWidget::new( ])
BottomWidgetType::Disk, .canvas_handle_width(true),
4, BottomCol::new(vec![
) BottomColRow::new(vec![proc_sort, proc])
.canvas_handle_width(true)
.up_neighbour(Some(100))
.left_neighbour(Some(8))
.right_neighbour(Some(DEFAULT_WIDGET_ID + 2))])
.canvas_handle_height(true)])
.build(),
BottomCol::builder()
.canvas_handle_width(true)
.children(vec![
BottomColRow::new(vec![
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))),
BottomWidget::new(BottomWidgetType::Proc, DEFAULT_WIDGET_ID)
.canvas_handle_width(true)
.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),
])
.canvas_handle_height(true) .canvas_handle_height(true)
.total_widget_ratio(3), .total_widget_ratio(3),
BottomColRow::new(vec![BottomWidget::new( BottomColRow::new(vec![proc_search]).canvas_handle_height(true),
BottomWidgetType::ProcSearch, ])
DEFAULT_WIDGET_ID + 1, .canvas_handle_width(true),
) BottomCol::new(vec![
.canvas_handle_width(true) BottomColRow::new(vec![temp]).canvas_handle_height(true)
.up_neighbour(Some(DEFAULT_WIDGET_ID)) ])
.left_neighbour(Some(4)) .canvas_handle_width(true),
.right_neighbour(Some(7)) BottomCol::new(vec![
.parent_reflector(Some((WidgetDirection::Up, 1)))]) BottomColRow::new(vec![battery]).canvas_handle_height(true)
.canvas_handle_height(true), ])
]) .canvas_handle_width(true),
.build(),
BottomCol::builder()
.canvas_handle_width(true)
.children(vec![BottomColRow::new(vec![BottomWidget::new(
BottomWidgetType::Temp,
7,
)
.canvas_handle_width(true)
.up_neighbour(Some(100))
.left_neighbour(Some(DEFAULT_WIDGET_ID))
.right_neighbour(Some(8))])
.canvas_handle_height(true)])
.build(),
BottomCol::builder()
.canvas_handle_width(true)
.children(vec![BottomColRow::new(vec![BottomWidget::new(
BottomWidgetType::Battery,
8,
)
.canvas_handle_width(true)
.up_neighbour(Some(100))
.left_neighbour(Some(7))
.right_neighbour(Some(4))])
.canvas_handle_height(true)])
.build(),
] ]
} else { } else {
let disk = BottomWidget::new(BottomWidgetType::Disk, 4)
.canvas_handle_width(true)
.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 = BottomWidget::new(BottomWidgetType::Proc, DEFAULT_WIDGET_ID)
.canvas_handle_width(true)
.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)
.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)
.up_neighbour(Some(100))
.left_neighbour(Some(DEFAULT_WIDGET_ID))
.right_neighbour(Some(4));
vec![ vec![
BottomCol::builder() BottomCol::new(vec![
.canvas_handle_width(true) BottomColRow::new(vec![disk]).canvas_handle_height(true)
.children(vec![BottomColRow::new(vec![BottomWidget::new( ])
BottomWidgetType::Disk, .canvas_handle_width(true),
4, BottomCol::new(vec![
) BottomColRow::new(vec![proc_sort, proc]).canvas_handle_height(true),
.canvas_handle_width(true) BottomColRow::new(vec![proc_search]).canvas_handle_height(true),
.up_neighbour(Some(100)) ])
.left_neighbour(Some(7)) .canvas_handle_width(true),
.right_neighbour(Some(DEFAULT_WIDGET_ID + 2))]) BottomCol::new(vec![
.canvas_handle_height(true)]) BottomColRow::new(vec![temp]).canvas_handle_height(true)
.build(), ])
BottomCol::builder() .canvas_handle_width(true),
.canvas_handle_width(true)
.children(vec![
BottomColRow::new(vec![
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))),
BottomWidget::new(BottomWidgetType::Proc, DEFAULT_WIDGET_ID)
.canvas_handle_width(true)
.up_neighbour(Some(100))
.down_neighbour(Some(DEFAULT_WIDGET_ID + 1))
.left_neighbour(Some(DEFAULT_WIDGET_ID + 2))
.right_neighbour(Some(7)),
])
.canvas_handle_height(true),
BottomColRow::new(vec![BottomWidget::new(
BottomWidgetType::ProcSearch,
DEFAULT_WIDGET_ID + 1,
)
.canvas_handle_width(true)
.up_neighbour(Some(DEFAULT_WIDGET_ID))
.left_neighbour(Some(4))
.right_neighbour(Some(7))
.parent_reflector(Some((WidgetDirection::Up, 1)))])
.canvas_handle_height(true),
])
.build(),
BottomCol::builder()
.canvas_handle_width(true)
.children(vec![BottomColRow::new(vec![BottomWidget::new(
BottomWidgetType::Temp,
7,
)
.canvas_handle_width(true)
.up_neighbour(Some(100))
.left_neighbour(Some(DEFAULT_WIDGET_ID))
.right_neighbour(Some(4))])
.canvas_handle_height(true)])
.build(),
] ]
}; };
let cpu = BottomWidget::new(BottomWidgetType::BasicCpu, 1)
.canvas_handle_width(true)
.down_neighbour(Some(2));
let mem = BottomWidget::new(BottomWidgetType::BasicMem, 2)
.canvas_handle_width(true)
.up_neighbour(Some(1))
.down_neighbour(Some(100))
.right_neighbour(Some(3));
let net = BottomWidget::new(BottomWidgetType::BasicNet, 3)
.canvas_handle_width(true)
.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));
BottomLayout { BottomLayout {
total_row_height_ratio: 3, total_row_height_ratio: 3,
rows: vec![ rows: vec![
BottomRow::builder() BottomRow::builder()
.canvas_handle_height(true) .canvas_handle_height(true)
.children(vec![BottomCol::builder() .children(vec![BottomCol::new(vec![
.canvas_handle_width(true) BottomColRow::new(vec![cpu]).canvas_handle_height(true)
.children(vec![BottomColRow::new(vec![BottomWidget::new( ])
BottomWidgetType::BasicCpu, .canvas_handle_width(true)])
1,
)
.canvas_handle_width(true)
.down_neighbour(Some(2))])
.canvas_handle_height(true)])
.build()])
.build(), .build(),
BottomRow::builder() BottomRow::builder()
.canvas_handle_height(true) .canvas_handle_height(true)
.children(vec![BottomCol::builder() .children(vec![BottomCol::new(vec![BottomColRow::new(vec![
.canvas_handle_width(true) mem, net,
.children(vec![BottomColRow::new(vec![ ])
BottomWidget::new(BottomWidgetType::BasicMem, 2) .canvas_handle_height(true)])
.canvas_handle_width(true) .canvas_handle_width(true)])
.up_neighbour(Some(1))
.down_neighbour(Some(100))
.right_neighbour(Some(3)),
BottomWidget::new(BottomWidgetType::BasicNet, 3)
.canvas_handle_width(true)
.up_neighbour(Some(1))
.down_neighbour(Some(100))
.left_neighbour(Some(2)),
])
.canvas_handle_height(true)])
.build()])
.build(), .build(),
BottomRow::builder() BottomRow::builder()
.canvas_handle_height(true) .canvas_handle_height(true)
.children(vec![BottomCol::builder() .children(vec![BottomCol::new(vec![
.canvas_handle_width(true) BottomColRow::new(vec![table]).canvas_handle_height(true)
.children(vec![BottomColRow::new(vec![BottomWidget::new( ])
BottomWidgetType::BasicTables, .canvas_handle_width(true)])
100,
)
.canvas_handle_width(true)
.up_neighbour(Some(2))])
.canvas_handle_height(true)])
.build()])
.build(), .build(),
BottomRow::builder() BottomRow::builder()
.canvas_handle_height(true) .canvas_handle_height(true)
@ -724,6 +706,28 @@ impl BottomLayout {
} }
} }
// pub enum BottomLayoutNode {
// Container(BottomContainer),
// Widget(BottomWidget),
// }
// pub struct BottomContainer {
// children: Vec<BottomLayoutNode>,
// root_ratio: u32,
// growth_type: BottomLayoutNodeSizing,
// }
// pub enum BottomContainerType {
// Row,
// Col,
// }
// pub enum BottomLayoutNodeSizing {
// Ratio(u32),
// CanvasHandles,
// FlexGrow,
// }
/// Represents a single row in the layout. /// Represents a single row in the layout.
#[derive(Clone, Debug, TypedBuilder)] #[derive(Clone, Debug, TypedBuilder)]
pub struct BottomRow { pub struct BottomRow {
@ -745,23 +749,47 @@ pub struct BottomRow {
/// Represents a single column in the layout. We assume that even if the column /// Represents a single column in the layout. We assume that even if the column
/// contains only ONE element, it is still a column (rather than either a col or /// contains only ONE element, it is still a column (rather than either a col or
/// a widget, as per the config, for simplicity's sake). /// a widget, as per the config, for simplicity's sake).
#[derive(Clone, Debug, TypedBuilder)] #[derive(Clone, Debug)]
pub struct BottomCol { pub struct BottomCol {
pub children: Vec<BottomColRow>, pub children: Vec<BottomColRow>,
#[builder(default = 1)]
pub total_col_row_ratio: u32, pub total_col_row_ratio: u32,
#[builder(default = 1)]
pub col_width_ratio: u32, pub col_width_ratio: u32,
#[builder(default = false)]
pub canvas_handle_width: bool, pub canvas_handle_width: bool,
#[builder(default = false)]
pub flex_grow: bool, pub flex_grow: bool,
} }
impl BottomCol {
pub fn new(children: Vec<BottomColRow>) -> Self {
Self {
children,
total_col_row_ratio: 1,
col_width_ratio: 1,
canvas_handle_width: false,
flex_grow: false,
}
}
pub fn total_col_row_ratio(mut self, total_col_row_ratio: u32) -> Self {
self.total_col_row_ratio = total_col_row_ratio;
self
}
pub fn col_width_ratio(mut self, col_width_ratio: u32) -> Self {
self.col_width_ratio = col_width_ratio;
self
}
pub fn canvas_handle_width(mut self, canvas_handle_width: bool) -> Self {
self.canvas_handle_width = canvas_handle_width;
self
}
pub fn flex_grow(mut self, flex_grow: bool) -> Self {
self.flex_grow = flex_grow;
self
}
}
#[derive(Clone, Default, Debug)] #[derive(Clone, Default, Debug)]
pub struct BottomColRow { pub struct BottomColRow {
pub children: Vec<BottomWidget>, pub children: Vec<BottomWidget>,

View file

@ -98,36 +98,32 @@ impl Row {
} }
children.push(match widget_type { children.push(match widget_type {
BottomWidgetType::Cpu => BottomCol::builder() BottomWidgetType::Cpu => {
.col_width_ratio(width_ratio) BottomCol::new(vec![new_cpu(left_legend, iter_id)])
.children(vec![new_cpu(left_legend, iter_id)]) .col_width_ratio(width_ratio)
.build(), }
BottomWidgetType::Proc => { BottomWidgetType::Proc => {
let proc_id = *iter_id; let proc_id = *iter_id;
let proc_search_id = *iter_id + 1; let proc_search_id = *iter_id + 1;
*iter_id += 2; *iter_id += 2;
BottomCol::builder() BottomCol::new(vec![
.total_col_row_ratio(2) BottomColRow::new(vec![
.col_width_ratio(width_ratio) new_proc_sort(*iter_id),
.children(vec![ new_proc(proc_id),
BottomColRow::new(vec![
new_proc_sort(*iter_id),
new_proc(proc_id),
])
.total_widget_ratio(3)
.flex_grow(true),
BottomColRow::new(vec![new_proc_search(proc_search_id)])
.canvas_handle_height(true),
]) ])
.build() .total_widget_ratio(3)
} .flex_grow(true),
_ => BottomCol::builder() BottomColRow::new(vec![new_proc_search(proc_search_id)])
.canvas_handle_height(true),
])
.total_col_row_ratio(2)
.col_width_ratio(width_ratio) .col_width_ratio(width_ratio)
.children(vec![BottomColRow::new(vec![BottomWidget::new( }
widget_type, _ => BottomCol::new(vec![BottomColRow::new(vec![BottomWidget::new(
*iter_id, widget_type,
)])]) *iter_id,
.build(), )])])
.col_width_ratio(width_ratio),
}); });
} }
RowChildren::Col { ratio, child } => { RowChildren::Col { ratio, child } => {
@ -213,11 +209,9 @@ impl Row {
} }
children.push( children.push(
BottomCol::builder() BottomCol::new(col_row_children)
.total_col_row_ratio(total_col_row_ratio) .total_col_row_ratio(total_col_row_ratio)
.col_width_ratio(col_width_ratio) .col_width_ratio(col_width_ratio),
.children(col_row_children)
.build(),
); );
} }
} }