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

use crate::{
    app::{
        event::ComponentEventResult, text_table::SimpleColumn, widgets::tui_stuff::BlockBuilder,
        Component, TextTable,
    },
    canvas::Painter,
};

use super::sort_text_table::SortableColumn;

/// A sortable, scrollable table with columns.
pub struct SortMenu {
    /// The underlying table.
    table: TextTable,

    /// The bounds.
    bounds: Rect,
}

impl SortMenu {
    /// Creates a new [`SortMenu`].
    pub fn new(num_columns: usize) -> Self {
        let sort_menu_columns = vec![SimpleColumn::new_hard("Sort By".into(), None)];
        let mut sort_menu = TextTable::new(sort_menu_columns);
        sort_menu.set_num_items(num_columns);

        Self {
            table: sort_menu,
            bounds: Default::default(),
        }
    }

    /// Updates the index of the [`SortMenu`].
    pub fn set_index(&mut self, index: usize) {
        self.table.scrollable.set_index(index);
    }

    /// Returns the current index of the [`SortMenu`].
    pub fn current_index(&mut self) -> usize {
        self.table.scrollable.current_index()
    }

    /// Draws a [`tui::widgets::Table`] on screen corresponding to the sort columns of this [`SortableTextTable`].
    pub fn draw_sort_menu<B: Backend, C: SortableColumn>(
        &mut self, painter: &Painter, f: &mut Frame<'_, B>, columns: &[C], block: BlockBuilder,
        block_area: Rect,
    ) {
        self.set_bounds(block_area);

        let data = columns
            .iter()
            .map(|c| vec![(c.original_name().clone(), None, None)])
            .collect::<Vec<_>>();

        self.table
            .draw_tui_table(painter, f, &data, block, block_area, true, false);
    }
}

impl Component for SortMenu {
    fn bounds(&self) -> Rect {
        self.bounds
    }

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

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