1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
use crate::{
app::App,
canvas::{drawing_utils::calculate_basic_use_bars, Painter},
constants::*,
};
use tui::{
backend::Backend,
layout::{Constraint, Direction, Layout, Rect},
terminal::Frame,
widgets::{Block, Borders, Paragraph, Row, Table, Tabs, Text},
};
pub trait BatteryDisplayWidget {
fn draw_battery_display<B: Backend>(
&self, f: &mut Frame<'_, B>, app_state: &mut App, draw_loc: Rect, draw_border: bool,
widget_id: u64,
);
}
impl BatteryDisplayWidget for Painter {
fn draw_battery_display<B: Backend>(
&self, f: &mut Frame<'_, B>, app_state: &mut App, draw_loc: Rect, draw_border: bool,
widget_id: u64,
) {
let should_get_widget_bounds = app_state.should_get_widget_bounds();
if let Some(battery_widget_state) =
app_state.battery_state.widget_states.get_mut(&widget_id)
{
let is_on_widget = widget_id == app_state.current_widget.widget_id;
let border_and_title_style = if is_on_widget {
self.colours.highlighted_border_style
} else {
self.colours.border_style
};
// let title = if app_state.is_expanded {
// const TITLE_BASE: &str = " Battery ── Esc to go back ";
// Span::styled(
// format!(
// " Battery ─{}─ Esc to go back ",
// "─".repeat(
// usize::from(draw_loc.width)
// .saturating_sub(TITLE_BASE.chars().count() + 2)
// )
// ),
// border_and_title_style,
// )
// } else {
// Span::styled(" Battery ".to_string(), self.colours.widget_title_style)
// };
let title = if app_state.is_expanded {
const TITLE_BASE: &str = " Battery ── Esc to go back ";
format!(
" Battery ─{}─ Esc to go back ",
"─".repeat(
usize::from(draw_loc.width).saturating_sub(TITLE_BASE.chars().count() + 2)
)
)
} else {
" Battery ".to_string()
};
let title_style = if app_state.is_expanded {
border_and_title_style
} else {
self.colours.widget_title_style
};
let battery_block = if draw_border {
Block::default()
.title(&title)
.title_style(title_style)
.borders(Borders::ALL)
.border_style(border_and_title_style)
} else if is_on_widget {
Block::default()
.borders(*SIDE_BORDERS)
.border_style(self.colours.highlighted_border_style)
} else {
Block::default().borders(Borders::NONE)
};
// f.render_widget(
// // Tabs::new(
// // (app_state
// // .canvas_data
// // .battery_data
// // .iter()
// // .map(|battery| Spans::from(battery.battery_name.clone())))
// // .collect::<Vec<_>>(),
// // )
// Tabs::default()
// .titles(
// app_state
// .canvas_data
// .battery_data
// .iter()
// .map(|battery| &battery.battery_name)
// .collect::<Vec<_>>()
// .as_ref(),
// )
// .block(battery_block)
// .divider(tui::symbols::line::VERTICAL)
// .style(self.colours.text_style)
// .highlight_style(self.colours.currently_selected_text_style)
// .select(battery_widget_state.currently_selected_battery_index),
// draw_loc,
// );
let margined_draw_loc = Layout::default()
.constraints([Constraint::Percentage(100)].as_ref())
.horizontal_margin(if is_on_widget || draw_border { 0 } else { 1 })
.direction(Direction::Horizontal)
.split(draw_loc)[0];
let tab_draw_loc = Layout::default()
.constraints(
[
Constraint::Length(1),
Constraint::Length(2),
Constraint::Min(0),
]
.as_ref(),
)
.direction(Direction::Vertical)
.split(draw_loc)[1];
if let Some(battery_details) = app_state
.canvas_data
.battery_data
.get(battery_widget_state.currently_selected_battery_index)
{
// Assuming a 50/50 split in width
let bar_length =
usize::from((draw_loc.width.saturating_sub(2) / 2).saturating_sub(8));
let charge_percentage = battery_details.charge_percentage;
let num_bars = calculate_basic_use_bars(charge_percentage, bar_length);
let bars = format!(
"[{}{}{:3.0}%]",
"|".repeat(num_bars),
" ".repeat(bar_length - num_bars),
charge_percentage,
);
let battery_items = vec![
["Charge %", &bars],
["Consumption", &battery_details.watt_consumption],
if let Some(duration_until_full) = &battery_details.duration_until_full {
["Time to full", duration_until_full]
} else if let Some(duration_until_empty) = &battery_details.duration_until_empty
{
["Time to empty", duration_until_empty]
} else {
["Time to full/empty", "N/A"]
},
["Health %", &battery_details.health],
];
let battery_rows = battery_items.iter().enumerate().map(|(itx, item)| {
Row::StyledData(
item.iter(),
if itx == 0 {
let colour_index = ((charge_percentage
* self.colours.battery_bar_styles.len() as f64
- 1.0)
/ 100.0)
.floor() as usize;
*self
.colours
.battery_bar_styles
.get(colour_index)
.unwrap_or(&self.colours.text_style)
} else {
self.colours.text_style
},
)
});
// Draw
f.render_widget(
Table::new([""].iter(), battery_rows)
.block(battery_block)
.header_style(self.colours.table_header_style)
.widths([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref()),
margined_draw_loc,
);
} else {
f.render_widget(
Paragraph::new(
[Text::styled(
"No data found for this battery",
self.colours.text_style,
)]
.iter(),
)
.block(battery_block),
margined_draw_loc,
);
}
let battery_names = app_state
.canvas_data
.battery_data
.iter()
.map(|battery| &battery.battery_name)
.collect::<Vec<_>>();
// Has to be placed AFTER for tui 0.9, place BEFORE for 0.10.
f.render_widget(
// Tabs::new(
// (app_state
// .canvas_data
// .battery_data
// .iter()
// .map(|battery| Spans::from(battery.battery_name.clone())))
// .collect::<Vec<_>>(),
// )
Tabs::default()
.titles(battery_names.as_ref())
.block(Block::default())
.divider(tui::symbols::line::VERTICAL)
.style(self.colours.text_style)
.highlight_style(self.colours.currently_selected_text_style)
.select(battery_widget_state.currently_selected_battery_index),
tab_draw_loc,
);
if should_get_widget_bounds {
// Tab wizardry
if !battery_names.is_empty() {
let mut current_x = tab_draw_loc.x;
let current_y = tab_draw_loc.y;
let mut tab_click_locs: Vec<((u16, u16), (u16, u16))> = vec![];
for battery in battery_names {
// +1 because there's a space after the tab label.
let width =
unicode_width::UnicodeWidthStr::width(battery.as_str()) as u16 + 1;
tab_click_locs
.push(((current_x, current_y), (current_x + width, current_y)));
// +2 because we want to go one space past to get to the '|', then one MORE
// to start at the blank space before the tab label.
current_x = current_x + width + 2;
}
battery_widget_state.tab_click_locs = Some(tab_click_locs);
}
// Update draw loc in widget map
if let Some(widget) = app_state.widget_map.get_mut(&widget_id) {
widget.top_left_corner = Some((margined_draw_loc.x, margined_draw_loc.y));
widget.bottom_right_corner = Some((
margined_draw_loc.x + margined_draw_loc.width,
margined_draw_loc.y + margined_draw_loc.height,
));
}
}
}
}
}
|