From 713dc8c2de2e3676101fc57339311084083036c9 Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Mon, 5 May 2025 22:06:49 -0700 Subject: [PATCH] Use Enum for left selection type --- boot_diags/src/app.rs | 36 +++++++++++++++++++++--------------- core/src/action.rs | 15 ++++++++------- core/src/components/left.rs | 21 +++++++++++++++------ deja_vu/src/app.rs | 30 ++++++++++++++++++------------ pe_menu/src/app.rs | 12 +++++++++--- 5 files changed, 71 insertions(+), 43 deletions(-) diff --git a/boot_diags/src/app.rs b/boot_diags/src/app.rs index 2f9496c..ee754c8 100644 --- a/boot_diags/src/app.rs +++ b/boot_diags/src/app.rs @@ -17,8 +17,14 @@ use crate::diags; use core::{ action::Action, components::{ - Component, footer::Footer, fps::FpsCounter, left::Left, popup, right::Right, - state::StatefulList, title::Title, + Component, + footer::Footer, + fps::FpsCounter, + left::{Left, SelectionType}, + popup, + right::Right, + state::StatefulList, + title::Title, }, config::Config, line::{DVLine, get_disk_description_right, get_part_description}, @@ -740,15 +746,15 @@ fn build_footer_string(cur_mode: Mode) -> String { fn build_left_items(app: &App) -> Action { let mut items = Vec::new(); let mut labels = vec![String::new(), String::new()]; - let select_num: usize; + let select_type: SelectionType; let title: String; match app.cur_mode { Mode::Home => { - select_num = 0; + select_type = SelectionType::Loop; title = String::from("Home"); } Mode::DiagMenu => { - select_num = 0; + select_type = SelectionType::Loop; title = String::from("Troubleshooting"); app.list.items.iter().for_each(|mode| { let (name, _) = get_mode_strings(*mode); @@ -756,7 +762,7 @@ fn build_left_items(app: &App) -> Action { }); } Mode::InstallDrivers => { - select_num = 1; + select_type = SelectionType::One; title = String::from("Install Drivers"); app.clone .driver_list @@ -764,7 +770,7 @@ fn build_left_items(app: &App) -> Action { .for_each(|driver| items.push(driver.to_string())); } Mode::InjectDrivers => { - select_num = 1; + select_type = SelectionType::One; title = String::from("Select Drivers"); app.clone .driver_list @@ -772,7 +778,7 @@ fn build_left_items(app: &App) -> Action { .for_each(|driver| items.push(driver.to_string())); } Mode::SelectDisks => { - select_num = 1; + select_type = SelectionType::One; title = String::from("Select Disk"); let disk_list = app.clone.disk_list.lock().unwrap(); disk_list @@ -780,11 +786,11 @@ fn build_left_items(app: &App) -> Action { .for_each(|disk| items.push(disk.description.to_string())); } Mode::BootScan | Mode::ScanDisks => { - select_num = 0; + select_type = SelectionType::Loop; title = String::from("Processing"); } Mode::SelectParts => { - select_num = 2; + select_type = SelectionType::Two; title = String::from("Select Boot and OS Partitions"); labels[0] = String::from("boot"); labels[1] = String::from("os"); @@ -798,7 +804,7 @@ fn build_left_items(app: &App) -> Action { } } Mode::BootDiags => { - select_num = 0; + select_type = SelectionType::Loop; let (new_title, _) = get_mode_strings(app.cur_mode); title = new_title; app.diag_groups.get().iter().for_each(|group| { @@ -810,19 +816,19 @@ fn build_left_items(app: &App) -> Action { }); } Mode::BootSetup => { - select_num = 0; + select_type = SelectionType::Loop; let (new_title, _) = get_mode_strings(app.cur_mode); title = new_title; } Mode::SetBootMode => { - select_num = 1; + select_type = SelectionType::One; title = String::from("Set Boot Mode"); app.boot_modes.iter().for_each(|entry| { items.push(format!("{:?} Safe Mode", entry)); }); } Mode::Done | Mode::Failed => { - select_num = 0; + select_type = SelectionType::Loop; title = String::from("Done"); } // Invalid states @@ -835,7 +841,7 @@ fn build_left_items(app: &App) -> Action { panic!("This shouldn't happen?") } }; - Action::UpdateLeft(title, labels, items, select_num) + Action::UpdateLeft(title, labels, items, select_type) } fn build_right_items(app: &App) -> Action { diff --git a/core/src/action.rs b/core/src/action.rs index 486cdbb..5580390 100644 --- a/core/src/action.rs +++ b/core/src/action.rs @@ -16,7 +16,12 @@ use serde::{Deserialize, Serialize}; use strum::Display; -use crate::{components::popup::Type, line::DVLine, state::Mode, system::disk::Disk}; +use crate::{ + components::{left::SelectionType, popup::Type as PopupType}, + line::DVLine, + state::Mode, + system::disk::Disk, +}; #[derive(Debug, Clone, PartialEq, Eq, Display, Serialize, Deserialize)] pub enum Action { @@ -32,11 +37,7 @@ pub enum Action { TasksComplete, UpdateDiskList(Vec), UpdateFooter(String), - UpdateLeft(String, Vec, Vec, usize), // (title, labels, items, select_num) - // NOTE: select_num should be set to 0, 1, or 2. - // 0: For repeating selections - // 1: For a single choice - // 2: For two selections (obviously) + UpdateLeft(String, Vec, Vec, SelectionType), // (title, labels, items, select_type) UpdateRight(Vec>, usize, Vec>), // (labels, start_index, items) - items before start are always shown // App (PE-Menu) OpenTerminal, @@ -44,7 +45,7 @@ pub enum Action { Shutdown, // Screens DismissPopup, - DisplayPopup(Type, String), + DisplayPopup(PopupType, String), NextScreen, PrevScreen, SetMode(Mode), diff --git a/core/src/components/left.rs b/core/src/components/left.rs index e83a6e1..d891947 100644 --- a/core/src/components/left.rs +++ b/core/src/components/left.rs @@ -19,18 +19,27 @@ use ratatui::{ prelude::*, widgets::{Block, Borders, HighlightSpacing, List, ListItem, Padding, Paragraph}, }; +use serde::{Deserialize, Serialize}; use tokio::sync::mpsc::UnboundedSender; use super::{Component, state::StatefulList}; use crate::{action::Action, config::Config}; +#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)] +pub enum SelectionType { + #[default] + Loop, + One, + Two, +} + #[derive(Default)] pub struct Left { command_tx: Option>, config: Config, labels: Vec, list: StatefulList, - select_num: usize, + select_type: SelectionType, selections: Vec>, selections_saved: Vec>, title_text: String, @@ -40,8 +49,8 @@ impl Left { #[must_use] pub fn new() -> Self { Self { - select_num: 0, labels: vec![String::from("one"), String::from("two")], + select_type: SelectionType::Loop, selections: vec![None, None], selections_saved: vec![None, None], title_text: String::from("Home"), @@ -76,14 +85,14 @@ impl Component for Left { Action::KeyUp => self.list.previous(), Action::KeyDown => self.list.next(), Action::Process => { - if self.select_num == 0 { + if self.select_type == SelectionType::Loop { // Selections aren't being used so this is a no-op } else if let Some(command_tx) = self.command_tx.clone() { match (self.selections[0], self.selections[1]) { (None, None) => { // Making first selection command_tx.send(Action::Select(self.list.selected(), None))?; - if self.select_num == 1 { + if self.select_type == SelectionType::One { // Confirm selection command_tx.send(Action::NextScreen)?; } @@ -118,14 +127,14 @@ impl Component for Left { self.selections[0] = None; self.selections[1] = None; } - Action::UpdateLeft(title, labels, items, select_num) => { + Action::UpdateLeft(title, labels, items, select_type) => { self.title_text = title; self.labels = labels .iter() .map(|label| format!(" ~{}~", label.to_lowercase())) .collect(); self.list.set_items(items); - self.select_num = select_num; + self.select_type = select_type; } _ => {} } diff --git a/deja_vu/src/app.rs b/deja_vu/src/app.rs index c0f9fb6..4a4f595 100644 --- a/deja_vu/src/app.rs +++ b/deja_vu/src/app.rs @@ -16,8 +16,14 @@ use core::{ action::Action, components::{ - Component, footer::Footer, fps::FpsCounter, left::Left, popup, right::Right, - state::StatefulList, title::Title, + Component, + footer::Footer, + fps::FpsCounter, + left::{Left, SelectionType}, + popup, + right::Right, + state::StatefulList, + title::Title, }, config::Config, line::{DVLine, get_disk_description_right, get_part_description}, @@ -627,17 +633,17 @@ fn build_footer_string(cur_mode: Mode) -> String { } fn build_left_items(app: &App, cur_mode: Mode) -> Action { - let select_num: usize; + let select_type: SelectionType; let title: String; let mut items = Vec::new(); let mut labels: Vec = Vec::new(); match cur_mode { Mode::Home => { - select_num = 0; + select_type = SelectionType::Loop; title = String::from("Home"); } Mode::InstallDrivers => { - select_num = 1; + select_type = SelectionType::One; title = String::from("Install Drivers"); app.clone .driver_list @@ -645,7 +651,7 @@ fn build_left_items(app: &App, cur_mode: Mode) -> Action { .for_each(|driver| items.push(driver.to_string())); } Mode::SelectDisks => { - select_num = 2; + select_type = SelectionType::Two; title = String::from("Select Source and Destination Disks"); labels.push(String::from("source")); labels.push(String::from("dest")); @@ -655,21 +661,21 @@ fn build_left_items(app: &App, cur_mode: Mode) -> Action { .for_each(|disk| items.push(disk.description.to_string())); } Mode::SelectTableType => { - select_num = 1; + select_type = SelectionType::One; title = String::from("Select Partition Table Type"); items.push(format!("{}", PartitionTableType::Guid)); items.push(format!("{}", PartitionTableType::Legacy)); } Mode::Confirm => { - select_num = 0; + select_type = SelectionType::Loop; title = String::from("Confirm Selections"); } Mode::ScanDisks | Mode::PreClone | Mode::Clone | Mode::PostClone => { - select_num = 0; + select_type = SelectionType::Loop; title = String::from("Processing"); } Mode::SelectParts => { - select_num = 2; + select_type = SelectionType::Two; title = String::from("Select Boot and OS Partitions"); labels.push(String::from("boot")); labels.push(String::from("os")); @@ -683,7 +689,7 @@ fn build_left_items(app: &App, cur_mode: Mode) -> Action { } } Mode::Done | Mode::Failed => { - select_num = 0; + select_type = SelectionType::Loop; title = String::from("Done"); } // Invalid states @@ -695,7 +701,7 @@ fn build_left_items(app: &App, cur_mode: Mode) -> Action { | Mode::PEMenu | Mode::SetBootMode => panic!("This shouldn't happen?"), }; - Action::UpdateLeft(title, labels, items, select_num) + Action::UpdateLeft(title, labels, items, select_type) } fn build_right_items(app: &App, cur_mode: Mode) -> Action { diff --git a/pe_menu/src/app.rs b/pe_menu/src/app.rs index ed9afee..4572597 100644 --- a/pe_menu/src/app.rs +++ b/pe_menu/src/app.rs @@ -16,8 +16,14 @@ use core::{ action::Action, components::{ - Component, footer::Footer, fps::FpsCounter, left::Left, popup, right::Right, - state::StatefulList, title::Title, + Component, + footer::Footer, + fps::FpsCounter, + left::{Left, SelectionType}, + popup, + right::Right, + state::StatefulList, + title::Title, }, config::Config, line::DVLine, @@ -411,7 +417,7 @@ fn build_left_items(app: &App) -> Action { }) // ─ .collect(); - Action::UpdateLeft(title, labels, items, 0) + Action::UpdateLeft(title, labels, items, SelectionType::Loop) } fn build_right_items(app: &App) -> Action {