|
|
|
|
@ -88,9 +88,7 @@ where
|
|
|
|
|
pub struct CalendarMonth<C> { |
|
|
|
|
pub top_left: Point, |
|
|
|
|
pub size: Size, |
|
|
|
|
pub border: u32, |
|
|
|
|
pub fg_color: C, |
|
|
|
|
pub bg_color: C, |
|
|
|
|
pub style: ComponentStyle<C>, |
|
|
|
|
pub month: chrono::Month, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -107,14 +105,17 @@ where
|
|
|
|
|
{ |
|
|
|
|
let rect = Rectangle::new( |
|
|
|
|
self.top_left, |
|
|
|
|
self.size + Size::new(self.border / 2 + 1, self.border / 2 + 1), |
|
|
|
|
self.size + Size::new(self.style.border / 2 + 1, self.style.border / 2 + 1), |
|
|
|
|
); |
|
|
|
|
rect |
|
|
|
|
.into_styled(PrimitiveStyle::with_stroke(self.fg_color, self.border)) |
|
|
|
|
.into_styled(PrimitiveStyle::with_stroke( |
|
|
|
|
self.style.fg_color, |
|
|
|
|
self.style.border, |
|
|
|
|
)) |
|
|
|
|
.draw(target)?; |
|
|
|
|
|
|
|
|
|
let month_font = font((self.size.height as f32 * 0.8 - 2.) as u32); |
|
|
|
|
let text_style = MonoTextStyle::new(&month_font, self.fg_color); |
|
|
|
|
let text_style = MonoTextStyle::new(&month_font, self.style.fg_color); |
|
|
|
|
Text::new(self.month.name(), self.top_left, text_style) |
|
|
|
|
.align_to(&rect, horizontal::Center, vertical::Center) |
|
|
|
|
.draw(target)?; |
|
|
|
|
@ -126,11 +127,7 @@ where
|
|
|
|
|
pub struct Calendar<C> { |
|
|
|
|
pub top_left: Point, |
|
|
|
|
pub size: Size, |
|
|
|
|
pub border: u32, |
|
|
|
|
pub fg_color: C, |
|
|
|
|
pub bg_color: C, |
|
|
|
|
// If hi_color is None, the fg and bg colors are inverted instead.
|
|
|
|
|
pub hi_color: Option<C>, |
|
|
|
|
pub style: ComponentStyle<C>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<C> Drawable for Calendar<C> |
|
|
|
|
@ -144,7 +141,7 @@ where
|
|
|
|
|
where |
|
|
|
|
D: DrawTarget<Color = C>, |
|
|
|
|
{ |
|
|
|
|
let border = self.border; |
|
|
|
|
let border = self.style.border; |
|
|
|
|
|
|
|
|
|
let date_now = chrono::offset::Local::now().date(); |
|
|
|
|
let first_day = date_now.with_day(1).unwrap(); |
|
|
|
|
@ -171,12 +168,11 @@ where
|
|
|
|
|
} else { |
|
|
|
|
width * empty_squares |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
let calendar_month = CalendarMonth { |
|
|
|
|
border, |
|
|
|
|
style: self.style.clone(), |
|
|
|
|
top_left: self.top_left, |
|
|
|
|
size: Size::new(month_width, height), |
|
|
|
|
fg_color: self.fg_color, |
|
|
|
|
bg_color: self.bg_color, |
|
|
|
|
month: Month::from_u32(date_now.month()).unwrap(), |
|
|
|
|
}; |
|
|
|
|
calendar_month.draw(target)?; |
|
|
|
|
@ -196,22 +192,19 @@ where
|
|
|
|
|
>= 5; |
|
|
|
|
let is_today = day_of_month == date_now.day(); |
|
|
|
|
|
|
|
|
|
let (mut fg_color, mut bg_color, mut border) = match is_weekend ^ is_today { |
|
|
|
|
false => (self.fg_color, self.bg_color, border), |
|
|
|
|
true => (self.bg_color, self.fg_color, 0), |
|
|
|
|
}; |
|
|
|
|
if is_today && self.hi_color.is_some() { |
|
|
|
|
(fg_color, bg_color, border) = (self.bg_color, self.hi_color.unwrap(), 0); |
|
|
|
|
} |
|
|
|
|
let style = self.style.clone(); |
|
|
|
|
|
|
|
|
|
let style = ComponentStyle { |
|
|
|
|
fg_color, |
|
|
|
|
bg_color, |
|
|
|
|
hi_color: fg_color, |
|
|
|
|
border_color: fg_color, |
|
|
|
|
border, |
|
|
|
|
bezel: border, |
|
|
|
|
let mut style = match is_weekend ^ is_today { |
|
|
|
|
false => style.with_border(border), |
|
|
|
|
true => style.invert().with_border(0), |
|
|
|
|
}; |
|
|
|
|
if is_today && self.style.hi_color != self.style.bg_color { |
|
|
|
|
style = ComponentStyle { |
|
|
|
|
fg_color: style.hi_color, |
|
|
|
|
bg_color: style.fg_color, |
|
|
|
|
..style |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let calendar_day = CalendarDay { |
|
|
|
|
style, |
|
|
|
|
@ -262,13 +255,6 @@ impl ScrollingWeek {
|
|
|
|
|
.succ() |
|
|
|
|
.first_day_of_week()? |
|
|
|
|
.checked_sub_signed(chrono::Duration::days(1)); |
|
|
|
|
|
|
|
|
|
match self.succ().first_day_of_week() { |
|
|
|
|
None => self |
|
|
|
|
.first_day_of_week()? |
|
|
|
|
.checked_add_signed(chrono::Duration::days(6)), |
|
|
|
|
Some(date) => date.checked_sub_signed(chrono::Duration::days(1)), |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn from_date(date: chrono::NaiveDate) -> ScrollingWeek { |
|
|
|
|
@ -419,28 +405,24 @@ where
|
|
|
|
|
|
|
|
|
|
if first_day.weekday().number_from_monday() > 4 { |
|
|
|
|
let month = CalendarMonth { |
|
|
|
|
style: self.style.clone(), |
|
|
|
|
top_left: self.top_left, |
|
|
|
|
size: Size::new( |
|
|
|
|
width * first_day.weekday().number_from_monday(), |
|
|
|
|
width * (first_day.weekday().number_from_monday() - 1), |
|
|
|
|
self.size.height, |
|
|
|
|
), |
|
|
|
|
border: self.style.border, |
|
|
|
|
fg_color: self.style.fg_color, |
|
|
|
|
bg_color: self.style.bg_color, |
|
|
|
|
month: chrono::Month::from_u32(first_day.month()).unwrap(), |
|
|
|
|
}; |
|
|
|
|
month.draw(target)?; |
|
|
|
|
} else if last_day.weekday().number_from_monday() < 4 { |
|
|
|
|
let month = CalendarMonth { |
|
|
|
|
style: self.style.clone(), |
|
|
|
|
top_left: self.top_left |
|
|
|
|
+ Point::new((width * last_day.weekday().number_from_monday()) as i32, 0), |
|
|
|
|
size: Size::new( |
|
|
|
|
width * (7 - last_day.weekday().number_from_monday()), |
|
|
|
|
self.size.height, |
|
|
|
|
), |
|
|
|
|
border: self.style.border, |
|
|
|
|
fg_color: self.style.fg_color, |
|
|
|
|
bg_color: self.style.bg_color, |
|
|
|
|
month: chrono::Month::from_u32( |
|
|
|
|
last_day |
|
|
|
|
.checked_add_signed(chrono::Duration::days(1)) |
|
|
|
|
|