AppEvent (#61)

Extend `events` to provide a `mpsc` channel to send `AppEvent`'s from
anywhere in the app straight to the `App`.
This commit is contained in:
Jens Krause
2025-02-04 15:05:02 +01:00
committed by GitHub
parent d3c436da0b
commit 8f50bc5fc6
9 changed files with 232 additions and 96 deletions

View File

@@ -12,6 +12,7 @@ use ratatui::{
use crate::{
common::Style,
duration::{DurationEx, MAX_DURATION, ONE_DECI_SECOND, ONE_HOUR, ONE_MINUTE, ONE_SECOND},
events::{AppEvent, AppEventTx},
utils::center_horizontal,
widgets::clock_elements::{
Colon, Digit, Dot, COLON_WIDTH, DIGIT_HEIGHT, DIGIT_SPACE_WIDTH, DIGIT_WIDTH, DOT_WIDTH,
@@ -73,6 +74,7 @@ pub struct ClockState<T> {
format: Format,
pub with_decis: bool,
on_done: Option<Box<dyn Fn() + 'static>>,
app_tx: Option<AppEventTx>,
phantom: PhantomData<T>,
}
@@ -81,6 +83,7 @@ pub struct ClockStateArgs {
pub current_value: Duration,
pub tick_value: Duration,
pub with_decis: bool,
pub app_tx: Option<AppEventTx>,
}
impl<T> ClockState<T> {
@@ -329,6 +332,9 @@ impl<T> ClockState<T> {
if let Some(handler) = &mut self.on_done {
handler();
};
if let Some(tx) = &mut self.app_tx {
_ = tx.send(AppEvent::ClockDone);
};
}
}
@@ -363,6 +369,7 @@ impl ClockState<Countdown> {
current_value,
tick_value,
with_decis,
app_tx,
} = args;
let mut instance = Self {
initial_value: initial_value.into(),
@@ -378,6 +385,7 @@ impl ClockState<Countdown> {
format: Format::S,
with_decis,
on_done: None,
app_tx,
phantom: PhantomData,
};
// update format once
@@ -432,6 +440,7 @@ impl ClockState<Timer> {
current_value,
tick_value,
with_decis,
app_tx,
} = args;
let mut instance = Self {
initial_value: initial_value.into(),
@@ -447,6 +456,7 @@ impl ClockState<Timer> {
format: Format::S,
with_decis,
on_done: None,
app_tx,
phantom: PhantomData,
};
// update format once

View File

@@ -11,6 +11,7 @@ fn test_toggle_edit() {
current_value: ONE_HOUR,
tick_value: ONE_DECI_SECOND,
with_decis: true,
app_tx: None,
});
// off by default
assert!(!c.is_edit_mode());
@@ -29,6 +30,7 @@ fn test_default_edit_mode_hhmmss() {
current_value: ONE_HOUR,
tick_value: ONE_DECI_SECOND,
with_decis: true,
app_tx: None,
});
// toggle on
@@ -43,6 +45,7 @@ fn test_default_edit_mode_mmss() {
current_value: ONE_MINUTE,
tick_value: ONE_DECI_SECOND,
with_decis: true,
app_tx: None,
});
// toggle on
c.toggle_edit();
@@ -56,6 +59,7 @@ fn test_default_edit_mode_ss() {
current_value: ONE_SECOND,
tick_value: ONE_DECI_SECOND,
with_decis: true,
app_tx: None,
});
// toggle on
c.toggle_edit();
@@ -69,6 +73,7 @@ fn test_edit_next_hhmmssd() {
current_value: ONE_HOUR,
tick_value: ONE_DECI_SECOND,
with_decis: true,
app_tx: None,
});
// toggle on
@@ -90,6 +95,7 @@ fn test_edit_next_hhmmss() {
current_value: ONE_HOUR,
tick_value: ONE_DECI_SECOND,
with_decis: false,
app_tx: None,
});
// toggle on
@@ -109,6 +115,7 @@ fn test_edit_next_mmssd() {
current_value: ONE_MINUTE,
tick_value: ONE_DECI_SECOND,
with_decis: true,
app_tx: None,
});
// toggle on
@@ -128,6 +135,7 @@ fn test_edit_next_mmss() {
current_value: ONE_MINUTE,
tick_value: ONE_DECI_SECOND,
with_decis: false,
app_tx: None,
});
// toggle on
@@ -145,6 +153,7 @@ fn test_edit_next_ssd() {
current_value: ONE_SECOND * 3,
tick_value: ONE_DECI_SECOND,
with_decis: true,
app_tx: None,
});
// toggle on
@@ -160,6 +169,7 @@ fn test_edit_next_ss() {
current_value: ONE_SECOND * 3,
tick_value: ONE_DECI_SECOND,
with_decis: false,
app_tx: None,
});
// toggle on
@@ -176,6 +186,7 @@ fn test_edit_prev_hhmmssd() {
current_value: ONE_HOUR,
tick_value: ONE_DECI_SECOND,
with_decis: true,
app_tx: None,
});
// toggle on
@@ -196,6 +207,7 @@ fn test_edit_prev_hhmmss() {
current_value: ONE_HOUR,
tick_value: ONE_DECI_SECOND,
with_decis: false,
app_tx: None,
});
// toggle on
@@ -214,6 +226,7 @@ fn test_edit_prev_mmssd() {
current_value: ONE_MINUTE,
tick_value: ONE_DECI_SECOND,
with_decis: true,
app_tx: None,
});
// toggle on
@@ -234,6 +247,7 @@ fn test_edit_prev_mmss() {
current_value: ONE_MINUTE,
tick_value: ONE_DECI_SECOND,
with_decis: false,
app_tx: None,
});
// toggle on
@@ -252,6 +266,7 @@ fn test_edit_prev_ssd() {
current_value: ONE_SECOND,
tick_value: ONE_DECI_SECOND,
with_decis: true,
app_tx: None,
});
// toggle on
@@ -270,6 +285,7 @@ fn test_edit_prev_ss() {
current_value: ONE_SECOND,
tick_value: ONE_DECI_SECOND,
with_decis: false,
app_tx: None,
});
// toggle on
@@ -285,7 +301,8 @@ fn test_edit_up_ss() {
initial_value: Duration::ZERO,
current_value: Duration::ZERO,
tick_value: ONE_DECI_SECOND,
with_decis: false,
with_decis: true,
app_tx: None,
});
// toggle on
@@ -301,7 +318,8 @@ fn test_edit_up_mmss() {
initial_value: Duration::ZERO,
current_value: Duration::from_secs(60),
tick_value: ONE_DECI_SECOND,
with_decis: false,
with_decis: true,
app_tx: None,
});
// toggle on
@@ -320,7 +338,8 @@ fn test_edit_up_hhmmss() {
initial_value: Duration::ZERO,
current_value: Duration::from_secs(3600),
tick_value: ONE_DECI_SECOND,
with_decis: false,
with_decis: true,
app_tx: None,
});
// toggle on
@@ -341,7 +360,8 @@ fn test_edit_down_ss() {
initial_value: Duration::ZERO,
current_value: ONE_SECOND,
tick_value: ONE_DECI_SECOND,
with_decis: false,
with_decis: true,
app_tx: None,
});
// toggle on
@@ -361,7 +381,8 @@ fn test_edit_down_mmss() {
initial_value: Duration::ZERO,
current_value: Duration::from_secs(120),
tick_value: ONE_DECI_SECOND,
with_decis: false,
with_decis: true,
app_tx: None,
});
// toggle on
@@ -383,7 +404,8 @@ fn test_edit_down_hhmmss() {
initial_value: Duration::ZERO,
current_value: Duration::from_secs(3600),
tick_value: ONE_DECI_SECOND,
with_decis: false,
with_decis: true,
app_tx: None,
});
// toggle on

