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.
|
|
|
|
|
|
|
|
use iced_native::layout::{self, Layout};
|
|
|
|
use iced_native::renderer;
|
|
|
|
use iced_native::{Color, Element, Length, Point, Rectangle, Size, Widget};
|
|
|
|
use iced_native::text;
|
|
|
|
use iced_native::alignment;
|
2022-08-15 12:05:00 +02:00
|
|
|
use iced_native::widget::Tree;
|
2022-10-16 14:51:37 +02:00
|
|
|
use chrono::{NaiveDate, Datelike, Duration, Weekday, Local};
|
2022-09-25 08:23:14 +02:00
|
|
|
|
|
|
|
//use std::cmp;
|
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,
|
|
|
|
day_text: Color,
|
|
|
|
day_text_other_month: Color,
|
|
|
|
day_weekend_bg: Color,
|
|
|
|
day_today_bg: Color,
|
|
|
|
day_text_margin: 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),
|
|
|
|
day_text: Color::BLACK,
|
|
|
|
day_text_other_month: Color::from_rgb8(220, 220, 220),
|
|
|
|
// day_background: Color::from_rgb8(230, 230, 255),
|
|
|
|
day_weekend_bg: Color::from_rgb8(245, 245, 245),
|
|
|
|
day_text_margin: 5.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
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
pub struct CalendarMonthView {
|
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-10-16 14:51:37 +02:00
|
|
|
weekday_on_first: Weekday,
|
2022-09-25 08:23:14 +02:00
|
|
|
week_column_width: f32,
|
|
|
|
week_column_font_size: f32,
|
2022-08-02 08:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CalendarMonthView {
|
2022-10-16 14:51:37 +02:00
|
|
|
pub fn new(params: &CalendarParams, day: NaiveDate) -> Self {
|
|
|
|
// first day of the month
|
|
|
|
let first_day = if day.day() == 1 {
|
|
|
|
day
|
|
|
|
} else {
|
|
|
|
NaiveDate::from_ymd(day.year(), day.month(), 1)
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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-10-16 14:51:37 +02:00
|
|
|
weekday_on_first,
|
2022-09-25 08:23:14 +02:00
|
|
|
week_column_width: 30.0,
|
|
|
|
week_column_font_size: 18.0,
|
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 {
|
|
|
|
NaiveDate::from_ymd(day.year(), day.month(), 1)
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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.weekday_on_first = weekday_on_first;
|
|
|
|
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,
|
|
|
|
border_radius: 0.0,
|
|
|
|
border_width: 0.0,
|
|
|
|
border_color: Color::TRANSPARENT,
|
|
|
|
},
|
|
|
|
self.params.header_bg);
|
|
|
|
}
|
2022-09-25 08:23:14 +02:00
|
|
|
|
|
|
|
// redefine bounds to skip the week column
|
|
|
|
let bounds = Rectangle {
|
|
|
|
x: bounds.x + week_w,
|
|
|
|
y: bounds.y,
|
|
|
|
width: bounds.width - week_w,
|
|
|
|
height: bounds.height};
|
|
|
|
|
2022-08-02 08:04:48 +02:00
|
|
|
let origin = bounds.position();
|
|
|
|
|
|
|
|
// font dimension
|
|
|
|
let font_size = renderer.default_size() as f32;
|
|
|
|
|
|
|
|
// dimensions of each box representing a day
|
|
|
|
let w: f32 = bounds.width / 7.0;
|
|
|
|
let h: f32 = bounds.height;
|
|
|
|
|
2022-09-25 08:23:14 +02:00
|
|
|
let days_of_week = ["LUN", "MAR", "MER", "GIO", "VEN", "SAB", "DOM"];
|
2022-08-02 08:04:48 +02:00
|
|
|
|
|
|
|
for weekday in 0..7i32 {
|
|
|
|
let bounds = Rectangle {
|
|
|
|
x: (weekday as f32) * w + origin.x,
|
|
|
|
y: origin.y,
|
|
|
|
width: w,
|
|
|
|
height: h
|
|
|
|
};
|
|
|
|
|
|
|
|
// label (day letter on row 0, day number on the rest)
|
|
|
|
let t = days_of_week[weekday as usize];
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2022-10-16 14:51:37 +02:00
|
|
|
let x = bounds.x + self.params.day_text_margin;
|
2022-08-02 08:04:48 +02:00
|
|
|
let y = bounds.center_y();
|
|
|
|
renderer.fill_text(text::Text {
|
|
|
|
content : t,
|
|
|
|
size: font_size,
|
|
|
|
bounds: Rectangle {x, y, ..bounds},
|
|
|
|
color: fg,
|
|
|
|
font: Default::default(),
|
2022-09-25 08:23:14 +02:00
|
|
|
horizontal_alignment: alignment::Horizontal::Left,
|
2022-08-02 08:04:48 +02:00
|
|
|
vertical_alignment: alignment::Vertical::Center,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 08:23:14 +02:00
|
|
|
fn draw_week_column(
|
|
|
|
&self,
|
|
|
|
renderer: &mut impl text::Renderer,
|
|
|
|
bounds: Rectangle,
|
|
|
|
) {
|
|
|
|
// dimensions of each box representing a week number
|
|
|
|
let h: f32 = bounds.height / 6.0;
|
|
|
|
let r: f32 = if h > self.week_column_width {
|
|
|
|
self.week_column_width
|
|
|
|
} else {
|
|
|
|
h
|
|
|
|
} / 2.0;
|
|
|
|
|
2022-10-16 14:51:37 +02:00
|
|
|
let mut day = self.first_day;
|
|
|
|
|
|
|
|
for week in 0..6u32 {
|
2022-09-25 08:23:14 +02:00
|
|
|
// where to place the week number
|
|
|
|
let day_bounds = Rectangle {
|
|
|
|
x: bounds.x,
|
2022-10-16 14:51:37 +02:00
|
|
|
y: (week as f32) * h + bounds.y + self.params.day_text_margin,
|
2022-09-25 08:23:14 +02:00
|
|
|
width: r * 2.0,
|
|
|
|
height: r * 2.0
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
renderer.fill_text(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
|
|
|
size: self.week_column_font_size,
|
|
|
|
bounds: Rectangle {
|
|
|
|
x: day_bounds.center_x(),
|
|
|
|
y: day_bounds.y,
|
|
|
|
..day_bounds
|
|
|
|
},
|
2022-10-16 14:51:37 +02:00
|
|
|
color: self.params.day_text,
|
2022-09-25 08:23:14 +02:00
|
|
|
font: Default::default(),
|
|
|
|
horizontal_alignment: alignment::Horizontal::Center,
|
|
|
|
vertical_alignment: alignment::Vertical::Top,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 08:04:48 +02:00
|
|
|
fn draw_days(
|
|
|
|
&self,
|
|
|
|
renderer: &mut impl text::Renderer,
|
|
|
|
bounds: Rectangle,
|
|
|
|
) {
|
|
|
|
let size: Size = bounds.size();
|
|
|
|
let origin = bounds.position();
|
|
|
|
|
|
|
|
// font dimension
|
|
|
|
let font_size = renderer.default_size() as f32;
|
|
|
|
|
|
|
|
// dimensions of each box representing a day
|
|
|
|
let w: f32 = size.width / 7.0;
|
|
|
|
let h: f32 = size.height / 6.0;
|
|
|
|
|
2022-10-16 14:51:37 +02:00
|
|
|
let mut current_day = self.first_day_in_view;
|
2022-08-02 08:04:48 +02:00
|
|
|
for week in 0..6i32 {
|
|
|
|
for weekday in 0..7i32 {
|
|
|
|
let day_bounds = Rectangle {
|
2022-09-25 08:23:14 +02:00
|
|
|
x: (weekday as f32) * w + origin.x,
|
|
|
|
y: (week as f32) * h + origin.y,
|
2022-10-16 14:51:37 +02:00
|
|
|
width: w,
|
|
|
|
height: h
|
2022-08-02 08:04:48 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// label (day letter on row 0, day number on the rest)
|
2022-10-16 14:51:37 +02:00
|
|
|
let t = current_day.day().to_string();
|
|
|
|
let content = t.as_str();
|
2022-08-02 08:04:48 +02:00
|
|
|
|
|
|
|
// color of text
|
2022-10-16 14:51:37 +02:00
|
|
|
let fg = if current_day.month() == self.first_day.month() {
|
|
|
|
self.params.day_text
|
|
|
|
} else {
|
|
|
|
self.params.day_text_other_month
|
|
|
|
};
|
|
|
|
|
|
|
|
// background color of the day cell
|
|
|
|
let bg_color = if current_day == Local::today().naive_local() {
|
|
|
|
self.params.day_today_bg
|
|
|
|
} else if weekday > 4 {
|
|
|
|
self.params.day_weekend_bg
|
2022-08-02 08:04:48 +02:00
|
|
|
} else {
|
2022-10-16 14:51:37 +02:00
|
|
|
Color::TRANSPARENT
|
2022-08-02 08:04:48 +02:00
|
|
|
};
|
|
|
|
|
2022-09-25 08:23:14 +02:00
|
|
|
// where to place the day content
|
2022-10-16 14:51:37 +02:00
|
|
|
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
|
|
|
|
|
|
|
renderer.fill_quad(renderer::Quad {
|
2022-10-16 14:51:37 +02:00
|
|
|
bounds: Rectangle {width: day_bounds.width + 0.5, height: day_bounds.height + 0.5, ..day_bounds},
|
2022-09-25 08:23:14 +02:00
|
|
|
border_radius: 0.0,
|
2022-10-16 14:51:37 +02:00
|
|
|
border_width: 1.0,
|
|
|
|
border_color: self.params.day_text_other_month,
|
2022-09-25 08:23:14 +02:00
|
|
|
},
|
2022-10-16 14:51:37 +02:00
|
|
|
bg_color);
|
2022-09-25 08:23:14 +02:00
|
|
|
|
|
|
|
// render day cell text
|
2022-08-02 08:04:48 +02:00
|
|
|
renderer.fill_text(text::Text {
|
2022-10-16 14:51:37 +02:00
|
|
|
content,
|
2022-08-02 08:04:48 +02:00
|
|
|
size: font_size,
|
|
|
|
bounds: Rectangle {x, y, ..day_bounds},
|
|
|
|
color: fg,
|
|
|
|
font: Default::default(),
|
2022-09-25 08:23:14 +02:00
|
|
|
horizontal_alignment: alignment::Horizontal::Left,
|
|
|
|
vertical_alignment: alignment::Vertical::Top,
|
2022-08-02 08:04:48 +02:00
|
|
|
});
|
2022-10-16 14:51:37 +02:00
|
|
|
current_day = current_day.succ();
|
2022-08-02 08:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // CalendarMonthView
|
|
|
|
|
|
|
|
impl<Message, Renderer> Widget<Message, Renderer> for CalendarMonthView
|
|
|
|
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,
|
|
|
|
layout: Layout<'_>,
|
|
|
|
_cursor_position: Point,
|
|
|
|
_viewport: &Rectangle,
|
|
|
|
) {
|
|
|
|
let size: Size = layout.bounds().size();
|
|
|
|
let origin = layout.bounds().position();
|
|
|
|
let margin: f32 = 20.0;
|
|
|
|
|
2022-09-25 08:23:14 +02:00
|
|
|
// week column only visible if there is enough space
|
2022-10-16 14:51:37 +02:00
|
|
|
let week_w = if self.params.show_weeks && size.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
|
|
|
|
let x = origin.x;
|
|
|
|
let y = origin.y;
|
|
|
|
let width = size.width;
|
|
|
|
let height = first_row_h;
|
2022-09-25 08:23:14 +02:00
|
|
|
self.draw_header(renderer, Rectangle {x, y, width, height}, week_w);
|
|
|
|
|
|
|
|
// week column
|
|
|
|
if week_w > 0.0 {
|
|
|
|
let x = origin.x;
|
|
|
|
let y = origin.y + first_row_h;
|
|
|
|
let width = self.week_column_width;
|
|
|
|
let height = size.height - first_row_h;
|
|
|
|
self.draw_week_column(renderer, Rectangle{x, y, width, height});
|
|
|
|
}
|
|
|
|
|
2022-08-02 08:04:48 +02:00
|
|
|
// monthly calendar cells
|
2022-09-25 08:23:14 +02:00
|
|
|
let x = origin.x + week_w;
|
2022-08-02 08:04:48 +02:00
|
|
|
let y = origin.y + first_row_h;
|
2022-09-25 08:23:14 +02:00
|
|
|
let width = size.width - week_w;
|
2022-08-02 08:04:48 +02:00
|
|
|
let height = size.height - first_row_h;
|
|
|
|
self.draw_days(renderer, Rectangle{x, y, width, height});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, Message, Renderer> From<CalendarMonthView> for Element<'a, Message, Renderer>
|
|
|
|
where
|
|
|
|
Renderer: text::Renderer,
|
|
|
|
{
|
|
|
|
fn from(circle: CalendarMonthView) -> Self {
|
|
|
|
Self::new(circle)
|
|
|
|
}
|
|
|
|
}
|