aboutsummaryrefslogtreecommitdiffstats
path: root/default-plugins/mobile/src/screens/menu.rs
blob: e016f133a1feedaf75c4740b62550134f2852f85 (plain) (blame)
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
use unicode_width::UnicodeWidthStr;
use zellij_tile::prelude::*;

use crate::click::{ClickAction, ClickRegion};
use crate::fit::Fit;
use crate::frame::Frame;
use crate::screens::ActiveScreen;

const H_PAD: usize = 1;
const SEPARATOR_CHAR: char = '\u{2500}';

const ENTRIES: [HamburgerEntry; 5] = [
    HamburgerEntry::Item(HamburgerItem::Fit),
    HamburgerEntry::Item(HamburgerItem::ChangePane),
    HamburgerEntry::Item(HamburgerItem::ChangeSession),
    HamburgerEntry::Separator,
    HamburgerEntry::Item(HamburgerItem::SwitchToDesktop),
];

#[derive(Default)]
pub struct MenuScreen {
    pub open: bool,
}

impl MenuScreen {
    pub fn toggle(&mut self, active: &mut ActiveScreen) -> bool {
        if self.open {
            self.open = false;
        } else {
            *active = ActiveScreen::Viewport;
            self.open = true;
        }
        true
    }

    pub fn render(
        &self,
        fit: &Fit,
        frame: &mut Frame,
        row_start: usize,
        row_end: usize,
        cols: usize,
    ) {
        let Some(layout) = MenuLayout::compute(&ENTRIES, cols) else {
            return;
        };

        let max_visible = row_end.saturating_sub(row_start);
        for (i, entry) in ENTRIES.iter().take(max_visible).enumerate() {
            let row = row_start + i;
            match entry {
                HamburgerEntry::Item(item) => draw_item_row(frame, fit, item, &layout, row),
                HamburgerEntry::Separator => draw_separator_row(&layout, row),
            }
        }
    }
}

enum HamburgerItem {
    Fit,
    ChangePane,
    ChangeSession,
    SwitchToDesktop,
}

impl HamburgerItem {
    fn label(&self) -> &'static str {
        match self {
            HamburgerItem::Fit => "Fit to Screen",
            HamburgerItem::ChangePane => "Change Pane",
            HamburgerItem::ChangeSession => "Change Session",
            HamburgerItem::SwitchToDesktop => "Switch to Desktop",
        }
    }

    fn action(&self) -> ClickAction {
        match self {
            HamburgerItem::Fit => ClickAction::ToggleFit,
            HamburgerItem::ChangePane => ClickAction::ExpandPanes,
            HamburgerItem::ChangeSession => ClickAction::ExpandSessions,
            HamburgerItem::SwitchToDesktop => ClickAction::ExitMobileMode,
        }
    }

    fn is_armed(&self, fit: &Fit) -> bool {
        matches!(self, HamburgerItem::Fit) && fit.active
    }
}

enum HamburgerEntry {
    Item(HamburgerItem),
    Separator,
}

impl HamburgerEntry {
    fn label_width(&self) -> Option<usize> {
        match self {
            HamburgerEntry::Item(item) => Some(UnicodeWidthStr::width(item.label())),
            HamburgerEntry::Separator => None,
        }
    }
}

struct MenuLayout {
    menu_x: usize,
    menu_w: usize,
    label_max: usize,
}

impl MenuLayout {
    fn compute(entries: &[HamburgerEntry], cols: usize) -> Option<Self> {
        let label_max = entries
            .iter()
            .filter_map(HamburgerEntry::label_width)
            .max()
            .unwrap_or(0);
        let menu_w = label_max + 2 * H_PAD;
        if label_max == 0 || menu_w > cols {
            return None;
        }
        Some(MenuLayout {
            menu_x: cols - menu_w,
            menu_w,
            label_max,
        })
    }

    fn padded_row(&self, content: &str, content_w: usize) -> String {
        let pad = " ".repeat(H_PAD);
        let trailing = " ".repeat(self.label_max.saturating_sub(content_w));
        format!("{pad}{content}{trailing}{pad}")
    }
}

fn draw_item_row(
    frame: &mut Frame,
    fit: &Fit,
    item: &HamburgerItem,
    layout: &MenuLayout,
    row: usize,
) {
    let label = item.label();
    let text = layout.padded_row(label, UnicodeWidthStr::width(label));
    let label_chars = H_PAD..H_PAD + label.chars().count();

    let mut t = Text::new(&text).selected();
    t = if item.is_armed(fit) {
        t.success_color_range(label_chars)
    } else {
        t.color_range(3, label_chars)
    };
    print_text_with_coordinates(t, layout.menu_x, row, Some(layout.menu_w), None);

    frame.click_regions.push(ClickRegion::tight(
        row,
        layout.menu_x,
        layout.menu_x + layout.menu_w,
        item.action(),
    ));
}

fn draw_separator_row(layout: &MenuLayout, row: usize) {
    let rule: String = std::iter::repeat(SEPARATOR_CHAR)
        .take(layout.label_max)
        .collect();
    let text = layout.padded_row(&rule, layout.label_max);
    print_text_with_coordinates(
        Text::new(&text).selected(),
        layout.menu_x,
        row,
        Some(layout.menu_w),
        None,
    );
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::state::State;

    #[test]
    fn hamburger_menu_emits_four_click_regions_with_separator_above_exit() {
        let mut state = State::default();
        let cols = 40;
        state.menu.render(&state.fit, &mut state.frame, 0, 20, cols);

        assert_eq!(state.frame.click_regions.len(), 4);
        let actions: Vec<ClickAction> = state
            .frame
            .click_regions
            .iter()
            .map(|r| r.action.clone())
            .collect();
        assert!(matches!(actions[0], ClickAction::ToggleFit));
        assert!(matches!(actions[1], ClickAction::ExpandPanes));
        assert!(matches!(actions[2], ClickAction::ExpandSessions));
        assert!(matches!(actions[3], ClickAction::ExitMobileMode));

        let rows: Vec<usize> = state
            .frame
            .click_regions
            .iter()
            .map(|r| r.row_start)
            .collect();
        assert_eq!(rows, vec![0, 1, 2, 4]);

        let menu_x = state.frame.click_regions[0].col_start;
        let menu_end = state.frame.click_regions[0].col_end;
        for c in menu_x..menu_end {
            assert_eq!(
                state.frame.click_to_action(3, c),
                None,
                "separator row should be non-interactive at col {c}",
            );
        }
    }
}