View File

@@ -2,11 +2,11 @@ use crate::{
common::{AppTime, Style},
constants::TICK_VALUE_MS,
duration::{DurationEx, MAX_DURATION},
events::{Event, EventHandler},
events::{AppEventTx, TuiEvent, TuiEventHandler},
utils::center,
widgets::{
clock::{self, ClockState, ClockStateArgs, ClockWidget, Mode as ClockMode},
edit_time::EditTimeState,
edit_time::{EditTimeState, EditTimeStateArgs, EditTimeWidget},
},
};
@@ -25,8 +25,6 @@ use std::ops::Sub;
use std::{cmp::max, time::Duration};
use time::OffsetDateTime;
use super::edit_time::{EditTimeStateArgs, EditTimeWidget};
pub struct CountdownStateArgs {
pub initial_value: Duration,
pub current_value: Duration,
@@ -34,6 +32,7 @@ pub struct CountdownStateArgs {
pub app_time: AppTime,
pub with_decis: bool,
pub with_notification: bool,
pub app_tx: AppEventTx,
}
/// State for Countdown Widget
@@ -56,6 +55,7 @@ impl CountdownState {
with_notification,
with_decis,
app_time,
app_tx,
} = args;
Self {
@@ -64,6 +64,7 @@ impl CountdownState {
current_value,
tick_value: Duration::from_millis(TICK_VALUE_MS),
with_decis,
app_tx: Some(app_tx.clone()),
})
.with_on_done_by_condition(with_notification, || {
debug!("on_done COUNTDOWN");
@@ -79,6 +80,7 @@ impl CountdownState {
current_value: elapsed_value,
tick_value: Duration::from_millis(TICK_VALUE_MS),
with_decis: false,
app_tx: None,
})
// A previous `elapsed_value > 0` means the `Clock` was running before,
// but not in `Initial` state anymore. Updating `Mode` here
@@ -154,12 +156,12 @@ impl CountdownState {
}
}
impl EventHandler for CountdownState {
fn update(&mut self, event: Event) -> Option<Event> {
impl TuiEventHandler for CountdownState {
fn update(&mut self, event: TuiEvent) -> Option<TuiEvent> {
let is_edit_clock = self.clock.is_edit_mode();
let is_edit_time = self.edit_time.is_some();
match event {
Event::Tick => {
TuiEvent::Tick => {
if !self.clock.is_done() {
self.clock.tick();
} else {
@@ -175,7 +177,7 @@ impl EventHandler for CountdownState {
edit_time.set_max_time(max_time);
}
}
Event::Key(key) => match key.code {
TuiEvent::Key(key) => match key.code {
KeyCode::Char('r') => {
// reset both clocks to use intial values
self.clock.reset();

View File

@@ -1,7 +1,7 @@
use crate::{
common::Style,
constants::TICK_VALUE_MS,
events::{Event, EventHandler},
events::{AppEventTx, TuiEvent, TuiEventHandler},
utils::center,
widgets::clock::{ClockState, ClockStateArgs, ClockWidget, Countdown},
};
@@ -57,6 +57,7 @@ pub struct PomodoroStateArgs {
pub current_value_pause: Duration,
pub with_decis: bool,
pub with_notification: bool,
pub app_tx: AppEventTx,
}
impl PomodoroState {
@@ -69,6 +70,7 @@ impl PomodoroState {
current_value_pause,
with_decis,
with_notification,
app_tx,
} = args;
Self {
mode,
@@ -78,6 +80,7 @@ impl PomodoroState {
current_value: current_value_work,
tick_value: Duration::from_millis(TICK_VALUE_MS),
with_decis,
app_tx: Some(app_tx.clone()),
})
.with_on_done_by_condition(with_notification, || {
debug!("on_done WORK");
@@ -93,6 +96,7 @@ impl PomodoroState {
current_value: current_value_pause,
tick_value: Duration::from_millis(TICK_VALUE_MS),
with_decis,
app_tx: Some(app_tx),
})
.with_on_done_by_condition(with_notification, || {
debug!("on_done PAUSE");
@@ -140,14 +144,14 @@ impl PomodoroState {
}
}
impl EventHandler for PomodoroState {
fn update(&mut self, event: Event) -> Option<Event> {
impl TuiEventHandler for PomodoroState {
fn update(&mut self, event: TuiEvent) -> Option<TuiEvent> {
let edit_mode = self.get_clock().is_edit_mode();
match event {
Event::Tick => {
TuiEvent::Tick => {
self.get_clock_mut().tick();
}
Event::Key(key) => match key.code {
TuiEvent::Key(key) => match key.code {
KeyCode::Char('s') => {
self.get_clock_mut().toggle_pause();
}

View File

@@ -1,6 +1,6 @@
use crate::{
common::Style,
events::{Event, EventHandler},
events::{TuiEvent, TuiEventHandler},
utils::center,
widgets::clock::{self, ClockState, ClockWidget},
};
@@ -31,14 +31,14 @@ impl TimerState {
}
}
impl EventHandler for TimerState {
fn update(&mut self, event: Event) -> Option<Event> {
impl TuiEventHandler for TimerState {
fn update(&mut self, event: TuiEvent) -> Option<TuiEvent> {
let edit_mode = self.clock.is_edit_mode();
match event {
Event::Tick => {
TuiEvent::Tick => {
self.clock.tick();
}
Event::Key(key) => match key.code {
TuiEvent::Key(key) => match key.code {
KeyCode::Char('s') => {
self.clock.toggle_pause();
}