Improved cell grid

* CellAxis struct to interate over cells in a direction (hor. or vert.)
* used in MontlyView

Signed-off-by: Fabrizio Iannetti <fabrizio.iannetti@gmail.com>
This commit is contained in:
Fabrizio Iannetti 2022-12-10 12:24:58 +01:00
parent 150216a920
commit deb2335fe8
3 changed files with 71 additions and 39 deletions

View File

@ -1 +1,2 @@
pub mod calendar;
pub mod calendar;
pub mod basics;

34
src/ui/basics.rs Normal file
View File

@ -0,0 +1,34 @@
pub struct CellAxis {
min: f32,
max: f32,
num: u32,
idx: u32
}
impl CellAxis {
pub fn new(min: f32, len: f32, num: u32) -> Self {
Self {
min,
max: min + len,
num,
idx: 0
}
}
}
impl Iterator for CellAxis {
type Item = (f32, f32);
fn next(&mut self) -> Option<Self::Item> {
if self.idx < self.num {
let beg: f32 = self.min + ((self.max - self.min) * self.idx as f32) / self.num as f32;
let end: f32 = self.min + ((self.max - self.min) * (self.idx + 1) as f32) / self.num as f32;
self.idx += 1;
return Some((beg.trunc(), end.trunc()));
}
return None;
}
}

View File

@ -15,6 +15,7 @@ use iced_native::text;
use iced_native::alignment;
use iced_native::widget::Tree;
use chrono::{NaiveDate, Datelike, Duration, Weekday, Local};
use super::basics::CellAxis;
const MONTH_NAMES: [&str;12] = [
"gen",
@ -140,42 +141,38 @@ impl CalendarMonthView {
width: bounds.width - week_w,
height: bounds.height};
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;
let days_of_week = ["LUN", "MAR", "MER", "GIO", "VEN", "SAB", "DOM"];
for weekday in 0..7i32 {
let h_axis = CellAxis::new(bounds.x, bounds.width, 7);
let mut day_num: usize = 0;
for weekday in h_axis {
let bounds = Rectangle {
x: (weekday as f32) * w + origin.x,
y: origin.y,
width: w,
height: h
x: weekday.0+ self.params.day_text_margin,
y: bounds.center_y(),
width: weekday.1 - weekday.0,
height: bounds.height
};
// label (day letter on row 0, day number on the rest)
let t = days_of_week[weekday as usize];
let t = days_of_week[day_num];
// color of text
let fg = self.params.header_fg;
let x = bounds.x + self.params.day_text_margin;
let y = bounds.center_y();
renderer.fill_text(text::Text {
content : t,
size: font_size,
bounds: Rectangle {x, y, ..bounds},
bounds,
color: fg,
font: Default::default(),
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Center,
});
day_num += 1;
}
}
@ -194,11 +191,12 @@ impl CalendarMonthView {
let mut day = self.first_day;
for week in 0..6u32 {
let v_axis = CellAxis::new(bounds.y, bounds.height, 6);
for week_row in v_axis {
// where to place the week number
let day_bounds = Rectangle {
x: bounds.x,
y: (week as f32) * h + bounds.y + self.params.day_text_margin,
y: week_row.0 + self.params.day_text_margin,
width: r * 2.0,
height: r * 2.0
};
@ -234,18 +232,17 @@ impl CalendarMonthView {
// 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;
let mut current_day = self.first_day_in_view;
for week in 0..6i32 {
for weekday in 0..7i32 {
let v_axis = CellAxis::new(origin.y, size.height, 6);
for row in v_axis {
let h_axis = CellAxis::new(origin.x, size.width, 7);
for col in h_axis {
let day_bounds = Rectangle {
x: (weekday as f32) * w + origin.x,
y: (week as f32) * h + origin.y,
width: w,
height: h
x: col.0,
y: row.0,
width: col.1 - col.0,
height : row.1 - row.0
};
// label (day letter on row 0, day number on the rest)
@ -262,7 +259,7 @@ impl CalendarMonthView {
// 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 {
} else if current_day.weekday().num_days_from_monday() > 4 {
self.params.day_weekend_bg
} else {
Color::TRANSPARENT
@ -449,15 +446,15 @@ impl CalendarYearView {
let font_size = renderer.default_size() as f32;
// dimensions of each box representing a day
let w: f32 = bounds.width / (7.0 * 6.0);
let w: f32 = (bounds.width / (7.0 * 6.0)).trunc();
let h: f32 = bounds.height;
let days_of_week = ["L", "M", "M", "G", "V", "S", "D"];
for col in 0..42i32 {
let bounds = Rectangle {
x: (col as f32) * w + origin.x,
y: origin.y,
x: (col as f32) * w + origin.x + 0.5,
y: origin.y + 0.5,
width: w,
height: h
};
@ -474,7 +471,7 @@ impl CalendarYearView {
renderer.fill_quad(renderer::Quad {
bounds,
border_radius: 0.0.into(),
border_width: 1.0,
border_width: 0.0,
border_color: Color::TRANSPARENT,
},
bg_color);
@ -545,8 +542,8 @@ impl CalendarYearView {
let font_size = renderer.default_size() as f32;
// dimensions of each box representing a day
let w: f32 = size.width / 42.0;
let h: f32 = size.height / 12.0;
let w: f32 = (size.width / 42.0).trunc();
let h: f32 = (size.height / 12.0).trunc();
for current_day in self.first_day.iter_days() {
if current_day.year() != self.first_day.year() {
@ -557,8 +554,8 @@ impl CalendarYearView {
let first_day_of_month = current_day.with_day0(0).unwrap().weekday().num_days_from_monday();
let monthday = current_day.day0() + first_day_of_month;
let day_bounds = Rectangle {
x: (monthday as f32) * w + origin.x,
y: (month as f32) * h + origin.y,
x: (monthday as f32) * w + origin.x + 0.5,
y: (month as f32) * h + origin.y + 0.5,
width: w,
height: h
};
@ -585,7 +582,7 @@ impl CalendarYearView {
let y = day_bounds.y + self.params.day_text_margin;
renderer.fill_quad(renderer::Quad {
bounds: Rectangle {width: day_bounds.width + 0.5, height: day_bounds.height + 0.5, ..day_bounds},
bounds: day_bounds,
border_radius: 0.0.into(),
border_width: 1.0,
border_color: self.params.day_text_other_month,