hwt/src/win/rest.rs

80 lines
2.7 KiB
Rust
Raw Normal View History

2022-02-11 23:45:02 +03:00
use crate::cmd;
use crate::comp;
use crate::env;
use crate::sound;
2022-02-11 23:45:02 +03:00
use crate::state;
use druid::widget::Button;
2022-02-11 23:45:02 +03:00
use druid::{MenuDesc, Target, Widget, WidgetExt, WidgetId, WindowDesc};
use std::rc::Rc;
2022-02-11 23:45:02 +03:00
pub fn create(
parent_widget_id: WidgetId,
rest_duration_secs: f64,
sound_sender: Rc<sound::Sender>,
) -> WindowDesc<state::App> {
2022-02-11 23:45:02 +03:00
let win_width = 450.0;
let win_height = 200.0;
let rect = druid::Screen::get_display_rect();
let x = (rect.width() - win_width) / 2.0;
let y = (rect.height() - win_height) / 2.0;
2022-02-28 14:47:17 +03:00
WindowDesc::new(move || build(parent_widget_id, rest_duration_secs, sound_sender))
2022-02-11 23:45:02 +03:00
.show_titlebar(false)
.menu(MenuDesc::empty())
.set_position((x, y))
.with_min_size((win_width, win_height))
2022-02-28 14:47:17 +03:00
.window_size((win_width, win_height))
2022-02-11 23:45:02 +03:00
}
fn build(
parent_widget_id: WidgetId,
rest_duration_secs: f64,
sound_sender: Rc<sound::Sender>,
) -> impl Widget<state::App> {
comp::flex::col_cen_cen()
2022-02-12 16:59:07 +03:00
.with_child(
comp::flex::col_sta_end()
.with_child(
build_idle_timer(parent_widget_id, rest_duration_secs, sound_sender)
.lens(state::App::notifier),
)
.with_default_spacer()
.with_child(build_finish_btn(parent_widget_id)),
2022-02-12 16:59:07 +03:00
)
2022-02-11 23:45:02 +03:00
.padding((8.0, 8.0))
}
2022-02-12 16:59:07 +03:00
fn build_idle_timer(
parent_widget_id: WidgetId,
rest_duration_secs: f64,
sound_sender: Rc<sound::Sender>,
2022-02-12 16:59:07 +03:00
) -> impl Widget<state::Timer> {
2022-02-11 23:45:02 +03:00
comp::timer::build()
.controller(
2022-02-28 14:47:17 +03:00
comp::timer::Controller::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())));
2022-02-20 16:56:38 +03:00
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);
})
2022-02-12 22:53:24 +03:00
.with_duration(rest_duration_secs)
.with_init_duration(env::BREAK_NOTIFIER_TIMER_DURATION),
)
2022-02-28 14:47:17 +03:00
.controller(comp::deinit::Controller::default())
2022-02-11 23:45:02 +03:00
}
fn build_finish_btn(parent_widget_id: WidgetId) -> impl Widget<state::App> {
2022-02-14 23:17:24 +03:00
Button::new("Finish").on_click(move |ctx, _data, _env| {
ctx.submit_command(cmd::UNPAUSE_ALL_TIMER_COMP.with(false).to(Target::Global));
2022-02-20 16:56:38 +03:00
ctx.submit_command(cmd::RESET_TIMER_COMP.to(Target::Widget(parent_widget_id)));
ctx.submit_command(druid::commands::CLOSE_WINDOW);
})
}