Pipe events into subviews (#13)
and match only unhandled events afterwards
This commit is contained in:
parent
64300631c7
commit
6c7bedabe3
39
src/app.rs
39
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,
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
|
||||
@ -76,5 +76,5 @@ fn crossterm_stream() -> Pin<Box<dyn Stream<Item = Event>>> {
|
||||
}
|
||||
|
||||
pub trait EventHandler {
|
||||
fn update(&mut self, _: Event);
|
||||
fn update(&mut self, _: Event) -> Option<Event>;
|
||||
}
|
||||
|
||||
@ -22,14 +22,11 @@ impl Countdown {
|
||||
pub const fn new(clock: Clock<clock::Countdown>) -> 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<Event> {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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<Event> {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -24,7 +24,7 @@ impl Timer {
|
||||
}
|
||||
|
||||
impl EventHandler for Timer {
|
||||
fn update(&mut self, event: Event) {
|
||||
fn update(&mut self, event: Event) -> Option<Event> {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user