diff --git a/src/app.rs b/src/app.rs index d09c08c..01f9e95 100644 --- a/src/app.rs +++ b/src/app.rs @@ -71,18 +71,19 @@ impl App { pub async fn run(&mut self, mut terminal: Terminal, mut events: Events) -> Result<()> { while self.is_running() { if let Some(event) = events.next().await { - // Pipe events into subviews and handle 'rest' events only - match self.content { + // Pipe events into subviews and handle only 'unhandled' events afterwards + if let Some(unhandled) = match self.content { Content::Countdown => self.countdown.update(event.clone()), Content::Timer => self.timer.update(event.clone()), Content::Pomodoro => self.pomodoro.update(event.clone()), - }; - match event { - Event::Render | Event::Resize => { - self.draw(&mut terminal)?; + } { + match unhandled { + Event::Render | Event::Resize => { + self.draw(&mut terminal)?; + } + Event::Key(key) => self.handle_key_event(key), + _ => {} } - Event::Key(key) => self.handle_key_event(key), - _ => {} } } } @@ -93,14 +94,6 @@ impl App { self.mode != Mode::Quit } - fn is_edit_mode(&mut self) -> bool { - match self.content { - Content::Countdown => self.countdown.is_edit_mode(), - Content::Pomodoro => self.pomodoro.is_edit_mode(), - _ => false, - } - } - fn handle_key_event(&mut self, key: KeyEvent) { debug!("Received key {:?}", key.code); match key.code { @@ -109,18 +102,8 @@ impl App { KeyCode::Char('t') => self.content = Content::Timer, KeyCode::Char('p') => self.content = Content::Pomodoro, KeyCode::Char('m') => self.show_menu = !self.show_menu, - KeyCode::Up => { - // TODO: Pipe events into subviews properly - if !self.is_edit_mode() { - self.show_menu = true - } - } - KeyCode::Down => { - // TODO: Pipe events into subviews properly - if !self.is_edit_mode() { - self.show_menu = false - } - } + KeyCode::Up => self.show_menu = true, + KeyCode::Down => self.show_menu = false, _ => {} }; } diff --git a/src/events.rs b/src/events.rs index e3f7d2d..7aedea0 100644 --- a/src/events.rs +++ b/src/events.rs @@ -76,5 +76,5 @@ fn crossterm_stream() -> Pin>> { } pub trait EventHandler { - fn update(&mut self, _: Event); + fn update(&mut self, _: Event) -> Option; } diff --git a/src/widgets/countdown.rs b/src/widgets/countdown.rs index 0359443..b890718 100644 --- a/src/widgets/countdown.rs +++ b/src/widgets/countdown.rs @@ -22,14 +22,11 @@ impl Countdown { pub const fn new(clock: Clock) -> Self { Self { clock } } - - pub fn is_edit_mode(&mut self) -> bool { - self.clock.is_edit_mode() - } } impl EventHandler for Countdown { - fn update(&mut self, event: Event) { + fn update(&mut self, event: Event) -> Option { + let edit_mode = self.clock.is_edit_mode(); match event { Event::Tick => { self.clock.tick(); @@ -47,22 +44,23 @@ impl EventHandler for Countdown { KeyCode::Char('e') => { self.clock.toggle_edit(); } - KeyCode::Left => { + KeyCode::Left if edit_mode => { self.clock.edit_next(); } - KeyCode::Right => { + KeyCode::Right if edit_mode => { self.clock.edit_prev(); } - KeyCode::Up => { + KeyCode::Up if edit_mode => { self.clock.edit_up(); } - KeyCode::Down => { + KeyCode::Down if edit_mode => { self.clock.edit_down(); } - _ => {} + _ => return Some(event), }, - _ => {} + _ => return Some(event), } + None } } diff --git a/src/widgets/pomodoro.rs b/src/widgets/pomodoro.rs index d6c6d9c..346a52f 100644 --- a/src/widgets/pomodoro.rs +++ b/src/widgets/pomodoro.rs @@ -72,14 +72,11 @@ impl Pomodoro { Mode::Work => Mode::Pause, }; } - - pub fn is_edit_mode(&mut self) -> bool { - self.get_clock().is_edit_mode() - } } impl EventHandler for Pomodoro { - fn update(&mut self, event: Event) { + fn update(&mut self, event: Event) -> Option { + let edit_mode = self.get_clock().is_edit_mode(); match event { Event::Tick => { self.get_clock().tick(); @@ -91,35 +88,33 @@ impl EventHandler for Pomodoro { KeyCode::Char('e') => { self.get_clock().toggle_edit(); } - KeyCode::Left => { - if self.get_clock().is_edit_mode() { - self.get_clock().edit_next(); - } else { - // `next` is acting as same as a `prev` function, we don't have - self.next(); - } + KeyCode::Left if edit_mode => { + self.get_clock().edit_next(); + } + KeyCode::Left if edit_mode => { + // `next` is acting as same as a `prev` function, we don't have + self.next(); + } + KeyCode::Right if edit_mode => { + self.get_clock().edit_prev(); } KeyCode::Right => { - if self.get_clock().is_edit_mode() { - self.get_clock().edit_prev(); - } else { - self.next(); - } + self.next(); } - KeyCode::Up => { + KeyCode::Up if edit_mode => { self.get_clock().edit_up(); } - KeyCode::Down => { + KeyCode::Down if edit_mode => { self.get_clock().edit_down(); } KeyCode::Char('r') => { self.get_clock().reset(); } - _ => {} + _ => return Some(event), }, - - _ => {} + _ => return Some(event), } + None } } diff --git a/src/widgets/timer.rs b/src/widgets/timer.rs index 7bd1a1c..ae5e6ab 100644 --- a/src/widgets/timer.rs +++ b/src/widgets/timer.rs @@ -24,7 +24,7 @@ impl Timer { } impl EventHandler for Timer { - fn update(&mut self, event: Event) { + fn update(&mut self, event: Event) -> Option { match event { Event::Tick => { self.clock.tick(); @@ -35,8 +35,9 @@ impl EventHandler for Timer { Event::Key(key) if key.code == KeyCode::Char('r') => { self.clock.reset(); } - _ => {} + _ => return Some(event), } + None } }