StatefulWidgets (#7)

* trait EventHandler
* StatefulWidget: AppWidget, CountdownWidget
* StatefulWidget: TimerWidget
This commit is contained in:
Jens K.
2024-12-03 11:23:43 +01:00
committed by GitHub
parent 4f66ea86d4
commit cbb6a60ee9
4 changed files with 107 additions and 73 deletions

View File

@@ -1,12 +1,16 @@
use ratatui::{
buffer::Buffer,
crossterm::event::KeyCode,
layout::{Constraint, Layout, Rect},
widgets::{Paragraph, Widget},
widgets::{Paragraph, StatefulWidget, Widget},
};
use crate::clock::{self, Clock};
use crate::{
clock::{self, Clock},
events::{Event, EventHandler},
};
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Countdown {
headline: String,
clock: Clock<clock::Countdown>,
@@ -18,13 +22,33 @@ impl Countdown {
}
}
impl Widget for Countdown {
fn render(mut self, area: Rect, buf: &mut Buffer) {
let h = Paragraph::new(self.headline).centered();
let c = Paragraph::new(self.clock.format()).centered();
impl EventHandler for Countdown {
fn update(&mut self, event: Event) {
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();
}
_ => {}
}
}
}
pub struct CountdownWidget;
impl StatefulWidget for &CountdownWidget {
type State = Countdown;
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
let h = Paragraph::new(state.headline.clone()).centered();
let c = Paragraph::new(state.clock.format()).centered();
let [v1, v2] = Layout::vertical([Constraint::Length(1), Constraint::Length(1)]).areas(area);
h.render(v1, buf);
c.render(v2, buf)
c.render(v2, buf);
}
}