morpheus_bootloader/tui/iso_manager/
state.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
266
267
268
269
270
271
272
273
274
275
276
//! ISO Manager State
//!
//! State management for the ISO manager TUI.

use morpheus_core::iso::{IsoEntry, IsoStorageManager, MAX_ISOS};

/// View mode for the ISO manager
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ViewMode {
    /// List view showing all ISOs
    List,
    /// Detail view for selected ISO
    Details,
    /// Confirm delete dialog
    ConfirmDelete,
    /// Confirm boot dialog
    ConfirmBoot,
}

/// Action result from user input
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Action {
    /// No action, continue
    None,
    /// Return to previous screen
    Back,
    /// Boot the selected ISO
    Boot(usize),
    /// Delete the selected ISO
    Delete(usize),
    /// Refresh the ISO list
    Refresh,
}

/// ISO manager state
pub struct IsoManagerState {
    /// Current view mode
    pub mode: ViewMode,
    /// Selected ISO index
    pub selected: usize,
    /// Total number of ISOs
    pub count: usize,
    /// Cached ISO names for display (avoid repeated string ops)
    pub names: [[u8; 64]; MAX_ISOS],
    /// Name lengths
    pub name_lens: [usize; MAX_ISOS],
    /// Cached sizes (in MB)
    pub sizes_mb: [u64; MAX_ISOS],
    /// Cached chunk counts
    pub chunk_counts: [usize; MAX_ISOS],
    /// Cached completion status
    pub complete: [bool; MAX_ISOS],
    /// Error message to display (if any)
    pub error_msg: Option<&'static str>,
}

impl IsoManagerState {
    /// Create new state
    pub fn new() -> Self {
        Self {
            mode: ViewMode::List,
            selected: 0,
            count: 0,
            names: [[0u8; 64]; MAX_ISOS],
            name_lens: [0; MAX_ISOS],
            sizes_mb: [0; MAX_ISOS],
            chunk_counts: [0; MAX_ISOS],
            complete: [false; MAX_ISOS],
            error_msg: None,
        }
    }

    /// Load ISO data from storage manager
    pub fn load_from_manager(&mut self, manager: &IsoStorageManager) {
        self.count = manager.count();
        self.selected = self.selected.min(self.count.saturating_sub(1));

        for (i, (_, entry)) in manager.iter().enumerate() {
            if i >= MAX_ISOS {
                break;
            }

            // Copy name
            let manifest = &entry.manifest;
            let name_len = manifest.name_len.min(64);
            self.names[i][..name_len].copy_from_slice(&manifest.name[..name_len]);
            self.name_lens[i] = name_len;

            // Size in MB
            self.sizes_mb[i] = manifest.total_size / (1024 * 1024);

            // Chunk count
            self.chunk_counts[i] = manifest.chunks.count;

            // Completion status
            self.complete[i] = manifest.is_complete();
        }
    }

    /// Get selected ISO name as str
    pub fn selected_name(&self) -> &str {
        if self.selected < self.count {
            core::str::from_utf8(&self.names[self.selected][..self.name_lens[self.selected]])
                .unwrap_or("???")
        } else {
            ""
        }
    }

    /// Get selected ISO size in MB
    pub fn selected_size_mb(&self) -> u64 {
        if self.selected < self.count {
            self.sizes_mb[self.selected]
        } else {
            0
        }
    }

    /// Get selected ISO chunk count
    pub fn selected_chunks(&self) -> usize {
        if self.selected < self.count {
            self.chunk_counts[self.selected]
        } else {
            0
        }
    }

    /// Check if selected ISO is complete
    pub fn selected_complete(&self) -> bool {
        if self.selected < self.count {
            self.complete[self.selected]
        } else {
            false
        }
    }

    /// Move selection up
    pub fn select_prev(&mut self) {
        if self.selected > 0 {
            self.selected -= 1;
        }
    }

    /// Move selection down
    pub fn select_next(&mut self) {
        if self.count > 0 && self.selected < self.count - 1 {
            self.selected += 1;
        }
    }

    /// Handle key input, return action
    pub fn handle_key(&mut self, scan_code: u16, unicode: u16) -> Action {
        match self.mode {
            ViewMode::List => self.handle_list_key(scan_code, unicode),
            ViewMode::Details => self.handle_details_key(scan_code, unicode),
            ViewMode::ConfirmDelete => self.handle_confirm_delete_key(scan_code, unicode),
            ViewMode::ConfirmBoot => self.handle_confirm_boot_key(scan_code, unicode),
        }
    }

    fn handle_list_key(&mut self, scan_code: u16, unicode: u16) -> Action {
        // ESC - return to main menu
        if scan_code == 0x17 {
            return Action::Back;
        }

        // Up arrow
        if scan_code == 0x01 {
            self.select_prev();
            return Action::None;
        }

        // Down arrow
        if scan_code == 0x02 {
            self.select_next();
            return Action::None;
        }

        // Enter - show details
        if unicode == 0x0D && self.count > 0 {
            self.mode = ViewMode::Details;
            return Action::None;
        }

        // 'd' or 'D' - delete
        if (unicode == 0x64 || unicode == 0x44) && self.count > 0 {
            self.mode = ViewMode::ConfirmDelete;
            return Action::None;
        }

        // 'b' or 'B' - boot
        if (unicode == 0x62 || unicode == 0x42) && self.count > 0 && self.selected_complete() {
            self.mode = ViewMode::ConfirmBoot;
            return Action::None;
        }

        // 'r' or 'R' - refresh
        if unicode == 0x72 || unicode == 0x52 {
            return Action::Refresh;
        }

        Action::None
    }

    fn handle_details_key(&mut self, scan_code: u16, unicode: u16) -> Action {
        // ESC or Backspace - back to list
        if scan_code == 0x17 || unicode == 0x08 {
            self.mode = ViewMode::List;
            return Action::None;
        }

        // 'b' or 'B' - boot from details
        if (unicode == 0x62 || unicode == 0x42) && self.selected_complete() {
            self.mode = ViewMode::ConfirmBoot;
            return Action::None;
        }

        // 'd' or 'D' - delete from details
        if unicode == 0x64 || unicode == 0x44 {
            self.mode = ViewMode::ConfirmDelete;
            return Action::None;
        }

        Action::None
    }

    fn handle_confirm_delete_key(&mut self, _scan_code: u16, unicode: u16) -> Action {
        // 'y' or 'Y' - confirm delete
        if unicode == 0x79 || unicode == 0x59 {
            let idx = self.selected;
            self.mode = ViewMode::List;
            return Action::Delete(idx);
        }

        // 'n' or 'N' or ESC - cancel
        if unicode == 0x6E || unicode == 0x4E || unicode == 0x1B {
            self.mode = ViewMode::List;
            return Action::None;
        }

        Action::None
    }

    fn handle_confirm_boot_key(&mut self, _scan_code: u16, unicode: u16) -> Action {
        // 'y' or 'Y' - confirm boot
        if unicode == 0x79 || unicode == 0x59 {
            let idx = self.selected;
            self.mode = ViewMode::List;
            return Action::Boot(idx);
        }

        // 'n' or 'N' or ESC - cancel
        if unicode == 0x6E || unicode == 0x4E || unicode == 0x1B {
            self.mode = ViewMode::List;
            return Action::None;
        }

        Action::None
    }

    /// Set error message
    pub fn set_error(&mut self, msg: &'static str) {
        self.error_msg = Some(msg);
    }

    /// Clear error message
    pub fn clear_error(&mut self) {
        self.error_msg = None;
    }
}

impl Default for IsoManagerState {
    fn default() -> Self {
        Self::new()
    }
}