Debian Codegraph @

src/options/config/layout.rs

raw symbols SCIP
1 use serde::{Deserialize, Serialize};
2
3 use crate::{app::layout_manager::*, options::OptionResult};
4
5 /// Represents a row. This has a length of some sort (optional) and a vector
6 /// of children.
7 #[derive(Clone, Deserialize, Debug, Serialize)]
8 #[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
9 #[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))]
10 #[serde(rename = "row")]
11 pub struct Row {
12 pub ratio: Option<u16>,
13 pub child: Option<Vec<RowChildren>>,
14 }
15
16 fn new_cpu(cpu_left_legend: bool, iter_id: &mut u64) -> BottomColRow {
17 let cpu_id = *iter_id;
18 *iter_id += 1;
19 let legend_id = *iter_id;
20
21 if cpu_left_legend {
22 BottomColRow::new(vec![
23 BottomWidget::new(BottomWidgetType::CpuLegend, legend_id)
24 .canvas_handled()
25 .with_ratio_override(3)
26 .parent_reflector(Some((WidgetDirection::Right, 1))),
27 BottomWidget::new(BottomWidgetType::Cpu, cpu_id).grow(Some(17)),
28 ])
29 } else {
30 BottomColRow::new(vec![
31 BottomWidget::new(BottomWidgetType::Cpu, cpu_id).grow(Some(17)),
32 BottomWidget::new(BottomWidgetType::CpuLegend, legend_id)
33 .canvas_handled()
34 .with_ratio_override(3)
35 .parent_reflector(Some((WidgetDirection::Left, 1))),
36 ])
37 }
38 .total_widget_ratio(20)
39 }
40
41 fn new_proc_sort(sort_id: u64) -> BottomWidget {
42 BottomWidget::new(BottomWidgetType::ProcSort, sort_id)
43 .canvas_handled()
44 .parent_reflector(Some((WidgetDirection::Right, 2)))
45 }
46
47 fn new_proc(proc_id: u64) -> BottomWidget {
48 BottomWidget::new(BottomWidgetType::Proc, proc_id).ratio(2)
49 }
50
51 fn new_proc_search(search_id: u64) -> BottomWidget {
52 BottomWidget::new(BottomWidgetType::ProcSearch, search_id)
53 .parent_reflector(Some((WidgetDirection::Up, 1)))
54 }
55
56 impl Row {
57 pub fn convert_row_to_bottom_row(
58 &self, iter_id: &mut u64, total_height_ratio: &mut u16, default_widget_id: &mut u64,
59 default_widget_type: &Option<BottomWidgetType>, default_widget_count: &mut u64,
60 cpu_left_legend: bool,
61 ) -> OptionResult<BottomRow> {
62 // TODO: In the future we want to also add percentages.
63 // But for MVP, we aren't going to bother.
64 let row_ratio = self.ratio.unwrap_or(1);
65 let mut children = Vec::new();
66
67 *total_height_ratio += row_ratio;
68
69 let mut total_col_ratio = 0;
70 if let Some(row_children) = &self.child {
71 for row_child in row_children {
72 match row_child {
73 RowChildren::Widget(widget) => {
74 *iter_id += 1;
75 let width_ratio = widget.ratio.unwrap_or(1);
76 total_col_ratio += width_ratio;
77 let widget_type = widget.widget_type.parse::<BottomWidgetType>()?;
78
79 if let Some(default_widget_type_val) = default_widget_type {
80 if *default_widget_type_val == widget_type && *default_widget_count > 0
81 {
82 *default_widget_count -= 1;
83 if *default_widget_count == 0 {
84 *default_widget_id = *iter_id;
85 }
86 }
87 } else {
88 // Check default flag
89 if let Some(default_widget_flag) = widget.default
90 && default_widget_flag
91 {
92 *default_widget_id = *iter_id;
93 }
94 }
95
96 children.push(match widget_type {
97 BottomWidgetType::Cpu => {
98 BottomCol::new(vec![new_cpu(cpu_left_legend, iter_id)])
99 .ratio(width_ratio)
100 }
101 BottomWidgetType::Proc => {
102 let proc_id = *iter_id;
103 let proc_search_id = *iter_id + 1;
104 *iter_id += 2;
105 BottomCol::new(vec![
106 BottomColRow::new(vec![
107 new_proc_sort(*iter_id),
108 new_proc(proc_id),
109 ])
110 .grow(None)
111 .total_widget_ratio(3),
112 BottomColRow::new(vec![new_proc_search(proc_search_id)])
113 .canvas_handled(),
114 ])
115 .total_col_row_ratio(2)
116 .ratio(width_ratio)
117 }
118 _ => BottomCol::new(vec![BottomColRow::new(vec![BottomWidget::new(
119 widget_type,
120 *iter_id,
121 )])])
122 .ratio(width_ratio),
123 });
124 }
125 RowChildren::Col { ratio, child } => {
126 let col_width_ratio = ratio.unwrap_or(1);
127 total_col_ratio += col_width_ratio;
128 let mut total_col_row_ratio = 0;
129
130 let mut col_row_children: Vec<BottomColRow> = Vec::new();
131
132 for widget in child {
133 let widget_type = widget.widget_type.parse::<BottomWidgetType>()?;
134 *iter_id += 1;
135
136 if let Some(default_widget_type_val) = default_widget_type {
137 if *default_widget_type_val == widget_type
138 && *default_widget_count > 0
139 {
140 *default_widget_count -= 1;
141 if *default_widget_count == 0 {
142 *default_widget_id = *iter_id;
143 }
144 }
145 } else {
146 // Check default flag
147 if let Some(default_widget_flag) = widget.default
148 && default_widget_flag
149 {
150 *default_widget_id = *iter_id;
151 }
152 }
153
154 match widget_type {
155 BottomWidgetType::Cpu => {
156 let col_row_height_ratio = widget.ratio.unwrap_or(1);
157 total_col_row_ratio += col_row_height_ratio;
158
159 col_row_children.push(
160 new_cpu(cpu_left_legend, iter_id)
161 .ratio(col_row_height_ratio),
162 );
163 }
164 BottomWidgetType::Proc => {
165 let col_row_height_ratio = widget.ratio.unwrap_or(1) + 1;
166 total_col_row_ratio += col_row_height_ratio;
167
168 let proc_id = *iter_id;
169 let proc_search_id = *iter_id + 1;
170 *iter_id += 2;
171 col_row_children.push(
172 BottomColRow::new(vec![
173 new_proc_sort(*iter_id),
174 new_proc(proc_id),
175 ])
176 .ratio(col_row_height_ratio)
177 .total_widget_ratio(3),
178 );
179 col_row_children.push(
180 BottomColRow::new(vec![new_proc_search(proc_search_id)])
181 .canvas_handled(),
182 );
183 }
184 _ => {
185 let col_row_height_ratio = widget.ratio.unwrap_or(1);
186 total_col_row_ratio += col_row_height_ratio;
187
188 col_row_children.push(
189 BottomColRow::new(vec![BottomWidget::new(
190 widget_type,
191 *iter_id,
192 )])
193 .ratio(col_row_height_ratio),
194 )
195 }
196 }
197 }
198
199 children.push(
200 BottomCol::new(col_row_children)
201 .total_col_row_ratio(total_col_row_ratio)
202 .ratio(col_width_ratio),
203 );
204 }
205 }
206 }
207 }
208
209 Ok(BottomRow::new(children)
210 .total_col_ratio(total_col_ratio)
211 .ratio(row_ratio))
212 }
213 }
214
215 /// Represents a child of a Row - either a Col (column) or a FinalWidget.
216 ///
217 /// A Col can also have an optional length and children. We only allow columns
218 /// to have FinalWidgets as children, lest we get some amount of mutual
219 /// recursion between Row and Col.
220 #[derive(Clone, Deserialize, Debug, Serialize)]
221 #[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
222 #[serde(untagged)]
223 #[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))]
224 pub enum RowChildren {
225 Widget(FinalWidget),
226 Col {
227 ratio: Option<u16>,
228 child: Vec<FinalWidget>,
229 },
230 }
231
232 /// Represents a widget.
233 #[derive(Clone, Deserialize, Debug, Serialize)]
234 #[cfg_attr(feature = "generate_schema", derive(schemars::JsonSchema))]
235 #[cfg_attr(test, serde(deny_unknown_fields), derive(PartialEq, Eq))]
236 pub struct FinalWidget {
237 pub ratio: Option<u16>,
238 #[serde(rename = "type")]
239 pub widget_type: String,
240 pub default: Option<bool>,
241 }
242
243 #[cfg(test)]
244 mod test {
245 use toml_edit::de::from_str;
246
247 use super::*;
248 use crate::{
249 constants::{DEFAULT_LAYOUT, DEFAULT_WIDGET_ID},
250 options::Config,
251 };
252
253 const PROC_LAYOUT: &str = r#"
254 [[row]]
255 [[row.child]]
256 type="proc"
257 [[row]]
258 [[row.child]]
259 type="proc"
260 [[row.child]]
261 type="proc"
262 [[row]]
263 [[row.child]]
264 type="proc"
265 [[row.child]]
266 type="proc"
267 "#;
268
269 fn test_create_layout(
270 rows: &[Row], default_widget_id: u64, default_widget_type: Option<BottomWidgetType>,
271 default_widget_count: u64, left_legend: bool,
272 ) -> BottomLayout {
273 let mut iter_id = 0; // A lazy way of forcing unique IDs *shrugs*
274 let mut total_height_ratio = 0;
275 let mut default_widget_count = default_widget_count;
276 let mut default_widget_id = default_widget_id;
277
278 let mut ret_bottom_layout = BottomLayout {
279 rows: rows
280 .iter()
281 .map(|row| {
282 row.convert_row_to_bottom_row(
283 &mut iter_id,
284 &mut total_height_ratio,
285 &mut default_widget_id,
286 &default_widget_type,
287 &mut default_widget_count,
288 left_legend,
289 )
290 })
291 .collect::<OptionResult<Vec<_>>>()
292 .unwrap(),
293 total_row_height_ratio: total_height_ratio,
294 };
295 ret_bottom_layout.get_movement_mappings();
296
297 ret_bottom_layout
298 }
299
300 #[test]
301 /// Tests the default setup.
302 fn test_default_movement() {
303 let rows = from_str::<Config>(DEFAULT_LAYOUT).unwrap().row.unwrap();
304 let ret_bottom_layout = test_create_layout(&rows, DEFAULT_WIDGET_ID, None, 1, false);
305
306 // Simple tests for the top CPU widget
307 assert_eq!(
308 ret_bottom_layout.rows[0].children[0].children[0].children[0].down_neighbour,
309 Some(3)
310 );
311 assert_eq!(
312 ret_bottom_layout.rows[0].children[0].children[0].children[0].right_neighbour,
313 Some(2)
314 );
315 assert_eq!(
316 ret_bottom_layout.rows[0].children[0].children[0].children[0].left_neighbour,
317 None
318 );
319 assert_eq!(
320 ret_bottom_layout.rows[0].children[0].children[0].children[0].up_neighbour,
321 None
322 );
323
324 // Test CPU legend
325 assert_eq!(
326 ret_bottom_layout.rows[0].children[0].children[0].children[1].down_neighbour,
327 Some(4)
328 );
329 assert_eq!(
330 ret_bottom_layout.rows[0].children[0].children[0].children[1].right_neighbour,
331 None
332 );
333 assert_eq!(
334 ret_bottom_layout.rows[0].children[0].children[0].children[1].left_neighbour,
335 Some(1)
336 );
337 assert_eq!(
338 ret_bottom_layout.rows[0].children[0].children[0].children[1].up_neighbour,
339 None
340 );
341
342 // Test memory->temp, temp->disk, disk->memory mappings
343 assert_eq!(
344 ret_bottom_layout.rows[1].children[0].children[0].children[0].right_neighbour,
345 Some(4)
346 );
347 assert_eq!(
348 ret_bottom_layout.rows[1].children[1].children[0].children[0].down_neighbour,
349 Some(5)
350 );
351 assert_eq!(
352 ret_bottom_layout.rows[1].children[1].children[1].children[0].left_neighbour,
353 Some(3)
354 );
355
356 // Test disk -> processes, processes -> process sort, process sort -> network
357 assert_eq!(
358 ret_bottom_layout.rows[1].children[1].children[1].children[0].down_neighbour,
359 Some(7)
360 );
361 assert_eq!(
362 ret_bottom_layout.rows[2].children[1].children[0].children[1].left_neighbour,
363 Some(9)
364 );
365 assert_eq!(
366 ret_bottom_layout.rows[2].children[1].children[0].children[0].left_neighbour,
367 Some(6)
368 );
369 }
370
371 #[cfg(feature = "battery")]
372 #[test]
373 /// Tests battery movement in the default setup.
374 fn test_default_battery_movement() {
375 use crate::constants::DEFAULT_BATTERY_LAYOUT;
376
377 let rows = from_str::<Config>(DEFAULT_BATTERY_LAYOUT)
378 .unwrap()
379 .row
380 .unwrap();
381 let ret_bottom_layout = test_create_layout(&rows, DEFAULT_WIDGET_ID, None, 1, false);
382
383 // Simple tests for the top CPU widget
384 assert_eq!(
385 ret_bottom_layout.rows[0].children[0].children[0].children[0].down_neighbour,
386 Some(4)
387 );
388 assert_eq!(
389 ret_bottom_layout.rows[0].children[0].children[0].children[0].right_neighbour,
390 Some(2)
391 );
392 assert_eq!(
393 ret_bottom_layout.rows[0].children[0].children[0].children[0].left_neighbour,
394 None
395 );
396 assert_eq!(
397 ret_bottom_layout.rows[0].children[0].children[0].children[0].up_neighbour,
398 None
399 );
400
401 // Test CPU legend
402 assert_eq!(
403 ret_bottom_layout.rows[0].children[0].children[0].children[1].down_neighbour,
404 Some(5)
405 );
406 assert_eq!(
407 ret_bottom_layout.rows[0].children[0].children[0].children[1].right_neighbour,
408 Some(3)
409 );
410 assert_eq!(
411 ret_bottom_layout.rows[0].children[0].children[0].children[1].left_neighbour,
412 Some(1)
413 );
414 assert_eq!(
415 ret_bottom_layout.rows[0].children[0].children[0].children[1].up_neighbour,
416 None
417 );
418 }
419
420 #[test]
421 /// Tests using cpu_left_legend.
422 fn test_cpu_left_legend() {
423 let rows = from_str::<Config>(DEFAULT_LAYOUT).unwrap().row.unwrap();
424 let ret_bottom_layout = test_create_layout(&rows, DEFAULT_WIDGET_ID, None, 1, true);
425
426 // Legend
427 assert_eq!(
428 ret_bottom_layout.rows[0].children[0].children[0].children[0].down_neighbour,
429 Some(3)
430 );
431 assert_eq!(
432 ret_bottom_layout.rows[0].children[0].children[0].children[0].right_neighbour,
433 Some(1)
434 );
435 assert_eq!(
436 ret_bottom_layout.rows[0].children[0].children[0].children[0].left_neighbour,
437 None
438 );
439 assert_eq!(
440 ret_bottom_layout.rows[0].children[0].children[0].children[0].up_neighbour,
441 None
442 );
443
444 // Widget
445 assert_eq!(
446 ret_bottom_layout.rows[0].children[0].children[0].children[1].down_neighbour,
447 Some(3)
448 );
449 assert_eq!(
450 ret_bottom_layout.rows[0].children[0].children[0].children[1].right_neighbour,
451 None
452 );
453 assert_eq!(
454 ret_bottom_layout.rows[0].children[0].children[0].children[1].left_neighbour,
455 Some(2)
456 );
457 assert_eq!(
458 ret_bottom_layout.rows[0].children[0].children[0].children[1].up_neighbour,
459 None
460 );
461 }
462
463 #[test]
464 /// Tests explicit default widget.
465 fn test_default_widget_in_layout() {
466 let proc_layout = r#"
467 [[row]]
468 [[row.child]]
469 type="proc"
470 [[row]]
471 [[row.child]]
472 type="proc"
473 [[row.child]]
474 type="proc"
475 [[row]]
476 [[row.child]]
477 type="proc"
478 default=true
479 [[row.child]]
480 type="proc"
481 "#;
482
483 let rows = from_str::<Config>(proc_layout).unwrap().row.unwrap();
484 let mut iter_id = 0; // A lazy way of forcing unique IDs *shrugs*
485 let mut total_height_ratio = 0;
486 let mut default_widget_count = 1;
487 let mut default_widget_id = DEFAULT_WIDGET_ID;
488 let default_widget_type = None;
489 let cpu_left_legend = false;
490
491 let mut ret_bottom_layout = BottomLayout {
492 rows: rows
493 .iter()
494 .map(|row| {
495 row.convert_row_to_bottom_row(
496 &mut iter_id,
497 &mut total_height_ratio,
498 &mut default_widget_id,
499 &default_widget_type,
500 &mut default_widget_count,
501 cpu_left_legend,
502 )
503 })
504 .collect::<OptionResult<Vec<_>>>()
505 .unwrap(),
506 total_row_height_ratio: total_height_ratio,
507 };
508 ret_bottom_layout.get_movement_mappings();
509
510 assert_eq!(default_widget_id, 10);
511 }
512
513 #[test]
514 /// Tests default widget by setting type and count.
515 fn test_default_widget_by_option() {
516 let rows = from_str::<Config>(PROC_LAYOUT).unwrap().row.unwrap();
517 let mut iter_id = 0; // A lazy way of forcing unique IDs *shrugs*
518 let mut total_height_ratio = 0;
519 let mut default_widget_count = 3;
520 let mut default_widget_id = DEFAULT_WIDGET_ID;
521 let default_widget_type = Some(BottomWidgetType::Proc);
522 let cpu_left_legend = false;
523
524 let mut ret_bottom_layout = BottomLayout {
525 rows: rows
526 .iter()
527 .map(|row| {
528 row.convert_row_to_bottom_row(
529 &mut iter_id,
530 &mut total_height_ratio,
531 &mut default_widget_id,
532 &default_widget_type,
533 &mut default_widget_count,
534 cpu_left_legend,
535 )
536 })
537 .collect::<OptionResult<Vec<_>>>()
538 .unwrap(),
539 total_row_height_ratio: total_height_ratio,
540 };
541 ret_bottom_layout.get_movement_mappings();
542
543 assert_eq!(default_widget_id, 7);
544 }
545
546 #[test]
547 fn test_proc_custom_layout() {
548 let rows = from_str::<Config>(PROC_LAYOUT).unwrap().row.unwrap();
549 let ret_bottom_layout = test_create_layout(&rows, DEFAULT_WIDGET_ID, None, 1, false);
550
551 // First proc widget
552 assert_eq!(
553 ret_bottom_layout.rows[0].children[0].children[0].children[1].down_neighbour,
554 Some(2)
555 );
556 assert_eq!(
557 ret_bottom_layout.rows[0].children[0].children[0].children[1].left_neighbour,
558 Some(3)
559 );
560 assert_eq!(
561 ret_bottom_layout.rows[0].children[0].children[0].children[1].right_neighbour,
562 None
563 );
564 assert_eq!(
565 ret_bottom_layout.rows[0].children[0].children[0].children[1].up_neighbour,
566 None
567 );
568
569 // Its search
570 assert_eq!(
571 ret_bottom_layout.rows[0].children[0].children[1].children[0].down_neighbour,
572 Some(4)
573 );
574 assert_eq!(
575 ret_bottom_layout.rows[0].children[0].children[1].children[0].left_neighbour,
576 None
577 );
578 assert_eq!(
579 ret_bottom_layout.rows[0].children[0].children[1].children[0].right_neighbour,
580 None
581 );
582 assert_eq!(
583 ret_bottom_layout.rows[0].children[0].children[1].children[0].up_neighbour,
584 Some(1)
585 );
586
587 // Its sort
588 assert_eq!(
589 ret_bottom_layout.rows[0].children[0].children[0].children[0].down_neighbour,
590 Some(2)
591 );
592 assert_eq!(
593 ret_bottom_layout.rows[0].children[0].children[0].children[0].left_neighbour,
594 None
595 );
596 assert_eq!(
597 ret_bottom_layout.rows[0].children[0].children[0].children[0].right_neighbour,
598 Some(1)
599 );
600 assert_eq!(
601 ret_bottom_layout.rows[0].children[0].children[0].children[0].up_neighbour,
602 None
603 );
604
605 // Let us now test the second row's first widget...
606 assert_eq!(
607 ret_bottom_layout.rows[1].children[0].children[0].children[1].down_neighbour,
608 Some(5)
609 );
610 assert_eq!(
611 ret_bottom_layout.rows[1].children[0].children[0].children[1].left_neighbour,
612 Some(6)
613 );
614 assert_eq!(
615 ret_bottom_layout.rows[1].children[0].children[0].children[1].right_neighbour,
616 Some(9)
617 );
618 assert_eq!(
619 ret_bottom_layout.rows[1].children[0].children[0].children[1].up_neighbour,
620 Some(2)
621 );
622
623 // Sort
624 assert_eq!(
625 ret_bottom_layout.rows[1].children[0].children[0].children[0].down_neighbour,
626 Some(5)
627 );
628 assert_eq!(
629 ret_bottom_layout.rows[1].children[0].children[0].children[0].left_neighbour,
630 None
631 );
632 assert_eq!(
633 ret_bottom_layout.rows[1].children[0].children[0].children[0].right_neighbour,
634 Some(4)
635 );
636 assert_eq!(
637 ret_bottom_layout.rows[1].children[0].children[0].children[0].up_neighbour,
638 Some(2)
639 );
640
641 // Search
642 assert_eq!(
643 ret_bottom_layout.rows[1].children[0].children[1].children[0].down_neighbour,
644 Some(10)
645 );
646 assert_eq!(
647 ret_bottom_layout.rows[1].children[0].children[1].children[0].left_neighbour,
648 None
649 );
650 assert_eq!(
651 ret_bottom_layout.rows[1].children[0].children[1].children[0].right_neighbour,
652 Some(8)
653 );
654 assert_eq!(
655 ret_bottom_layout.rows[1].children[0].children[1].children[0].up_neighbour,
656 Some(4)
657 );
658
659 // Third row, second
660 assert_eq!(
661 ret_bottom_layout.rows[2].children[1].children[0].children[1].down_neighbour,
662 Some(14)
663 );
664 assert_eq!(
665 ret_bottom_layout.rows[2].children[1].children[0].children[1].left_neighbour,
666 Some(15)
667 );
668 assert_eq!(
669 ret_bottom_layout.rows[2].children[1].children[0].children[1].right_neighbour,
670 None
671 );
672 assert_eq!(
673 ret_bottom_layout.rows[2].children[1].children[0].children[1].up_neighbour,
674 Some(8)
675 );
676
677 // Sort
678 assert_eq!(
679 ret_bottom_layout.rows[2].children[1].children[0].children[0].down_neighbour,
680 Some(14)
681 );
682 assert_eq!(
683 ret_bottom_layout.rows[2].children[1].children[0].children[0].left_neighbour,
684 Some(10)
685 );
686 assert_eq!(
687 ret_bottom_layout.rows[2].children[1].children[0].children[0].right_neighbour,
688 Some(13)
689 );
690 assert_eq!(
691 ret_bottom_layout.rows[2].children[1].children[0].children[0].up_neighbour,
692 Some(8)
693 );
694
695 // Search
696 assert_eq!(
697 ret_bottom_layout.rows[2].children[1].children[1].children[0].down_neighbour,
698 None
699 );
700 assert_eq!(
701 ret_bottom_layout.rows[2].children[1].children[1].children[0].left_neighbour,
702 Some(11)
703 );
704 assert_eq!(
705 ret_bottom_layout.rows[2].children[1].children[1].children[0].right_neighbour,
706 None
707 );
708 assert_eq!(
709 ret_bottom_layout.rows[2].children[1].children[1].children[0].up_neighbour,
710 Some(13)
711 );
712 }
713 }