morpheus_bootloader/tui/
main_menu.rs

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
use crate::tui::debug::DebugOverlay;
use crate::tui::input::{InputKey, Keyboard};
use crate::tui::renderer::{Screen, EFI_BLACK, EFI_DARKGREEN, EFI_GREEN, EFI_LIGHTGREEN};

const HEADER_ART: &[&str] = &[
    "  _____ _____ _____ _____ _   _ _____ _   _ _____ ",
    " |     |     | __  |  _  |   | |   __| | | |   __|",
    " | | | |  |  |    -|   __|     |   __| |_| |__   |",
    " |_|_|_|_____|__|__|__|  |__|__|_____|\\___|_____|",
    "      C O N T R O L   C E N T E R   v 1 . 2       ",
];

const SIDE_BORDER_LEFT: &str = "|";
const SIDE_BORDER_RIGHT: &str = "|";
const TOP_BORDER: &str =
    "+===========================================================================+";
const BOTTOM_BORDER: &str =
    "+===========================================================================+";
const DIVIDER: &str =
    "+===========================================================================+";

pub struct MainMenu {
    selected_index: usize,
    menu_items: [MenuItem; 6],
    debug: DebugOverlay,
}

pub struct MenuItem {
    pub label: &'static str,
    pub description: &'static str,
    pub icon: &'static str,
}

impl MainMenu {
    pub fn new(_screen: &Screen) -> Self {
        Self {
            selected_index: 0,
            debug: DebugOverlay::new(),
            menu_items: [
                MenuItem {
                    label: "Distro Launcher",
                    description: "Boot into ephemeral Linux distribution",
                    icon: "[>>]",
                },
                MenuItem {
                    label: "Distro Downloader",
                    description: "Download and manage distro templates",
                    icon: "[DN]",
                },
                MenuItem {
                    label: "Storage Manager",
                    description: "Manage partitions and overlay filesystems",
                    icon: "[FS]",
                },
                MenuItem {
                    label: "Installation",
                    description: "Persists the bootloader to disk",
                    icon: "[INS]",
                },
                MenuItem {
                    label: "Admin Functions",
                    description: "Advanced operations and diagnostics",
                    icon: "[ADM]",
                },
                MenuItem {
                    label: "Exit to Firmware",
                    description: "Return to UEFI boot menu",
                    icon: "[EXT]",
                },
            ],
        }
    }

    pub fn select_next(&mut self) {
        if self.selected_index < self.menu_items.len() - 1 {
            self.selected_index += 1;
        }
    }

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

