Debian Codegraph @

src/app/layout_manager.rs

raw symbols SCIP
1 use std::collections::BTreeMap;
2
3 use ratatui::layout::Constraint;
4
5 use crate::{constants::DEFAULT_WIDGET_ID, options::OptionError};
6
7 // Represents a start and end coordinate in some dimension.
8 type LineSegment = (u16, u16);
9
10 type WidgetMappings = (u16, BTreeMap<LineSegment, u64>);
11 type ColumnRowMappings = (u16, BTreeMap<LineSegment, WidgetMappings>);
12 type ColumnMappings = (u16, BTreeMap<LineSegment, ColumnRowMappings>);
13
14 /// Represents a more usable representation of the layout, derived from the
15 /// config.
16 ///
17 /// FIXME: This is kinda gross. Ideally optimize out the hard-coded stuff.
18 #[derive(Clone, Debug)]
19 pub struct BottomLayout {
20 pub rows: Vec<BottomRow>,
21 pub total_row_height_ratio: u16,
22 }
23
24 trait Ratio {
25 fn ratio(&self) -> u16;
26 }
27
28 impl Ratio for Constraint {
29 fn ratio(&self) -> u16 {
30 match self {
31 Constraint::Min(min) => std::cmp::max(*min, 1),
32 Constraint::Length(_) => 1,
33 Constraint::Fill(scaling) => *scaling,
34 _ => unreachable!("if this gets hit then you're refactoring layouts"),
35 }
36 }
37 }
38
39 impl BottomLayout {
40 pub fn get_movement_mappings(&mut self) {
41 #[expect(clippy::suspicious_operation_groupings)] // Have to enable this, clippy really doesn't like me doing this with tuples...
42 fn is_intersecting(a: LineSegment, b: LineSegment) -> bool {
43 a.0 >= b.0 && a.1 <= b.1
44 || a.1 >= b.1 && a.0 <= b.0
45 || a.0 <= b.0 && a.1 >= b.0
46 || a.0 >= b.0 && a.0 < b.1 && a.1 >= b.1
47 }
48
49 fn get_distance(target: LineSegment, candidate: LineSegment) -> u16 {
50 if candidate.0 < target.0 {
51 candidate.1 - target.0
52 } else if candidate.1 < target.1 {
53 candidate.1 - candidate.0
54 } else {
55 target.1 - candidate.0
56 }
57 }
58
59 // Now we need to create the correct mapping for moving from a specific
60 // widget to another
61 let mut layout_mapping: BTreeMap<LineSegment, ColumnMappings> = BTreeMap::new();
62 let mut total_height = 0;
63 for row in &self.rows {
64 let mut row_width = 0;
65 let mut row_mapping: BTreeMap<LineSegment, ColumnRowMappings> = BTreeMap::new();
66 let mut is_valid_row = false;
67 for col in &row.children {
68 let mut col_row_height = 0;
69 let mut col_mapping: BTreeMap<LineSegment, WidgetMappings> = BTreeMap::new();
70 let mut is_valid_col = false;
71
72 for col_row in &col.children {
73 let mut widget_width = 0;
74 let mut col_row_mapping: BTreeMap<LineSegment, u64> = BTreeMap::new();
75 let mut is_valid_col_row = false;
76 for widget in &col_row.children {
77 let widget_ratio = widget
78 .ratio_override
79 .unwrap_or_else(|| widget.constraint.ratio());
80
81 match widget.widget_type {
82 BottomWidgetType::Empty => {}
83 _ => {
84 is_valid_col_row = true;
85 col_row_mapping.insert(
86 (
87 widget_width * 100 / col_row.total_widget_ratio,
88 (widget_width + widget_ratio) * 100
89 / col_row.total_widget_ratio,
90 ),
91 widget.widget_id,
92 );
93 }
94 }
95 widget_width += widget_ratio;
96 }
97 if is_valid_col_row {
98 col_mapping.insert(
99 (
100 col_row_height * 100 / col.total_col_row_ratio,
101 (col_row_height + col_row.constraint.ratio()) * 100
102 / col.total_col_row_ratio,
103 ),
104 (col.total_col_row_ratio, col_row_mapping),
105 );
106 is_valid_col = true;
107 }
108
109 col_row_height += col_row.constraint.ratio();
110 }
111 if is_valid_col {
112 row_mapping.insert(
113 (
114 row_width * 100 / row.total_col_ratio,
115 (row_width + col.constraint.ratio()) * 100 / row.total_col_ratio,
116 ),
117 (row.total_col_ratio, col_mapping),
118 );
119 is_valid_row = true;
120 }
121
122 row_width += col.constraint.ratio();
123 }
124 if is_valid_row {
125 layout_mapping.insert(
126 (
127 total_height * 100 / self.total_row_height_ratio,
128 (total_height + row.constraint.ratio()) * 100 / self.total_row_height_ratio,
129 ),
130 (self.total_row_height_ratio, row_mapping),
131 );
132 }
133 total_height += row.constraint.ratio();
134 }
135
136 // Now pass through a second time; this time we want to build up
137 // our neighbour profile.
138 let mut height_cursor = 0;
139 for row in &mut self.rows {
140 let mut col_cursor = 0;
141 let row_height_percentage_start = height_cursor * 100 / self.total_row_height_ratio;
142 let row_height_percentage_end =
143 (height_cursor + row.constraint.ratio()) * 100 / self.total_row_height_ratio;
144
145 for col in &mut row.children {
146 let mut col_row_cursor = 0;
147 let col_width_percentage_start = col_cursor * 100 / row.total_col_ratio;
148 let col_width_percentage_end =
149 (col_cursor + col.constraint.ratio()) * 100 / row.total_col_ratio;
150
151 for col_row in &mut col.children {
152 let mut widget_cursor = 0;
153 let col_row_height_percentage_start =
154 col_row_cursor * 100 / col.total_col_row_ratio;
155 let col_row_height_percentage_end =
156 (col_row_cursor + col_row.constraint.ratio()) * 100
157 / col.total_col_row_ratio;
158 let col_row_children_len = col_row.children.len();
159
160 for widget in &mut col_row.children {
161 // Bail if empty.
162 if let BottomWidgetType::Empty = widget.widget_type {
163 continue;
164 }
165
166 let widget_ratio = widget
167 .ratio_override
168 .unwrap_or_else(|| widget.constraint.ratio());
169
170 let widget_width_percentage_start =
171 widget_cursor * 100 / col_row.total_widget_ratio;
172 let widget_width_percentage_end =
173 (widget_cursor + widget_ratio) * 100 / col_row.total_widget_ratio;
174
175 if let Some(current_row) = layout_mapping
176 .get(&(row_height_percentage_start, row_height_percentage_end))
177 {
178 // First check for within the same col_row for left and right
179 if let Some(current_col) = current_row
180 .1
181 .get(&(col_width_percentage_start, col_width_percentage_end))
182 && let Some(current_col_row) = current_col.1.get(&(
183 col_row_height_percentage_start,
184 col_row_height_percentage_end,
185 ))
186 {
187 if let Some(to_left_widget) = current_col_row
188 .1
189 .range(
190 ..(
191 widget_width_percentage_start,
192 widget_width_percentage_start,
193 ),
194 )
195 .next_back()
196 {
197 widget.left_neighbour = Some(*to_left_widget.1);
198 }
199
200 // Right
201 if let Some(to_right_neighbour) = current_col_row
202 .1
203 .range(
204 (
205 widget_width_percentage_end,
206 widget_width_percentage_end,
207 )..,
208 )
209 .next()
210 {
211 widget.right_neighbour = Some(*to_right_neighbour.1);
212 }
213 }
214
215 if widget.left_neighbour.is_none()
216 && let Some(to_left_col) = current_row
217 .1
218 .range(
219 ..(col_width_percentage_start, col_width_percentage_start),
220 )
221 .next_back()
222 {
223 // Check left in same row
224 let mut current_best_distance = 0;
225 let mut current_best_widget_id = widget.widget_id;
226
227 for widget_position in &(to_left_col.1).1 {
228 let candidate_start = (widget_position.0).0;
229 let candidate_end = (widget_position.0).1;
230
231 if is_intersecting(
232 (
233 col_row_height_percentage_start,
234 col_row_height_percentage_end,
235 ),
236 (candidate_start, candidate_end),
237 ) {
238 let candidate_distance = get_distance(
239 (
240 col_row_height_percentage_start,
241 col_row_height_percentage_end,
242 ),
243 (candidate_start, candidate_end),
244 );
245
246 if current_best_distance < candidate_distance
247 && let Some(new_best_widget) =
248 (widget_position.1).1.iter().next_back()
249 {
250 current_best_distance = candidate_distance + 1;
251 current_best_widget_id = *(new_best_widget.1);
252 }
253 }
254 }
255 if current_best_distance > 0 {
256 widget.left_neighbour = Some(current_best_widget_id);
257 }
258 }
259
260 if widget.right_neighbour.is_none()
261 && let Some(to_right_col) = current_row
262 .1
263 .range((col_width_percentage_end, col_width_percentage_end)..)
264 .next()
265 {
266 // Check right in same row
267 let mut current_best_distance = 0;
268 let mut current_best_widget_id = widget.widget_id;
269
270 for widget_position in &(to_right_col.1).1 {
271 let candidate_start = (widget_position.0).0;
272 let candidate_end = (widget_position.0).1;
273
274 if is_intersecting(
275 (
276 col_row_height_percentage_start,
277 col_row_height_percentage_end,
278 ),
279 (candidate_start, candidate_end),
280 ) {
281 let candidate_distance = get_distance(
282 (
283 col_row_height_percentage_start,
284 col_row_height_percentage_end,
285 ),
286 (candidate_start, candidate_end),
287 );
288
289 if current_best_distance < candidate_distance
290 && let Some(new_best_widget) =
291 (widget_position.1).1.iter().next()
292 {
293 current_best_distance = candidate_distance + 1;
294 current_best_widget_id = *(new_best_widget.1);
295 }
296 }
297 }
298 if current_best_distance > 0 {
299 widget.right_neighbour = Some(current_best_widget_id);
300 }
301 }
302
303 // Check up/down within same row;
304 // else check up/down with other rows
305 if let Some(current_col) = current_row
306 .1
307 .get(&(col_width_percentage_start, col_width_percentage_end))
308 {
309 if let Some(to_up) = current_col
310 .1
311 .range(
312 ..(
313 col_row_height_percentage_start,
314 col_row_height_percentage_start,
315 ),
316 )
317 .next_back()
318 {
319 // Now check each widget_width and pick the best
320 for candidate_widget in &(to_up.1).1 {
321 let mut current_best_distance = 0;
322 let mut current_best_widget_id = widget.widget_id;
323 if is_intersecting(
324 (
325 widget_width_percentage_start,
326 widget_width_percentage_end,
327 ),
328 ((candidate_widget.0).0, (candidate_widget.0).1),
329 ) {
330 let candidate_best_distance = get_distance(
331 (
332 widget_width_percentage_start,
333 widget_width_percentage_end,
334 ),
335 ((candidate_widget.0).0, (candidate_widget.0).1),
336 );
337
338 if current_best_distance < candidate_best_distance {
339 current_best_distance = candidate_best_distance + 1;
340 current_best_widget_id = *candidate_widget.1;
341 }
342 }
343
344 if current_best_distance > 0 {
345 widget.up_neighbour = Some(current_best_widget_id);
346 }
347 }
348 } else {
349 for next_row_up in layout_mapping
350 .range(
351 ..(
352 row_height_percentage_start,
353 row_height_percentage_start,
354 ),
355 )
356 .rev()
357 {
358 let mut current_best_distance = 0;
359 let mut current_best_widget_id = widget.widget_id;
360 let (target_start_width, target_end_width) =
361 if col_row_children_len > 1 {
362 (
363 col_width_percentage_start
364 + widget_width_percentage_start
365 * (col_width_percentage_end
366 - col_width_percentage_start)
367 / 100,
368 col_width_percentage_start
369 + widget_width_percentage_end
370 * (col_width_percentage_end
371 - col_width_percentage_start)
372 / 100,
373 )
374 } else {
375 (
376 col_width_percentage_start,
377 col_width_percentage_end,
378 )
379 };
380
381 for col_position in &(next_row_up.1).1 {
382 if let Some(next_col_row) =
383 (col_position.1).1.iter().next_back()
384 {
385 let (candidate_col_start, candidate_col_end) =
386 ((col_position.0).0, (col_position.0).1);
387 let candidate_difference =
388 candidate_col_end - candidate_col_start;
389 for candidate_widget in &(next_col_row.1).1 {
390 let candidate_start = candidate_col_start
391 + (candidate_widget.0).0
392 * candidate_difference
393 / 100;
394 let candidate_end = candidate_col_start
395 + (candidate_widget.0).1
396 * candidate_difference
397 / 100;
398
399 if is_intersecting(
400 (target_start_width, target_end_width),
401 (candidate_start, candidate_end),
402 ) {
403 let candidate_distance = get_distance(
404 (target_start_width, target_end_width),
405 (candidate_start, candidate_end),
406 );
407
408 if current_best_distance
409 < candidate_distance
410 {
411 current_best_distance =
412 candidate_distance + 1;
413 current_best_widget_id =
414 *(candidate_widget.1);
415 }
416 }
417 }
418 }
419 }
420
421 if current_best_distance > 0 {
422 widget.up_neighbour = Some(current_best_widget_id);
423 break;
424 }
425 }
426 }
427
428 if let Some(to_down) = current_col
429 .1
430 .range(
431 (
432 col_row_height_percentage_start + 1,
433 col_row_height_percentage_start + 1,
434 )..,
435 )
436 .next()
437 {
438 for candidate_widget in &(to_down.1).1 {
439 let mut current_best_distance = 0;
440 let mut current_best_widget_id = widget.widget_id;
441 if is_intersecting(
442 (
443 widget_width_percentage_start,
444 widget_width_percentage_end,
445 ),
446 ((candidate_widget.0).0, (candidate_widget.0).1),
447 ) {
448 let candidate_best_distance = get_distance(
449 (
450 widget_width_percentage_start,
451 widget_width_percentage_end,
452 ),
453 ((candidate_widget.0).0, (candidate_widget.0).1),
454 );
455
456 if current_best_distance < candidate_best_distance {
457 current_best_distance = candidate_best_distance + 1;
458 current_best_widget_id = *candidate_widget.1;
459 }
460 }
461
462 if current_best_distance > 0 {
463 widget.down_neighbour = Some(current_best_widget_id);
464 }
465 }
466 } else {
467 for next_row_down in layout_mapping.range(
468 (
469 row_height_percentage_start + 1,
470 row_height_percentage_start + 1,
471 )..,
472 ) {
473 let mut current_best_distance = 0;
474 let mut current_best_widget_id = widget.widget_id;
475 let (target_start_width, target_end_width) =
476 if col_row_children_len > 1 {
477 (
478 col_width_percentage_start
479 + widget_width_percentage_start
480 * (col_width_percentage_end
481 - col_width_percentage_start)
482 / 100,
483 col_width_percentage_start
484 + widget_width_percentage_end
485 * (col_width_percentage_end
486 - col_width_percentage_start)
487 / 100,
488 )
489 } else {
490 (
491 col_width_percentage_start,
492 col_width_percentage_end,
493 )
494 };
495
496 for col_position in &(next_row_down.1).1 {
497 if let Some(next_col_row) =
498 (col_position.1).1.iter().next()
499 {
500 let (candidate_col_start, candidate_col_end) =
501 ((col_position.0).0, (col_position.0).1);
502 let candidate_difference =
503 candidate_col_end - candidate_col_start;
504 for candidate_widget in &(next_col_row.1).1 {
505 let candidate_start = candidate_col_start
506 + (candidate_widget.0).0
507 * candidate_difference
508 / 100;
509 let candidate_end = candidate_col_start
510 + (candidate_widget.0).1
511 * candidate_difference
512 / 100;
513
514 if is_intersecting(
515 (target_start_width, target_end_width),
516 (candidate_start, candidate_end),
517 ) {
518 let candidate_distance = get_distance(
519 (target_start_width, target_end_width),
520 (candidate_start, candidate_end),
521 );
522
523 if current_best_distance
524 < candidate_distance
525 {
526 current_best_distance =
527 candidate_distance + 1;
528 current_best_widget_id =
529 *(candidate_widget.1);
530 }
531 }
532 }
533 }
534 }
535
536 if current_best_distance > 0 {
537 widget.down_neighbour = Some(current_best_widget_id);
538 break;
539 }
540 }
541 }
542 }
543 }
544 widget_cursor += widget_ratio;
545 }
546 col_row_cursor += col_row.constraint.ratio();
547 }
548 col_cursor += col.constraint.ratio();
549 }
550 height_cursor += row.constraint.ratio();
551 }
552 }
553
554 pub fn init_basic_default(use_battery: bool) -> Self {
555 let table_widgets = if use_battery {
556 let disk_widget = BottomWidget::new(BottomWidgetType::Disk, 4)
557 .canvas_handled()
558 .up_neighbour(Some(100))
559 .left_neighbour(Some(8))
560 .right_neighbour(Some(DEFAULT_WIDGET_ID + 2));
561
562 let proc_sort = BottomWidget::new(BottomWidgetType::ProcSort, DEFAULT_WIDGET_ID + 2)
563 .canvas_handled()
564 .up_neighbour(Some(100))
565 .down_neighbour(Some(DEFAULT_WIDGET_ID + 1))
566 .left_neighbour(Some(4))
567 .right_neighbour(Some(DEFAULT_WIDGET_ID))
568 .ratio(1)
569 .parent_reflector(Some((WidgetDirection::Right, 2)));
570
571 let proc = BottomWidget::new(BottomWidgetType::Proc, DEFAULT_WIDGET_ID)
572 .canvas_handled()
573 .up_neighbour(Some(100))
574 .down_neighbour(Some(DEFAULT_WIDGET_ID + 1))
575 .left_neighbour(Some(DEFAULT_WIDGET_ID + 2))
576 .right_neighbour(Some(7))
577 .ratio(2);
578
579 let proc_search =
580 BottomWidget::new(BottomWidgetType::ProcSearch, DEFAULT_WIDGET_ID + 1)
581 .canvas_handled()
582 .up_neighbour(Some(DEFAULT_WIDGET_ID))
583 .left_neighbour(Some(4))
584 .right_neighbour(Some(7))
585 .parent_reflector(Some((WidgetDirection::Up, 1)));
586
587 let temp = BottomWidget::new(BottomWidgetType::Temp, 7)
588 .canvas_handled()
589 .up_neighbour(Some(100))
590 .left_neighbour(Some(DEFAULT_WIDGET_ID))
591 .right_neighbour(Some(8));
592
593 let battery = BottomWidget::new(BottomWidgetType::Battery, 8)
594 .canvas_handled()
595 .up_neighbour(Some(100))
596 .left_neighbour(Some(7))
597 .right_neighbour(Some(4));
598
599 vec![
600 BottomCol::new(vec![BottomColRow::new(vec![disk_widget]).canvas_handled()])
601 .canvas_handled(),
602 BottomCol::new(vec![
603 BottomColRow::new(vec![proc_sort, proc])
604 .canvas_handled()
605 .total_widget_ratio(3),
606 BottomColRow::new(vec![proc_search]).canvas_handled(),
607 ])
608 .canvas_handled(),
609 BottomCol::new(vec![BottomColRow::new(vec![temp]).canvas_handled()])
610 .canvas_handled(),
611 BottomCol::new(vec![BottomColRow::new(vec![battery]).canvas_handled()])
612 .canvas_handled(),
613 ]
614 } else {
615 let disk = BottomWidget::new(BottomWidgetType::Disk, 4)
616 .canvas_handled()
617 .up_neighbour(Some(100))
618 .left_neighbour(Some(7))
619 .right_neighbour(Some(DEFAULT_WIDGET_ID + 2));
620
621 let proc_sort = BottomWidget::new(BottomWidgetType::ProcSort, DEFAULT_WIDGET_ID + 2)
622 .canvas_handled()
623 .up_neighbour(Some(100))
624 .down_neighbour(Some(DEFAULT_WIDGET_ID + 1))
625 .left_neighbour(Some(4))
626 .right_neighbour(Some(DEFAULT_WIDGET_ID))
627 .parent_reflector(Some((WidgetDirection::Right, 2)));
628
629 let proc = BottomWidget::new(BottomWidgetType::Proc, DEFAULT_WIDGET_ID)
630 .canvas_handled()
631 .up_neighbour(Some(100))
632 .down_neighbour(Some(DEFAULT_WIDGET_ID + 1))
633 .left_neighbour(Some(DEFAULT_WIDGET_ID + 2))
634 .right_neighbour(Some(7));
635
636 let proc_search =
637 BottomWidget::new(BottomWidgetType::ProcSearch, DEFAULT_WIDGET_ID + 1)
638 .canvas_handled()
639 .up_neighbour(Some(DEFAULT_WIDGET_ID))
640 .left_neighbour(Some(4))
641 .right_neighbour(Some(7))
642 .parent_reflector(Some((WidgetDirection::Up, 1)));
643
644 let temp = BottomWidget::new(BottomWidgetType::Temp, 7)
645 .canvas_handled()
646 .up_neighbour(Some(100))
647 .left_neighbour(Some(DEFAULT_WIDGET_ID))
648 .right_neighbour(Some(4));
649
650 vec![
651 BottomCol::new(vec![BottomColRow::new(vec![disk]).canvas_handled()])
652 .canvas_handled(),
653 BottomCol::new(vec![
654 BottomColRow::new(vec![proc_sort, proc]).canvas_handled(),
655 BottomColRow::new(vec![proc_search]).canvas_handled(),
656 ])
657 .canvas_handled(),
658 BottomCol::new(vec![BottomColRow::new(vec![temp]).canvas_handled()])
659 .canvas_handled(),
660 ]
661 };
662
663 let cpu = BottomWidget::new(BottomWidgetType::BasicCpu, 1)
664 .canvas_handled()
665 .down_neighbour(Some(2));
666
667 let mem = BottomWidget::new(BottomWidgetType::BasicMem, 2)
668 .canvas_handled()
669 .up_neighbour(Some(1))
670 .down_neighbour(Some(100))
671 .right_neighbour(Some(3));
672
673 let net = BottomWidget::new(BottomWidgetType::BasicNet, 3)
674 .canvas_handled()
675 .up_neighbour(Some(1))
676 .down_neighbour(Some(100))
677 .left_neighbour(Some(2));
678
679 let table = BottomWidget::new(BottomWidgetType::BasicTables, 100)
680 .canvas_handled()
681 .up_neighbour(Some(2));
682
683 BottomLayout {
684 total_row_height_ratio: 3,
685 rows: vec![
686 BottomRow::new(vec![
687 BottomCol::new(vec![BottomColRow::new(vec![cpu]).canvas_handled()])
688 .canvas_handled(),
689 ])
690 .canvas_handled(),
691 BottomRow::new(vec![
692 BottomCol::new(vec![BottomColRow::new(vec![mem, net]).canvas_handled()])
693 .canvas_handled(),
694 ])
695 .canvas_handled(),
696 BottomRow::new(vec![
697 BottomCol::new(vec![BottomColRow::new(vec![table]).canvas_handled()])
698 .canvas_handled(),
699 ])
700 .canvas_handled(),
701 BottomRow::new(table_widgets).canvas_handled(),
702 ],
703 }
704 }
705 }
706
707 /// Represents a single row in the layout.
708 #[derive(Clone, Debug)]
709 pub struct BottomRow {
710 pub children: Vec<BottomCol>,
711 pub total_col_ratio: u16,
712 pub constraint: Constraint,
713 }
714
715 impl BottomRow {
716 pub fn new(children: Vec<BottomCol>) -> Self {
717 Self {
718 children,
719 total_col_ratio: 1,
720 constraint: Constraint::Fill(1),
721 }
722 }
723
724 pub fn total_col_ratio(mut self, total_col_ratio: u16) -> Self {
725 self.total_col_ratio = total_col_ratio;
726 self
727 }
728
729 pub fn ratio(mut self, value: u16) -> Self {
730 self.constraint = Constraint::Fill(value);
731 self
732 }
733
734 pub fn canvas_handled(mut self) -> Self {
735 self.constraint = Constraint::Length(0);
736 self
737 }
738 }
739
740 /// Represents a single column in the layout. We assume that even if the column
741 /// contains only ONE element, it is still a column (rather than either a col or
742 /// a widget, as per the config, for simplicity's sake).
743 #[derive(Clone, Debug)]
744 pub struct BottomCol {
745 pub children: Vec<BottomColRow>,
746 pub total_col_row_ratio: u16,
747 pub constraint: Constraint,
748 }
749
750 impl BottomCol {
751 pub fn new(children: Vec<BottomColRow>) -> Self {
752 Self {
753 children,
754 total_col_row_ratio: 1,
755 constraint: Constraint::Fill(1),
756 }
757 }
758
759 pub fn total_col_row_ratio(mut self, total_col_row_ratio: u16) -> Self {
760 self.total_col_row_ratio = total_col_row_ratio;
761 self
762 }
763
764 pub fn ratio(mut self, value: u16) -> Self {
765 self.constraint = Constraint::Fill(value);
766 self
767 }
768
769 pub fn canvas_handled(mut self) -> Self {
770 self.constraint = Constraint::Length(0);
771 self
772 }
773 }
774
775 #[derive(Clone, Default, Debug)]
776 pub struct BottomColRow {
777 pub children: Vec<BottomWidget>,
778 pub total_widget_ratio: u16,
779 pub constraint: Constraint,
780 }
781
782 impl BottomColRow {
783 pub(crate) fn new(children: Vec<BottomWidget>) -> Self {
784 Self {
785 children,
786 total_widget_ratio: 1,
787 constraint: Constraint::Fill(1),
788 }
789 }
790
791 pub(crate) fn total_widget_ratio(mut self, total_widget_ratio: u16) -> Self {
792 self.total_widget_ratio = total_widget_ratio;
793 self
794 }
795
796 pub fn ratio(mut self, value: u16) -> Self {
797 self.constraint = Constraint::Fill(value);
798 self
799 }
800
801 pub fn canvas_handled(mut self) -> Self {
802 self.constraint = Constraint::Length(0);
803 self
804 }
805
806 pub fn grow(mut self, minimum: Option<u16>) -> Self {
807 self.constraint = Constraint::Min(minimum.unwrap_or(0));
808 self
809 }
810 }
811
812 #[derive(Debug, Clone, Eq, PartialEq)]
813 pub enum WidgetDirection {
814 Left,
815 Right,
816 Up,
817 Down,
818 }
819
820 impl WidgetDirection {
821 pub fn is_opposite(&self, other_direction: &WidgetDirection) -> bool {
822 let to_compare = match &self {
823 WidgetDirection::Left => WidgetDirection::Right,
824 WidgetDirection::Right => WidgetDirection::Left,
825 WidgetDirection::Up => WidgetDirection::Down,
826 WidgetDirection::Down => WidgetDirection::Up,
827 };
828
829 *other_direction == to_compare
830 }
831 }
832
833 /// Represents a single widget.
834 #[derive(Debug, Default, Clone)]
835 pub struct BottomWidget {
836 pub widget_type: BottomWidgetType,
837 pub widget_id: u64,
838 pub constraint: Constraint,
839 pub left_neighbour: Option<u64>,
840 pub right_neighbour: Option<u64>,
841 pub up_neighbour: Option<u64>,
842 pub down_neighbour: Option<u64>,
843
844 /// The value is the direction to bounce, as well as the parent offset.
845 pub parent_reflector: Option<(WidgetDirection, u64)>,
846
847 /// Top left corner when drawn, for mouse click detection. (x, y)
848 ///
849 /// TODO: Replace this with just an Option<Rect> for top + bottom.
850 pub top_left_corner: Option<(u16, u16)>,
851
852 /// Bottom right corner when drawn, for mouse click detection. (x, y)
853 pub bottom_right_corner: Option<(u16, u16)>,
854
855 /// TODO: REMOVE THIS LATER. This is temporary code to bridge the
856 /// old layout system with a newer system later.
857 ratio_override: Option<u16>,
858 }
859
860 impl BottomWidget {
861 pub(crate) fn new(widget_type: BottomWidgetType, widget_id: u64) -> Self {
862 Self {
863 widget_type,
864 widget_id,
865 constraint: Constraint::Fill(1),
866 left_neighbour: None,
867 right_neighbour: None,
868 up_neighbour: None,
869 down_neighbour: None,
870 parent_reflector: None,
871 top_left_corner: None,
872 bottom_right_corner: None,
873 ratio_override: None,
874 }
875 }
876
877 pub(crate) fn left_neighbour(mut self, left_neighbour: Option<u64>) -> Self {
878 self.left_neighbour = left_neighbour;
879 self
880 }
881
882 pub(crate) fn right_neighbour(mut self, right_neighbour: Option<u64>) -> Self {
883 self.right_neighbour = right_neighbour;
884 self
885 }
886
887 pub(crate) fn up_neighbour(mut self, up_neighbour: Option<u64>) -> Self {
888 self.up_neighbour = up_neighbour;
889 self
890 }
891
892 pub(crate) fn down_neighbour(mut self, down_neighbour: Option<u64>) -> Self {
893 self.down_neighbour = down_neighbour;
894 self
895 }
896
897 pub(crate) fn ratio(mut self, value: u16) -> Self {
898 self.constraint = Constraint::Fill(value);
899 self
900 }
901
902 pub fn canvas_handled(mut self) -> Self {
903 self.constraint = Constraint::Length(0);
904 self
905 }
906
907 pub fn grow(mut self, minimum: Option<u16>) -> Self {
908 self.constraint = Constraint::Min(minimum.unwrap_or(0));
909 self
910 }
911
912 /// TODO: REMOVE THIS LATER. This is temporary code to bridge the
913 /// old layout system with a newer system later.
914 pub fn with_ratio_override(mut self, ratio_override: u16) -> Self {
915 self.ratio_override = Some(ratio_override);
916 self
917 }
918
919 pub(crate) fn parent_reflector(
920 mut self, parent_reflector: Option<(WidgetDirection, u64)>,
921 ) -> Self {
922 self.parent_reflector = parent_reflector;
923 self
924 }
925 }
926
927 #[derive(Debug, Clone, Eq, PartialEq, Hash, Default)]
928 pub enum BottomWidgetType {
929 #[default]
930 Empty,
931 Cpu,
932 CpuLegend,
933 Mem,
934 Net,
935 Proc,
936 ProcSearch,
937 ProcSort,
938 Temp,
939 TempGraph,
940 Disk,
941 DiskIoGraph,
942 BasicCpu,
943 BasicMem,
944 BasicNet,
945 BasicTables,
946 Battery,
947 }
948
949 impl BottomWidgetType {
950 pub fn is_widget_table(&self) -> bool {
951 use BottomWidgetType::*;
952 matches!(self, Disk | Proc | ProcSort | Temp | CpuLegend)
953 }
954
955 pub fn is_widget_graph(&self) -> bool {
956 use BottomWidgetType::*;
957 matches!(self, Cpu | Net | Mem | TempGraph | DiskIoGraph)
958 }
959
960 pub fn get_pretty_name(&self) -> &str {
961 use BottomWidgetType::*;
962 match self {
963 Cpu => "CPU",
964 Mem => "Memory",
965 Net => "Network",
966 Proc => "Processes",
967 Temp => "Temperature",
968 Disk => "Disks",
969 Battery => "Battery",
970 TempGraph => "Temperature",
971 _ => "",
972 }
973 }
974 }
975
976 impl std::str::FromStr for BottomWidgetType {
977 type Err = OptionError;
978
979 fn from_str(s: &str) -> Result<Self, Self::Err> {
980 let lower_case = s.to_lowercase();
981 match lower_case.as_str() {
982 "cpu" => Ok(BottomWidgetType::Cpu),
983 "mem" | "memory" => Ok(BottomWidgetType::Mem),
984 "net" | "network" => Ok(BottomWidgetType::Net),
985 "proc" | "process" | "processes" => Ok(BottomWidgetType::Proc),
986 "temp" | "temperature" => Ok(BottomWidgetType::Temp),
987 "temp_graph" | "temperature_graph" => Ok(BottomWidgetType::TempGraph),
988 "disk" => Ok(BottomWidgetType::Disk),
989 "disk_io_graph" => Ok(BottomWidgetType::DiskIoGraph),
990 "empty" => Ok(BottomWidgetType::Empty),
991 #[cfg(feature = "battery")]
992 "battery" | "batt" => Ok(BottomWidgetType::Battery),
993 _ => {
994 #[cfg(feature = "battery")]
995 {
996 Err(OptionError::config(format!(
997 "'{s}' is an invalid widget name.
998
999 Supported widget names:
1000 +--------------------------------+
1001 | cpu |
1002 +--------------------------------+
1003 | mem, memory |
1004 +--------------------------------+
1005 | net, network |
1006 +--------------------------------+
1007 | proc, process, processes |
1008 +--------------------------------+
1009 | temp, temperature |
1010 +--------------------------------+
1011 | temp_graph, temperature_graph |
1012 +--------------------------------+
1013 | disk |
1014 +--------------------------------+
1015 | disk_io_graph |
1016 +--------------------------------+
1017 | batt, battery |
1018 +--------------------------------+
1019 | empty |
1020 +--------------------------------+
1021 ",
1022 )))
1023 }
1024 #[cfg(not(feature = "battery"))]
1025 {
1026 Err(OptionError::config(format!(
1027 "'{s}' is an invalid widget name.
1028
1029 Supported widget names:
1030 +--------------------------------+
1031 | cpu |
1032 +--------------------------------+
1033 | mem, memory |
1034 +--------------------------------+
1035 | net, network |
1036 +--------------------------------+
1037 | proc, process, processes |
1038 +--------------------------------+
1039 | temp, temperature |
1040 +--------------------------------+
1041 | temp_graph, temperature_graph |
1042 +--------------------------------+
1043 | disk |
1044 +--------------------------------+
1045 | disk_io_graph |
1046 +--------------------------------+
1047 | empty |
1048 +--------------------------------+
1049 ",
1050 )))
1051 }
1052 }
1053 }
1054 }
1055 }
1056
1057 #[derive(Clone, Default, Debug, Copy)]
1058 pub struct UsedWidgets {
1059 pub use_cpu: bool,
1060 pub use_mem: bool,
1061 pub use_cache: bool,
1062 pub use_gpu: bool,
1063 pub use_net: bool,
1064 pub use_proc: bool,
1065 pub use_disk: bool,
1066 pub use_temp: bool,
1067 pub use_temp_graph: bool,
1068 pub use_disk_io_graph: bool,
1069 pub use_battery: bool,
1070 }