refac(comp/timer): configure timer via build methods
This commit is contained in:
parent
9955c966be
commit
098712e577
|
@ -13,17 +13,14 @@ pub fn build(
|
|||
let name_label = Label::new(name);
|
||||
Flex::row().with_child(name_label).with_child(
|
||||
comp::timer::build()
|
||||
.controller(comp::timer::TimerController::new(|ctx| {
|
||||
ctx.submit_command(cmd::PAUSE_ALL_TIMER_COMP);
|
||||
ctx.submit_command(cmd::OPEN_NOTIFIER_WINDOW.with(ctx.widget_id()))
|
||||
}))
|
||||
.env_scope(move |env, _| {
|
||||
env.set(env::TIMER_DURATION, env.get(duration_env_key.clone()));
|
||||
env.set(
|
||||
env::TIMER_POSTPONE_DURATION,
|
||||
env.get(postpone_duration_env_key.clone()),
|
||||
);
|
||||
})
|
||||
.controller(
|
||||
comp::timer::TimerController::new(|ctx| {
|
||||
ctx.submit_command(cmd::PAUSE_ALL_TIMER_COMP);
|
||||
ctx.submit_command(cmd::OPEN_NOTIFIER_WINDOW.with(ctx.widget_id()))
|
||||
})
|
||||
.with_duration_env(duration_env_key.clone())
|
||||
.with_postpone_duration_env(postpone_duration_env_key.clone()),
|
||||
)
|
||||
.lens(state::BreakTimer::work_timer),
|
||||
)
|
||||
}
|
||||
|
|
|
@ -15,6 +15,9 @@ pub fn build() -> impl Widget<state::Timer> {
|
|||
}
|
||||
|
||||
pub struct TimerController {
|
||||
env_duration: Key<f64>,
|
||||
env_init_duration: Option<Key<f64>>,
|
||||
env_postpone_duration: Option<Key<f64>>,
|
||||
start_time: Instant,
|
||||
pause_time: Option<Instant>,
|
||||
render_timer_id: TimerToken,
|
||||
|
@ -38,6 +41,9 @@ impl TimerController {
|
|||
impl Default for TimerController {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
env_duration: env::TIMER_DURATION,
|
||||
env_init_duration: None,
|
||||
env_postpone_duration: None,
|
||||
start_time: Instant::now(),
|
||||
pause_time: None,
|
||||
render_timer_id: TimerToken::INVALID,
|
||||
|
@ -48,6 +54,23 @@ impl Default for TimerController {
|
|||
}
|
||||
}
|
||||
|
||||
impl TimerController {
|
||||
pub fn with_duration_env(mut self, key: Key<f64>) -> Self {
|
||||
self.env_duration = key;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_init_duration_env(mut self, key: Key<f64>) -> Self {
|
||||
self.env_init_duration = Some(key);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_postpone_duration_env(mut self, key: Key<f64>) -> Self {
|
||||
self.env_postpone_duration = Some(key);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<W> Controller<state::Timer, W> for TimerController
|
||||
where
|
||||
W: Widget<state::Timer>,
|
||||
|
@ -65,8 +88,12 @@ where
|
|||
match event {
|
||||
Event::WindowConnected => {
|
||||
let shift_start_time = Duration::from_secs_f64(
|
||||
env.try_get(env::TIMER_INIT_DURATION).unwrap_or_default(),
|
||||
self.env_init_duration
|
||||
.as_ref()
|
||||
.map(|k| env.get(k))
|
||||
.unwrap_or_default(),
|
||||
);
|
||||
|
||||
self.start_time = Instant::now() - shift_start_time;
|
||||
self.render_timer_id = ctx.request_timer(TIMER_INTERVAL);
|
||||
self.finish_timer_id = ctx.request_timer(duration - shift_start_time);
|
||||
|
@ -127,18 +154,24 @@ where
|
|||
}
|
||||
|
||||
impl TimerController {
|
||||
fn full_duration(&self, env: &Env) -> Duration {
|
||||
self.duration(env) + self.postpone_times * self.postpone_duration(env)
|
||||
}
|
||||
|
||||
fn duration(&self, env: &Env) -> Duration {
|
||||
Duration::from_secs_f64(env.get(env::TIMER_DURATION))
|
||||
Duration::from_secs_f64(env.get(&self.env_duration))
|
||||
}
|
||||
|
||||
fn postpone_duration(&self, env: &Env) -> Duration {
|
||||
match self.postpone_times {
|
||||
0 => Duration::ZERO,
|
||||
_ => Duration::from_secs_f64(env.get(env::TIMER_POSTPONE_DURATION)),
|
||||
_ => {
|
||||
let key = self
|
||||
.env_postpone_duration
|
||||
.as_ref()
|
||||
.unwrap_or(&self.env_duration);
|
||||
Duration::from_secs_f64(env.get(key))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn full_duration(&self, env: &Env) -> Duration {
|
||||
self.duration(env) + self.postpone_times * self.postpone_duration(env)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,22 +31,19 @@ fn build(parent_widget_id: WidgetId) -> impl Widget<state::App> {
|
|||
|
||||
fn build_notifier_timer(parent_widget_id: WidgetId) -> impl Widget<state::Timer> {
|
||||
comp::timer::build()
|
||||
.controller(comp::timer::TimerController::new(move |ctx| {
|
||||
ctx.submit_command(cmd::DEINIT_COMP.to(Target::Widget(ctx.widget_id())));
|
||||
ctx.submit_command(
|
||||
cmd::OPEN_IDLE_WINDOW
|
||||
.with((parent_widget_id, 30.0))
|
||||
.to(Target::Global),
|
||||
);
|
||||
ctx.submit_command(druid::commands::CLOSE_WINDOW);
|
||||
}))
|
||||
.controller(
|
||||
comp::timer::TimerController::new(move |ctx| {
|
||||
ctx.submit_command(cmd::DEINIT_COMP.to(Target::Widget(ctx.widget_id())));
|
||||
ctx.submit_command(
|
||||
cmd::OPEN_IDLE_WINDOW
|
||||
.with((parent_widget_id, 30.0))
|
||||
.to(Target::Global),
|
||||
);
|
||||
ctx.submit_command(druid::commands::CLOSE_WINDOW);
|
||||
})
|
||||
.with_duration_env(env::BREAK_NOTIFIER_TIMER_DURATION),
|
||||
)
|
||||
.controller(comp::deinit::DeinitController::default())
|
||||
.env_scope(move |env, _| {
|
||||
env.set(
|
||||
env::TIMER_DURATION,
|
||||
env.get(env::BREAK_NOTIFIER_TIMER_DURATION),
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
fn build_postpone_btn<D: druid::Data>(parent_widget_id: WidgetId) -> impl Widget<D> {
|
||||
|
|
|
@ -29,18 +29,15 @@ fn build(parent_widget_id: WidgetId, rest_duration: f64) -> impl Widget<state::A
|
|||
|
||||
fn build_idle_timer(parent_widget_id: WidgetId, rest_duration: f64) -> impl Widget<state::Timer> {
|
||||
comp::timer::build()
|
||||
.controller(comp::timer::TimerController::new(move |ctx| {
|
||||
ctx.submit_command(cmd::DEINIT_COMP.to(Target::Widget(ctx.widget_id())));
|
||||
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(druid::commands::CLOSE_WINDOW);
|
||||
}))
|
||||
.controller(
|
||||
comp::timer::TimerController::new(move |ctx| {
|
||||
ctx.submit_command(cmd::DEINIT_COMP.to(Target::Widget(ctx.widget_id())));
|
||||
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(druid::commands::CLOSE_WINDOW);
|
||||
})
|
||||
.with_init_duration_env(env::BREAK_NOTIFIER_TIMER_DURATION),
|
||||
)
|
||||
.controller(comp::deinit::DeinitController::default())
|
||||
.env_scope(move |env, _| {
|
||||
env.set(
|
||||
env::TIMER_INIT_DURATION,
|
||||
env.get(env::BREAK_NOTIFIER_TIMER_DURATION),
|
||||
);
|
||||
env.set(env::TIMER_DURATION, rest_duration)
|
||||
})
|
||||
.env_scope(move |env, _| env.set(env::TIMER_DURATION, rest_duration))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue