Skip to content

Commit

Permalink
fix(launchpad): check if component is active before handling events
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin committed May 13, 2024
1 parent 2afeccd commit 4c12171
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 17 deletions.
14 changes: 9 additions & 5 deletions node-launchpad/src/components/discord_username.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ use ratatui::{prelude::*, widgets::*};
use tui_input::{backend::crossterm::EventHandler, Input};

pub struct DiscordUsernameInputBox {
show_scene: bool,
/// Whether the component is active right now, capturing keystrokes + draw things.
active: bool,
discord_input_filed: Input,
// cache the old value incase user presses Esc.
old_value: String,
Expand All @@ -26,7 +27,7 @@ pub struct DiscordUsernameInputBox {
impl DiscordUsernameInputBox {
pub fn new(username: String) -> Self {
Self {
show_scene: false,
active: false,
discord_input_filed: Input::default().with_value(username),
old_value: Default::default(),
}
Expand All @@ -35,6 +36,9 @@ impl DiscordUsernameInputBox {

impl Component for DiscordUsernameInputBox {
fn handle_key_events(&mut self, key: KeyEvent) -> Result<Vec<Action>> {
if !self.active {
return Ok(vec![]);
}
// while in entry mode, keybinds are not captured, so gotta exit entry mode from here
let send_back = match key.code {
KeyCode::Enter => {
Expand Down Expand Up @@ -79,14 +83,14 @@ impl Component for DiscordUsernameInputBox {
let send_back = match action {
Action::SwitchScene(scene) => match scene {
Scene::DiscordUsernameInputBox => {
self.show_scene = true;
self.active = true;
self.old_value = self.discord_input_filed.value().to_string();
// set to entry input mode as we want to handle everything within our handle_key_events
// so by default if this scene is active, we capture inputs.
Some(Action::SwitchInputMode(InputMode::Entry))
}
_ => {
self.show_scene = false;
self.active = false;
None
}
},
Expand All @@ -96,7 +100,7 @@ impl Component for DiscordUsernameInputBox {
}

fn draw(&mut self, f: &mut crate::tui::Frame<'_>, area: Rect) -> Result<()> {
if !self.show_scene {
if !self.active {
return Ok(());
}

Expand Down
2 changes: 1 addition & 1 deletion node-launchpad/src/components/footer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Component for Footer {

let text: Cow<_> = match self.current_scene {
Scene::Home => {
"[Ctrl+g] Start nodes, [Ctrl+x] Stop node, [O] Set Resources, [D]iscord Username, [Q]uit".into()
"[Ctrl+g] Start nodes, [Ctrl+x] Stop nodes, [O] Set Resources, [D]iscord Username, [Q]uit".into()
}
Scene::Options => "none".into(),
Scene::DiscordUsernameInputBox => "⏎ Accept, [Esc] Cancel".into(),
Expand Down
13 changes: 7 additions & 6 deletions node-launchpad/src/components/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ use tokio::sync::mpsc::UnboundedSender;
const NODE_START_INTERVAL: usize = 10;

pub struct Home {
/// Whether the component is active right now, capturing keystrokes + draw things.
active: bool,
action_sender: Option<UnboundedSender<Action>>,
config: Config,
// state
show_scene: bool,
node_services: Vec<NodeServiceData>,
node_table_state: TableState,
allocated_disk_space: usize,
Expand All @@ -41,7 +42,7 @@ impl Home {
let mut home = Self {
action_sender: Default::default(),
config: Default::default(),
show_scene: true,
active: true,
node_services: Default::default(),
allocated_disk_space,
node_table_state: Default::default(),
Expand Down Expand Up @@ -165,14 +166,14 @@ impl Component for Home {
match action {
Action::SwitchScene(scene) => match scene {
Scene::Home => {
self.show_scene = true;
self.active = true;
// make sure we're in navigation mode
return Ok(Some(Action::SwitchInputMode(InputMode::Navigation)));
}
Scene::DiscordUsernameInputBox | Scene::ResourceAllocationInputBox => {
self.show_scene = true
self.active = true
}
_ => self.show_scene = false,
_ => self.active = false,
},
Action::StoreAllocatedDiskSpace(space) => {
self.allocated_disk_space = space;
Expand Down Expand Up @@ -281,7 +282,7 @@ impl Component for Home {
}

fn draw(&mut self, f: &mut Frame<'_>, area: Rect) -> Result<()> {
if !self.show_scene {
if !self.active {
return Ok(());
}

Expand Down
15 changes: 10 additions & 5 deletions node-launchpad/src/components/resource_allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ use super::{utils::centered_rect_fixed, Component};
pub const GB_PER_NODE: usize = 5;

pub struct ResourceAllocationInputBox {
show_scene: bool,
/// Whether the component is active right now, capturing keystrokes + draw things.
active: bool,
available_disk_space_bytes: usize,
allocated_space_input: Input,
// cache the old value incase user presses Esc.
Expand All @@ -33,7 +34,7 @@ pub struct ResourceAllocationInputBox {
impl ResourceAllocationInputBox {
pub fn new(allocated_space: usize) -> Result<Self> {
let new = Self {
show_scene: false,
active: false,
available_disk_space_bytes: Self::get_available_space_gb()?,
allocated_space_input: Input::default().with_value(allocated_space.to_string()),
old_value: Default::default(),
Expand Down Expand Up @@ -66,6 +67,10 @@ impl ResourceAllocationInputBox {

impl Component for ResourceAllocationInputBox {
fn handle_key_events(&mut self, key: KeyEvent) -> Result<Vec<Action>> {
if !self.active {
return Ok(vec![]);
}

// while in entry mode, keybinds are not captured, so gotta exit entry mode from here
let send_back = match key.code {
KeyCode::Enter => {
Expand Down Expand Up @@ -124,14 +129,14 @@ impl Component for ResourceAllocationInputBox {
let send_back = match action {
Action::SwitchScene(scene) => match scene {
Scene::ResourceAllocationInputBox => {
self.show_scene = true;
self.active = true;
self.old_value = self.allocated_space_input.value().to_string();
// set to entry input mode as we want to handle everything within our handle_key_events
// so by default if this scene is active, we capture inputs.
Some(Action::SwitchInputMode(InputMode::Entry))
}
_ => {
self.show_scene = false;
self.active = false;
None
}
},
Expand All @@ -141,7 +146,7 @@ impl Component for ResourceAllocationInputBox {
}

fn draw(&mut self, f: &mut crate::tui::Frame<'_>, area: Rect) -> Result<()> {
if !self.show_scene {
if !self.active {
return Ok(());
}

Expand Down

0 comments on commit 4c12171

Please sign in to comment.