framework_tool_tui/tui/
component.rs1pub mod pd_ports_panel;
2use ratatui::{crossterm::event::Event, layout::Rect, prelude::*, Frame};
3
4use crate::{
5 app::AppEvent,
6 framework::info::FrameworkInfo,
7 tui::{control::AdjustableControl, theme::Theme},
8};
9
10pub mod brightness_panel;
11pub mod charge_panel;
12pub mod footer;
13pub mod main;
14pub mod privacy_panel;
15pub mod smbios_panel;
16pub mod title;
17
18pub trait Component {
19 fn handle_input(&mut self, _event: Event) -> Option<AppEvent> {
20 None
21 }
22
23 fn render(&mut self, _frame: &mut Frame, _area: Rect, _theme: &Theme, _info: &FrameworkInfo) {}
24}
25
26pub trait AdjustableComponent: Component {
27 fn panel(&mut self) -> &mut AdjustablePanel;
28}
29
30pub struct AdjustablePanel {
31 selected: bool,
32 controls: Vec<AdjustableControl>,
33 selected_control: usize,
34}
35
36impl AdjustablePanel {
37 fn toggle(&mut self) {
38 self.selected = !self.is_selected();
39 }
40
41 fn is_selected(&self) -> bool {
42 self.selected
43 }
44
45 fn cycle_controls_up(&mut self) {
46 let len = self.controls.len();
47
48 if self.selected_control == 0 {
49 self.selected_control = len - 1;
50 } else {
51 self.selected_control -= 1;
52 }
53 }
54
55 fn cycle_controls_down(&mut self) {
56 let len = self.controls.len();
57
58 if self.selected_control < len - 1 {
59 self.selected_control += 1;
60 } else {
61 self.selected_control = 0;
62 }
63 }
64
65 fn toggle_selected_control_focus(&mut self) {
66 self.controls[self.selected_control] = self.get_selected_control().toggle_focus();
67 }
68
69 fn adjust_focused_percentage_control_by_delta(&mut self, delta: i8) {
70 if let Some(AdjustableControl::Percentage(focused, value)) =
71 self.get_selected_and_focused_control()
72 {
73 let new_value = *value as i8 + delta;
74
75 if (0..=100).contains(&new_value) {
76 self.controls[self.selected_control] =
77 AdjustableControl::Percentage(*focused, new_value as u8);
78 }
79 }
80 }
81
82 fn set_percentage_control_by_index(&mut self, index: usize, control: AdjustableControl) {
83 self.controls[index] = control;
84 }
85
86 fn get_selected_and_focused_control(&self) -> Option<&AdjustableControl> {
87 let selected = self.get_selected_control();
88
89 if selected.is_focused() {
90 Some(selected)
91 } else {
92 None
93 }
94 }
95
96 fn get_selected_control(&self) -> &AdjustableControl {
97 &self.controls[self.selected_control]
98 }
99
100 fn is_panel_selected_and_control_focused_by_index(&self, index: usize) -> bool {
101 self.selected && self.selected_control == index && self.get_selected_control().is_focused()
102 }
103
104 fn adjustable_control_style(&self, selected: Style, default: Style, index: usize) -> Style {
105 if self.selected && self.selected_control == index {
106 selected
107 } else {
108 default
109 }
110 }
111
112 fn borders_style(&self, theme: &Theme) -> Style {
113 if self.selected {
114 Style::default().fg(theme.border_active).bold()
115 } else {
116 Style::default().fg(theme.border)
117 }
118 }
119}