feat(time): implement prev/next month

This commit is contained in:
Lyu Ji 2024-02-26 10:24:07 -08:00 • committed by GitHub
parent a6494e8ed5
commit 5be328f8ad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -15,7 +15,7 @@ use cosmic::{
Element, Theme, Element, Theme,
}; };
use chrono::{DateTime, Datelike, Local, Timelike, Weekday}; use chrono::{DateTime, Datelike, Local, Months, NaiveDate, Timelike, Weekday};
use std::time::Duration; use std::time::Duration;
use crate::fl; use crate::fl;
@ -36,6 +36,7 @@ pub struct Window {
popup: Option<window::Id>, popup: Option<window::Id>,
update_at: Every, update_at: Every,
now: DateTime<Local>, now: DateTime<Local>,
date_selected: NaiveDate,
rectangle_tracker: Option<RectangleTracker<u32>>, rectangle_tracker: Option<RectangleTracker<u32>>,
rectangle: Rectangle, rectangle: Rectangle,
token_tx: Option<calloop::channel::Sender<TokenRequest>>, token_tx: Option<calloop::channel::Sender<TokenRequest>>,
@ -64,12 +65,14 @@ impl cosmic::Application for Window {
core: app::Core, core: app::Core,
_flags: Self::Flags, _flags: Self::Flags,
) -> (Self, cosmic::iced::Command<app::Message<Self::Message>>) { ) -> (Self, cosmic::iced::Command<app::Message<Self::Message>>) {
let now = Local::now();
( (
Self { Self {
core, core,
popup: None, popup: None,
update_at: Every::Minute, update_at: Every::Minute,
now: Local::now(), now,
date_selected: NaiveDate::from(now.naive_local()),
rectangle_tracker: None, rectangle_tracker: None,
rectangle: Rectangle::default(), rectangle: Rectangle::default(),
token_tx: None, token_tx: None,
@ -175,11 +178,17 @@ impl cosmic::Application for Window {
Command::none() Command::none()
} }
Message::PreviousMonth => { Message::PreviousMonth => {
// TODO self.date_selected = self
.date_selected
.checked_sub_months(Months::new(1))
.expect("valid naivedate");
Command::none() Command::none()
} }
Message::NextMonth => { Message::NextMonth => {
// TODO self.date_selected = self
.date_selected
.checked_add_months(Months::new(1))
.expect("valid naivedate");
Command::none() Command::none()
} }
Message::OpenDateTimeSettings => { Message::OpenDateTimeSettings => {
@ -259,8 +268,8 @@ impl cosmic::Application for Window {
} }
fn view_window(&self, _id: window::Id) -> Element<Message> { fn view_window(&self, _id: window::Id) -> Element<Message> {
let date = text(self.now.format("%B %-d, %Y").to_string()).size(18); let date = text(self.date_selected.format("%B %-d, %Y").to_string()).size(18);
let day_of_week = text(self.now.format("%A").to_string()).size(14); let day_of_week = text(self.date_selected.format("%A").to_string()).size(14);
let month_controls = row![ let month_controls = row![
button::icon(icon::from_name("go-previous-symbolic")) button::icon(icon::from_name("go-previous-symbolic"))
@ -286,16 +295,21 @@ impl cosmic::Application for Window {
} }
calender = calender.insert_row(); calender = calender.insert_row();
let monday = get_calender_first(self.now.year(), self.now.month(), first_day_of_week); let monday = get_calender_first(
self.date_selected.year(),
self.date_selected.month(),
first_day_of_week,
);
let mut day_iter = monday.iter_days(); let mut day_iter = monday.iter_days();
for i in 0..35 { for i in 0..42 {
if i > 0 && i % 7 == 0 { if i > 0 && i % 7 == 0 {
calender = calender.insert_row(); calender = calender.insert_row();
} }
let date = day_iter.next().unwrap(); let date = day_iter.next().unwrap();
let is_month = date.month() == self.now.month() && date.year_ce() == self.now.year_ce(); let is_month = date.month() == self.date_selected.month()
let is_day = date.day() == self.now.day() && is_month; && date.year_ce() == self.date_selected.year_ce();
let is_day = date.day() == self.date_selected.day() && is_month;
calender = calender.push(date_button(date.day(), is_month, is_day)); calender = calender.push(date_button(date.day(), is_month, is_day));
} }