Clock<T> (#6)
This commit is contained in:
57
src/app.rs
57
src/app.rs
@@ -1,4 +1,6 @@
|
||||
use crate::{
|
||||
clock::{self, Clock},
|
||||
constants::TICK_VALUE_MS,
|
||||
events::{Event, Events},
|
||||
terminal::Terminal,
|
||||
utils::center,
|
||||
@@ -32,7 +34,8 @@ pub struct App {
|
||||
content: Content,
|
||||
mode: Mode,
|
||||
show_menu: bool,
|
||||
tick: u128,
|
||||
clock_countdown: Clock<clock::Countdown>,
|
||||
clock_timer: Clock<clock::Timer>,
|
||||
}
|
||||
|
||||
impl Default for App {
|
||||
@@ -41,7 +44,11 @@ impl Default for App {
|
||||
mode: Mode::Running,
|
||||
content: Content::Countdown,
|
||||
show_menu: false,
|
||||
tick: 0,
|
||||
clock_countdown: Clock::<clock::Countdown>::new(
|
||||
10 * 60 * 1000, /* 10min in milliseconds */
|
||||
TICK_VALUE_MS,
|
||||
),
|
||||
clock_timer: Clock::<clock::Timer>::new(0, TICK_VALUE_MS),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -59,7 +66,7 @@ impl App {
|
||||
self.draw(&mut terminal)?;
|
||||
}
|
||||
Event::Tick => {
|
||||
self.tick = self.tick.saturating_add(1);
|
||||
self.tick();
|
||||
}
|
||||
Event::Key(key) => self.handle_key_event(key),
|
||||
_ => {}
|
||||
@@ -77,9 +84,11 @@ impl App {
|
||||
match key.code {
|
||||
KeyCode::Char('q') | KeyCode::Esc => self.mode = Mode::Quit,
|
||||
KeyCode::Char('c') => self.content = Content::Countdown,
|
||||
KeyCode::Char('s') => self.toggle(),
|
||||
KeyCode::Char('t') => self.content = Content::Timer,
|
||||
KeyCode::Char('p') => self.content = Content::Pomodoro,
|
||||
KeyCode::Char('m') => self.show_menu = !self.show_menu,
|
||||
KeyCode::Char('r') => self.reset(),
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
@@ -93,13 +102,41 @@ impl App {
|
||||
|
||||
fn render_content(&self, area: Rect, buf: &mut Buffer) {
|
||||
// center content
|
||||
let area = center(area, Constraint::Length(50), Constraint::Length(1));
|
||||
let area = center(area, Constraint::Length(50), Constraint::Length(2));
|
||||
match self.content {
|
||||
Content::Timer => Timer::new(200, "Timer".into()).render(area, buf),
|
||||
Content::Countdown => Countdown::new("Countdown".into()).render(area, buf),
|
||||
Content::Timer => {
|
||||
Timer::new("Timer".into(), self.clock_timer.clone()).render(area, buf)
|
||||
}
|
||||
Content::Countdown => {
|
||||
Countdown::new("Countdown".into(), self.clock_countdown.clone()).render(area, buf)
|
||||
}
|
||||
Content::Pomodoro => Pomodoro::new("Pomodoro".into()).render(area, buf),
|
||||
};
|
||||
}
|
||||
|
||||
fn reset(&mut self) {
|
||||
match self.content {
|
||||
Content::Timer => self.clock_timer.reset(),
|
||||
Content::Countdown => self.clock_countdown.reset(),
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
|
||||
fn toggle(&mut self) {
|
||||
match self.content {
|
||||
Content::Timer => self.clock_timer.toggle_pause(),
|
||||
Content::Countdown => self.clock_countdown.toggle_pause(),
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
|
||||
fn tick(&mut self) {
|
||||
match self.content {
|
||||
Content::Timer => self.clock_timer.tick(),
|
||||
Content::Countdown => self.clock_countdown.tick(),
|
||||
_ => {}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for &App {
|
||||
@@ -109,11 +146,11 @@ impl Widget for &App {
|
||||
Constraint::Fill(0),
|
||||
Constraint::Length(if self.show_menu { 2 } else { 1 }),
|
||||
]);
|
||||
let [header_area, content_area, footer_area] = vertical.areas(area);
|
||||
let [v0, v1, v4] = vertical.areas(area);
|
||||
|
||||
Block::new().render(area, buf);
|
||||
Header::new(self.tick).render(header_area, buf);
|
||||
self.render_content(content_area, buf);
|
||||
Footer::new(self.show_menu, self.content).render(footer_area, buf);
|
||||
Header::new(true).render(v0, buf);
|
||||
self.render_content(v1, buf);
|
||||
Footer::new(self.show_menu, self.content).render(v4, buf);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user