From 3c9ae881c9cc9062acb586096256e5ab99b5c929 Mon Sep 17 00:00:00 2001 From: "Jens K." <47693+sectore@users.noreply.github.com> Date: Wed, 18 Dec 2024 08:49:57 +0100 Subject: [PATCH] Editable timer (#17) --- src/widgets/clock.rs | 29 +++++++++++++++++++++++------ src/widgets/timer.rs | 31 +++++++++++++++++++++++++------ 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/src/widgets/clock.rs b/src/widgets/clock.rs index a33cbf0..300ea2f 100644 --- a/src/widgets/clock.rs +++ b/src/widgets/clock.rs @@ -57,8 +57,8 @@ const MINS_PER_HOUR: u64 = 60; const HOURS_PER_DAY: u64 = 24; // max. 99:59:59 -const MAX_EDITABLE_DURATION: Duration = - Duration::from_secs(100 * MINS_PER_HOUR * SECS_PER_MINUTE - 1); +const MAX_DURATION: Duration = + Duration::from_secs(100 * MINS_PER_HOUR * SECS_PER_MINUTE).saturating_sub(ONE_SECOND); const ONE_SECOND: Duration = Duration::from_secs(1); const ONE_MINUTE: Duration = Duration::from_secs(SECS_PER_MINUTE); @@ -127,7 +127,7 @@ impl Clock { if self .current_value // < 99:59:58 - .le(&MAX_EDITABLE_DURATION.saturating_sub(ONE_SECOND)) + .le(&MAX_DURATION.saturating_sub(ONE_SECOND)) { self.current_value.saturating_add(ONE_SECOND) } else { @@ -138,7 +138,7 @@ impl Clock { if self .current_value // < 99:58:59 - .le(&MAX_EDITABLE_DURATION.saturating_sub(ONE_MINUTE)) + .le(&MAX_DURATION.saturating_sub(ONE_MINUTE)) { self.current_value.saturating_add(ONE_MINUTE) } else { @@ -149,7 +149,7 @@ impl Clock { if self .current_value // < 98:59:59 - .lt(&MAX_EDITABLE_DURATION.saturating_sub(ONE_HOUR)) + .lt(&MAX_DURATION.saturating_sub(ONE_HOUR)) { self.current_value.saturating_add(ONE_HOUR) } else { @@ -229,6 +229,7 @@ impl Clock { pub fn reset(&mut self) { self.mode = Mode::Initial; self.current_value = self.initial_value; + self.update_format(); } fn current_hours(&self) -> u64 { @@ -381,10 +382,26 @@ impl Clock { } fn check_done(&mut self) { - if self.current_value == self.initial_value { + if self.current_value >= MAX_DURATION { self.mode = Mode::Done; } } + + pub fn edit_next(&mut self) { + self.edit_mode_next(); + } + + pub fn edit_prev(&mut self) { + self.edit_mode_prev(); + } + + pub fn edit_up(&mut self) { + self.edit_current_up(); + } + + pub fn edit_down(&mut self) { + self.edit_current_down(); + } } const DIGIT_SYMBOL: &str = "█"; diff --git a/src/widgets/timer.rs b/src/widgets/timer.rs index c811a7d..a2aa60c 100644 --- a/src/widgets/timer.rs +++ b/src/widgets/timer.rs @@ -25,16 +25,35 @@ impl Timer { impl EventHandler for Timer { fn update(&mut self, event: Event) -> Option { + let edit_mode = self.clock.is_edit_mode(); match event { Event::Tick => { self.clock.tick(); } - Event::Key(key) if key.code == KeyCode::Char('s') => { - self.clock.toggle_pause(); - } - Event::Key(key) if key.code == KeyCode::Char('r') => { - self.clock.reset(); - } + Event::Key(key) => match key.code { + KeyCode::Char('s') => { + self.clock.toggle_pause(); + } + KeyCode::Char('r') => { + self.clock.reset(); + } + KeyCode::Char('e') => { + self.clock.toggle_edit(); + } + KeyCode::Left if edit_mode => { + self.clock.edit_next(); + } + KeyCode::Right if edit_mode => { + self.clock.edit_prev(); + } + KeyCode::Up if edit_mode => { + self.clock.edit_up(); + } + KeyCode::Down if edit_mode => { + self.clock.edit_down(); + } + _ => return Some(event), + }, _ => return Some(event), } None