diff --git a/config.example.toml b/config.example.toml index 30641f5..71010cf 100644 --- a/config.example.toml +++ b/config.example.toml @@ -1,3 +1,5 @@ +auto_restart = true + [notifier] duration = 5.0 diff --git a/src/cmd.rs b/src/cmd.rs index bb5a401..76b4612 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -8,7 +8,7 @@ pub const DEINIT_COMP: Selector = Selector::new("hwt.cmd.comp.deinit"); pub const PAUSE_ALL_TIMER_COMP: Selector = Selector::new("hwt.cmd.comp.timer.pause.all"); pub const UNPAUSE_ALL_TIMER_COMP: Selector = Selector::new("hwt.cmd.comp.timer.unpause.all"); pub const POSTPONE_TIMER_COMP: Selector = Selector::new("hwt.cmd.comp.timer.postpone"); -pub const RESTART_TIMER_COMP: Selector = Selector::new("hwt.cmd.comp.timer.restart"); +pub const RESET_TIMER_COMP: Selector = Selector::new("hwt.cmd.comp.timer.reset"); // pub const CYCLE_NOTIFICATION: Selector = // Selector::new("hwt.comp.timer.cycle_notification"); diff --git a/src/comp/break_timer.rs b/src/comp/break_timer.rs index 76e201c..34f9741 100644 --- a/src/comp/break_timer.rs +++ b/src/comp/break_timer.rs @@ -18,7 +18,7 @@ pub fn build( .with_child( comp::timer::build() .controller( - comp::timer::TimerController::new(move |ctx, rest_duration_secs| { + comp::timer::TimerController::new(move |ctx, _env, rest_duration_secs| { sound_sender.send(sound::Type::EndBreakTimer).ok(); ctx.submit_command(cmd::PAUSE_ALL_TIMER_COMP); diff --git a/src/comp/timer.rs b/src/comp/timer.rs index 1197f3e..da1fb20 100644 --- a/src/comp/timer.rs +++ b/src/comp/timer.rs @@ -26,14 +26,14 @@ pub struct TimerController { pause_time: Option, render_timer_id: TimerToken, finish_timer_id: TimerToken, - finish_handler: Option>, + finish_handler: Option>, postpone_times: u32, } impl TimerController { pub fn new(finish_handler: Handler) -> Self where - Handler: Fn(&mut EventCtx, f64) + 'static, + Handler: Fn(&mut EventCtx, &Env, f64) + 'static, { Self { finish_handler: Some(Box::new(finish_handler)), @@ -123,7 +123,7 @@ where } Event::Timer(id) if *id == self.finish_timer_id => { if let Some(finish_handler) = &self.finish_handler { - finish_handler(ctx, self.full_rest_duration(env).as_secs_f64()); + finish_handler(ctx, env, self.full_rest_duration(env).as_secs_f64()); } } Event::Command(cmd) if cmd.is(cmd::PAUSE_ALL_TIMER_COMP) => { @@ -155,7 +155,7 @@ where self.render_timer_id = ctx.request_timer(TIMER_INTERVAL); } - Event::Command(cmd) if cmd.is(cmd::RESTART_TIMER_COMP) => { + Event::Command(cmd) if cmd.is(cmd::RESET_TIMER_COMP) => { self.postpone_times = 0; self.start_time = Instant::now(); if data.paused { diff --git a/src/config.rs b/src/config.rs index d57659c..a5f3940 100644 --- a/src/config.rs +++ b/src/config.rs @@ -13,6 +13,7 @@ pub fn default() -> Env { rest_duration: mins(10.0), }, notifier: Notifier { duration: 10.0 }, + auto_restart: Some(false), } } @@ -25,6 +26,7 @@ pub struct Env { pub micro_timer: BreakTimer, pub rest_timer: BreakTimer, pub notifier: Notifier, + pub auto_restart: Option, } #[derive(Deserialize)] diff --git a/src/env.rs b/src/env.rs index 3420be7..c840763 100644 --- a/src/env.rs +++ b/src/env.rs @@ -17,6 +17,8 @@ pub const REST_BREAK_TIMER_REST_DURATION: Key = pub const BREAK_NOTIFIER_TIMER_DURATION: Key = Key::new("hwt.env.widget.notifier.duration"); +pub const WIN_REST_AUTO_RESTART_BREAK_TIMERS: Key = Key::new("hwt.env.rest.auto_restart"); + pub fn configure(env: &mut Env, config: &config::Env) { let col_def_white = hsl(0.0, 0.0, 1.0); // let col_def_black = hsl(0.0, 0.0, 0.0); @@ -56,6 +58,11 @@ pub fn configure(env: &mut Env, config: &config::Env) { ); env.set(BREAK_NOTIFIER_TIMER_DURATION, config.notifier.duration); + + env.set( + WIN_REST_AUTO_RESTART_BREAK_TIMERS, + config.auto_restart.unwrap_or_default(), + ); } fn hsl(h: f64, s: f64, l: f64) -> Color { diff --git a/src/win/notifier.rs b/src/win/notifier.rs index 508b311..441a70d 100644 --- a/src/win/notifier.rs +++ b/src/win/notifier.rs @@ -49,7 +49,7 @@ fn build_notifier_timer( ) -> impl Widget { comp::timer::build() .controller( - comp::timer::TimerController::new(move |ctx, _| { + comp::timer::TimerController::new(move |ctx, _env, _rest_duration| { sound_sender.send(sound::Type::EndNotifier).ok(); ctx.submit_command(cmd::DEINIT_COMP.to(Target::Widget(ctx.widget_id()))); diff --git a/src/win/rest.rs b/src/win/rest.rs index 5e4c0d8..55de4ba 100644 --- a/src/win/rest.rs +++ b/src/win/rest.rs @@ -52,11 +52,16 @@ fn build_idle_timer( ) -> impl Widget { comp::timer::build() .controller( - comp::timer::TimerController::new(move |ctx, _| { + comp::timer::TimerController::new(move |ctx, env, _rest_duration| { sound_sender.send(sound::Type::EndRest).ok(); ctx.submit_command(cmd::DEINIT_COMP.to(Target::Widget(ctx.widget_id()))); - ctx.submit_command(cmd::RESTART_TIMER_COMP.to(Target::Widget(parent_widget_id))); + + if env.get(env::WIN_REST_AUTO_RESTART_BREAK_TIMERS) { + ctx.submit_command(cmd::UNPAUSE_ALL_TIMER_COMP.with(false).to(Target::Global)); + } + + ctx.submit_command(cmd::RESET_TIMER_COMP.to(Target::Widget(parent_widget_id))); ctx.submit_command(druid::commands::CLOSE_WINDOW); }) .with_duration(rest_duration_secs) @@ -68,7 +73,7 @@ fn build_idle_timer( fn build_finish_btn(parent_widget_id: WidgetId) -> impl Widget { Button::new("Finish").on_click(move |ctx, _data, _env| { ctx.submit_command(cmd::UNPAUSE_ALL_TIMER_COMP.with(false).to(Target::Global)); - ctx.submit_command(cmd::RESTART_TIMER_COMP.to(Target::Widget(parent_widget_id))); + ctx.submit_command(cmd::RESET_TIMER_COMP.to(Target::Widget(parent_widget_id))); ctx.submit_command(druid::commands::CLOSE_WINDOW); }) } diff --git a/src/win/status.rs b/src/win/status.rs index 06c7965..17971b5 100644 --- a/src/win/status.rs +++ b/src/win/status.rs @@ -63,7 +63,7 @@ fn build_pause_btn() -> impl Widget { .with_default_spacer() .with_child( Button::new("Reset") - .on_click(|ctx, _data, _env| ctx.submit_command(cmd::RESTART_TIMER_COMP)), + .on_click(|ctx, _data, _env| ctx.submit_command(cmd::RESET_TIMER_COMP)), ), comp::flex::row_sta_sta().with_child(Button::new("Pause").on_click( |ctx, data: &mut state::App, _env| {