    pub fn render(&mut self, screen: &mut Screen) {
        // Calculate centered X position based on border width (77 chars)
        let border_width = 77;
        let x = if screen.width() > border_width {
            (screen.width() - border_width) / 2
        } else {
            0
        };
        let y = 0;

        // Draw top border
        screen.put_str_at(x, y, TOP_BORDER, EFI_GREEN, EFI_BLACK);

        // Draw header ASCII art centered within border
        for (i, line) in HEADER_ART.iter().enumerate() {
            let art_y = y + 1 + i;
            let padding = (76 - line.len()) / 2;

            screen.put_str_at(x, art_y, SIDE_BORDER_LEFT, EFI_GREEN, EFI_BLACK);

            // Center the art
            let art_x = x + 1 + padding;
            screen.put_str_at(art_x, art_y, line, EFI_LIGHTGREEN, EFI_BLACK);

            screen.put_str_at(x + 75, art_y, SIDE_BORDER_RIGHT, EFI_GREEN, EFI_BLACK);
        }

        // Divider after header
        let divider_y = y + 1 + HEADER_ART.len();
        screen.put_str_at(x, divider_y, DIVIDER, EFI_GREEN, EFI_BLACK);

        // Instructions
        let instr_y = divider_y + 1;
        screen.put_str_at(x, instr_y, SIDE_BORDER_LEFT, EFI_GREEN, EFI_BLACK);
        let instr = "  [UP/DOWN] Navigate  |  [ENTER] Select  |  [ESC] Exit";
        let instr_x = x + (76 - instr.len()) / 2;
        screen.put_str_at(instr_x, instr_y, instr, EFI_DARKGREEN, EFI_BLACK);
        screen.put_str_at(x + 75, instr_y, SIDE_BORDER_RIGHT, EFI_GREEN, EFI_BLACK);

        // Another divider
        let menu_divider_y = instr_y + 1;
        screen.put_str_at(x, menu_divider_y, DIVIDER, EFI_GREEN, EFI_BLACK);

        // Menu items - each takes 2 lines
        let menu_start_y = menu_divider_y + 2;
        for (i, item) in self.menu_items.iter().enumerate() {
            let item_y = menu_start_y + i * 2;

            // Left border
            screen.put_str_at(x, item_y, SIDE_BORDER_LEFT, EFI_GREEN, EFI_BLACK);

            if i == self.selected_index {
                // Selection bracket and icon
                screen.put_str_at(x + 3, item_y, ">>", EFI_LIGHTGREEN, EFI_BLACK);
                screen.put_str_at(x + 6, item_y, item.icon, EFI_LIGHTGREEN, EFI_BLACK);
                screen.put_str_at(x + 12, item_y, item.label, EFI_LIGHTGREEN, EFI_BLACK);
                screen.put_str_at(x + 32, item_y, "-", EFI_GREEN, EFI_BLACK);
                screen.put_str_at(x + 34, item_y, item.description, EFI_GREEN, EFI_BLACK);
            } else {
                // Unselected item
                screen.put_str_at(x + 6, item_y, item.icon, EFI_DARKGREEN, EFI_BLACK);
                screen.put_str_at(x + 12, item_y, item.label, EFI_GREEN, EFI_BLACK);
            }

            // Right border
            screen.put_str_at(x + 75, item_y, SIDE_BORDER_RIGHT, EFI_GREEN, EFI_BLACK);
        }

        // Bottom divider
        let bottom_div_y = menu_start_y + (self.menu_items.len() * 2) + 1;
        screen.put_str_at(x, bottom_div_y, DIVIDER, EFI_GREEN, EFI_BLACK);

        // Status bar
        let status_y = bottom_div_y + 1;
        screen.put_str_at(x, status_y, SIDE_BORDER_LEFT, EFI_GREEN, EFI_BLACK);
        screen.put_str_at(x + 3, status_y, "Status: READY", EFI_LIGHTGREEN, EFI_BLACK);
        screen.put_str_at(x + 20, status_y, "|", EFI_GREEN, EFI_BLACK);
        screen.put_str_at(
            x + 22,
            status_y,
            "System: Operational",
            EFI_GREEN,
            EFI_BLACK,
        );
        screen.put_str_at(x + 75, status_y, SIDE_BORDER_RIGHT, EFI_GREEN, EFI_BLACK);

        // Bottom border
        screen.put_str_at(x, status_y + 1, BOTTOM_BORDER, EFI_GREEN, EFI_BLACK);
    }

    pub fn handle_input(&mut self, key: &InputKey) -> MenuAction {
        // Arrow up
        if key.scan_code == 0x01 {
            self.select_prev();
            return MenuAction::Navigate;
        }

        // Arrow down
        if key.scan_code == 0x02 {
            self.select_next();
            return MenuAction::Navigate;
        }

        // Enter key
        if key.unicode_char == 0x0D {
            return match self.selected_index {
                0 => MenuAction::DistroLauncher,
                1 => MenuAction::DistroDownloader,
                2 => MenuAction::StorageManager,
                3 => MenuAction::SystemSettings,
                4 => MenuAction::AdminFunctions,
                5 => MenuAction::ExitToFirmware,
                _ => MenuAction::Navigate,
            };
        }

        // ESC key
        if key.scan_code == 0x17 {
            return MenuAction::ExitToFirmware;
        }

        MenuAction::Navigate
    }

    pub fn run(&mut self, screen: &mut Screen, keyboard: &mut Keyboard) -> MenuAction {
        // Initial render
        screen.clear();
        self.render(screen);
        self.debug.render(screen);

        loop {
            // Render global rain if active
            crate::tui::rain::render_rain(screen);

            // Always render debug overlay on top
            self.debug.render(screen);

            // Check for input with frame limiting (~60 FPS)
            if let Some(key) = keyboard.poll_key_with_delay() {
                // Debug overlay toggle
                if key.unicode_char == b'd' as u16 || key.unicode_char == b'D' as u16 {
                    self.debug.toggle();
                    screen.clear();
                    self.render(screen);
                    self.debug.render(screen);
                    continue;
                }

                // Global rain toggle
                if key.unicode_char == b'x' as u16 || key.unicode_char == b'X' as u16 {
                    crate::tui::rain::toggle_rain(screen);
                    screen.clear();
                    self.render(screen);
                    self.debug.render(screen);
                    continue;
                }

                let action = self.handle_input(&key);
                if !matches!(action, MenuAction::Navigate) {
                    return action;
                }

                // Re-render UI after navigation (without clearing)
                self.render(screen);
                self.debug.render(screen);
            }
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum MenuAction {
    Navigate,
    DistroLauncher,
    DistroDownloader,
    StorageManager,
    SystemSettings,
    AdminFunctions,
    ExitToFirmware,
}