summaryrefslogtreecommitdiffstats
path: root/src/app/widgets/bottom_widgets/temp.rs
blob: c6dfcff7a67549b22b2cf11f18ecb76cc1f24207 (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
use crossterm::event::{KeyEvent, MouseEvent};
use tui::{backend::Backend, layout::Rect, widgets::Borders, Frame};

use crate::{
    app::{
        data_farmer::DataCollection, data_harvester::temperature::TemperatureType,
        event::ComponentEventResult, sort_text_table::SimpleSortableColumn,
        text_table::TextTableData, AppConfigFields, Component, TextTable, Widget,
    },
    canvas::Painter,
    data_conversion::convert_temp_row,
    options::layout_options::LayoutRule,
};

/// A table displaying temperature data.
pub struct TempTable {
    table: TextTable<SimpleSortableColumn>,
    bounds: Rect,
    display_data: TextTableData,
    temp_type: TemperatureType,
    width: LayoutRule,
    height: LayoutRule,
    block_border: Borders,
    show_scroll_index: bool,
}

impl TempTable {
    /// Creates a [`TempTable`] from a config.
    pub fn from_config(app_config_fields: &AppConfigFields) -> Self {
        let table = TextTable::new(vec![
            SimpleSortableColumn::new_flex("Sensor".into(), None, false, 0.8),
            SimpleSortableColumn::new_hard("Temp".into(), None, false, Some(5)),
        ])
        .default_ltr(false)
        .try_show_gap(app_config_fields.table_gap);

        Self {
            table,
            bounds: Rect::default(),
            display_data: Default::default(),
            temp_type: TemperatureType::default(),
            width: LayoutRule::default(),
            height: LayoutRule::default(),
            block_border: Borders::ALL,
            show_scroll_index: false,
        }
    }

    /// Sets the [`TemperatureType`] for the [`TempTable`].
    pub fn set_temp_type(mut self, temp_type: TemperatureType) -> Self {
        self.temp_type = temp_type;
        self
    }

    /// 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
    }

    /// 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
    }

    /// Sets whether to show the scroll index.
    pub fn show_scroll_index(mut self, show_scroll_index: bool) -> Self {
        self.show_scroll_index = show_scroll_index;
        self
    }
}

impl Component for TempTable {
    fn handle_key_event(&mut self, event: KeyEvent) -> ComponentEventResult {
        self.table.handle_key_event(event)
    }

    fn handle_mouse_event(&mut self, event: MouseEvent) -> ComponentEventResult {
        self.table.handle_mouse_event(event)
    }

    fn bounds(&self) -> Rect {
        self.bounds
    }

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

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

    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);

        self.table.draw_tui_table(
            painter,
            f,
            &self.display_data,
            block,
            area,
            selected,
            self.show_scroll_index,
        );
    }

    fn update_data(&mut self, data_collection: &DataCollection) {
        self.display_data = convert_temp_row(data_collection, &self.temp_type);
    }

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

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