feat(screen): LocalTime (#90)
This commit is contained in:
@@ -56,6 +56,7 @@ impl StatefulWidget for Footer {
|
||||
(Content::Countdown, "[c]ountdown"),
|
||||
(Content::Timer, "[t]imer"),
|
||||
(Content::Pomodoro, "[p]omodoro"),
|
||||
(Content::LocalTime, "[l]ocal time"),
|
||||
]);
|
||||
|
||||
let [_, area] =
|
||||
@@ -71,11 +72,12 @@ impl StatefulWidget for Footer {
|
||||
)
|
||||
.title(
|
||||
Line::from(
|
||||
match state.app_time_format {
|
||||
// `Hidden` -> no (empty) title
|
||||
None => "".into(),
|
||||
// others -> add some space around
|
||||
Some(v) => format!(" {} ", self.app_time.format(&v))
|
||||
match (state.app_time_format, self.selected_content) {
|
||||
// Show time
|
||||
(Some(v), content) if content != Content::LocalTime => format!(" {} " // add some space around
|
||||
, self.app_time.format(&v)),
|
||||
// Hide time -> empty
|
||||
_ => "".into(),
|
||||
}
|
||||
).right_aligned())
|
||||
.border_set(border::PLAIN)
|
||||
@@ -102,36 +104,39 @@ impl StatefulWidget for Footer {
|
||||
|
||||
const SPACE: &str = " "; // 2 empty spaces
|
||||
let widths = [Constraint::Length(12), Constraint::Percentage(100)];
|
||||
let table = Table::new(
|
||||
[
|
||||
// screens
|
||||
Row::new(vec![
|
||||
Cell::from(Span::styled(
|
||||
"screens",
|
||||
Style::default().add_modifier(Modifier::BOLD),
|
||||
let mut table_rows = vec![
|
||||
// screens
|
||||
Row::new(vec![
|
||||
Cell::from(Span::styled(
|
||||
"screens",
|
||||
Style::default().add_modifier(Modifier::BOLD),
|
||||
)),
|
||||
Cell::from(Line::from(content_labels)),
|
||||
]),
|
||||
// appearance
|
||||
Row::new(vec![
|
||||
Cell::from(Span::styled(
|
||||
"appearance",
|
||||
Style::default().add_modifier(Modifier::BOLD),
|
||||
)),
|
||||
Cell::from(Line::from(vec![
|
||||
Span::from("[,]change style"),
|
||||
Span::from(SPACE),
|
||||
Span::from("[.]toggle deciseconds"),
|
||||
Span::from(SPACE),
|
||||
Span::from(format!(
|
||||
"[:]toggle {} time",
|
||||
match self.app_time {
|
||||
AppTime::Local(_) => "local",
|
||||
AppTime::Utc(_) => "utc",
|
||||
}
|
||||
)),
|
||||
Cell::from(Line::from(content_labels)),
|
||||
]),
|
||||
// appearance
|
||||
Row::new(vec![
|
||||
Cell::from(Span::styled(
|
||||
"appearance",
|
||||
Style::default().add_modifier(Modifier::BOLD),
|
||||
)),
|
||||
Cell::from(Line::from(vec![
|
||||
Span::from("[,]change style"),
|
||||
Span::from(SPACE),
|
||||
Span::from("[.]toggle deciseconds"),
|
||||
Span::from(SPACE),
|
||||
Span::from(format!(
|
||||
"[:]toggle {} time",
|
||||
match self.app_time {
|
||||
AppTime::Local(_) => "local",
|
||||
AppTime::Utc(_) => "utc",
|
||||
}
|
||||
)),
|
||||
])),
|
||||
]),
|
||||
])),
|
||||
]),
|
||||
];
|
||||
|
||||
if self.selected_content != Content::LocalTime {
|
||||
table_rows.extend_from_slice(&[
|
||||
// controls - 1. row
|
||||
Row::new(vec![
|
||||
Cell::from(Span::styled(
|
||||
@@ -224,10 +229,10 @@ impl StatefulWidget for Footer {
|
||||
}
|
||||
})),
|
||||
]),
|
||||
],
|
||||
widths,
|
||||
)
|
||||
.column_spacing(1);
|
||||
])
|
||||
}
|
||||
|
||||
let table = Table::new(table_rows, widths).column_spacing(1);
|
||||
|
||||
Widget::render(table, menu_area, buf);
|
||||
}
|
||||
|
||||
185
src/widgets/local_time.rs
Normal file
185
src/widgets/local_time.rs
Normal file
@@ -0,0 +1,185 @@
|
||||
use ratatui::{
|
||||
buffer::Buffer,
|
||||
layout::{Constraint, Layout, Rect},
|
||||
style::{Modifier, Style},
|
||||
text::{Line, Span},
|
||||
widgets::{StatefulWidget, Widget},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
common::{AppTime, AppTimeFormat, Style as DigitStyle},
|
||||
duration::DurationEx,
|
||||
events::{TuiEvent, TuiEventHandler},
|
||||
utils::center,
|
||||
widgets::clock_elements::{
|
||||
COLON_WIDTH, Colon, DIGIT_HEIGHT, DIGIT_SPACE_WIDTH, DIGIT_WIDTH, Digit,
|
||||
},
|
||||
};
|
||||
use std::cmp::max;
|
||||
|
||||
/// State for `LocalTimeWidget`
|
||||
pub struct LocalTimeState {
|
||||
time: AppTime,
|
||||
format: AppTimeFormat,
|
||||
}
|
||||
|
||||
pub struct LocalTimeStateArgs {
|
||||
pub app_time: AppTime,
|
||||
pub app_time_format: AppTimeFormat,
|
||||
}
|
||||
|
||||
impl LocalTimeState {
|
||||
pub fn new(args: LocalTimeStateArgs) -> Self {
|
||||
let LocalTimeStateArgs {
|
||||
app_time,
|
||||
app_time_format,
|
||||
} = args;
|
||||
|
||||
Self {
|
||||
time: app_time,
|
||||
format: app_time_format,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_app_time(&mut self, app_time: AppTime) {
|
||||
self.time = app_time;
|
||||
}
|
||||
|
||||
pub fn set_app_time_format(&mut self, format: AppTimeFormat) {
|
||||
self.format = format;
|
||||
}
|
||||
}
|
||||
|
||||
impl TuiEventHandler for LocalTimeState {
|
||||
fn update(&mut self, event: TuiEvent) -> Option<TuiEvent> {
|
||||
Some(event)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct LocalTimeWidget {
|
||||
pub style: DigitStyle,
|
||||
}
|
||||
|
||||
impl LocalTimeWidget {
|
||||
fn get_horizontal_lengths(&self, format: &AppTimeFormat) -> Vec<u16> {
|
||||
const PERIOD_WIDTH: u16 = 2; // PM or AM
|
||||
|
||||
match format {
|
||||
AppTimeFormat::HhMmSs => vec![
|
||||
DIGIT_WIDTH, // H
|
||||
DIGIT_SPACE_WIDTH, // (space)
|
||||
DIGIT_WIDTH, // h
|
||||
COLON_WIDTH, // :
|
||||
DIGIT_WIDTH, // M
|
||||
DIGIT_SPACE_WIDTH, // (space)
|
||||
DIGIT_WIDTH, // m
|
||||
COLON_WIDTH, // :
|
||||
DIGIT_WIDTH, // S
|
||||
DIGIT_SPACE_WIDTH, // (space)
|
||||
DIGIT_WIDTH, // s
|
||||
],
|
||||
AppTimeFormat::HhMm => vec![
|
||||
DIGIT_WIDTH, // H
|
||||
DIGIT_SPACE_WIDTH, // (space)
|
||||
DIGIT_WIDTH, // h
|
||||
COLON_WIDTH, // :
|
||||
DIGIT_WIDTH, // M
|
||||
DIGIT_SPACE_WIDTH, // (space)
|
||||
DIGIT_WIDTH, // m
|
||||
],
|
||||
AppTimeFormat::Hh12Mm => vec![
|
||||
DIGIT_SPACE_WIDTH + PERIOD_WIDTH, // (space) + (empty period) to center everything well horizontally
|
||||
DIGIT_WIDTH, // H
|
||||
DIGIT_SPACE_WIDTH, // (space)
|
||||
DIGIT_WIDTH, // h
|
||||
COLON_WIDTH, // :
|
||||
DIGIT_WIDTH, // M
|
||||
DIGIT_SPACE_WIDTH, // (space)
|
||||
DIGIT_WIDTH, // m
|
||||
DIGIT_SPACE_WIDTH, // (space)
|
||||
PERIOD_WIDTH, // period
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl StatefulWidget for LocalTimeWidget {
|
||||
type State = LocalTimeState;
|
||||
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
|
||||
let current_value: DurationEx = state.time.as_duration_of_today().into();
|
||||
let hours = current_value.hours_mod();
|
||||
let hours12 = current_value.hours_mod_12();
|
||||
let minutes = current_value.minutes_mod();
|
||||
let seconds = current_value.seconds_mod();
|
||||
let symbol = self.style.get_digit_symbol();
|
||||
|
||||
let label = Line::raw("Local Time".to_uppercase());
|
||||
|
||||
let format = state.format;
|
||||
let widths = self.get_horizontal_lengths(&format);
|
||||
let mut widths = widths;
|
||||
// Special case for `Hh12Mm`
|
||||
// It might be `h:Mm` OR `Hh:Mm` depending on `hours12`
|
||||
if state.format == AppTimeFormat::Hh12Mm && hours12 < 10 {
|
||||
// single digit means, no (zero) width's for `H` and `space`
|
||||
widths[1] = 0; // `H`
|
||||
widths[2] = 0; // `space`
|
||||
}
|
||||
|
||||
let width = widths.iter().sum();
|
||||
let area = center(
|
||||
area,
|
||||
Constraint::Length(max(width, label.width() as u16)),
|
||||
Constraint::Length(DIGIT_HEIGHT + 1 /* height of label */),
|
||||
);
|
||||
|
||||
let [v1, v2] = Layout::vertical(Constraint::from_lengths([DIGIT_HEIGHT, 1])).areas(area);
|
||||
|
||||
match state.format {
|
||||
AppTimeFormat::HhMmSs => {
|
||||
let [hh, _, h, c_hm, mm, _, m, c_ms, ss, _, s] =
|
||||
Layout::horizontal(Constraint::from_lengths(widths)).areas(v1);
|
||||
Digit::new(hours / 10, false, symbol).render(hh, buf);
|
||||
Digit::new(hours % 10, false, symbol).render(h, buf);
|
||||
Colon::new(symbol).render(c_hm, buf);
|
||||
Digit::new(minutes / 10, false, symbol).render(mm, buf);
|
||||
Digit::new(minutes % 10, false, symbol).render(m, buf);
|
||||
Colon::new(symbol).render(c_ms, buf);
|
||||
Digit::new(seconds / 10, false, symbol).render(ss, buf);
|
||||
Digit::new(seconds % 10, false, symbol).render(s, buf);
|
||||
}
|
||||
AppTimeFormat::HhMm => {
|
||||
let [hh, _, h, c_hm, mm, _, m] =
|
||||
Layout::horizontal(Constraint::from_lengths(widths)).areas(v1);
|
||||
Digit::new(hours / 10, false, symbol).render(hh, buf);
|
||||
Digit::new(hours % 10, false, symbol).render(h, buf);
|
||||
Colon::new(symbol).render(c_hm, buf);
|
||||
Digit::new(minutes / 10, false, symbol).render(mm, buf);
|
||||
Digit::new(minutes % 10, false, symbol).render(m, buf);
|
||||
}
|
||||
AppTimeFormat::Hh12Mm => {
|
||||
let [_, hh, _, h, c_hm, mm, _, m, _, p] =
|
||||
Layout::horizontal(Constraint::from_lengths(widths)).areas(v1);
|
||||
// Hh
|
||||
if hours12 >= 10 {
|
||||
Digit::new(hours12 / 10, false, symbol).render(hh, buf);
|
||||
Digit::new(hours12 % 10, false, symbol).render(h, buf);
|
||||
}
|
||||
// h
|
||||
else {
|
||||
Digit::new(hours12, false, symbol).render(h, buf);
|
||||
}
|
||||
Colon::new(symbol).render(c_hm, buf);
|
||||
Digit::new(minutes / 10, false, symbol).render(mm, buf);
|
||||
Digit::new(minutes % 10, false, symbol).render(m, buf);
|
||||
Span::styled(
|
||||
state.time.get_period().to_uppercase(),
|
||||
Style::default().add_modifier(Modifier::BOLD),
|
||||
)
|
||||
.render(p, buf);
|
||||
}
|
||||
}
|
||||
label.centered().render(v2, buf);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user