summaryrefslogtreecommitdiffstats
path: root/src/app/widgets/bottom_widgets/battery.rs
blob: 1f9b1a88917114bdb6b2e0e527935b8e635ed947 (plain)
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
use std::cmp::{max, min};

use crossterm::event::{KeyCode, KeyEvent, MouseEvent};
use tui::{
    backend::Backend,
    layout::{Constraint, Direction, Layout, Rect},
    text::{Span, Spans},
    widgets::{Borders, Paragraph, Tabs},
    Frame,
};

use crate::{
    app::{
        data_farmer::DataCollection, does_bound_intersect_coordinate, event::ComponentEventResult,
        widgets::tui_stuff::PipeGauge, Component, Widget,
    },
    canvas::Painter,
    constants::TABLE_GAP_HEIGHT_LIMIT,
    data_conversion::{convert_battery_harvest, ConvertedBatteryData},
    options::layout_options::LayoutRule,
};

/// A table displaying battery information on a per-battery basis.
pub struct BatteryTable {
    bounds: Rect,
    selected_index: usize,
    battery_data: Vec<ConvertedBatteryData>,
    width: LayoutRule,
    height: LayoutRule,
    block_border: Borders,
    tab_bounds: Vec<Rect>,
}

impl Default for BatteryTable {
    fn default() -> Self {
        Self {
            bounds: Default::default(),
            selected_index: 0,
            battery_data: Default::default(),
            width: LayoutRule::default(),
            height: LayoutRule::default(),
            block_border: Borders::ALL,
            tab_bounds: Default::default(),
        }
    }
}

impl BatteryTable {
    /// Sets the width.
    pub fn width(mut self, width: LayoutRule) -> Self {
        self.width = width;
        self
    }

    /// Sets the height.
    pub fn height(mut self, height: LayoutRule) -> Self {
        self.height = height;
        self
    }

    /// Returns the index of the currently selected battery.
    pub fn index(&self) -> usize {
        self.selected_index
    }

    fn increment_index(&mut self) {
        if self.selected_index + 1 < self.battery_data.len() {
            self.selected_index += 1;
        }
    }

    fn decrement_index(&mut self) {
        if self.selected_index > 0 {
            self.selected_index -= 1;
        }
    }

    /// Sets the block border style.
    pub fn basic_mode(mut self, basic_mode: bool) -> Self {
        if basic_mode {
            self.block_border = *crate::constants::SIDE_BORDERS;
        }

        self
    }
}

impl Component for BatteryTable {
    fn bounds(&self) -> tui::layout::Rect {
        self.bounds
    }

    fn set_bounds(&mut self, new_bounds: tui::layout::Rect) {
        self.bounds = new_bounds;
    }

    fn handle_key_event(&mut self, event: KeyEvent) -> ComponentEventResult {
        if event.modifiers.is_empty() {
            match event.code {
                KeyCode::Left => {
                    let current_index = self.selected_index;
                    self.decrement_index();
                    if current_index == self.selected_index {
                        ComponentEventResult::NoRedraw
                    } else {
                        ComponentEventResult::Redraw
                    }
                }
                KeyCode::Right => {
                    let current_index = self.selected_index;
                    self.increment_index();
                    if current_index == self.selected_index {
                        ComponentEventResult::NoRedraw
                    } else {
                        ComponentEventResult::Redraw
                    }
                }
                _ => ComponentEventResult::Unhandled,
            }
        } else {
            ComponentEventResult::Unhandled
        }
    }

    fn handle_mouse_event(&mut self, event: MouseEvent) -> ComponentEventResult {
        for (itx, bound) in self.tab_bounds.iter().enumerate() {
            if does_bound_intersect_coordinate(event.column, event.row, *bound)
                && itx < self.battery_data.len()
            {
                self.selected_index = itx;
                return ComponentEventResult::Redraw;
            }
        }
        ComponentEventResult::Unhandled
    }
}

impl Widget for BatteryTable {
    fn get_pretty_name(&self) -> &'static str {
        "Battery"
    }

    fn update_data(&mut self, data_collection: &DataCollection) {
        self.battery_data = convert_battery_harvest(data_collection);
        if self.battery_data.len() <= self.selected_index {
            self.selected_index = self.battery_data.len().saturating_sub(1);
        }
    }

    fn width(&self) -> LayoutRule {
        self.width
    }

    fn height(&self) -> LayoutRule {
        self.height
    }

    fn draw<B: Backend>(
        &mut self, painter: &Painter, f: &mut Frame<'_, B>, area: Rect, selected: bool,
        expanded: bool,
    ) {
        let block = self
            .block()
            .selected(selected)
            .borders(self.block_border)
            .show_esc(expanded)
            .build(painter, area);

        let inner_area = block.inner(area);
        const CONSTRAINTS: [Constraint; 2] = [Constraint::Length(1), Constraint::Min(0)];
        let split_area = Layout::default()
            .direction(Direction::Vertical)
            .constraints(CONSTRAINTS)
            .split(inner_area);

        if self.battery_data.is_empty() {
            f.render_widget(
                Paragraph::new("No batteries found").style(painter.colours.text_style),
                split_area[0],
            );
        } else {
            let tab_area = Rect::new(
                split_area[0].x.saturating_sub(1),
                split_area[0].y,
                split_area[0].width,
                split_area[0].height,
            );
            let data_area =
                if inner_area.height >= TABLE_GAP_HEIGHT_LIMIT && split_area[1].height > 0 {
                    Rect::new(
                        split_area[1].x,
                        split_area[1].y + 1,
                        split_area[1].width,
                        split_area[1].height - 1,
                    )
                } else {
                    split_area[1]
                };

            let battery_tab_names = self
                .battery_data
                .iter()
                .map(|d| Spans::from(d.battery_name.as_str()))
                .collect::<Vec<_>>();
            let mut start_x_offset = tab_area.x + 1;
            self.tab_bounds = battery_tab_names
                .iter()
                .map(|name| {
                    let length = name.width() as u16;
                    let start = start_x_offset;
                    start_x_offset += length;
                    start_x_offset += 3;

                    Rect::new(start, tab_area.y, length, 1)
                })
                .collect();
            f.render_widget(
                Tabs::new(battery_tab_names)
                    .divider(tui::symbols::line::VERTICAL)
                    .style(painter.colours.text_style)
                    .highlight_style(painter.colours.currently_selected_text_style)
                    .select(self.selected_index),
                tab_area,
            );

            if let Some(battery_details) = self.battery_data.get(self.selected_index) {
                let labels = vec![
                    Spans::from(Span::styled("Charge %", painter.colours.text_style)),
                    Spans::from(Span::styled("Consumption", painter.colours.text_style)),
                    match &battery_details.charge_times {
                        crate::data_conversion::BatteryDuration::Charging { .. } => {
                            Spans::from(Span::styled("Time to full", painter.colours.text_style))
                        }
                        crate::data_conversion::BatteryDuration::Discharging { .. } => {
                            Spans::from(Span::styled("Time to empty", painter.colours.text_style))
                        }
                        crate::data_conversion::BatteryDuration::Neither => Spans::from(
                            Span::styled("Time to full/empty", painter.colours.text_style),
                        ),
                    },
                    Spans::from(Span::styled("Health %", painter<