Add initial Home/Screen switching code

This commit is contained in:
2Shirt 2024-11-01 21:34:25 -07:00
parent 7458699859
commit 4d1ce4c108
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C
5 changed files with 110 additions and 19 deletions

View file

@ -1,6 +1,31 @@
{ {
"keybindings": { "keybindings": {
"Home": { "Home": {
"<b>": "PrevScreen",
"<Enter>": "Process",
"<q>": "Quit", // Quit the application
"<Ctrl-d>": "Quit", // Another way to quit
"<Ctrl-c>": "Quit", // Yet another way to quit
"<Ctrl-z>": "Suspend" // Suspend the application
},
"SelectDisks": {
"<b>": "PrevScreen",
"<Enter>": "Process",
"<q>": "Quit", // Quit the application
"<Ctrl-d>": "Quit", // Another way to quit
"<Ctrl-c>": "Quit", // Yet another way to quit
"<Ctrl-z>": "Suspend" // Suspend the application
},
"SelectParts": {
"<b>": "PrevScreen",
"<Enter>": "Process",
"<q>": "Quit", // Quit the application
"<Ctrl-d>": "Quit", // Another way to quit
"<Ctrl-c>": "Quit", // Yet another way to quit
"<Ctrl-z>": "Suspend" // Suspend the application
},
"Done": {
"<Enter>": "Quit",
"<q>": "Quit", // Quit the application "<q>": "Quit", // Quit the application
"<Ctrl-d>": "Quit", // Another way to quit "<Ctrl-d>": "Quit", // Another way to quit
"<Ctrl-c>": "Quit", // Yet another way to quit "<Ctrl-c>": "Quit", // Yet another way to quit

View file

@ -1,6 +1,8 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use strum::Display; use strum::Display;
use crate::app::Mode;
#[derive(Debug, Clone, PartialEq, Eq, Display, Serialize, Deserialize)] #[derive(Debug, Clone, PartialEq, Eq, Display, Serialize, Deserialize)]
pub enum Action { pub enum Action {
Tick, Tick,
@ -12,4 +14,9 @@ pub enum Action {
ClearScreen, ClearScreen,
Error(String), Error(String),
Help, Help,
SetMode(Mode),
PrevScreen,
Process,
SelectDisks(Option<u16>, Option<u16>), // indicies for (source, dest)
SelectParts(Option<u16>, Option<u16>), // indicies for (boot, os)
} }

View file

@ -1,4 +1,4 @@
use std::iter::zip; use std::{collections::HashMap, iter::zip};
use color_eyre::Result; use color_eyre::Result;
use crossterm::event::KeyEvent; use crossterm::event::KeyEvent;
@ -25,6 +25,7 @@ pub struct App {
tick_rate: f64, tick_rate: f64,
frame_rate: f64, frame_rate: f64,
components: Vec<Box<dyn Component>>, components: Vec<Box<dyn Component>>,
component_indices: HashMap<String, u64>,
should_quit: bool, should_quit: bool,
should_suspend: bool, should_suspend: bool,
mode: Mode, mode: Mode,
@ -37,6 +38,29 @@ pub struct App {
pub enum Mode { pub enum Mode {
#[default] #[default]
Home, Home,
SelectDisks,
SelectParts,
Done,
}
impl Mode {
pub fn next_screen(cur_mode: Mode) -> Mode {
match cur_mode {
Mode::Home => Mode::SelectDisks,
Mode::SelectDisks => Mode::SelectParts,
Mode::SelectParts => Mode::Done,
Mode::Done => Mode::Done,
}
}
pub fn prev_screen(cur_mode: Mode) -> Mode {
match cur_mode {
Mode::Home => Mode::Home,
Mode::SelectDisks => Mode::Home,
Mode::SelectParts => Mode::SelectDisks,
Mode::Done => Mode::SelectParts,
}
}
} }
impl App { impl App {
@ -53,6 +77,14 @@ impl App {
Box::new(Footer::new()), Box::new(Footer::new()),
Box::new(Popup::new()), Box::new(Popup::new()),
], ],
component_indices: HashMap::from([
(String::from("title"), 0),
(String::from("fps"), 1),
(String::from("left"), 2),
(String::from("right"), 3),
(String::from("footer"), 4),
(String::from("popup"), 5),
]),
should_quit: false, should_quit: false,
should_suspend: false, should_suspend: false,
config: Config::new()?, config: Config::new()?,
@ -160,6 +192,13 @@ impl App {
Action::ClearScreen => tui.terminal.clear()?, Action::ClearScreen => tui.terminal.clear()?,
Action::Resize(w, h) => self.handle_resize(tui, w, h)?, Action::Resize(w, h) => self.handle_resize(tui, w, h)?,
Action::Render => self.render(tui)?, Action::Render => self.render(tui)?,
Action::PrevScreen => self
.action_tx
.send(Action::SetMode(Mode::prev_screen(self.mode)))?,
Action::Process => self
.action_tx
.send(Action::SetMode(Mode::next_screen(self.mode)))?,
Action::SetMode(new_mode) => self.mode = new_mode,
_ => {} _ => {}
} }
for component in self.components.iter_mut() { for component in self.components.iter_mut() {

View file

@ -3,18 +3,21 @@ use ratatui::{prelude::*, widgets::*};
use tokio::sync::mpsc::UnboundedSender; use tokio::sync::mpsc::UnboundedSender;
use super::Component; use super::Component;
use crate::{action::Action, config::Config}; use crate::{action::Action, app::Mode, config::Config};
#[derive(Default)] #[derive(Default)]
pub struct Footer { pub struct Footer {
command_tx: Option<UnboundedSender<Action>>, command_tx: Option<UnboundedSender<Action>>,
config: Config, config: Config,
show_install_driver: bool, footer_text: String,
} }
impl Footer { impl Footer {
pub fn new() -> Self { pub fn new() -> Self {
Self::default() Self {
footer_text: String::from("(Enter) to select / (b) to go back / (q) to quit"),
..Default::default()
}
} }
} }
@ -37,22 +40,29 @@ impl Component for Footer {
Action::Render => { Action::Render => {
// add any logic here that should run on every render // add any logic here that should run on every render
} }
Action::SetMode(new_mode) => {
self.footer_text = match new_mode {
Mode::SelectDisks => String::from(
"(Enter) to select / (b) to go back / (i) to install driver / (q) to quit",
),
Mode::Done => String::from("(Enter) or (q) to quit"),
_ => String::from("(Enter) to select / (b) to go back / (q) to quit"),
}
}
_ => {} _ => {}
} }
Ok(None) Ok(None)
} }
fn draw(&mut self, frame: &mut Frame, area: Rect) -> Result<()> { fn draw(&mut self, frame: &mut Frame, area: Rect) -> Result<()> {
let footer_text = Span::styled( let footer = Paragraph::new(
if self.show_install_driver { Line::from(Span::styled(
"(Enter) to select / (b) to go back / (i) to install driver / (q) to quit" &self.footer_text,
} else { Style::default().fg(Color::DarkGray),
"(Enter) to select / (b) to go back / (q) to quit" ))
}, .centered(),
Style::default().fg(Color::DarkGray), )
); .block(Block::default().borders(Borders::ALL));
let footer = Paragraph::new(Line::from(footer_text).centered())
.block(Block::default().borders(Borders::ALL));
frame.render_widget(footer, area); frame.render_widget(footer, area);
Ok(()) Ok(())
} }

View file

@ -4,17 +4,21 @@ use ratatui::{prelude::*, widgets::*};
use tokio::sync::mpsc::UnboundedSender; use tokio::sync::mpsc::UnboundedSender;
use super::Component; use super::Component;
use crate::{action::Action, config::Config}; use crate::{action::Action, app::Mode, config::Config};
#[derive(Default)] #[derive(Default)]
pub struct Left { pub struct Left {
command_tx: Option<UnboundedSender<Action>>, command_tx: Option<UnboundedSender<Action>>,
config: Config, config: Config,
title_text: String,
} }
impl Left { impl Left {
pub fn new() -> Self { pub fn new() -> Self {
Self::default() Self {
title_text: String::from("Home"),
..Default::default()
}
} }
} }
@ -43,6 +47,12 @@ impl Component for Left {
Action::Render => { Action::Render => {
// add any logic here that should run on every render // add any logic here that should run on every render
} }
Action::SetMode(new_mode) => match new_mode {
Mode::Home => self.title_text = String::from("Home"),
Mode::SelectDisks => self.title_text = String::from("Select Source Disk"),
Mode::SelectParts => self.title_text = String::from("Select Boot Partition"),
Mode::Done => self.title_text = String::from("Done"),
},
_ => {} _ => {}
} }
Ok(None) Ok(None)
@ -55,9 +65,9 @@ impl Component for Left {
.areas(area); .areas(area);
// Title // Title
let title_text = String::from("Hello world!"); let title =
let title = Paragraph::new(Line::from(title_text).centered()) Paragraph::new(Line::from(Span::styled(&self.title_text, Style::default())).centered())
.block(Block::default().borders(Borders::NONE)); .block(Block::default().borders(Borders::NONE));
frame.render_widget(title, title_area); frame.render_widget(title, title_area);
// Body // Body