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