temp checkpoint

This commit is contained in:
Clement Tsang 2024-01-02 05:06:17 -05:00 committed by ClementTsang
parent f093902aef
commit 14cc5c400f
No known key found for this signature in database
GPG Key ID: DC3B7867D8D97095
5 changed files with 283 additions and 239 deletions

@ -1,6 +1,11 @@
// TODO: Combine with layout.sh
use std::collections::BTreeMap;
use tui::layout::Direction;
use crate::{
canvas::LayoutConstraint,
constants::DEFAULT_WIDGET_ID,
error::{BottomError, Result},
};
@ -9,8 +14,7 @@ use crate::{
/// config.
#[derive(Clone, Debug)]
pub struct BottomLayout {
pub rows: Vec<BottomRow>,
pub total_row_height_ratio: u32,
pub rows: BottomContainer,
}
// Represents a start and end coordinate in some dimension.
@ -534,190 +538,234 @@ impl BottomLayout {
}
pub fn init_basic_default(use_battery: bool) -> Self {
let cpu = BottomWidget::new(
BottomWidgetType::BasicCpu,
1,
LayoutConstraint::CanvasHandled,
)
.down_neighbour(Some(2));
let mem = BottomWidget::new(
BottomWidgetType::BasicMem,
2,
LayoutConstraint::CanvasHandled,
)
.up_neighbour(Some(1))
.down_neighbour(Some(100))
.right_neighbour(Some(3));
let net = BottomWidget::new(
BottomWidgetType::BasicNet,
3,
LayoutConstraint::CanvasHandled,
)
.up_neighbour(Some(1))
.down_neighbour(Some(100))
.left_neighbour(Some(2));
let table = BottomWidget::new(
BottomWidgetType::BasicTables,
100,
LayoutConstraint::CanvasHandled,
)
.up_neighbour(Some(2));
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 disk =
BottomWidget::new(BottomWidgetType::Disk, 4, LayoutConstraint::CanvasHandled)
.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(
BottomWidgetType::ProcSort,
DEFAULT_WIDGET_ID + 2,
LayoutConstraint::CanvasHandled,
)
.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))
.width_ratio(2);
let proc = BottomWidget::new(
BottomWidgetType::Proc,
DEFAULT_WIDGET_ID,
LayoutConstraint::CanvasHandled,
)
.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 proc_search = BottomWidget::new(
BottomWidgetType::ProcSearch,
DEFAULT_WIDGET_ID + 1,
LayoutConstraint::CanvasHandled,
)
.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 temp =
BottomWidget::new(BottomWidgetType::Temp, 7, LayoutConstraint::CanvasHandled)
.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));
let battery = BottomWidget::new(
BottomWidgetType::Battery,
8,
LayoutConstraint::CanvasHandled,
)
.up_neighbour(Some(100))
.left_neighbour(Some(7))
.right_neighbour(Some(4));
vec![
BottomCol::new(vec![
BottomColRow::new(vec![disk_widget]).canvas_handle_height(true)
])
.canvas_handle_width(true),
BottomCol::new(vec![
BottomColRow::new(vec![proc_sort, proc])
.canvas_handle_height(true)
.total_widget_ratio(3),
BottomColRow::new(vec![proc_search]).canvas_handle_height(true),
])
.canvas_handle_width(true),
BottomCol::new(vec![
BottomColRow::new(vec![temp]).canvas_handle_height(true)
])
.canvas_handle_width(true),
BottomCol::new(vec![
BottomColRow::new(vec![battery]).canvas_handle_height(true)
])
.canvas_handle_width(true),
BottomElement::Widget(disk),
BottomElement::Container(BottomContainer::column(
vec![
BottomElement::Container(BottomContainer::row(
vec![
BottomElement::Widget(proc_sort),
BottomElement::Widget(proc),
],
LayoutConstraint::CanvasHandled,
)),
BottomElement::Element(proc_search),
],
LayoutConstraint::CanvasHandled,
)),
BottomElement::Widget(temp),
BottomElement::Widget(battery),
]
} 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 disk =
BottomWidget::new(BottomWidgetType::Disk, 4, LayoutConstraint::CanvasHandled)
.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(
BottomWidgetType::ProcSort,
DEFAULT_WIDGET_ID + 2,
LayoutConstraint::CanvasHandled,
)
.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 = BottomWidget::new(
BottomWidgetType::Proc,
DEFAULT_WIDGET_ID,
LayoutConstraint::CanvasHandled,
)
.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 proc_search = BottomWidget::new(
BottomWidgetType::ProcSearch,
DEFAULT_WIDGET_ID + 1,
LayoutConstraint::CanvasHandled,
)
.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));
let temp =
BottomWidget::new(BottomWidgetType::Temp, 7, LayoutConstraint::CanvasHandled)
.up_neighbour(Some(100))
.left_neighbour(Some(DEFAULT_WIDGET_ID))
.right_neighbour(Some(4));
vec![
BottomCol::new(vec![
BottomColRow::new(vec![disk]).canvas_handle_height(true)
])
.canvas_handle_width(true),
BottomCol::new(vec![
BottomColRow::new(vec![proc_sort, proc]).canvas_handle_height(true),
BottomColRow::new(vec![proc_search]).canvas_handle_height(true),
])
.canvas_handle_width(true),
BottomCol::new(vec![
BottomColRow::new(vec![temp]).canvas_handle_height(true)
])
.canvas_handle_width(true),
BottomElement::Widget(disk),
BottomElement::Container(BottomContainer::column(
vec![
BottomElement::Container(BottomContainer::row(
vec![
BottomElement::Widget(proc_sort),
BottomElement::Widget(proc),
],
LayoutConstraint::CanvasHandled,
)),
BottomElement::Element(proc_search),
],
LayoutConstraint::CanvasHandled,
)),
BottomElement::Widget(temp),
]
};
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));
let children = vec![
BottomElement::Container(BottomContainer::row(
vec![BottomElement::Widget(cpu)],
LayoutConstraint::CanvasHandled,
)),
BottomElement::Container(BottomContainer::row(
vec![BottomElement::Widget(mem), BottomElement::Widget(net)],
LayoutConstraint::CanvasHandled,
)),
BottomElement::Container(BottomContainer::row(
vec![BottomElement::Widget(table)],
LayoutConstraint::CanvasHandled,
)),
BottomElement::Container(BottomContainer::row(
table_widgets,
LayoutConstraint::CanvasHandled,
)),
];
BottomLayout {
total_row_height_ratio: 3,
rows: vec![
BottomRow::new(vec![BottomCol::new(vec![
BottomColRow::new(vec![cpu]).canvas_handle_height(true)
])
.canvas_handle_width(true)])
.canvas_handle_height(true),
BottomRow::new(vec![BottomCol::new(vec![BottomColRow::new(vec![
mem, net,
])
.canvas_handle_height(true)])
.canvas_handle_width(true)])
.canvas_handle_height(true),
BottomRow::new(vec![BottomCol::new(vec![
BottomColRow::new(vec![table]).canvas_handle_height(true)
])
.canvas_handle_width(true)])
.canvas_handle_height(true),
BottomRow::new(table_widgets).canvas_handle_height(true),
],
rows: BottomContainer::column(children, LayoutConstraint::Grow),
}
}
}
// pub enum BottomLayoutNode {
// Container(BottomContainer),
// Widget(BottomWidget),
// }
/// Either a container or a leaf widget.
#[derive(Clone, Debug)]
pub enum BottomElement {
Container(BottomContainer),
Widget(BottomWidget),
}
// pub struct BottomContainer {
// children: Vec<BottomLayoutNode>,
// root_ratio: u32,
// growth_type: BottomLayoutNodeSizing,
// }
/// A container that contains other elements.
///
/// XXX: A somewhat temporary, intermediary implementation while we move around some things.
#[derive(Clone, Debug)]
pub struct BottomContainer {
pub direction: Direction,
pub children: Vec<BottomElement>,
pub constraint: LayoutConstraint,
}
// pub enum BottomContainerType {
// Row,
// Col,
// }
impl BottomContainer {
/// Create a new row container.
pub fn row(children: Vec<BottomElement>, constraint: LayoutConstraint) -> Self {
Self {
direction: Direction::Horizontal,
children,
constraint,
}
}
// pub enum BottomLayoutNodeSizing {
// Ratio(u32),
// CanvasHandles,
// FlexGrow,
// }
/// Create a new column container.
pub fn column(children: Vec<BottomElement>, constraint: LayoutConstraint) -> Self {
Self {
direction: Direction::Vertical,
children,
constraint,
}
}
}
/// Represents a single row in the layout.
#[derive(Clone, Debug)]
@ -872,51 +920,37 @@ impl WidgetDirection {
pub struct BottomWidget {
pub widget_type: BottomWidgetType,
pub widget_id: u64,
pub width_ratio: u32,
pub left_neighbour: Option<u64>,
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,
/// Whether we want this widget to take up all available room (and ignore any ratios).
pub flex_grow: bool,
pub constraint: LayoutConstraint,
/// The value is the direction to bounce, as well as the parent offset.
pub parent_reflector: Option<(WidgetDirection, u64)>,
/// Top left corner when drawn, for mouse click detection. (x, y)
pub top_left_corner: Option<(u16, u16)>,
/// Bottom right corner when drawn, for mouse click detection. (x, y)
pub bottom_right_corner: Option<(u16, u16)>,
}
impl BottomWidget {
pub(crate) fn new(widget_type: BottomWidgetType, widget_id: u64) -> Self {
pub(crate) fn new(
widget_type: BottomWidgetType, widget_id: u64, constraint: LayoutConstraint,
) -> Self {
Self {
widget_type,
widget_id,
width_ratio: 1,
left_neighbour: None,
right_neighbour: None,
up_neighbour: None,
down_neighbour: None,
canvas_handle_width: false,
flex_grow: false,
constraint,
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 left_neighbour(mut self, left_neighbour: Option<u64>) -> Self {
self.left_neighbour = left_neighbour;
self
@ -937,16 +971,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 {

@ -73,8 +73,10 @@ pub struct Painter {
/// The constraints of a widget relative to its parent.
///
/// This is used over ratatui's internal representation due to https://github.com/ClementTsang/bottom/issues/896.
#[derive(Clone, Debug, Default)]
pub enum LayoutConstraint {
CanvasHandled,
#[default]
Grow,
Ratio(u32, u32),
}

@ -24,7 +24,7 @@ use starship_battery::Manager;
use self::config::{cpu::CpuConfig, layout::Row, process_columns::ProcessConfig};
use crate::{
app::{filter::Filter, layout_manager::*, *},
canvas::{styling::CanvasStyling, ColourScheme},
canvas::{styling::CanvasStyling, ColourScheme, LayoutConstraint},
constants::*,
data_collection::temperature::TemperatureType,
utils::{
@ -503,7 +503,7 @@ pub fn get_widget_layout(
BottomLayout::init_basic_default(get_use_battery(matches, config))
} else {
let ref_row: Vec<Row>; // Required to handle reference
let rows = match &config.row {
let config_rows = match &config.row {
Some(r) => r,
None => {
// This cannot (like it really shouldn't) fail!
@ -519,15 +519,15 @@ pub fn get_widget_layout(
};
let mut iter_id = 0; // A lazy way of forcing unique IDs *shrugs*
let mut total_height_ratio = 0;
let total_height_ratio = config_rows.iter().map(|row| row.ratio.unwrap_or(1)).sum();
let mut ret_bottom_layout = BottomLayout {
rows: rows
let rows = BottomContainer::column(
config_rows
.iter()
.map(|row| {
row.convert_row_to_bottom_row(
row.create_row_layout(
&mut iter_id,
&mut total_height_ratio,
total_height_ratio,
&mut default_widget_id,
&default_widget_type,
&mut default_widget_count,
@ -535,8 +535,10 @@ pub fn get_widget_layout(
)
})
.collect::<error::Result<Vec<_>>>()?,
total_row_height_ratio: total_height_ratio,
};
LayoutConstraint::Grow,
);
let mut ret_bottom_layout = BottomLayout { rows };
// Confirm that we have at least ONE widget left - if not, error out!
if iter_id > 0 {

@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};
use crate::{app::layout_manager::*, error::Result};
use crate::{app::layout_manager::*, canvas::LayoutConstraint, error::Result};
/// Represents a row. This has a length of some sort (optional) and a vector
/// of children.
@ -11,33 +11,48 @@ pub struct Row {
pub child: Option<Vec<RowChildren>>,
}
fn new_cpu(left_legend: bool, iter_id: &mut u64) -> BottomColRow {
fn new_cpu(
left_legend: bool, iter_id: &mut u64, second_ratio: u32, second_total: u32,
) -> BottomElement {
let cpu_id = *iter_id;
*iter_id += 1;
let legend_id = *iter_id;
if left_legend {
BottomColRow::new(vec![
BottomWidget::new(BottomWidgetType::CpuLegend, legend_id)
.width_ratio(3)
.canvas_handle_width(true)
let constraint = LayoutConstraint::Ratio(second_ratio, second_total);
let children = if left_legend {
vec![
BottomElement::Widget(
BottomWidget::new(
BottomWidgetType::CpuLegend,
legend_id,
LayoutConstraint::CanvasHandled,
)
.parent_reflector(Some((WidgetDirection::Right, 1))),
BottomWidget::new(BottomWidgetType::Cpu, cpu_id)
.width_ratio(17)
.flex_grow(true),
])
),
BottomElement::Widget(BottomWidget::new(
BottomWidgetType::Cpu,
cpu_id,
LayoutConstraint::Grow,
)),
]
} 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)
vec![
BottomElement::Widget(BottomWidget::new(
BottomWidgetType::Cpu,
cpu_id,
LayoutConstraint::Grow,
)),
BottomElement::Widget(
BottomWidget::new(
BottomWidgetType::CpuLegend,
legend_id,
LayoutConstraint::CanvasHandled,
)
.parent_reflector(Some((WidgetDirection::Left, 1))),
])
}
.total_widget_ratio(20)
),
]
};
BottomElement::Container(BottomContainer::row(children, constraint))
}
fn new_proc_sort(sort_id: u64) -> BottomWidget {
@ -57,26 +72,24 @@ fn new_proc_search(search_id: u64) -> BottomWidget {
}
impl Row {
pub fn convert_row_to_bottom_row(
&self, iter_id: &mut u64, total_height_ratio: &mut u32, default_widget_id: &mut u64,
pub fn create_row_layout(
&self, iter_id: &mut u64, first_total: u32, default_widget_id: &mut u64,
default_widget_type: &Option<BottomWidgetType>, default_widget_count: &mut u64,
left_legend: bool,
) -> Result<BottomRow> {
) -> Result<BottomElement> {
// TODO: In the future we want to also add percentages.
// But for MVP, we aren't going to bother.
let row_ratio = self.ratio.unwrap_or(1);
let first_ratio = self.ratio.unwrap_or(1);
let mut children = Vec::new();
*total_height_ratio += row_ratio;
let mut total_col_ratio = 0;
let mut second_total = 0;
if let Some(row_children) = &self.child {
for row_child in row_children {
match row_child {
RowChildren::Widget(widget) => {
*iter_id += 1;
let width_ratio = widget.ratio.unwrap_or(1);
total_col_ratio += width_ratio;
let second_ratio = widget.ratio.unwrap_or(1);
second_total += second_ratio;
let widget_type = widget.widget_type.parse::<BottomWidgetType>()?;
if let Some(default_widget_type_val) = default_widget_type {
@ -98,8 +111,7 @@ impl Row {
children.push(match widget_type {
BottomWidgetType::Cpu => {
BottomCol::new(vec![new_cpu(left_legend, iter_id)])
.col_width_ratio(width_ratio)
new_cpu(left_legend, iter_id, second_ratio, second_total)
}
BottomWidgetType::Proc => {
let proc_id = *iter_id;
@ -183,13 +195,16 @@ impl Row {
.col_row_height_ratio(col_row_height_ratio),
);
}
_ => col_row_children.push(
BottomColRow::new(vec![BottomWidget::new(
_ => col_row_children.push(BottomColRow::new(vec![
BottomWidget::new(
widget_type,
*iter_id,
)])
.col_row_height_ratio(col_row_height_ratio),
),
LayoutConstraint::Ratio(
col_row_height_ratio,
total_col_row_ratio,
),
),
])),
}
}
@ -217,9 +232,10 @@ impl Row {
}
}
Ok(BottomRow::new(children)
.total_col_ratio(total_col_ratio)
.row_height_ratio(row_ratio))
Ok(BottomElement::Container(BottomContainer::row(
children,
LayoutConstraint::Ratio(first_ratio, first_total),
)))
}
}

@ -41,7 +41,7 @@ fn test_create_layout(
rows: rows
.iter()
.map(|row| {
row.convert_row_to_bottom_row(
row.create_row_layout(
&mut iter_id,
&mut total_height_ratio,
&mut default_widget_id,
@ -252,7 +252,7 @@ fn test_default_widget_in_layout() {
rows: rows
.iter()
.map(|row| {
row.convert_row_to_bottom_row(
row.create_row_layout(
&mut iter_id,
&mut total_height_ratio,
&mut default_widget_id,
@ -285,7 +285,7 @@ fn test_default_widget_by_option() {
rows: rows
.iter()
.map(|row| {
row.convert_row_to_bottom_row(
row.create_row_layout(
&mut iter_id,
&mut total_height_ratio,
&mut default_widget_id,