feat(config): add auto restart option

This commit is contained in:
Dmitriy Pleshevskiy 2022-02-20 16:56:38 +03:00
parent 03d3a4d463
commit 653503e12b
9 changed files with 27 additions and 11 deletions

View File

@ -1,3 +1,5 @@
auto_restart = true
[notifier]
duration = 5.0

View File

@ -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<bool> = 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<usize> =
// Selector::new("hwt.comp.timer.cycle_notification");

View File

@ -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);

View File

@ -26,14 +26,14 @@ pub struct TimerController {
pause_time: Option<Instant>,
render_timer_id: TimerToken,
finish_timer_id: TimerToken,
finish_handler: Option<Box<dyn Fn(&mut EventCtx, f64)>>,
finish_handler: Option<Box<dyn Fn(&mut EventCtx, &Env, f64)>>,
postpone_times: u32,
}
impl TimerController {
pub fn new<Handler>(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 {

View File

@ -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<bool>,
}
#[derive(Deserialize)]

View File

@ -17,6 +17,8 @@ pub const REST_BREAK_TIMER_REST_DURATION: Key<f64> =
pub const BREAK_NOTIFIER_TIMER_DURATION: Key<f64> = Key::new("hwt.env.widget.notifier.duration");
pub const WIN_REST_AUTO_RESTART_BREAK_TIMERS: Key<bool> = 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 {

View File

@ -49,7 +49,7 @@ fn build_notifier_timer(
) -> impl Widget<state::Timer> {
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())));

View File

@ -52,11 +52,16 @@ fn build_idle_timer(
) -> impl Widget<state::Timer> {
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<state::App> {
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);
})
}

View File

@ -63,7 +63,7 @@ fn build_pause_btn() -> impl Widget<state::App> {
.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| {