Files
wldash/src/widgets/date.rs
T

96 lines
2.7 KiB
Rust
Raw Normal View History

2019-09-06 21:54:04 +02:00
use crate::color::Color;
2020-06-03 21:06:06 +02:00
use crate::draw::Font;
use crate::{
fonts::FontRef,
widget::{DrawContext, DrawReport, KeyState, ModifiersState, WaitContext, Widget},
};
2019-09-06 21:54:04 +02:00
2020-04-22 00:52:08 +02:00
use chrono::{Datelike, NaiveDateTime};
2019-09-06 21:54:04 +02:00
2020-06-03 21:06:06 +02:00
pub struct Date<'a> {
2020-04-22 00:52:08 +02:00
cur_time: NaiveDateTime,
2020-06-03 21:06:06 +02:00
date_cache: Font<'a>,
2019-09-06 21:54:04 +02:00
size: f32,
2020-07-22 12:42:48 +02:00
ch_width: u32,
digit_width: u32,
spacing_width: u32,
2019-09-06 21:54:04 +02:00
}
2020-06-03 21:06:06 +02:00
impl<'a> Date<'a> {
2020-07-22 12:42:48 +02:00
pub fn new(time: NaiveDateTime, font: FontRef, size: f32) -> ::std::io::Result<Box<Date>> {
2020-06-03 21:06:06 +02:00
let mut date_cache = Font::new(font, size);
2020-07-22 12:42:48 +02:00
let chs = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
let digits = "0123456789,/ ";
let spacings = ",/ ";
2019-09-06 21:54:04 +02:00
2020-07-22 12:42:48 +02:00
date_cache.add_str_to_cache(chs);
date_cache.add_str_to_cache(digits);
date_cache.add_str_to_cache(spacings);
let ch_width = date_cache.auto_widest(chs)?;
let digit_width = date_cache.auto_widest(digits)?;
let spacing_width = date_cache.auto_widest(spacings)?;
Ok(Box::new(Date {
2020-04-22 01:31:22 +02:00
cur_time: time,
date_cache,
size,
2020-07-22 12:42:48 +02:00
ch_width,
digit_width,
spacing_width,
}))
2019-09-06 21:54:04 +02:00
}
}
2020-06-03 21:06:06 +02:00
impl<'a> Widget for Date<'a> {
2019-10-05 21:41:09 +02:00
fn wait(&mut self, _: &mut WaitContext) {}
2019-10-02 14:05:35 +02:00
fn enter(&mut self) {}
fn leave(&mut self) {}
2019-09-06 21:54:04 +02:00
fn size(&self) -> (u32, u32) {
2020-07-22 12:42:48 +02:00
(
(3 * self.ch_width + 8 * self.digit_width + 4 * self.spacing_width) as u32,
self.size.ceil() as u32,
)
2019-09-06 21:54:04 +02:00
}
fn draw(
&mut self,
ctx: &mut DrawContext,
pos: (u32, u32),
_expansion: (u32, u32),
2019-09-06 21:54:04 +02:00
) -> Result<DrawReport, ::std::io::Error> {
let (width, height) = self.size();
if !(ctx.time.date() != self.cur_time.date() || ctx.force) {
return Ok(DrawReport::empty(width, height));
}
2020-04-22 01:31:22 +02:00
self.cur_time = ctx.time;
2019-09-06 21:54:04 +02:00
let buf = &mut ctx.buf.subdimensions((pos.0, pos.1, width, height))?;
buf.memset(ctx.bg);
self.date_cache.draw_text(
buf,
ctx.bg,
&Color::new(1.0, 1.0, 1.0, 1.0),
&format!(
"{:?}, {:02}/{:02}/{:4}",
ctx.time.weekday(),
ctx.time.day(),
ctx.time.month(),
ctx.time.year()
),
)?;
Ok(DrawReport {
2020-04-22 01:31:22 +02:00
width,
height,
2019-09-06 21:54:04 +02:00
damage: vec![(pos.0 as i32, pos.1 as i32, width as i32, height as i32)],
full_damage: false,
})
}
fn keyboard_input(&mut self, _: u32, _: ModifiersState, _: KeyState, _: Option<String>) {}
fn mouse_click(&mut self, _: u32, _: (u32, u32)) {}
fn mouse_scroll(&mut self, _: (f64, f64), _: (u32, u32)) {}
}