From 23d5fbde56c1b4354ef7991ec1f98d9e2896be2c Mon Sep 17 00:00:00 2001 From: Florian Dehau Date: Wed, 16 Jun 2021 16:44:54 +0200 Subject: [PATCH] refactor(examples): remove exit key from Events handler The thread spawned by `Events` to listen for keyboard inputs had knowlegde of the exit key to exit on its own when it was pressed. It is however a source of confusion (#491) because the exit behavior is wired in both the event handler and the input handling performed by the app. In addition, this is not needed as the thread will exit anyway when the main thread finishes as it is already the case for the "tick" thread. Therefore, this commit removes both the option to configure the exit key in the `Events` handler and the option to temporarily ignore it. --- examples/user_input.rs | 4 +--- examples/util/event.rs | 24 ++---------------------- 2 files changed, 3 insertions(+), 25 deletions(-) diff --git a/examples/user_input.rs b/examples/user_input.rs index 6fcb631..dddea7f 100644 --- a/examples/user_input.rs +++ b/examples/user_input.rs @@ -60,7 +60,7 @@ fn main() -> Result<(), Box> { let mut terminal = Terminal::new(backend)?; // Setup event handlers - let mut events = Events::new(); + let events = Events::new(); // Create default app state let mut app = App::default(); @@ -151,7 +151,6 @@ fn main() -> Result<(), Box> { InputMode::Normal => match input { Key::Char('e') => { app.input_mode = InputMode::Editing; - events.disable_exit_key(); } Key::Char('q') => { break; @@ -170,7 +169,6 @@ fn main() -> Result<(), Box> { } Key::Esc => { app.input_mode = InputMode::Normal; - events.enable_exit_key(); } _ => {} }, diff --git a/examples/util/event.rs b/examples/util/event.rs index 333096a..33ee9ec 100644 --- a/examples/util/event.rs +++ b/examples/util/event.rs @@ -1,9 +1,5 @@ use std::io; use std::sync::mpsc; -use std::sync::{ - atomic::{AtomicBool, Ordering}, - Arc, -}; use std::thread; use std::time::Duration; @@ -20,20 +16,17 @@ pub enum Event { pub struct Events { rx: mpsc::Receiver>, input_handle: thread::JoinHandle<()>, - ignore_exit_key: Arc, tick_handle: thread::JoinHandle<()>, } #[derive(Debug, Clone, Copy)] pub struct Config { - pub exit_key: Key, pub tick_rate: Duration, } impl Default for Config { fn default() -> Config { Config { - exit_key: Key::Char('q'), tick_rate: Duration::from_millis(250), } } @@ -46,10 +39,8 @@ impl Events { pub fn with_config(config: Config) -> Events { let (tx, rx) = mpsc::channel(); - let ignore_exit_key = Arc::new(AtomicBool::new(false)); let input_handle = { let tx = tx.clone(); - let ignore_exit_key = ignore_exit_key.clone(); thread::spawn(move || { let stdin = io::stdin(); for evt in stdin.keys() { @@ -58,16 +49,14 @@ impl Events { eprintln!("{}", err); return; } - if !ignore_exit_key.load(Ordering::Relaxed) && key == config.exit_key { - return; - } } } }) }; let tick_handle = { thread::spawn(move || loop { - if tx.send(Event::Tick).is_err() { + if let Err(err) = tx.send(Event::Tick) { + eprintln!("{}", err); break; } thread::sleep(config.tick_rate); @@ -75,7 +64,6 @@ impl Events { }; Events { rx, - ignore_exit_key, input_handle, tick_handle, } @@ -84,12 +72,4 @@ impl Events { pub fn next(&self) -> Result, mpsc::RecvError> { self.rx.recv() } - - pub fn disable_exit_key(&mut self) { - self.ignore_exit_key.store(true, Ordering::Relaxed); - } - - pub fn enable_exit_key(&mut self) { - self.ignore_exit_key.store(false, Ordering::Relaxed); - } }