mirror of
https://github.com/ClementTsang/bottom.git
synced 2025-07-31 01:24:31 +02:00
temp checkpoint
This commit is contained in:
parent
f093902aef
commit
14cc5c400f
@ -1,6 +1,11 @@
|
|||||||
|
// TODO: Combine with layout.sh
|
||||||
|
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
|
use tui::layout::Direction;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
canvas::LayoutConstraint,
|
||||||
constants::DEFAULT_WIDGET_ID,
|
constants::DEFAULT_WIDGET_ID,
|
||||||
error::{BottomError, Result},
|
error::{BottomError, Result},
|
||||||
};
|
};
|
||||||
@ -9,8 +14,7 @@ use crate::{
|
|||||||
/// config.
|
/// config.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct BottomLayout {
|
pub struct BottomLayout {
|
||||||
pub rows: Vec<BottomRow>,
|
pub rows: BottomContainer,
|
||||||
pub total_row_height_ratio: u32,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Represents a start and end coordinate in some dimension.
|
// Represents a start and end coordinate in some dimension.
|
||||||
@ -534,190 +538,234 @@ impl BottomLayout {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_basic_default(use_battery: bool) -> Self {
|
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 table_widgets = if use_battery {
|
||||||
let disk_widget = BottomWidget::new(BottomWidgetType::Disk, 4)
|
let disk =
|
||||||
.canvas_handle_width(true)
|
BottomWidget::new(BottomWidgetType::Disk, 4, LayoutConstraint::CanvasHandled)
|
||||||
.up_neighbour(Some(100))
|
.up_neighbour(Some(100))
|
||||||
.left_neighbour(Some(8))
|
.left_neighbour(Some(8))
|
||||||
.right_neighbour(Some(DEFAULT_WIDGET_ID + 2));
|
.right_neighbour(Some(DEFAULT_WIDGET_ID + 2));
|
||||||
|
|
||||||
let proc_sort = BottomWidget::new(BottomWidgetType::ProcSort, DEFAULT_WIDGET_ID + 2)
|
let proc_sort = BottomWidget::new(
|
||||||
.canvas_handle_width(true)
|
BottomWidgetType::ProcSort,
|
||||||
.up_neighbour(Some(100))
|
DEFAULT_WIDGET_ID + 2,
|
||||||
.down_neighbour(Some(DEFAULT_WIDGET_ID + 1))
|
LayoutConstraint::CanvasHandled,
|
||||||
.left_neighbour(Some(4))
|
)
|
||||||
.right_neighbour(Some(DEFAULT_WIDGET_ID))
|
.up_neighbour(Some(100))
|
||||||
.width_ratio(1)
|
.down_neighbour(Some(DEFAULT_WIDGET_ID + 1))
|
||||||
.parent_reflector(Some((WidgetDirection::Right, 2)));
|
.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)
|
let proc = BottomWidget::new(
|
||||||
.canvas_handle_width(true)
|
BottomWidgetType::Proc,
|
||||||
.up_neighbour(Some(100))
|
DEFAULT_WIDGET_ID,
|
||||||
.down_neighbour(Some(DEFAULT_WIDGET_ID + 1))
|
LayoutConstraint::CanvasHandled,
|
||||||
.left_neighbour(Some(DEFAULT_WIDGET_ID + 2))
|
)
|
||||||
.right_neighbour(Some(7))
|
.up_neighbour(Some(100))
|
||||||
.width_ratio(2);
|
.down_neighbour(Some(DEFAULT_WIDGET_ID + 1))
|
||||||
|
.left_neighbour(Some(DEFAULT_WIDGET_ID + 2))
|
||||||
|
.right_neighbour(Some(7));
|
||||||
|
|
||||||
let proc_search =
|
let proc_search = BottomWidget::new(
|
||||||
BottomWidget::new(BottomWidgetType::ProcSearch, DEFAULT_WIDGET_ID + 1)
|
BottomWidgetType::ProcSearch,
|
||||||
.canvas_handle_width(true)
|
DEFAULT_WIDGET_ID + 1,
|
||||||
.up_neighbour(Some(DEFAULT_WIDGET_ID))
|
LayoutConstraint::CanvasHandled,
|
||||||
.left_neighbour(Some(4))
|
)
|
||||||
.right_neighbour(Some(7))
|
.up_neighbour(Some(DEFAULT_WIDGET_ID))
|
||||||
.parent_reflector(Some((WidgetDirection::Up, 1)));
|
.left_neighbour(Some(4))
|
||||||
|
.right_neighbour(Some(7))
|
||||||
|
.parent_reflector(Some((WidgetDirection::Up, 1)));
|
||||||
|
|
||||||
let temp = BottomWidget::new(BottomWidgetType::Temp, 7)
|
let temp =
|
||||||
.canvas_handle_width(true)
|
BottomWidget::new(BottomWidgetType::Temp, 7, LayoutConstraint::CanvasHandled)
|
||||||
.up_neighbour(Some(100))
|
.up_neighbour(Some(100))
|
||||||
.left_neighbour(Some(DEFAULT_WIDGET_ID))
|
.left_neighbour(Some(DEFAULT_WIDGET_ID))
|
||||||
.right_neighbour(Some(8));
|
.right_neighbour(Some(8));
|
||||||
|
|
||||||
let battery = BottomWidget::new(BottomWidgetType::Battery, 8)
|
let battery = BottomWidget::new(
|
||||||
.canvas_handle_width(true)
|
BottomWidgetType::Battery,
|
||||||
.up_neighbour(Some(100))
|
8,
|
||||||
.left_neighbour(Some(7))
|
LayoutConstraint::CanvasHandled,
|
||||||
.right_neighbour(Some(4));
|
)
|
||||||
|
.up_neighbour(Some(100))
|
||||||
|
.left_neighbour(Some(7))
|
||||||
|
.right_neighbour(Some(4));
|
||||||
|
|
||||||
vec![
|
vec![
|
||||||
BottomCol::new(vec![
|
BottomElement::Widget(disk),
|
||||||
BottomColRow::new(vec![disk_widget]).canvas_handle_height(true)
|
BottomElement::Container(BottomContainer::column(
|
||||||
])
|
vec![
|
||||||
.canvas_handle_width(true),
|
BottomElement::Container(BottomContainer::row(
|
||||||
BottomCol::new(vec![
|
vec![
|
||||||
BottomColRow::new(vec![proc_sort, proc])
|
BottomElement::Widget(proc_sort),
|
||||||
.canvas_handle_height(true)
|
BottomElement::Widget(proc),
|
||||||
.total_widget_ratio(3),
|
],
|
||||||
BottomColRow::new(vec![proc_search]).canvas_handle_height(true),
|
LayoutConstraint::CanvasHandled,
|
||||||
])
|
)),
|
||||||
.canvas_handle_width(true),
|
BottomElement::Element(proc_search),
|
||||||
BottomCol::new(vec![
|
],
|
||||||
BottomColRow::new(vec![temp]).canvas_handle_height(true)
|
LayoutConstraint::CanvasHandled,
|
||||||
])
|
)),
|
||||||
.canvas_handle_width(true),
|
BottomElement::Widget(temp),
|
||||||
BottomCol::new(vec![
|
BottomElement::Widget(battery),
|
||||||
BottomColRow::new(vec![battery]).canvas_handle_height(true)
|
|
||||||
])
|
|
||||||
.canvas_handle_width(true),
|
|
||||||
]
|
]
|
||||||
} else {
|
} else {
|
||||||
let disk = BottomWidget::new(BottomWidgetType::Disk, 4)
|
let disk =
|
||||||
.canvas_handle_width(true)
|
BottomWidget::new(BottomWidgetType::Disk, 4, LayoutConstraint::CanvasHandled)
|
||||||
.up_neighbour(Some(100))
|
.up_neighbour(Some(100))
|
||||||
.left_neighbour(Some(7))
|
.left_neighbour(Some(7))
|
||||||
.right_neighbour(Some(DEFAULT_WIDGET_ID + 2));
|
.right_neighbour(Some(DEFAULT_WIDGET_ID + 2));
|
||||||
|
|
||||||
let proc_sort = BottomWidget::new(BottomWidgetType::ProcSort, DEFAULT_WIDGET_ID + 2)
|
let proc_sort = BottomWidget::new(
|
||||||
.canvas_handle_width(true)
|
BottomWidgetType::ProcSort,
|
||||||
.up_neighbour(Some(100))
|
DEFAULT_WIDGET_ID + 2,
|
||||||
.down_neighbour(Some(DEFAULT_WIDGET_ID + 1))
|
LayoutConstraint::CanvasHandled,
|
||||||
.left_neighbour(Some(4))
|
)
|
||||||
.right_neighbour(Some(DEFAULT_WIDGET_ID))
|
.up_neighbour(Some(100))
|
||||||
.parent_reflector(Some((WidgetDirection::Right, 2)));
|
.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)
|
let proc = BottomWidget::new(
|
||||||
.canvas_handle_width(true)
|
BottomWidgetType::Proc,
|
||||||
.up_neighbour(Some(100))
|
DEFAULT_WIDGET_ID,
|
||||||
.down_neighbour(Some(DEFAULT_WIDGET_ID + 1))
|
LayoutConstraint::CanvasHandled,
|
||||||
.left_neighbour(Some(DEFAULT_WIDGET_ID + 2))
|
)
|
||||||
.right_neighbour(Some(7));
|
.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 =
|
let proc_search = BottomWidget::new(
|
||||||
BottomWidget::new(BottomWidgetType::ProcSearch, DEFAULT_WIDGET_ID + 1)
|
BottomWidgetType::ProcSearch,
|
||||||
.canvas_handle_width(true)
|
DEFAULT_WIDGET_ID + 1,
|
||||||
.up_neighbour(Some(DEFAULT_WIDGET_ID))
|
LayoutConstraint::CanvasHandled,
|
||||||
.left_neighbour(Some(4))
|
)
|
||||||
.right_neighbour(Some(7))
|
.up_neighbour(Some(DEFAULT_WIDGET_ID))
|
||||||
.parent_reflector(Some((WidgetDirection::Up, 1)));
|
.left_neighbour(Some(4))
|
||||||
|
.right_neighbour(Some(7))
|
||||||
|
.parent_reflector(Some((WidgetDirection::Up, 1)));
|
||||||
|
|
||||||
let temp = BottomWidget::new(BottomWidgetType::Temp, 7)
|
let temp =
|
||||||
.canvas_handle_width(true)
|
BottomWidget::new(BottomWidgetType::Temp, 7, LayoutConstraint::CanvasHandled)
|
||||||
.up_neighbour(Some(100))
|
.up_neighbour(Some(100))
|
||||||
.left_neighbour(Some(DEFAULT_WIDGET_ID))
|
.left_neighbour(Some(DEFAULT_WIDGET_ID))
|
||||||
.right_neighbour(Some(4));
|
.right_neighbour(Some(4));
|
||||||
|
|
||||||
vec![
|
vec![
|
||||||
BottomCol::new(vec![
|
BottomElement::Widget(disk),
|
||||||
BottomColRow::new(vec![disk]).canvas_handle_height(true)
|
BottomElement::Container(BottomContainer::column(
|
||||||
])
|
vec![
|
||||||
.canvas_handle_width(true),
|
BottomElement::Container(BottomContainer::row(
|
||||||
BottomCol::new(vec![
|
vec![
|
||||||
BottomColRow::new(vec![proc_sort, proc]).canvas_handle_height(true),
|
BottomElement::Widget(proc_sort),
|
||||||
BottomColRow::new(vec![proc_search]).canvas_handle_height(true),
|
BottomElement::Widget(proc),
|
||||||
])
|
],
|
||||||
.canvas_handle_width(true),
|
LayoutConstraint::CanvasHandled,
|
||||||
BottomCol::new(vec![
|
)),
|
||||||
BottomColRow::new(vec![temp]).canvas_handle_height(true)
|
BottomElement::Element(proc_search),
|
||||||
])
|
],
|
||||||
.canvas_handle_width(true),
|
LayoutConstraint::CanvasHandled,
|
||||||
|
)),
|
||||||
|
BottomElement::Widget(temp),
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
let cpu = BottomWidget::new(BottomWidgetType::BasicCpu, 1)
|
let children = vec![
|
||||||
.canvas_handle_width(true)
|
BottomElement::Container(BottomContainer::row(
|
||||||
.down_neighbour(Some(2));
|
vec![BottomElement::Widget(cpu)],
|
||||||
|
LayoutConstraint::CanvasHandled,
|
||||||
let mem = BottomWidget::new(BottomWidgetType::BasicMem, 2)
|
)),
|
||||||
.canvas_handle_width(true)
|
BottomElement::Container(BottomContainer::row(
|
||||||
.up_neighbour(Some(1))
|
vec![BottomElement::Widget(mem), BottomElement::Widget(net)],
|
||||||
.down_neighbour(Some(100))
|
LayoutConstraint::CanvasHandled,
|
||||||
.right_neighbour(Some(3));
|
)),
|
||||||
|
BottomElement::Container(BottomContainer::row(
|
||||||
let net = BottomWidget::new(BottomWidgetType::BasicNet, 3)
|
vec![BottomElement::Widget(table)],
|
||||||
.canvas_handle_width(true)
|
LayoutConstraint::CanvasHandled,
|
||||||
.up_neighbour(Some(1))
|
)),
|
||||||
.down_neighbour(Some(100))
|
BottomElement::Container(BottomContainer::row(
|
||||||
.left_neighbour(Some(2));
|
table_widgets,
|
||||||
|
LayoutConstraint::CanvasHandled,
|
||||||
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: BottomContainer::column(children, LayoutConstraint::Grow),
|
||||||
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),
|
|
||||||
],
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub enum BottomLayoutNode {
|
/// Either a container or a leaf widget.
|
||||||
// Container(BottomContainer),
|
#[derive(Clone, Debug)]
|
||||||
// Widget(BottomWidget),
|
pub enum BottomElement {
|
||||||
// }
|
Container(BottomContainer),
|
||||||
|
Widget(BottomWidget),
|
||||||
|
}
|
||||||
|
|
||||||
// pub struct BottomContainer {
|
/// A container that contains other elements.
|
||||||
// children: Vec<BottomLayoutNode>,
|
///
|
||||||
// root_ratio: u32,
|
/// XXX: A somewhat temporary, intermediary implementation while we move around some things.
|
||||||
// growth_type: BottomLayoutNodeSizing,
|
#[derive(Clone, Debug)]
|
||||||
// }
|
pub struct BottomContainer {
|
||||||
|
pub direction: Direction,
|
||||||
|
pub children: Vec<BottomElement>,
|
||||||
|
pub constraint: LayoutConstraint,
|
||||||
|
}
|
||||||
|
|
||||||
// pub enum BottomContainerType {
|
impl BottomContainer {
|
||||||
// Row,
|
/// Create a new row container.
|
||||||
// Col,
|
pub fn row(children: Vec<BottomElement>, constraint: LayoutConstraint) -> Self {
|
||||||
// }
|
Self {
|
||||||
|
direction: Direction::Horizontal,
|
||||||
|
children,
|
||||||
|
constraint,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// pub enum BottomLayoutNodeSizing {
|
/// Create a new column container.
|
||||||
// Ratio(u32),
|
pub fn column(children: Vec<BottomElement>, constraint: LayoutConstraint) -> Self {
|
||||||
// CanvasHandles,
|
Self {
|
||||||
// FlexGrow,
|
direction: Direction::Vertical,
|
||||||
// }
|
children,
|
||||||
|
constraint,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Represents a single row in the layout.
|
/// Represents a single row in the layout.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
@ -872,51 +920,37 @@ impl WidgetDirection {
|
|||||||
pub struct BottomWidget {
|
pub struct BottomWidget {
|
||||||
pub widget_type: BottomWidgetType,
|
pub widget_type: BottomWidgetType,
|
||||||
pub widget_id: u64,
|
pub widget_id: u64,
|
||||||
pub width_ratio: u32,
|
|
||||||
pub left_neighbour: Option<u64>,
|
pub left_neighbour: Option<u64>,
|
||||||
pub right_neighbour: Option<u64>,
|
pub right_neighbour: Option<u64>,
|
||||||
pub up_neighbour: Option<u64>,
|
pub up_neighbour: Option<u64>,
|
||||||
pub down_neighbour: Option<u64>,
|
pub down_neighbour: Option<u64>,
|
||||||
|
pub constraint: LayoutConstraint,
|
||||||
/// 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,
|
|
||||||
|
|
||||||
/// The value is the direction to bounce, as well as the parent offset.
|
/// The value is the direction to bounce, as well as the parent offset.
|
||||||
pub parent_reflector: Option<(WidgetDirection, u64)>,
|
pub parent_reflector: Option<(WidgetDirection, u64)>,
|
||||||
|
|
||||||
/// Top left corner when drawn, for mouse click detection. (x, y)
|
/// Top left corner when drawn, for mouse click detection. (x, y)
|
||||||
pub top_left_corner: Option<(u16, u16)>,
|
pub top_left_corner: Option<(u16, u16)>,
|
||||||
|
|
||||||
/// Bottom right corner when drawn, for mouse click detection. (x, y)
|
/// Bottom right corner when drawn, for mouse click detection. (x, y)
|
||||||
pub bottom_right_corner: Option<(u16, u16)>,
|
pub bottom_right_corner: Option<(u16, u16)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BottomWidget {
|
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 {
|
Self {
|
||||||
widget_type,
|
widget_type,
|
||||||
widget_id,
|
widget_id,
|
||||||
width_ratio: 1,
|
|
||||||
left_neighbour: None,
|
left_neighbour: None,
|
||||||
right_neighbour: None,
|
right_neighbour: None,
|
||||||
up_neighbour: None,
|
up_neighbour: None,
|
||||||
down_neighbour: None,
|
down_neighbour: None,
|
||||||
canvas_handle_width: false,
|
constraint,
|
||||||
flex_grow: false,
|
|
||||||
parent_reflector: None,
|
parent_reflector: None,
|
||||||
top_left_corner: None,
|
top_left_corner: None,
|
||||||
bottom_right_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 {
|
pub(crate) fn left_neighbour(mut self, left_neighbour: Option<u64>) -> Self {
|
||||||
self.left_neighbour = left_neighbour;
|
self.left_neighbour = left_neighbour;
|
||||||
self
|
self
|
||||||
@ -937,16 +971,6 @@ impl BottomWidget {
|
|||||||
self
|
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(
|
pub(crate) fn parent_reflector(
|
||||||
mut self, parent_reflector: Option<(WidgetDirection, u64)>,
|
mut self, parent_reflector: Option<(WidgetDirection, u64)>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
|
@ -73,8 +73,10 @@ pub struct Painter {
|
|||||||
/// The constraints of a widget relative to its parent.
|
/// 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.
|
/// This is used over ratatui's internal representation due to https://github.com/ClementTsang/bottom/issues/896.
|
||||||
|
#[derive(Clone, Debug, Default)]
|
||||||
pub enum LayoutConstraint {
|
pub enum LayoutConstraint {
|
||||||
CanvasHandled,
|
CanvasHandled,
|
||||||
|
#[default]
|
||||||
Grow,
|
Grow,
|
||||||
Ratio(u32, u32),
|
Ratio(u32, u32),
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ use starship_battery::Manager;
|
|||||||
use self::config::{cpu::CpuConfig, layout::Row, process_columns::ProcessConfig};
|
use self::config::{cpu::CpuConfig, layout::Row, process_columns::ProcessConfig};
|
||||||
use crate::{
|
use crate::{
|
||||||
app::{filter::Filter, layout_manager::*, *},
|
app::{filter::Filter, layout_manager::*, *},
|
||||||
canvas::{styling::CanvasStyling, ColourScheme},
|
canvas::{styling::CanvasStyling, ColourScheme, LayoutConstraint},
|
||||||
constants::*,
|
constants::*,
|
||||||
data_collection::temperature::TemperatureType,
|
data_collection::temperature::TemperatureType,
|
||||||
utils::{
|
utils::{
|
||||||
@ -503,7 +503,7 @@ pub fn get_widget_layout(
|
|||||||
BottomLayout::init_basic_default(get_use_battery(matches, config))
|
BottomLayout::init_basic_default(get_use_battery(matches, config))
|
||||||
} else {
|
} else {
|
||||||
let ref_row: Vec<Row>; // Required to handle reference
|
let ref_row: Vec<Row>; // Required to handle reference
|
||||||
let rows = match &config.row {
|
let config_rows = match &config.row {
|
||||||
Some(r) => r,
|
Some(r) => r,
|
||||||
None => {
|
None => {
|
||||||
// This cannot (like it really shouldn't) fail!
|
// 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 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 {
|
let rows = BottomContainer::column(
|
||||||
rows: rows
|
config_rows
|
||||||
.iter()
|
.iter()
|
||||||
.map(|row| {
|
.map(|row| {
|
||||||
row.convert_row_to_bottom_row(
|
row.create_row_layout(
|
||||||
&mut iter_id,
|
&mut iter_id,
|
||||||
&mut total_height_ratio,
|
total_height_ratio,
|
||||||
&mut default_widget_id,
|
&mut default_widget_id,
|
||||||
&default_widget_type,
|
&default_widget_type,
|
||||||
&mut default_widget_count,
|
&mut default_widget_count,
|
||||||
@ -535,8 +535,10 @@ pub fn get_widget_layout(
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
.collect::<error::Result<Vec<_>>>()?,
|
.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!
|
// Confirm that we have at least ONE widget left - if not, error out!
|
||||||
if iter_id > 0 {
|
if iter_id > 0 {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use serde::{Deserialize, Serialize};
|
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
|
/// Represents a row. This has a length of some sort (optional) and a vector
|
||||||
/// of children.
|
/// of children.
|
||||||
@ -11,33 +11,48 @@ pub struct Row {
|
|||||||
pub child: Option<Vec<RowChildren>>,
|
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;
|
let cpu_id = *iter_id;
|
||||||
*iter_id += 1;
|
*iter_id += 1;
|
||||||
let legend_id = *iter_id;
|
let legend_id = *iter_id;
|
||||||
|
let constraint = LayoutConstraint::Ratio(second_ratio, second_total);
|
||||||
if left_legend {
|
let children = if left_legend {
|
||||||
BottomColRow::new(vec![
|
vec![
|
||||||
BottomWidget::new(BottomWidgetType::CpuLegend, legend_id)
|
BottomElement::Widget(
|
||||||
.width_ratio(3)
|
BottomWidget::new(
|
||||||
.canvas_handle_width(true)
|
BottomWidgetType::CpuLegend,
|
||||||
|
legend_id,
|
||||||
|
LayoutConstraint::CanvasHandled,
|
||||||
|
)
|
||||||
.parent_reflector(Some((WidgetDirection::Right, 1))),
|
.parent_reflector(Some((WidgetDirection::Right, 1))),
|
||||||
BottomWidget::new(BottomWidgetType::Cpu, cpu_id)
|
),
|
||||||
.width_ratio(17)
|
BottomElement::Widget(BottomWidget::new(
|
||||||
.flex_grow(true),
|
BottomWidgetType::Cpu,
|
||||||
])
|
cpu_id,
|
||||||
|
LayoutConstraint::Grow,
|
||||||
|
)),
|
||||||
|
]
|
||||||
} else {
|
} else {
|
||||||
BottomColRow::new(vec![
|
vec![
|
||||||
BottomWidget::new(BottomWidgetType::Cpu, cpu_id)
|
BottomElement::Widget(BottomWidget::new(
|
||||||
.width_ratio(17)
|
BottomWidgetType::Cpu,
|
||||||
.flex_grow(true),
|
cpu_id,
|
||||||
BottomWidget::new(BottomWidgetType::CpuLegend, legend_id)
|
LayoutConstraint::Grow,
|
||||||
.width_ratio(3)
|
)),
|
||||||
.canvas_handle_width(true)
|
BottomElement::Widget(
|
||||||
|
BottomWidget::new(
|
||||||
|
BottomWidgetType::CpuLegend,
|
||||||
|
legend_id,
|
||||||
|
LayoutConstraint::CanvasHandled,
|
||||||
|
)
|
||||||
.parent_reflector(Some((WidgetDirection::Left, 1))),
|
.parent_reflector(Some((WidgetDirection::Left, 1))),
|
||||||
])
|
),
|
||||||
}
|
]
|
||||||
.total_widget_ratio(20)
|
};
|
||||||
|
|
||||||
|
BottomElement::Container(BottomContainer::row(children, constraint))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new_proc_sort(sort_id: u64) -> BottomWidget {
|
fn new_proc_sort(sort_id: u64) -> BottomWidget {
|
||||||
@ -57,26 +72,24 @@ fn new_proc_search(search_id: u64) -> BottomWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Row {
|
impl Row {
|
||||||
pub fn convert_row_to_bottom_row(
|
pub fn create_row_layout(
|
||||||
&self, iter_id: &mut u64, total_height_ratio: &mut u32, default_widget_id: &mut u64,
|
&self, iter_id: &mut u64, first_total: u32, default_widget_id: &mut u64,
|
||||||
default_widget_type: &Option<BottomWidgetType>, default_widget_count: &mut u64,
|
default_widget_type: &Option<BottomWidgetType>, default_widget_count: &mut u64,
|
||||||
left_legend: bool,
|
left_legend: bool,
|
||||||
) -> Result<BottomRow> {
|
) -> Result<BottomElement> {
|
||||||
// TODO: In the future we want to also add percentages.
|
// TODO: In the future we want to also add percentages.
|
||||||
// But for MVP, we aren't going to bother.
|
// 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();
|
let mut children = Vec::new();
|
||||||
|
|
||||||
*total_height_ratio += row_ratio;
|
let mut second_total = 0;
|
||||||
|
|
||||||
let mut total_col_ratio = 0;
|
|
||||||
if let Some(row_children) = &self.child {
|
if let Some(row_children) = &self.child {
|
||||||
for row_child in row_children {
|
for row_child in row_children {
|
||||||
match row_child {
|
match row_child {
|
||||||
RowChildren::Widget(widget) => {
|
RowChildren::Widget(widget) => {
|
||||||
*iter_id += 1;
|
*iter_id += 1;
|
||||||
let width_ratio = widget.ratio.unwrap_or(1);
|
let second_ratio = widget.ratio.unwrap_or(1);
|
||||||
total_col_ratio += width_ratio;
|
second_total += second_ratio;
|
||||||
let widget_type = widget.widget_type.parse::<BottomWidgetType>()?;
|
let widget_type = widget.widget_type.parse::<BottomWidgetType>()?;
|
||||||
|
|
||||||
if let Some(default_widget_type_val) = default_widget_type {
|
if let Some(default_widget_type_val) = default_widget_type {
|
||||||
@ -98,8 +111,7 @@ impl Row {
|
|||||||
|
|
||||||
children.push(match widget_type {
|
children.push(match widget_type {
|
||||||
BottomWidgetType::Cpu => {
|
BottomWidgetType::Cpu => {
|
||||||
BottomCol::new(vec![new_cpu(left_legend, iter_id)])
|
new_cpu(left_legend, iter_id, second_ratio, second_total)
|
||||||
.col_width_ratio(width_ratio)
|
|
||||||
}
|
}
|
||||||
BottomWidgetType::Proc => {
|
BottomWidgetType::Proc => {
|
||||||
let proc_id = *iter_id;
|
let proc_id = *iter_id;
|
||||||
@ -183,13 +195,16 @@ impl Row {
|
|||||||
.col_row_height_ratio(col_row_height_ratio),
|
.col_row_height_ratio(col_row_height_ratio),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
_ => col_row_children.push(
|
_ => col_row_children.push(BottomColRow::new(vec![
|
||||||
BottomColRow::new(vec![BottomWidget::new(
|
BottomWidget::new(
|
||||||
widget_type,
|
widget_type,
|
||||||
*iter_id,
|
*iter_id,
|
||||||
)])
|
LayoutConstraint::Ratio(
|
||||||
.col_row_height_ratio(col_row_height_ratio),
|
col_row_height_ratio,
|
||||||
),
|
total_col_row_ratio,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
])),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -217,9 +232,10 @@ impl Row {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(BottomRow::new(children)
|
Ok(BottomElement::Container(BottomContainer::row(
|
||||||
.total_col_ratio(total_col_ratio)
|
children,
|
||||||
.row_height_ratio(row_ratio))
|
LayoutConstraint::Ratio(first_ratio, first_total),
|
||||||
|
)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ fn test_create_layout(
|
|||||||
rows: rows
|
rows: rows
|
||||||
.iter()
|
.iter()
|
||||||
.map(|row| {
|
.map(|row| {
|
||||||
row.convert_row_to_bottom_row(
|
row.create_row_layout(
|
||||||
&mut iter_id,
|
&mut iter_id,
|
||||||
&mut total_height_ratio,
|
&mut total_height_ratio,
|
||||||
&mut default_widget_id,
|
&mut default_widget_id,
|
||||||
@ -252,7 +252,7 @@ fn test_default_widget_in_layout() {
|
|||||||
rows: rows
|
rows: rows
|
||||||
.iter()
|
.iter()
|
||||||
.map(|row| {
|
.map(|row| {
|
||||||
row.convert_row_to_bottom_row(
|
row.create_row_layout(
|
||||||
&mut iter_id,
|
&mut iter_id,
|
||||||
&mut total_height_ratio,
|
&mut total_height_ratio,
|
||||||
&mut default_widget_id,
|
&mut default_widget_id,
|
||||||
@ -285,7 +285,7 @@ fn test_default_widget_by_option() {
|
|||||||
rows: rows
|
rows: rows
|
||||||
.iter()
|
.iter()
|
||||||
.map(|row| {
|
.map(|row| {
|
||||||
row.convert_row_to_bottom_row(
|
row.create_row_layout(
|
||||||
&mut iter_id,
|
&mut iter_id,
|
||||||
&mut total_height_ratio,
|
&mut total_height_ratio,
|
||||||
&mut default_widget_id,
|
&mut default_widget_id,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user