fix(style): clippy warnings
This commit is contained in:
parent
07617e922e
commit
4130ae4a1d
|
@ -8,9 +8,9 @@ use std::rc::Rc;
|
|||
|
||||
pub fn build(
|
||||
name: &str,
|
||||
duration_env_key: Key<f64>,
|
||||
postpone_duration_env_key: Key<f64>,
|
||||
rest_duration_env_key: Key<f64>,
|
||||
duration_env_key: &Key<f64>,
|
||||
postpone_duration_env_key: &Key<f64>,
|
||||
rest_duration_env_key: &Key<f64>,
|
||||
sound_sender: Rc<sound::Sender>,
|
||||
) -> impl Widget<state::BreakTimer> {
|
||||
comp::flex::row_sta_sta()
|
||||
|
@ -18,7 +18,7 @@ pub fn build(
|
|||
.with_child(
|
||||
comp::timer::build()
|
||||
.controller(
|
||||
comp::timer::TimerController::new(move |ctx, _env, rest_duration_secs| {
|
||||
comp::timer::Controller::new(move |ctx, _env, rest_duration_secs| {
|
||||
sound_sender.send(sound::Type::EndBreakTimer).ok();
|
||||
|
||||
ctx.submit_command(cmd::PAUSE_ALL_TIMER_COMP);
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
use crate::cmd;
|
||||
use druid::widget::Controller;
|
||||
use druid::{Data, Env, Event, EventCtx, LifeCycle, LifeCycleCtx, UpdateCtx, Widget};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct DeinitController {
|
||||
pub struct Controller {
|
||||
deinit: bool,
|
||||
}
|
||||
|
||||
impl<W, D> Controller<D, W> for DeinitController
|
||||
impl<W, D> druid::widget::Controller<D, W> for Controller
|
||||
where
|
||||
D: Data,
|
||||
W: Widget<D>,
|
||||
|
@ -30,13 +29,13 @@ where
|
|||
env: &Env,
|
||||
) {
|
||||
if !self.deinit {
|
||||
child.lifecycle(ctx, event, data, env)
|
||||
child.lifecycle(ctx, event, data, env);
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self, child: &mut W, ctx: &mut UpdateCtx, old_data: &D, data: &D, env: &Env) {
|
||||
if !self.deinit {
|
||||
child.update(ctx, old_data, data, env)
|
||||
child.update(ctx, old_data, data, env);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ use crate::cmd;
|
|||
use crate::comp;
|
||||
use crate::env;
|
||||
use crate::state;
|
||||
use druid::widget::{Controller, Label, ProgressBar};
|
||||
use druid::widget::{Label, ProgressBar};
|
||||
use druid::{Env, Event, EventCtx, KeyOrValue, TimerToken, Widget, WidgetExt};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
|
@ -17,7 +17,9 @@ pub fn build() -> impl Widget<state::Timer> {
|
|||
.with_child(progress_bar)
|
||||
}
|
||||
|
||||
pub struct TimerController {
|
||||
type FinishHandler = Box<dyn Fn(&mut EventCtx, &Env, f64)>;
|
||||
|
||||
pub struct Controller {
|
||||
duration: KeyOrValue<f64>,
|
||||
init_duration: Option<KeyOrValue<f64>>,
|
||||
postpone_duration: Option<KeyOrValue<f64>>,
|
||||
|
@ -26,25 +28,25 @@ pub struct TimerController {
|
|||
pause_time: Option<Instant>,
|
||||
render_timer_id: TimerToken,
|
||||
finish_timer_id: TimerToken,
|
||||
finish_handler: Option<Box<dyn Fn(&mut EventCtx, &Env, f64)>>,
|
||||
finish_handler: Option<FinishHandler>,
|
||||
postpone_times: u32,
|
||||
}
|
||||
|
||||
impl TimerController {
|
||||
impl Controller {
|
||||
pub fn new<Handler>(finish_handler: Handler) -> Self
|
||||
where
|
||||
Handler: Fn(&mut EventCtx, &Env, f64) + 'static,
|
||||
{
|
||||
Self {
|
||||
Controller {
|
||||
finish_handler: Some(Box::new(finish_handler)),
|
||||
..Default::default()
|
||||
..Controller::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for TimerController {
|
||||
impl Default for Controller {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
Controller {
|
||||
duration: env::TIMER_DURATION.into(),
|
||||
init_duration: None,
|
||||
postpone_duration: None,
|
||||
|
@ -59,7 +61,7 @@ impl Default for TimerController {
|
|||
}
|
||||
}
|
||||
|
||||
impl TimerController {
|
||||
impl Controller {
|
||||
pub fn with_duration(mut self, secs: impl Into<KeyOrValue<f64>>) -> Self {
|
||||
self.duration = secs.into();
|
||||
self
|
||||
|
@ -81,7 +83,7 @@ impl TimerController {
|
|||
}
|
||||
}
|
||||
|
||||
impl<W> Controller<state::Timer, W> for TimerController
|
||||
impl<W> druid::widget::Controller<state::Timer, W> for Controller
|
||||
where
|
||||
W: Widget<state::Timer>,
|
||||
{
|
||||
|
@ -159,7 +161,7 @@ where
|
|||
self.postpone_times = 0;
|
||||
self.start_time = Instant::now();
|
||||
if data.paused {
|
||||
self.pause_time = Some(self.start_time)
|
||||
self.pause_time = Some(self.start_time);
|
||||
} else {
|
||||
self.render_timer_id = ctx.request_timer(TIMER_INTERVAL);
|
||||
self.finish_timer_id = ctx.request_timer(duration);
|
||||
|
@ -176,7 +178,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl TimerController {
|
||||
impl Controller {
|
||||
fn full_rest_duration(&self, env: &Env) -> Duration {
|
||||
self.rest_duration(env) + self.postpone_times * self.postpone_rest_duration(env)
|
||||
}
|
||||
|
@ -210,6 +212,7 @@ impl TimerController {
|
|||
Duration::from_secs_f64(self.duration.resolve(env))
|
||||
}
|
||||
|
||||
#[allow(clippy::single_match_else)]
|
||||
fn postpone_duration(&self, env: &Env) -> Duration {
|
||||
match self.postpone_times {
|
||||
0 => Duration::ZERO,
|
||||
|
|
|
@ -18,7 +18,7 @@ pub fn default() -> Env {
|
|||
}
|
||||
|
||||
fn mins(m: f64) -> f64 {
|
||||
return m * 60.0;
|
||||
m * 60.0
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
|
|
@ -71,7 +71,7 @@ impl AppDelegate<state::App> for Delegate {
|
|||
druid::commands::CONFIGURE_WINDOW
|
||||
.with(druid::WindowConfig::default().set_level(druid_shell::WindowLevel::Modal))
|
||||
.to(Target::Window(id)),
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
50
src/env.rs
50
src/env.rs
|
@ -65,52 +65,54 @@ pub fn configure(env: &mut Env, config: &config::Env) {
|
|||
);
|
||||
}
|
||||
|
||||
#[allow(clippy::many_single_char_names)]
|
||||
fn hsl(h: f64, s: f64, l: f64) -> Color {
|
||||
hsla(h, s, l, 1.0)
|
||||
}
|
||||
|
||||
#[allow(clippy::many_single_char_names)]
|
||||
fn hsla(h: f64, s: f64, l: f64, a: f64) -> Color {
|
||||
let (r, g, b) = hsl_to_rgb(h, s, l);
|
||||
Color::rgba(r, g, b, a)
|
||||
}
|
||||
|
||||
#[allow(clippy::many_single_char_names)]
|
||||
fn hsl_to_rgb(h: f64, s: f64, l: f64) -> (f64, f64, f64) {
|
||||
let r;
|
||||
let g;
|
||||
let b;
|
||||
|
||||
if s == 0.0 {
|
||||
r = l;
|
||||
g = l;
|
||||
b = l; // achromatic
|
||||
(l, l, l)
|
||||
} else {
|
||||
let q = if l < 0.5 { l * (1. + s) } else { l + s - l * s };
|
||||
let q = if l < 0.5 {
|
||||
l * (1.0 + s)
|
||||
} else {
|
||||
l + s - l * s
|
||||
};
|
||||
|
||||
let p = 2. * l - q;
|
||||
r = hue_to_rgb(p, q, h + 1. / 3.);
|
||||
g = hue_to_rgb(p, q, h);
|
||||
b = hue_to_rgb(p, q, h - 1. / 3.);
|
||||
let p = 2.0 * l - q;
|
||||
(
|
||||
hue_to_rgb(p, q, h + 1.0 / 3.0),
|
||||
hue_to_rgb(p, q, h),
|
||||
hue_to_rgb(p, q, h - 1.0 / 3.0),
|
||||
)
|
||||
}
|
||||
|
||||
return (r, g, b);
|
||||
}
|
||||
|
||||
fn hue_to_rgb(p: f64, q: f64, t: f64) -> f64 {
|
||||
let mut t = t;
|
||||
if t < 0. {
|
||||
t += 1.
|
||||
if t < 0.0 {
|
||||
t += 1.0;
|
||||
}
|
||||
if t > 1. {
|
||||
t -= 1.
|
||||
if t > 1.0 {
|
||||
t -= 1.0;
|
||||
};
|
||||
if t < 1. / 6. {
|
||||
return p + (q - p) * 6. * t;
|
||||
if t < 1.0 / 6.0 {
|
||||
return p + (q - p) * 6.0 * t;
|
||||
}
|
||||
if t < 1. / 2. {
|
||||
if t < 1.0 / 2.0 {
|
||||
return q;
|
||||
}
|
||||
if t < 2. / 3. {
|
||||
return p + (q - p) * (2. / 3. - t) * 6.;
|
||||
if t < 2.0 / 3.0 {
|
||||
return p + (q - p) * (2.0 / 3.0 - t) * 6.0;
|
||||
}
|
||||
return p;
|
||||
|
||||
p
|
||||
}
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#![deny(clippy::pedantic)]
|
||||
|
||||
mod cmd;
|
||||
mod comp;
|
||||
mod config;
|
||||
|
@ -14,7 +16,7 @@ use std::sync::mpsc::channel;
|
|||
use std::thread;
|
||||
|
||||
fn main() {
|
||||
let cfg = if let Some(config_path) = std::env::args().skip(1).next() {
|
||||
let cfg = if let Some(config_path) = std::env::args().nth(1) {
|
||||
log::debug!("[cli] config_path={}", config_path);
|
||||
let file_content = std::fs::read_to_string(config_path).unwrap_or_else(|err| {
|
||||
eprintln!("[ERROR] [fs] {}", err);
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
pub type Sender = std::sync::mpsc::Sender<Type>;
|
||||
|
||||
#[allow(clippy::enum_variant_names)]
|
||||
pub enum Type {
|
||||
EndBreakTimer,
|
||||
EndNotifier,
|
||||
|
|
|
@ -49,13 +49,13 @@ impl Timer {
|
|||
pub fn new(paused: bool) -> Self {
|
||||
Self {
|
||||
progress: Default::default(),
|
||||
time: Default::default(),
|
||||
time: String::default(),
|
||||
paused,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reset(&mut self, duration: Duration) {
|
||||
self.update_progress_and_time(Duration::ZERO, duration)
|
||||
self.update_progress_and_time(Duration::ZERO, duration);
|
||||
}
|
||||
|
||||
pub fn update_progress_and_time(&mut self, elapsed: Duration, full_duration: Duration) {
|
||||
|
@ -67,6 +67,7 @@ impl Timer {
|
|||
self.progress = elapsed.as_secs_f64().div(duration.as_secs_f64()).min(1.0);
|
||||
}
|
||||
|
||||
#[allow(clippy::cast_possible_truncation)]
|
||||
fn update_time(&mut self, elapsed: Duration, duration: Duration) {
|
||||
let all_secs = duration.as_secs_f64() - elapsed.as_secs_f64();
|
||||
let sign = if all_secs < 0.0 { "-" } else { "" };
|
||||
|
@ -79,7 +80,7 @@ impl Timer {
|
|||
sign,
|
||||
mins as i32,
|
||||
if secs < 10.0 { "0" } else { "" },
|
||||
secs as u32
|
||||
secs as i32
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,12 +19,12 @@ pub fn create(
|
|||
let x = (rect.width() - win_width) / 2.0;
|
||||
let y = 0.0;
|
||||
|
||||
return WindowDesc::new(move || build(parent_widget_id, rest_duration_secs, sound_sender))
|
||||
WindowDesc::new(move || build(parent_widget_id, rest_duration_secs, sound_sender))
|
||||
.show_titlebar(false)
|
||||
.menu(MenuDesc::empty())
|
||||
.set_position((x, y))
|
||||
.with_min_size((win_width, win_height))
|
||||
.window_size((win_width, win_height));
|
||||
.window_size((win_width, win_height))
|
||||
}
|
||||
|
||||
fn build(
|
||||
|
@ -49,7 +49,7 @@ fn build_notifier_timer(
|
|||
) -> impl Widget<state::Timer> {
|
||||
comp::timer::build()
|
||||
.controller(
|
||||
comp::timer::TimerController::new(move |ctx, _env, _rest_duration| {
|
||||
comp::timer::Controller::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())));
|
||||
|
@ -62,7 +62,7 @@ fn build_notifier_timer(
|
|||
})
|
||||
.with_duration(env::BREAK_NOTIFIER_TIMER_DURATION),
|
||||
)
|
||||
.controller(comp::deinit::DeinitController::default())
|
||||
.controller(comp::deinit::Controller::default())
|
||||
}
|
||||
|
||||
fn build_postpone_btn<D: druid::Data>(parent_widget_id: WidgetId) -> impl Widget<D> {
|
||||
|
|
|
@ -19,12 +19,12 @@ pub fn create(
|
|||
let x = (rect.width() - win_width) / 2.0;
|
||||
let y = (rect.height() - win_height) / 2.0;
|
||||
|
||||
return WindowDesc::new(move || build(parent_widget_id, rest_duration_secs, sound_sender))
|
||||
WindowDesc::new(move || build(parent_widget_id, rest_duration_secs, sound_sender))
|
||||
.show_titlebar(false)
|
||||
.menu(MenuDesc::empty())
|
||||
.set_position((x, y))
|
||||
.with_min_size((win_width, win_height))
|
||||
.window_size((win_width, win_height));
|
||||
.window_size((win_width, win_height))
|
||||
}
|
||||
|
||||
fn build(
|
||||
|
@ -52,7 +52,7 @@ fn build_idle_timer(
|
|||
) -> impl Widget<state::Timer> {
|
||||
comp::timer::build()
|
||||
.controller(
|
||||
comp::timer::TimerController::new(move |ctx, env, _rest_duration| {
|
||||
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())));
|
||||
|
@ -67,7 +67,7 @@ fn build_idle_timer(
|
|||
.with_duration(rest_duration_secs)
|
||||
.with_init_duration(env::BREAK_NOTIFIER_TIMER_DURATION),
|
||||
)
|
||||
.controller(comp::deinit::DeinitController::default())
|
||||
.controller(comp::deinit::Controller::default())
|
||||
}
|
||||
|
||||
fn build_finish_btn(parent_widget_id: WidgetId) -> impl Widget<state::App> {
|
||||
|
|
|
@ -30,9 +30,9 @@ fn build_timers(sender: Rc<sound::Sender>) -> impl Widget<state::App> {
|
|||
.with_child(
|
||||
comp::break_timer::build(
|
||||
"Micro",
|
||||
env::MICRO_BREAK_TIMER_DURATION,
|
||||
env::MICRO_BREAK_TIMER_POSTPONE_DURATION,
|
||||
env::MICRO_BREAK_TIMER_REST_DURATION,
|
||||
&env::MICRO_BREAK_TIMER_DURATION,
|
||||
&env::MICRO_BREAK_TIMER_POSTPONE_DURATION,
|
||||
&env::MICRO_BREAK_TIMER_REST_DURATION,
|
||||
sender.clone(),
|
||||
)
|
||||
.lens(state::App::micro_break),
|
||||
|
@ -41,10 +41,10 @@ fn build_timers(sender: Rc<sound::Sender>) -> impl Widget<state::App> {
|
|||
.with_child(
|
||||
comp::break_timer::build(
|
||||
"Rest",
|
||||
env::REST_BREAK_TIMER_DURATION,
|
||||
env::REST_BREAK_TIMER_POSTPONE_DURATION,
|
||||
env::REST_BREAK_TIMER_REST_DURATION,
|
||||
sender.clone(),
|
||||
&env::REST_BREAK_TIMER_DURATION,
|
||||
&env::REST_BREAK_TIMER_POSTPONE_DURATION,
|
||||
&env::REST_BREAK_TIMER_REST_DURATION,
|
||||
sender,
|
||||
)
|
||||
.lens(state::App::rest_break),
|
||||
)
|
||||
|
@ -57,7 +57,7 @@ fn build_pause_btn() -> impl Widget<state::App> {
|
|||
.with_child(
|
||||
Button::new("Start").on_click(|ctx, data: &mut state::App, _env| {
|
||||
data.paused = false;
|
||||
ctx.submit_command(cmd::UNPAUSE_ALL_TIMER_COMP.with(false))
|
||||
ctx.submit_command(cmd::UNPAUSE_ALL_TIMER_COMP.with(false));
|
||||
}),
|
||||
)
|
||||
.with_default_spacer()
|
||||
|
@ -68,7 +68,7 @@ fn build_pause_btn() -> impl Widget<state::App> {
|
|||
comp::flex::row_sta_sta().with_child(Button::new("Pause").on_click(
|
||||
|ctx, data: &mut state::App, _env| {
|
||||
data.paused = true;
|
||||
ctx.submit_command(cmd::PAUSE_ALL_TIMER_COMP)
|
||||
ctx.submit_command(cmd::PAUSE_ALL_TIMER_COMP);
|
||||
},
|
||||
)),
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue