feat(comp/flex): add utils to create flex

This commit is contained in:
Dmitriy Pleshevskiy 2022-02-13 00:04:31 +03:00
parent f330ea2865
commit 79064997a7
8 changed files with 79 additions and 31 deletions

View File

@ -1,7 +1,7 @@
use crate::cmd;
use crate::comp;
use crate::state;
use druid::widget::{Flex, Label};
use druid::widget::Label;
use druid::{Key, Widget, WidgetExt};
pub fn build(
@ -10,20 +10,26 @@ pub fn build(
postpone_duration_env_key: Key<f64>,
rest_duration_env_key: Key<f64>,
) -> impl Widget<state::BreakTimer> {
let name_label = Label::new(name);
Flex::row().with_child(name_label).with_child(
comp::timer::build()
.controller(
comp::timer::TimerController::new(|ctx, rest_duration_secs| {
ctx.submit_command(cmd::PAUSE_ALL_TIMER_COMP);
ctx.submit_command(
cmd::OPEN_NOTIFIER_WINDOW.with((ctx.widget_id(), rest_duration_secs)),
)
})
.with_duration(duration_env_key.clone())
.with_postpone_duration(postpone_duration_env_key.clone())
.with_rest_duration_env(rest_duration_env_key.clone()),
)
.lens(state::BreakTimer::work_timer),
)
comp::flex::row_sta_sta()
.with_child(
Label::new(name)
.align_right()
.fix_width(50.0)
.background(druid::Color::AQUA),
)
.with_child(
comp::timer::build()
.controller(
comp::timer::TimerController::new(|ctx, rest_duration_secs| {
ctx.submit_command(cmd::PAUSE_ALL_TIMER_COMP);
ctx.submit_command(
cmd::OPEN_NOTIFIER_WINDOW.with((ctx.widget_id(), rest_duration_secs)),
)
})
.with_duration(duration_env_key.clone())
.with_postpone_duration(postpone_duration_env_key.clone())
.with_rest_duration_env(rest_duration_env_key.clone()),
)
.lens(state::BreakTimer::work_timer),
)
}

26
src/comp/flex.rs Normal file
View File

@ -0,0 +1,26 @@
use druid::widget::{Axis, CrossAxisAlignment, Flex, MainAxisAlignment};
use druid::Data;
macro_rules! flex {
($($name:ident: $axis:ident $main:ident $cross:ident)+) => {
$(pub fn $name<D: Data>() -> Flex<D> {
flex_layout(Axis::$axis, MainAxisAlignment::$main, CrossAxisAlignment::$cross)
})+
};
}
flex! {
col_sta_sta: Vertical Start Start
col_sta_end: Vertical Start End
col_cen_cen: Vertical Center Center
row_sta_sta: Horizontal Start Start
}
fn flex_layout<D>(axis: Axis, main: MainAxisAlignment, cross: CrossAxisAlignment) -> Flex<D>
where
D: Data,
{
Flex::for_axis(axis)
.main_axis_alignment(main)
.cross_axis_alignment(cross)
}

View File

@ -1,3 +1,4 @@
pub mod break_timer;
pub mod deinit;
pub mod flex;
pub mod timer;

View File

@ -1,7 +1,8 @@
use crate::cmd;
use crate::comp;
use crate::env;
use crate::state;
use druid::widget::{Controller, Flex, Label, ProgressBar};
use druid::widget::{Controller, Label, ProgressBar};
use druid::{Env, Event, EventCtx, KeyOrValue, TimerToken, Widget, WidgetExt};
use std::time::{Duration, Instant};
@ -11,7 +12,9 @@ pub fn build() -> impl Widget<state::Timer> {
let time_label = Label::dynamic(|data: &String, _: &Env| data.clone()).lens(state::Timer::time);
let progress_bar = ProgressBar::new().lens(state::Timer::progress);
Flex::row().with_child(time_label).with_child(progress_bar)
comp::flex::row_sta_sta()
.with_child(time_label)
.with_child(progress_bar)
}
pub struct TimerController {

View File

@ -36,7 +36,7 @@ pub fn configure(env: &mut Env, _data: &state::App) {
// timers
env.set(MICRO_BREAK_TIMER_DURATION, 10.0);
env.set(MICRO_BREAK_TIMER_POSTPONE_DURATION, 10.0);
env.set(MICRO_BREAK_TIMER_POSTPONE_DURATION, 5.0);
env.set(MICRO_BREAK_TIMER_REST_DURATION, 30.0);
env.set(REST_BREAK_TIMER_DURATION, mins(45.0));

View File

@ -2,7 +2,7 @@ use crate::cmd;
use crate::comp;
use crate::env;
use crate::state;
use druid::widget::{Button, Flex};
use druid::widget::Button;
use druid::{MenuDesc, Target, Widget, WidgetExt, WidgetId, WindowDesc};
pub fn create(parent_widget_id: WidgetId, rest_duration_secs: f64) -> WindowDesc<state::App> {
@ -22,7 +22,7 @@ pub fn create(parent_widget_id: WidgetId, rest_duration_secs: f64) -> WindowDesc
}
fn build(parent_widget_id: WidgetId, rest_duration_secs: f64) -> impl Widget<state::App> {
Flex::column()
comp::flex::col_cen_cen()
.with_child(
build_notifier_timer(parent_widget_id, rest_duration_secs).lens(state::App::notifier),
)

View File

@ -2,7 +2,7 @@ use crate::cmd;
use crate::comp;
use crate::env;
use crate::state;
use druid::widget::{Flex, MainAxisAlignment};
use druid::widget::Button;
use druid::{MenuDesc, Target, Widget, WidgetExt, WidgetId, WindowDesc};
pub fn create(parent_widget_id: WidgetId, rest_duration_secs: f64) -> WindowDesc<state::App> {
@ -22,10 +22,15 @@ pub fn create(parent_widget_id: WidgetId, rest_duration_secs: f64) -> WindowDesc
}
fn build(parent_widget_id: WidgetId, rest_duration_secs: f64) -> impl Widget<state::App> {
Flex::column()
.main_axis_alignment(MainAxisAlignment::Center)
comp::flex::col_cen_cen()
.with_child(
build_idle_timer(parent_widget_id, rest_duration_secs).lens(state::App::notifier),
comp::flex::col_sta_end()
.with_child(
build_idle_timer(parent_widget_id, rest_duration_secs)
.lens(state::App::notifier),
)
.with_default_spacer()
.with_child(build_finish_btn(parent_widget_id)),
)
.padding((8.0, 8.0))
}
@ -47,3 +52,11 @@ fn build_idle_timer(
)
.controller(comp::deinit::DeinitController::default())
}
fn build_finish_btn(parent_widget_id: WidgetId) -> impl Widget<state::App> {
Button::new("Finish the rest").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(druid::commands::CLOSE_WINDOW);
})
}

View File

@ -2,11 +2,11 @@ use crate::cmd;
use crate::comp;
use crate::env;
use crate::state;
use druid::widget::{Button, CrossAxisAlignment, Either, Flex};
use druid::widget::{Button, Either};
use druid::{LocalizedString, MenuDesc, Widget, WidgetExt, WindowDesc};
pub fn create() -> WindowDesc<state::App> {
let win_width = 200.0;
let win_width = 220.0;
let win_height = 100.0;
return WindowDesc::new(build)
.title(LocalizedString::new("HWT Status"))
@ -16,8 +16,7 @@ pub fn create() -> WindowDesc<state::App> {
}
fn build() -> impl Widget<state::App> {
Flex::column()
.cross_axis_alignment(CrossAxisAlignment::Start)
comp::flex::col_sta_sta()
.with_child(build_timers())
.with_default_spacer()
.with_child(build_pause_btn())
@ -25,7 +24,7 @@ fn build() -> impl Widget<state::App> {
}
fn build_timers() -> impl Widget<state::App> {
Flex::column()
comp::flex::col_sta_sta()
.with_child(
comp::break_timer::build(
"Micro",