temp commit

This commit is contained in:
ClementTsang 2023-08-05 17:41:14 -04:00
parent 000ea7cce4
commit a0c2efe09d
No known key found for this signature in database
GPG Key ID: DC3B7867D8D97095
3 changed files with 102 additions and 57 deletions

View File

@ -15,14 +15,14 @@ type WidgetMappings = (u32, BTreeMap<LineSegment, u64>);
type ColumnRowMappings = (u32, BTreeMap<LineSegment, WidgetMappings>); type ColumnRowMappings = (u32, BTreeMap<LineSegment, WidgetMappings>);
type ColumnMappings = (u32, BTreeMap<LineSegment, ColumnRowMappings>); type ColumnMappings = (u32, BTreeMap<LineSegment, ColumnRowMappings>);
/// A "container" that contains more [`Node`]s. /// A "container" that contains more child elements, stored as [`NodeId`]s.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub(crate) struct Container { pub(crate) struct Container {
/// The children elements. /// The children elements.
pub(crate) children: Vec<NodeId>, children: Vec<NodeId>,
/// How the container should be sized. /// How the container should be sized.
pub(crate) constraint: LayoutConstraint, constraint: LayoutConstraint,
/// The direction. /// The direction.
direction: ContainerDirection, direction: ContainerDirection,
@ -45,7 +45,20 @@ impl Container {
} }
} }
/// Returns the constraint of the container.
#[inline]
pub fn constraint(&self) -> LayoutConstraint {
self.constraint
}
/// Returns a reference to the children.
#[inline]
pub fn children(&self) -> &[NodeId] {
&self.children
}
/// Returns the direction of the container. /// Returns the direction of the container.
#[inline]
pub fn direction(&self) -> ContainerDirection { pub fn direction(&self) -> ContainerDirection {
self.direction self.direction
} }
@ -188,16 +201,24 @@ impl BottomLayout {
NodeId::Widget(id) NodeId::Widget(id)
} }
/// Get the node with the corresponding ID. /// Get a reference to the [`Container`] with the corresponding ID if
/// it exists.
pub fn get_container(&self, id: usize) -> Option<&Container> { pub fn get_container(&self, id: usize) -> Option<&Container> {
self.containers.get(id) self.containers.get(id)
} }
/// Get the node with the corresponding ID. /// Get a reference to the [`BottomWidget`] with the corresponding ID if
/// it exists.
pub fn get_widget(&self, id: usize) -> Option<&BottomWidget> { pub fn get_widget(&self, id: usize) -> Option<&BottomWidget> {
self.widgets.get(id) self.widgets.get(id)
} }
/// Get a mutable reference to the [`BottomWidget`] with the corresponding
/// ID if it exists.
pub fn get_widget_mut(&mut self, id: usize) -> Option<&mut BottomWidget> {
self.widgets.get_mut(id)
}
/// Returns an iterator of all widgets. /// Returns an iterator of all widgets.
pub fn widgets_iter(&self) -> impl Iterator<Item = &BottomWidget> { pub fn widgets_iter(&self) -> impl Iterator<Item = &BottomWidget> {
self.widgets.iter() self.widgets.iter()
@ -534,7 +555,64 @@ impl BottomLayout {
layout layout
} }
/// Creates mappings to move from one widget to another.
fn get_movement_mappings(&mut self) { fn get_movement_mappings(&mut self) {
type LineSegment = (u32, u32);
// Have to enable this, clippy really doesn't like me doing this with tuples...
#[allow(clippy::suspicious_operation_groupings)]
fn is_intersecting(a: LineSegment, b: LineSegment) -> bool {
a.0 >= b.0 && a.1 <= b.1
|| a.1 >= b.1 && a.0 <= b.0
|| a.0 <= b.0 && a.1 >= b.0
|| a.0 >= b.0 && a.0 < b.1 && a.1 >= b.1
}
fn distance(target: LineSegment, candidate: LineSegment) -> u32 {
if candidate.0 < target.0 {
candidate.1 - target.0
} else if candidate.1 < target.1 {
candidate.1 - candidate.0
} else {
target.1 - candidate.0
}
}
if let Some(root_id) = self.root_id() {
let mut queue = vec![root_id];
// Build a k-d tree to have a simple virtual mapping of where each
// widget is relative to each other.
while let Some(current) = queue.pop() {
match current {
NodeId::Container(id) => if let Some(children) = self.get_container(id) {},
NodeId::Widget(id) => if let Some(widget) = self.get_widget(id) {},
}
}
// Now traverse the layout tree a second time, assigning any missing
// widget mappings where it makes sense.
queue.push(root_id);
while let Some(current) = queue.pop() {
match current {
NodeId::Container(id) => if let Some(children) = self.get_container(id) {},
NodeId::Widget(id) => {
if let Some(widget) = self.get_widget_mut(id) {
if widget.left_neighbour.is_none() {}
if widget.right_neighbour.is_none() {}
if widget.up_neighbour.is_none() {}
if widget.down_neighbour.is_none() {}
}
}
}
}
}
}
fn old_get_movement_mappings(&mut self) {
#[allow(clippy::suspicious_operation_groupings)] // Have to enable this, clippy really doesn't like me doing this with tuples... #[allow(clippy::suspicious_operation_groupings)] // Have to enable this, clippy really doesn't like me doing this with tuples...
fn is_intersecting(a: LineSegment, b: LineSegment) -> bool { fn is_intersecting(a: LineSegment, b: LineSegment) -> bool {
a.0 >= b.0 && a.1 <= b.1 a.0 >= b.0 && a.1 <= b.1

View File

@ -13,7 +13,7 @@ use tui::{
use crate::{ use crate::{
app::{ app::{
self, self,
layout_manager::{BottomColRow, BottomLayout, BottomWidget, BottomWidgetType, NodeId}, layout_manager::{BottomLayout, BottomWidget, BottomWidgetType, NodeId},
App, App,
}, },
constants::*, constants::*,
@ -620,19 +620,20 @@ impl Painter {
if let Some(container) = if let Some(container) =
self.widget_layout.get_container(current_id) self.widget_layout.get_container(current_id)
{ {
let constraints = container.children.iter().map(|child| { let constraints =
match child { container.children().iter().map(|child| {
NodeId::Container(child) => self match child {
.widget_layout NodeId::Container(child) => self
.get_container(*child) .widget_layout
.map(|c| c.constraint), .get_container(*child)
NodeId::Widget(child) => self .map(|c| c.constraint()),
.widget_layout NodeId::Widget(child) => self
.get_widget(*child) .widget_layout
.map(|w| w.constraint), .get_widget(*child)
} .map(|w| w.constraint),
.unwrap_or(LayoutConstraint::FlexGrow) }
}); .unwrap_or(LayoutConstraint::FlexGrow)
});
let rects = get_rects( let rects = get_rects(
container.direction().into(), container.direction().into(),
@ -642,7 +643,7 @@ impl Painter {
// If it's a container, push in reverse order to the stack. // If it's a container, push in reverse order to the stack.
for child in container for child in container
.children .children()
.iter() .iter()
.cloned() .cloned()
.zip(rects.into_iter()) .zip(rects.into_iter())
@ -690,40 +691,6 @@ impl Painter {
Ok(()) Ok(())
} }
fn draw_widgets_with_constraints<B: Backend>(
&self, f: &mut Frame<'_, B>, app_state: &mut App, widgets: &BottomColRow,
widget_draw_locs: &[Rect],
) {
use BottomWidgetType::*;
for (widget, widget_draw_loc) in widgets.children.iter().zip(widget_draw_locs) {
if widget_draw_loc.width >= 2 && widget_draw_loc.height >= 2 {
match &widget.widget_type {
Empty => {}
Cpu => self.draw_cpu(f, app_state, *widget_draw_loc, widget.widget_id),
Mem => self.draw_memory_graph(f, app_state, *widget_draw_loc, widget.widget_id),
Net => self.draw_network(f, app_state, *widget_draw_loc, widget.widget_id),
Temp => self.draw_temp_table(f, app_state, *widget_draw_loc, widget.widget_id),
Disk => self.draw_disk_table(f, app_state, *widget_draw_loc, widget.widget_id),
Proc => self.draw_process_widget(
f,
app_state,
*widget_draw_loc,
true,
widget.widget_id,
),
Battery => self.draw_battery_display(
f,
app_state,
*widget_draw_loc,
true,
widget.widget_id,
),
_ => {}
}
}
}
}
fn draw_widget<B: Backend>( fn draw_widget<B: Backend>(
&self, f: &mut Frame<'_, B>, app_state: &mut App, widget: &BottomWidget, rect: Rect, &self, f: &mut Frame<'_, B>, app_state: &mut App, widget: &BottomWidget, rect: Rect,
) { ) {

View File

@ -468,18 +468,18 @@ pub fn get_widget_layout(
default_widget_type, default_widget_type,
)) ))
} 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 rows = match &config.row {
Some(r) => r, Some(r) => r,
None => { None => {
// This cannot (like it really shouldn't) fail!
ref_row = toml_edit::de::from_str::<Config>(if get_use_battery(matches, config) { ref_row = toml_edit::de::from_str::<Config>(if get_use_battery(matches, config) {
DEFAULT_BATTERY_LAYOUT DEFAULT_BATTERY_LAYOUT
} else { } else {
DEFAULT_LAYOUT DEFAULT_LAYOUT
})? })?
.row .row
.unwrap(); .expect("parsing the default layouts should never fail!");
&ref_row &ref_row
} }
}; };