2022-08-02 08:04:48 +02:00
|
|
|
// For now, to implement a custom native widget you will need to add
|
|
|
|
// `iced_native` and `iced_wgpu` to your dependencies.
|
|
|
|
//
|
|
|
|
// Then, you simply need to define your widget type and implement the
|
|
|
|
// `iced_native::Widget` trait with the `iced_wgpu::Renderer`.
|
|
|
|
//
|
|
|
|
// Of course, you can choose to make the implementation renderer-agnostic,
|
|
|
|
// if you wish to, by creating your own `Renderer` trait, which could be
|
|
|
|
// implemented by `iced_wgpu` and other renderers.
|
|
|
|
|
2023-05-21 13:41:16 +02:00
|
|
|
use iced::advanced::{layout, renderer};
|
|
|
|
use iced::advanced::widget::{Tree, Widget};
|
|
|
|
use iced::{Color, Element, Length, Point, Rectangle, alignment};
|
|
|
|
use iced::advanced::text::{self, Text, LineHeight, Shaping};
|
2022-12-27 09:29:29 +01:00
|
|
|
use chrono::{NaiveDate, Datelike, Duration, Local};
|
2022-12-25 12:14:01 +01:00
|
|
|
use super::basics::CellGrid;
|
2023-01-28 09:30:33 +01:00
|
|
|
use crate::model::events::EventsCollection;
|
|
|
|
|
|
|
|
#[cfg(feature = "tracing")]
|
|
|
|
extern crate lttng_ust;
|
|
|
|
|
|
|
|
#[cfg(feature = "tracing")]
|
|
|
|
use lttng_ust::import_tracepoints;
|
|
|
|
|
|
|
|
#[cfg(feature = "tracing")]
|
|
|
|
import_tracepoints!(
|
|
|
|
concat!(env!("OUT_DIR"), "/tracepoints.rs"),
|
|
|
|
tracepoints
|
|
|
|
);
|
2022-09-25 08:23:14 +02:00
|
|
|
|
2022-11-23 21:54:17 +01:00
|
|
|
const MONTH_NAMES: [&str;12] = [
|
|
|
|
"gen",
|
|
|
|
"feb",
|
|
|
|
"mar",
|
|
|
|
"apr",
|
|
|
|
"mag",
|
|
|
|
"giu",
|
|
|
|
"lug",
|
|
|
|
"ago",
|
|
|
|
"set",
|
|
|
|
"ott",
|
|
|
|
"nov",
|
|
|
|
"dic",
|
|
|
|
];
|
2022-08-02 08:04:48 +02:00
|
|
|
|
2022-12-25 12:14:01 +01:00
|
|
|
const DAY_NAMES: [&str;7] = ["LUN", "MAR", "MER", "GIO", "VEN", "SAB", "DOM"];
|
|
|
|
|
2022-08-02 08:04:48 +02:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct CalendarParams {
|
2022-10-16 14:51:37 +02:00
|
|
|
show_weeks: bool,
|
|
|
|
header_fg: Color,
|
|
|
|
header_bg: Color,
|
2022-12-27 09:29:29 +01:00
|
|
|
day_fg: Color,
|
|
|
|
day_other_month_fg: Color,
|
2022-10-16 14:51:37 +02:00
|
|
|
day_weekend_bg: Color,
|
|
|
|
day_today_bg: Color,
|
|
|
|
day_text_margin: f32,
|
2023-01-15 15:38:41 +01:00
|
|
|
|
2023-02-11 11:43:24 +01:00
|
|
|
ev_height: f32,
|
2023-01-15 15:38:41 +01:00
|
|
|
ev_bg: Color,
|
2023-02-11 11:43:24 +01:00
|
|
|
ev_fontsize: f32
|
2022-08-02 08:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CalendarParams {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
2022-10-16 14:51:37 +02:00
|
|
|
show_weeks: true,
|
|
|
|
header_fg: Color::BLACK,
|
|
|
|
header_bg: Color::TRANSPARENT,
|
|
|
|
day_today_bg: Color::from_rgb8(214, 242, 252),
|
2022-12-27 09:29:29 +01:00
|
|
|
day_fg: Color::BLACK,
|
|
|
|
day_other_month_fg: Color::from_rgb8(220, 220, 220),
|
2022-10-16 14:51:37 +02:00
|
|
|
day_weekend_bg: Color::from_rgb8(245, 245, 245),
|
|
|
|
day_text_margin: 5.0,
|
2023-02-11 11:43:24 +01:00
|
|
|
ev_height: 20.0,
|
|
|
|
ev_bg: Color::from_rgb8(200, 245, 200),
|
|
|
|
ev_fontsize: 16.0
|
2022-08-02 08:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-16 14:51:37 +02:00
|
|
|
|
2022-08-02 08:04:48 +02:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
2023-05-21 13:41:16 +02:00
|
|
|
fn render_events_in_day(
|
|
|
|
params: &CalendarParams,
|
|
|
|
renderer: &mut impl text::Renderer,
|
|
|
|
current_day: NaiveDate,
|
|
|
|
day_bounds: Rectangle,
|
|
|
|
font_size: f32,
|
|
|
|
fg: Color,
|
|
|
|
content: &str,
|
|
|
|
events: &EventsCollection
|
|
|
|
) {
|
|
|
|
// render events, if enough space
|
|
|
|
let day_text_size = renderer.measure(
|
|
|
|
content,
|
|
|
|
font_size,
|
|
|
|
LineHeight::default(),
|
|
|
|
renderer.default_font(),
|
|
|
|
day_bounds.size(),
|
|
|
|
Shaping::default());
|
|
|
|
let ev_height = params.ev_height;
|
|
|
|
let mut ev_y: f32 = 0.0;
|
|
|
|
let y = day_bounds.y + params.day_text_margin;
|
|
|
|
for ev_day in events.for_day(current_day) {
|
|
|
|
if day_bounds.height - day_text_size.1 > ev_y + ev_height {
|
|
|
|
let ev_bounds = Rectangle {
|
|
|
|
y: y + ev_y + day_text_size.1,
|
|
|
|
height: ev_height,
|
|
|
|
..day_bounds
|
|
|
|
};
|
|
|
|
renderer.fill_quad(renderer::Quad {
|
|
|
|
bounds: ev_bounds,
|
|
|
|
border_radius: 0.0.into(),
|
|
|
|
border_width: 1.0,
|
|
|
|
border_color: params.day_other_month_fg,
|
|
|
|
},
|
|
|
|
params.ev_bg);
|
|
|
|
renderer.fill_text(Text {
|
|
|
|
content: ev_day.text.as_str(),
|
|
|
|
bounds: ev_bounds,
|
|
|
|
size: params.ev_fontsize,
|
|
|
|
line_height: LineHeight::default(),
|
|
|
|
color: fg,
|
|
|
|
font: renderer.default_font(),
|
|
|
|
horizontal_alignment: alignment::Horizontal::Left,
|
|
|
|
vertical_alignment: alignment::Vertical::Top,
|
|
|
|
shaping: Shaping::default(),
|
|
|
|
});
|
|
|
|
ev_y += ev_height
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if day_bounds.height - day_text_size.1 > ev_height {
|
|
|
|
if let Some(ev_day) = events.is_any_in_day(current_day) {
|
|
|
|
let ev_bounds = Rectangle {
|
|
|
|
y: y + day_text_size.1,
|
|
|
|
height: ev_height,
|
|
|
|
..day_bounds
|
|
|
|
};
|
|
|
|
renderer.fill_quad(renderer::Quad {
|
|
|
|
bounds: ev_bounds,
|
|
|
|
border_radius: 0.0.into(),
|
|
|
|
border_width: 1.0,
|
|
|
|
border_color: params.day_other_month_fg,
|
|
|
|
},
|
|
|
|
params.ev_bg);
|
|
|
|
renderer.fill_text(Text {
|
|
|
|
content: ev_day.text.as_str(),
|
|
|
|
bounds: ev_bounds,
|
|
|
|
size: params.ev_fontsize,
|
|
|
|
line_height: LineHeight::default(),
|
|
|
|
color: fg,
|
|
|
|
font: renderer.default_font(),
|
|
|
|
horizontal_alignment: alignment::Horizontal::Left,
|
|
|
|
vertical_alignment: alignment::Vertical::Top,
|
|
|
|
shaping: Shaping::default(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
pub struct CalendarMonthView<'a> {
|
2022-09-25 08:23:14 +02:00
|
|
|
first_day: NaiveDate,
|
2022-10-16 14:51:37 +02:00
|
|
|
first_day_in_view: NaiveDate,
|
2022-08-02 08:04:48 +02:00
|
|
|
params: CalendarParams,
|
2022-09-25 08:23:14 +02:00
|
|
|
week_column_width: f32,
|
|
|
|
week_column_font_size: f32,
|
2023-05-21 13:41:16 +02:00
|
|
|
events: &'a EventsCollection,
|
2022-08-02 08:04:48 +02:00
|
|
|
}
|
|
|
|
|
2023-05-21 13:41:16 +02:00
|
|
|
impl<'a> CalendarMonthView<'a> {
|
|
|
|
pub fn new(params: &CalendarParams, day: NaiveDate, events: &'a EventsCollection) -> Self {
|
2022-10-16 14:51:37 +02:00
|
|
|
// first day of the month
|
|
|
|
let first_day = if day.day() == 1 {
|
|
|
|
day
|
|
|
|
} else {
|
2023-05-21 13:41:16 +02:00
|
|
|
NaiveDate::from_ymd_opt(day.year(), day.month(), 1).unwrap()
|
2022-10-16 14:51:37 +02:00
|
|
|
};
|
2022-12-25 12:14:01 +01:00
|
|
|
|
2022-10-16 14:51:37 +02:00
|
|
|
// weekday on first day of the month
|
|
|
|
let weekday_on_first = first_day.weekday();
|
|
|
|
|
|
|
|
// first visible day in the view
|
|
|
|
let first_day_in_view = first_day - Duration::days(weekday_on_first.num_days_from_monday() as i64);
|
|
|
|
|
|
|
|
Self {
|
2022-09-25 08:23:14 +02:00
|
|
|
first_day,
|
2022-10-16 14:51:37 +02:00
|
|
|
first_day_in_view,
|
2022-08-02 08:04:48 +02:00
|
|
|
params: params.clone(),
|
2022-09-25 08:23:14 +02:00
|
|
|
week_column_width: 30.0,
|
|
|
|
week_column_font_size: 18.0,
|
2023-05-21 13:41:16 +02:00
|
|
|
events,
|
2022-08-02 08:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-16 14:51:37 +02:00
|
|
|
pub fn set_month(&mut self, day: NaiveDate) {
|
|
|
|
// first day of the month
|
|
|
|
let first_day = if day.day() == 1 {
|
|
|
|
day
|
|
|
|
} else {
|
2023-05-21 13:41:16 +02:00
|
|
|
NaiveDate::from_ymd_opt(day.year(), day.month(), 1).unwrap()
|
2022-10-16 14:51:37 +02:00
|
|
|
};
|
2022-12-25 12:14:01 +01:00
|
|
|
|
2022-10-16 14:51:37 +02:00
|
|
|
// weekday on first day of the month
|
|
|
|
let weekday_on_first = first_day.weekday();
|
|
|
|
|
|
|
|
// first visible day in the view
|
|
|
|
let first_day_in_view = first_day - Duration::days(weekday_on_first.num_days_from_monday() as i64);
|
|
|
|
|
|
|
|
self.first_day = first_day;
|
|
|
|
self.first_day_in_view = first_day_in_view;
|
2022-08-02 08:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn draw_header(
|
|
|
|
&self,
|
|
|
|
renderer: &mut impl text::Renderer,
|
|
|
|
bounds: Rectangle,
|
2022-09-25 08:23:14 +02:00
|
|
|
week_w: f32,
|
2022-08-02 08:04:48 +02:00
|
|
|
) {
|
2022-09-25 08:23:14 +02:00
|
|
|
// paint background over full width
|
2022-10-16 14:51:37 +02:00
|
|
|
if self.params.header_bg != Color::TRANSPARENT {
|
|
|
|
renderer.fill_quad(renderer::Quad {
|
|
|
|
bounds,
|
2022-12-03 09:47:57 +01:00
|
|
|
border_radius: 0.0.into(),
|
2022-10-16 14:51:37 +02:00
|
|
|
border_width: 0.0,
|
|
|
|
border_color: Color::TRANSPARENT,
|
2022-12-25 12:14:01 +01:00
|
|
|
},
|
2022-10-16 14:51:37 +02:00
|
|
|
self.params.header_bg);
|
|
|
|
}
|
2022-12-25 12:14:01 +01:00
|
|
|
|
2022-09-25 08:23:14 +02:00
|
|
|
// redefine bounds to skip the week column
|
|
|
|
let bounds = Rectangle {
|
2022-12-25 12:14:01 +01:00
|
|
|
x: bounds.x + week_w,
|
|
|
|
y: bounds.y,
|
|
|
|
width: bounds.width - week_w,
|
2022-09-25 08:23:14 +02:00
|
|
|
height: bounds.height};
|
|
|
|
|
2022-08-02 08:04:48 +02:00
|
|
|
// font dimension
|
|
|
|
let font_size = renderer.default_size() as f32;
|
|
|
|
|
2022-12-25 12:14:01 +01:00
|
|
|
let h_axis = CellGrid::new(bounds.x, bounds.y, bounds.width, bounds.height, 7, 1);
|
2022-12-10 12:24:58 +01:00
|
|
|
|
2022-12-25 12:14:01 +01:00
|
|
|
for cell in h_axis {
|
|
|
|
let bounds = Rectangle {
|
|
|
|
x: cell.x + self.params.day_text_margin,
|
2022-12-10 12:24:58 +01:00
|
|
|
y: bounds.center_y(),
|
2022-12-25 12:14:01 +01:00
|
|
|
width: cell.width,
|
2022-12-10 12:24:58 +01:00
|
|
|
height: bounds.height
|
2022-08-02 08:04:48 +02:00
|
|
|
};
|
2022-12-10 12:24:58 +01:00
|
|
|
|
2022-08-02 08:04:48 +02:00
|
|
|
// label (day letter on row 0, day number on the rest)
|
2022-12-25 12:14:01 +01:00
|
|
|
let t = DAY_NAMES[cell.pos_x as usize];
|
2022-08-02 08:04:48 +02:00
|
|
|
|
|
|
|
// color of text
|
2022-10-16 14:51:37 +02:00
|
|
|
let fg = self.params.header_fg;
|
2022-08-02 08:04:48 +02:00
|
|
|
|
2023-05-21 13:41:16 +02:00
|
|
|
renderer.fill_text(Text {
|
2022-08-02 08:04:48 +02:00
|
|
|
content : t,
|
2022-12-10 12:24:58 +01:00
|
|
|
bounds,
|
2023-05-21 13:41:16 +02:00
|
|
|
size: font_size,
|
|
|
|
line_height: LineHeight::default(),
|
2022-08-02 08:04:48 +02:00
|
|
|
color: fg,
|
2023-05-21 13:41:16 +02:00
|
|
|
font: renderer.default_font(),
|
|
|
|
horizontal_alignment: alignment::Horizontal::Right,
|
2022-08-02 08:04:48 +02:00
|
|
|
vertical_alignment: alignment::Vertical::Center,
|
2023-05-21 13:41:16 +02:00
|
|
|
shaping: Shaping::default(),
|
2022-08-02 08:04:48 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 08:23:14 +02:00
|
|
|
fn draw_week_column(
|
|
|
|
&self,
|
|
|
|
renderer: &mut impl text::Renderer,
|
|
|
|
bounds: Rectangle,
|
|
|
|
) {
|
2022-10-16 14:51:37 +02:00
|
|
|
let mut day = self.first_day;
|
|
|
|
|
2022-12-25 12:14:01 +01:00
|
|
|
let v_axis = CellGrid::new(bounds.x, bounds.y, self.week_column_width, bounds.height, 1, 6);
|
|
|
|
for cell in v_axis {
|
2022-09-25 08:23:14 +02:00
|
|
|
// where to place the week number
|
2022-12-25 12:14:01 +01:00
|
|
|
let day_bounds = Rectangle {
|
|
|
|
x: bounds.x,
|
|
|
|
y: cell.y + self.params.day_text_margin,
|
|
|
|
width: cell.width,
|
|
|
|
height: cell.height
|
2022-09-25 08:23:14 +02:00
|
|
|
};
|
|
|
|
|
2022-10-16 14:51:37 +02:00
|
|
|
let week_of_first_day_of_month = day.iso_week().week();
|
|
|
|
day += Duration::weeks(1);
|
|
|
|
|
2022-09-25 08:23:14 +02:00
|
|
|
// render week cell text
|
2023-05-21 13:41:16 +02:00
|
|
|
renderer.fill_text(Text {
|
2022-10-16 14:51:37 +02:00
|
|
|
content : &(week_of_first_day_of_month).to_string(),
|
2022-09-25 08:23:14 +02:00
|
|
|
bounds: Rectangle {
|
|
|
|
x: day_bounds.center_x(),
|
|
|
|
y: day_bounds.y,
|
|
|
|
..day_bounds
|
|
|
|
},
|
2023-05-21 13:41:16 +02:00
|
|
|
size: self.week_column_font_size,
|
|
|
|
line_height: LineHeight::default(),
|
2022-12-27 09:29:29 +01:00
|
|
|
color: self.params.day_fg,
|
2023-05-21 13:41:16 +02:00
|
|
|
font: renderer.default_font(),
|
2022-09-25 08:23:14 +02:00
|
|
|
horizontal_alignment: alignment::Horizontal::Center,
|
|
|
|
vertical_alignment: alignment::Vertical::Top,
|
2023-05-21 13:41:16 +02:00
|
|
|
shaping: Shaping::default(),
|
2022-09-25 08:23:14 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 08:04:48 +02:00
|
|
|
fn draw_days(
|
|
|
|
&self,
|
|
|
|
renderer: &mut impl text::Renderer,
|
|
|
|
bounds: Rectangle,
|
|
|
|
) {
|
|
|
|
// font dimension
|
|
|
|
let font_size = renderer.default_size() as f32;
|
|
|
|
|
2022-10-16 14:51:37 +02:00
|
|
|
let mut current_day = self.first_day_in_view;
|
2022-12-10 12:24:58 +01:00
|
|
|
|
2022-12-25 12:14:01 +01:00
|
|
|
let grid = CellGrid::new(bounds.x, bounds.y, bounds.width, bounds.height, 7, 6);
|
|
|
|
for cell in grid {
|
|
|
|
let day_bounds = Rectangle {
|
|
|
|
x: cell.x,
|
|
|
|
y: cell.y,
|
|
|
|
width: cell.width,
|
|
|
|
height : cell.height
|
|
|
|
};
|
2022-08-02 08:04:48 +02:00
|
|
|
|
2022-12-25 12:14:01 +01:00
|
|
|
// label (day letter on row 0, day number on the rest)
|
|
|
|
let t = current_day.day().to_string();
|
|
|
|
let content = t.as_str();
|
2022-10-16 14:51:37 +02:00
|
|
|
|
2022-12-25 12:14:01 +01:00
|
|
|
// color of text
|
|
|
|
let fg = if current_day.month() == self.first_day.month() {
|
2022-12-27 09:29:29 +01:00
|
|
|
self.params.day_fg
|
2022-12-25 12:14:01 +01:00
|
|
|
} else {
|
2022-12-27 09:29:29 +01:00
|
|
|
self.params.day_other_month_fg
|
2022-12-25 12:14:01 +01:00
|
|
|
};
|
2022-08-02 08:04:48 +02:00
|
|
|
|
2022-12-25 12:14:01 +01:00
|
|
|
// background color of the day cell
|
2023-05-21 13:41:16 +02:00
|
|
|
let bg_color = if current_day == Local::now().date_naive() {
|
2022-12-25 12:14:01 +01:00
|
|
|
self.params.day_today_bg
|
|
|
|
} else if current_day.weekday().num_days_from_monday() > 4 {
|
|
|
|
self.params.day_weekend_bg
|
|
|
|
} else {
|
|
|
|
Color::TRANSPARENT
|
|
|
|
};
|
2022-09-25 08:23:14 +02:00
|
|
|
|
2022-12-25 12:14:01 +01:00
|
|
|
// where to place the day content
|
|
|
|
let x = day_bounds.x + self.params.day_text_margin;
|
|
|
|
let y = day_bounds.y + self.params.day_text_margin;
|
2022-09-25 08:23:14 +02:00
|
|
|
|
2022-12-25 12:14:01 +01:00
|
|
|
renderer.fill_quad(renderer::Quad {
|
|
|
|
bounds: Rectangle {width: day_bounds.width + 0.5, height: day_bounds.height + 0.5, ..day_bounds},
|
|
|
|
border_radius: 0.0.into(),
|
|
|
|
border_width: 1.0,
|
2022-12-27 09:29:29 +01:00
|
|
|
border_color: self.params.day_other_month_fg,
|
2022-12-25 12:14:01 +01:00
|
|
|
},
|
|
|
|
bg_color);
|
|
|
|
|
|
|
|
// render day cell text
|
2023-05-21 13:41:16 +02:00
|
|
|
renderer.fill_text(Text {
|
2022-12-25 12:14:01 +01:00
|
|
|
content,
|
|
|
|
bounds: Rectangle {x, y, ..day_bounds},
|
2023-05-21 13:41:16 +02:00
|
|
|
size: font_size,
|
|
|
|
line_height: LineHeight::default(),
|
2022-12-25 12:14:01 +01:00
|
|
|
color: fg,
|
2023-05-21 13:41:16 +02:00
|
|
|
font: renderer.default_font(),
|
2022-12-25 12:14:01 +01:00
|
|
|
horizontal_alignment: alignment::Horizontal::Left,
|
|
|
|
vertical_alignment: alignment::Vertical::Top,
|
2023-05-21 13:41:16 +02:00
|
|
|
shaping: Shaping::default()
|
2022-12-25 12:14:01 +01:00
|
|
|
});
|
2023-05-21 13:41:16 +02:00
|
|
|
|
|
|
|
render_events_in_day(
|
|
|
|
&self.params,
|
|
|
|
renderer,
|
|
|
|
current_day,
|
|
|
|
day_bounds,
|
|
|
|
font_size,
|
|
|
|
fg,
|
|
|
|
content,
|
|
|
|
self.events
|
|
|
|
);
|
|
|
|
|
|
|
|
current_day = current_day.succ_opt().unwrap();
|
2022-08-02 08:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // CalendarMonthView
|
|
|
|
|
2023-05-21 13:41:16 +02:00
|
|
|
impl<Message, Renderer> Widget<Message, Renderer> for CalendarMonthView<'_>
|
2022-08-02 08:04:48 +02:00
|
|
|
where
|
|
|
|
Renderer: text::Renderer,
|
|
|
|
{
|
|
|
|
fn width(&self) -> Length {
|
|
|
|
Length::Shrink
|
|
|
|
}
|
|
|
|
|
|
|
|
fn height(&self) -> Length {
|
|
|
|
Length::Shrink
|
|
|
|
}
|
|
|
|
|
|
|
|
fn layout(
|
|
|
|
&self,
|
|
|
|
_renderer: &Renderer,
|
|
|
|
limits: &layout::Limits,
|
|
|
|
) -> layout::Node {
|
|
|
|
layout::Node::new(limits.max())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn draw(
|
|
|
|
&self,
|
2022-08-15 12:05:00 +02:00
|
|
|
_state: &Tree,
|
2022-08-02 08:04:48 +02:00
|
|
|
renderer: &mut Renderer,
|
|
|
|
_theme: &Renderer::Theme,
|
|
|
|
_style: &renderer::Style,
|
2023-05-21 13:41:16 +02:00
|
|
|
layout: layout::Layout<'_>,
|
2022-08-02 08:04:48 +02:00
|
|
|
_cursor_position: Point,
|
|
|
|
_viewport: &Rectangle,
|
|
|
|
) {
|
2022-12-25 12:14:01 +01:00
|
|
|
let bounds = layout.bounds();
|
2022-08-02 08:04:48 +02:00
|
|
|
let margin: f32 = 20.0;
|
|
|
|
|
2022-09-25 08:23:14 +02:00
|
|
|
// week column only visible if there is enough space
|
2022-12-25 12:14:01 +01:00
|
|
|
let week_w = if self.params.show_weeks && bounds.width > self.week_column_width {
|
2022-09-25 08:23:14 +02:00
|
|
|
self.week_column_width
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
};
|
|
|
|
|
2022-08-02 08:04:48 +02:00
|
|
|
// font and header dimension
|
|
|
|
let font_size = renderer.default_size() as f32;
|
|
|
|
let first_row_h = font_size + margin;
|
|
|
|
|
|
|
|
// header
|
2022-12-25 12:14:01 +01:00
|
|
|
self.draw_header(renderer, Rectangle {height: first_row_h, ..bounds}, week_w);
|
2022-09-25 08:23:14 +02:00
|
|
|
|
|
|
|
// week column
|
|
|
|
if week_w > 0.0 {
|
2022-12-25 12:14:01 +01:00
|
|
|
let x = bounds.x;
|
|
|
|
let y = bounds.y + first_row_h;
|
2022-09-25 08:23:14 +02:00
|
|
|
let width = self.week_column_width;
|
2022-12-25 12:14:01 +01:00
|
|
|
let height = bounds.height - first_row_h;
|
2022-09-25 08:23:14 +02:00
|
|
|
self.draw_week_column(renderer, Rectangle{x, y, width, height});
|
|
|
|
}
|
2022-12-25 12:14:01 +01:00
|
|
|
|
2022-08-02 08:04:48 +02:00
|
|
|
// monthly calendar cells
|
2022-12-25 12:14:01 +01:00
|
|
|
let x = bounds.x + week_w;
|
|
|
|
let y = bounds.y + first_row_h;
|
|
|
|
let width = bounds.width - week_w;
|
|
|
|
let height = bounds.height - first_row_h;
|
2022-08-02 08:04:48 +02:00
|
|
|
self.draw_days(renderer, Rectangle{x, y, width, height});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-21 13:41:16 +02:00
|
|
|
impl<'a, Message, Renderer> From<CalendarMonthView<'a>> for Element<'a, Message, Renderer>
|
2022-08-02 08:04:48 +02:00
|
|
|
where
|
|
|
|
Renderer: text::Renderer,
|
|
|
|
{
|
2023-05-21 13:41:16 +02:00
|
|
|
fn from(month_view: CalendarMonthView<'a>) -> Self {
|
2022-11-23 21:54:17 +01:00
|
|
|
Self::new(month_view)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
2023-01-15 15:38:41 +01:00
|
|
|
pub struct CalendarYearView<'a> {
|
2022-11-23 21:54:17 +01:00
|
|
|
first_day: NaiveDate,
|
|
|
|
first_day_in_view: NaiveDate,
|
|
|
|
params: CalendarParams,
|
|
|
|
month_column_font_size: f32,
|
|
|
|
margin: f32,
|
2023-01-15 15:38:41 +01:00
|
|
|
events: &'a EventsCollection,
|
2022-11-23 21:54:17 +01:00
|
|
|
}
|
|
|
|
|
2023-01-15 15:38:41 +01:00
|
|
|
impl<'a> CalendarYearView<'a> {
|
|
|
|
pub fn new(params: &CalendarParams, day: NaiveDate, events: &'a EventsCollection) -> Self {
|
2022-11-23 21:54:17 +01:00
|
|
|
// first day of the year
|
2023-05-21 13:41:16 +02:00
|
|
|
let first_day = NaiveDate::from_ymd_opt(day.year(), 1, 1).unwrap();
|
2022-12-25 12:14:01 +01:00
|
|
|
|
2022-11-23 21:54:17 +01:00
|
|
|
// weekday on first day of the year
|
|
|
|
let weekday_on_first = first_day.weekday();
|
|
|
|
|
|
|
|
// first visible day in the view
|
|
|
|
let first_day_in_view = first_day - Duration::days(weekday_on_first.num_days_from_monday() as i64);
|
|
|
|
|
|
|
|
Self {
|
|
|
|
first_day,
|
|
|
|
first_day_in_view,
|
|
|
|
params: params.clone(),
|
|
|
|
month_column_font_size: 24.0,
|
2023-01-15 15:38:41 +01:00
|
|
|
margin: 10.0,
|
|
|
|
events
|
2022-11-23 21:54:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_year(&mut self, day: NaiveDate) {
|
|
|
|
// first day of the year
|
2023-05-21 13:41:16 +02:00
|
|
|
self.first_day = NaiveDate::from_ymd_opt(day.year(), 1, 1).unwrap();
|
2022-12-25 12:14:01 +01:00
|
|
|
|
2022-11-23 21:54:17 +01:00
|
|
|
// weekday on first day of the year
|
2022-12-27 09:29:29 +01:00
|
|
|
let weekday_on_first = self.first_day.weekday();
|
2022-11-23 21:54:17 +01:00
|
|
|
|
|
|
|
// first visible day in the view
|
2022-12-27 09:29:29 +01:00
|
|
|
self.first_day_in_view = self.first_day - Duration::days(weekday_on_first.num_days_from_monday() as i64);
|
2022-11-23 21:54:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn draw_header(
|
|
|
|
&self,
|
|
|
|
renderer: &mut impl text::Renderer,
|
|
|
|
bounds: Rectangle,
|
|
|
|
week_w: f32,
|
|
|
|
) {
|
|
|
|
// paint background over full width
|
|
|
|
if self.params.header_bg != Color::TRANSPARENT {
|
|
|
|
renderer.fill_quad(renderer::Quad {
|
|
|
|
bounds,
|
2022-12-03 09:47:57 +01:00
|
|
|
border_radius: 0.0.into(),
|
2022-11-23 21:54:17 +01:00
|
|
|
border_width: 0.0,
|
|
|
|
border_color: Color::TRANSPARENT,
|
2022-12-25 12:14:01 +01:00
|
|
|
},
|
2022-11-23 21:54:17 +01:00
|
|
|
self.params.header_bg);
|
|
|
|
}
|
2022-12-25 12:14:01 +01:00
|
|
|
|
2022-11-23 21:54:17 +01:00
|
|
|
// redefine bounds to skip the week column
|
|
|
|
let bounds = Rectangle {
|
2022-12-25 12:14:01 +01:00
|
|
|
x: bounds.x + week_w,
|
|
|
|
y: bounds.y,
|
|
|
|
width: bounds.width - week_w,
|
2022-11-23 21:54:17 +01:00
|
|
|
height: bounds.height};
|
|
|
|
|
|
|
|
// font dimension
|
|
|
|
let font_size = renderer.default_size() as f32;
|
|
|
|
|
|
|
|
let days_of_week = ["L", "M", "M", "G", "V", "S", "D"];
|
|
|
|
|
2022-12-25 12:14:01 +01:00
|
|
|
let grid = CellGrid::new(bounds.x, bounds.y, bounds.width, bounds.height, 42, 1);
|
|
|
|
for cell in grid {
|
|
|
|
let bounds = Rectangle {
|
|
|
|
x: cell.x + 0.5,
|
|
|
|
y: cell.y + 0.5,
|
|
|
|
width: cell.width,
|
|
|
|
height: cell.height
|
2022-11-23 21:54:17 +01:00
|
|
|
};
|
|
|
|
|
2022-12-25 12:14:01 +01:00
|
|
|
let weekday = (cell.pos_x as usize) % 7;
|
2022-11-23 21:54:17 +01:00
|
|
|
|
|
|
|
// background color of the day cell
|
|
|
|
let bg_color = if weekday > 4 {
|
|
|
|
self.params.day_weekend_bg
|
|
|
|
} else {
|
|
|
|
Color::TRANSPARENT
|
|
|
|
};
|
|
|
|
|
|
|
|
renderer.fill_quad(renderer::Quad {
|
|
|
|
bounds,
|
2022-12-03 09:47:57 +01:00
|
|
|
border_radius: 0.0.into(),
|
2022-12-10 12:24:58 +01:00
|
|
|
border_width: 0.0,
|
2022-11-23 21:54:17 +01:00
|
|
|
border_color: Color::TRANSPARENT,
|
2022-12-25 12:14:01 +01:00
|
|
|
},
|
2022-11-23 21:54:17 +01:00
|
|
|
bg_color);
|
|
|
|
|
|
|
|
// label (day letter on row 0, day number on the rest)
|
|
|
|
let t = days_of_week[weekday];
|
|
|
|
|
|
|
|
// color of text
|
|
|
|
let fg = self.params.header_fg;
|
|
|
|
|
|
|
|
let x = bounds.x + self.params.day_text_margin;
|
|
|
|
let y = bounds.center_y();
|
2023-05-21 13:41:16 +02:00
|
|
|
renderer.fill_text(Text {
|
2022-11-23 21:54:17 +01:00
|
|
|
content : t,
|
|
|
|
bounds: Rectangle {x, y, ..bounds},
|
2023-05-21 13:41:16 +02:00
|
|
|
size: font_size,
|
|
|
|
line_height: LineHeight::default(),
|
2022-11-23 21:54:17 +01:00
|
|
|
color: fg,
|
2023-05-21 13:41:16 +02:00
|
|
|
font: renderer.default_font(),
|
2022-11-23 21:54:17 +01:00
|
|
|
horizontal_alignment: alignment::Horizontal::Left,
|
|
|
|
vertical_alignment: alignment::Vertical::Center,
|
2023-05-21 13:41:16 +02:00
|
|
|
shaping: Shaping::default(),
|
2022-11-23 21:54:17 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn draw_month_column(
|
|
|
|
&self,
|
|
|
|
renderer: &mut impl text::Renderer,
|
|
|
|
bounds: Rectangle,
|
|
|
|
) {
|
|
|
|
// dimensions of each box representing a month name
|
|
|
|
let h: f32 = bounds.height / 12.0;
|
|
|
|
|
|
|
|
for month in 0..12usize {
|
|
|
|
// where to place the month name
|
2022-12-25 12:14:01 +01:00
|
|
|
let month_name_bounds = Rectangle {
|
|
|
|
x: bounds.x,
|
|
|
|
y: (month as f32) * h + bounds.y + self.params.day_text_margin,
|
|
|
|
width: bounds.width,
|
|
|
|
height: h
|
2022-11-23 21:54:17 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// render month name
|
2023-05-21 13:41:16 +02:00
|
|
|
renderer.fill_text(Text {
|
2022-11-23 21:54:17 +01:00
|
|
|
content : MONTH_NAMES[month],
|
|
|
|
bounds: Rectangle {
|
|
|
|
x: month_name_bounds.x + self.margin,
|
|
|
|
y: month_name_bounds.center_y(),
|
|
|
|
..month_name_bounds
|
|
|
|
},
|
2023-05-21 13:41:16 +02:00
|
|
|
size: self.month_column_font_size,
|
|
|
|
line_height: LineHeight::default(),
|
2022-12-27 09:29:29 +01:00
|
|
|
color: self.params.day_fg,
|
2023-05-21 13:41:16 +02:00
|
|
|
font: renderer.default_font(),
|
2022-11-23 21:54:17 +01:00
|
|
|
horizontal_alignment: alignment::Horizontal::Left,
|
|
|
|
vertical_alignment: alignment::Vertical::Center,
|
2023-05-21 13:41:16 +02:00
|
|
|
shaping: Shaping::default(),
|
2022-11-23 21:54:17 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn draw_days(
|
|
|
|
&self,
|
|
|
|
renderer: &mut impl text::Renderer,
|
|
|
|
bounds: Rectangle,
|
|
|
|
) {
|
|
|
|
// font dimension
|
|
|
|
let font_size = renderer.default_size() as f32;
|
|
|
|
|
2022-12-25 12:14:01 +01:00
|
|
|
let grid = CellGrid::new(bounds.x, bounds.y, bounds.width, bounds.height, 42, 12);
|
|
|
|
for cell in grid {
|
|
|
|
let day_bounds = Rectangle {
|
|
|
|
x: cell.x + 0.5,
|
|
|
|
y: cell.y + 0.5,
|
|
|
|
width: cell.width,
|
|
|
|
height: cell.height
|
2022-11-23 21:54:17 +01:00
|
|
|
};
|
|
|
|
|
2022-12-25 12:14:01 +01:00
|
|
|
// the row index is the month
|
|
|
|
let month = cell.pos_y;
|
|
|
|
let first_day_of_month = self.first_day.with_day0(0).unwrap().with_month0(month).unwrap();
|
2022-11-23 21:54:17 +01:00
|
|
|
|
2022-12-25 12:14:01 +01:00
|
|
|
let first_weekday = first_day_of_month.weekday().num_days_from_monday();
|
|
|
|
let current_day = first_day_of_month + Duration::days((cell.pos_x as i64) - (first_weekday as i64));
|
2022-11-23 21:54:17 +01:00
|
|
|
|
2022-12-25 12:14:01 +01:00
|
|
|
if current_day.month0() == month {
|
|
|
|
let weekday = current_day.weekday().num_days_from_monday();
|
2022-11-23 21:54:17 +01:00
|
|
|
|
2022-12-25 12:14:01 +01:00
|
|
|
// label (day letter on row 0, day number on the rest)
|
|
|
|
let t = current_day.day().to_string();
|
|
|
|
let content = t.as_str();
|
2022-11-23 21:54:17 +01:00
|
|
|
|
2022-12-25 12:14:01 +01:00
|
|
|
// color of text
|
2022-12-27 09:29:29 +01:00
|
|
|
let fg = self.params.day_fg;
|
2022-12-25 12:14:01 +01:00
|
|
|
|
|
|
|
// background color of the day cell
|
2023-05-21 13:41:16 +02:00
|
|
|
let bg_color = if current_day == Local::now().date_naive() {
|
2022-12-25 12:14:01 +01:00
|
|
|
self.params.day_today_bg
|
|
|
|
} else if weekday > 4 {
|
|
|
|
self.params.day_weekend_bg
|
|
|
|
} else {
|
|
|
|
Color::TRANSPARENT
|
|
|
|
};
|
|
|
|
|
|
|
|
// where to place the day content
|
|
|
|
let x = day_bounds.x + self.params.day_text_margin;
|
|
|
|
let y = day_bounds.y + self.params.day_text_margin;
|
|
|
|
|
|
|
|
renderer.fill_quad(renderer::Quad {
|
|
|
|
bounds: day_bounds,
|
|
|
|
border_radius: 0.0.into(),
|
|
|
|
border_width: 1.0,
|
2022-12-27 09:29:29 +01:00
|
|
|
border_color: self.params.day_other_month_fg,
|
2022-12-25 12:14:01 +01:00
|
|
|
},
|
|
|
|
bg_color);
|
|
|
|
|
|
|
|
// render day cell text
|
2023-05-21 13:41:16 +02:00
|
|
|
renderer.fill_text(Text {
|
2022-12-25 12:14:01 +01:00
|
|
|
content,
|
|
|
|
bounds: Rectangle {x, y, ..day_bounds},
|
2023-05-21 13:41:16 +02:00
|
|
|
size: font_size,
|
|
|
|
line_height: LineHeight::default(),
|
2022-12-25 12:14:01 +01:00
|
|
|
color: fg,
|
2023-05-21 13:41:16 +02:00
|
|
|
font: renderer.default_font(),
|
2022-12-25 12:14:01 +01:00
|
|
|
horizontal_alignment: alignment::Horizontal::Left,
|
|
|
|
vertical_alignment: alignment::Vertical::Top,
|
2023-05-21 13:41:16 +02:00
|
|
|
shaping: Shaping::default(),
|
2022-12-25 12:14:01 +01:00
|
|
|
});
|
2023-01-28 09:30:33 +01:00
|
|
|
|
2023-05-21 13:41:16 +02:00
|
|
|
render_events_in_day(
|
|
|
|
&self.params,
|
|
|
|
renderer,
|
|
|
|
current_day,
|
|
|
|
day_bounds,
|
|
|
|
font_size,
|
|
|
|
fg,
|
|
|
|
content,
|
|
|
|
self.events
|
|
|
|
);
|
2022-12-25 12:14:01 +01:00
|
|
|
}
|
2022-11-23 21:54:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compute_month_col_width(&self, renderer: &mut impl text::Renderer) -> f32 {
|
|
|
|
let mut max_max_font_width = 0.0;
|
|
|
|
for month_name in MONTH_NAMES {
|
2023-05-21 13:41:16 +02:00
|
|
|
let month_width = renderer.measure_width(
|
|
|
|
month_name,
|
|
|
|
self.month_column_font_size,
|
|
|
|
renderer.default_font(),
|
|
|
|
Shaping::default());
|
2022-11-23 21:54:17 +01:00
|
|
|
if month_width > max_max_font_width {
|
|
|
|
max_max_font_width = month_width;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return max_max_font_width;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // CalendarYearView
|
|
|
|
|
2023-01-15 15:38:41 +01:00
|
|
|
impl<Message, Renderer> Widget<Message, Renderer> for CalendarYearView<'_>
|
2022-11-23 21:54:17 +01:00
|
|
|
where
|
|
|
|
Renderer: text::Renderer,
|
|
|
|
{
|
|
|
|
fn width(&self) -> Length {
|
|
|
|
Length::Shrink
|
|
|
|
}
|
|
|
|
|
|
|
|
fn height(&self) -> Length {
|
|
|
|
Length::Shrink
|
|
|
|
}
|
|
|
|
|
|
|
|
fn layout(
|
|
|
|
&self,
|
|
|
|
_renderer: &Renderer,
|
|
|
|
limits: &layout::Limits,
|
|
|
|
) -> layout::Node {
|
|
|
|
layout::Node::new(limits.max())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn draw(
|
|
|
|
&self,
|
|
|
|
_state: &Tree,
|
|
|
|
renderer: &mut Renderer,
|
|
|
|
_theme: &Renderer::Theme,
|
|
|
|
_style: &renderer::Style,
|
2023-05-21 13:41:16 +02:00
|
|
|
layout: layout::Layout<'_>,
|
2022-11-23 21:54:17 +01:00
|
|
|
_cursor_position: Point,
|
|
|
|
_viewport: &Rectangle,
|
|
|
|
) {
|
2022-12-25 12:14:01 +01:00
|
|
|
let bounds = layout.bounds();
|
2022-11-23 21:54:17 +01:00
|
|
|
let margin: f32 = 20.0;
|
2023-01-28 09:30:33 +01:00
|
|
|
#[cfg(feature = "tracing")]
|
|
|
|
tracepoints::calendar::draw_entry(bounds.width as i32, bounds.height as i32);
|
2022-11-23 21:54:17 +01:00
|
|
|
|
|
|
|
// week column only visible if there is enough space
|
|
|
|
let month_w = if self.params.show_weeks {
|
|
|
|
//self.month_column_width
|
|
|
|
self.compute_month_col_width(renderer) + self.margin
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
};
|
|
|
|
|
|
|
|
// font and header dimension
|
|
|
|
let font_size = renderer.default_size() as f32;
|
|
|
|
let first_row_h = font_size + margin;
|
|
|
|
|
|
|
|
// header
|
2022-12-25 12:14:01 +01:00
|
|
|
self.draw_header(renderer, Rectangle {height: first_row_h, ..bounds}, month_w);
|
2022-11-23 21:54:17 +01:00
|
|
|
|
|
|
|
// month column
|
2022-12-25 12:14:01 +01:00
|
|
|
if month_w > 0.0 && bounds.width > month_w {
|
|
|
|
let x = bounds.x;
|
|
|
|
let y = bounds.y + first_row_h;
|
2022-11-23 21:54:17 +01:00
|
|
|
let width = month_w;
|
2022-12-25 12:14:01 +01:00
|
|
|
let height = bounds.height - first_row_h;
|
2022-11-23 21:54:17 +01:00
|
|
|
self.draw_month_column(renderer, Rectangle{x, y, width, height});
|
|
|
|
}
|
2022-12-25 12:14:01 +01:00
|
|
|
|
2022-11-23 21:54:17 +01:00
|
|
|
// monthly calendar cells
|
2022-12-25 12:14:01 +01:00
|
|
|
let x = bounds.x + month_w;
|
|
|
|
let y = bounds.y + first_row_h;
|
|
|
|
let width = bounds.width - month_w;
|
|
|
|
let height = bounds.height - first_row_h;
|
2022-11-23 21:54:17 +01:00
|
|
|
self.draw_days(renderer, Rectangle{x, y, width, height});
|
2023-01-28 09:30:33 +01:00
|
|
|
#[cfg(feature = "tracing")]
|
|
|
|
tracepoints::calendar::draw_exit(bounds.width as i32, bounds.height as i32);
|
2022-11-23 21:54:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-15 15:38:41 +01:00
|
|
|
impl<'a, Message, Renderer> From<CalendarYearView<'a>> for Element<'a, Message, Renderer>
|
2022-11-23 21:54:17 +01:00
|
|
|
where
|
|
|
|
Renderer: text::Renderer,
|
|
|
|
{
|
2023-01-15 15:38:41 +01:00
|
|
|
fn from(year_view: CalendarYearView<'a>) -> Self {
|
2022-11-23 21:54:17 +01:00
|
|
|
Self::new(year_view)
|
2022-08-02 08:04:48 +02:00
|
|
|
}
|
|
|
|
}
|