ClockWidget (#10)

This commit is contained in:
Jens K.
2024-12-05 17:25:09 +01:00
committed by GitHub
parent b5f9147b45
commit 929e453d85
6 changed files with 308 additions and 35 deletions

View File

@@ -2,12 +2,15 @@ use ratatui::{
buffer::Buffer,
crossterm::event::KeyCode,
layout::{Constraint, Layout, Rect},
widgets::{Paragraph, StatefulWidget, Widget},
text::Line,
widgets::{StatefulWidget, Widget},
};
use std::cmp::max;
use crate::{
clock::{self, Clock},
clock::{self, Clock, ClockWidget},
events::{Event, EventHandler},
utils::center,
};
#[derive(Debug, Clone)]
@@ -41,14 +44,21 @@ impl EventHandler for Countdown {
pub struct CountdownWidget;
impl StatefulWidget for &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(format!("{}", state.clock)).centered();
let [v1, v2] = Layout::vertical([Constraint::Length(1), Constraint::Length(1)]).areas(area);
let clock = ClockWidget::new();
let headline = Line::raw(state.headline.clone());
h.render(v1, buf);
c.render(v2, buf);
let area = center(
area,
Constraint::Length(max(clock.get_width(), headline.width() as u16)),
Constraint::Length(clock.get_height() + 2),
);
let [v1, _, v2] =
Layout::vertical(Constraint::from_lengths([clock.get_height(), 1, 1])).areas(area);
clock.render(v1, buf, &mut state.clock);
headline.centered().render(v2, buf);
}
}