Compare commits
2 Commits
d2a1092a7c
...
0d528bd961
Author | SHA1 | Date |
---|---|---|
Fabrizio Iannetti | 0d528bd961 | |
Fabrizio Iannetti | 5760873a86 |
|
@ -0,0 +1,12 @@
|
||||||
|
use crate::calendar::CalendarViewMode as ViewMode;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub enum Message {
|
||||||
|
NextWeek,
|
||||||
|
PrevWeek,
|
||||||
|
NextMonth,
|
||||||
|
PrevMonth,
|
||||||
|
NextYear,
|
||||||
|
PrevYear,
|
||||||
|
ViewModeSelected(ViewMode),
|
||||||
|
}
|
147
src/main.rs
147
src/main.rs
|
@ -1,14 +1,15 @@
|
||||||
//! Simple calendar applications.
|
//! Simple calendar applications.
|
||||||
|
mod app;
|
||||||
mod ui;
|
mod ui;
|
||||||
mod model;
|
mod model;
|
||||||
|
|
||||||
use iced::widget::button;
|
use ui::{calendar, controls::Controls};
|
||||||
use ui::calendar;
|
use app::Message;
|
||||||
use model::{
|
use model::{
|
||||||
events::EventsCollection,
|
events::EventsCollection,
|
||||||
ical_bridge::load_calendar,
|
ical_bridge::load_calendar,
|
||||||
};
|
};
|
||||||
use chrono::{Datelike, NaiveDate, Months, Utc};
|
use chrono::{Datelike, NaiveDate, Months, Utc, Days};
|
||||||
use calendar::{CalendarParams, CalendarView };
|
use calendar::{CalendarParams, CalendarView };
|
||||||
use calendar::CalendarViewMode as ViewMode;
|
use calendar::CalendarViewMode as ViewMode;
|
||||||
use std::path;
|
use std::path;
|
||||||
|
@ -16,18 +17,13 @@ use clap::Parser;
|
||||||
use log::info;
|
use log::info;
|
||||||
use simplelog::{SimpleLogger, Config};
|
use simplelog::{SimpleLogger, Config};
|
||||||
use iced::{
|
use iced::{
|
||||||
alignment,
|
Alignment,
|
||||||
Alignment,
|
|
||||||
Command,
|
Command,
|
||||||
Element,
|
Element,
|
||||||
Length,
|
Length,
|
||||||
widget::{
|
widget::{
|
||||||
Column,
|
Column,
|
||||||
Row,
|
|
||||||
Container,
|
Container,
|
||||||
Button,
|
|
||||||
Text,
|
|
||||||
pick_list,
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -68,35 +64,11 @@ fn set_up_logger() {
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct CalendarApp {
|
struct CalendarApp {
|
||||||
month: NaiveDate,
|
view_date: NaiveDate,
|
||||||
controls: Controls,
|
controls: Controls,
|
||||||
events: EventsCollection,
|
events: EventsCollection,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
|
||||||
enum Message {
|
|
||||||
NextWeek,
|
|
||||||
PrevWeek,
|
|
||||||
NextMonth,
|
|
||||||
PrevMonth,
|
|
||||||
NextYear,
|
|
||||||
PrevYear,
|
|
||||||
ViewModeSelected(ViewMode),
|
|
||||||
}
|
|
||||||
|
|
||||||
//#[derive(Default)]
|
|
||||||
struct Controls {
|
|
||||||
mode: Option<ViewMode>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for Controls {
|
|
||||||
fn default() -> Controls {
|
|
||||||
Controls {
|
|
||||||
mode : Some(ViewMode::Year)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::fmt::Display for ViewMode {
|
impl std::fmt::Display for ViewMode {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
write!(
|
write!(
|
||||||
|
@ -111,74 +83,11 @@ impl std::fmt::Display for ViewMode {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Controls {
|
|
||||||
const MODES : [ViewMode; 3] = [ViewMode::Week, ViewMode::Month, ViewMode::Year];
|
|
||||||
|
|
||||||
fn view<'a>(&'a self, month_name: &'a str, year: i32) -> Element<Message> {
|
|
||||||
let description = match self.mode {
|
|
||||||
Some(ViewMode::Month) => month_name,
|
|
||||||
_ => ""
|
|
||||||
};
|
|
||||||
Row::new()
|
|
||||||
.align_items(Alignment::Center)
|
|
||||||
.padding(5)
|
|
||||||
.spacing(10)
|
|
||||||
.push(
|
|
||||||
pick_list(
|
|
||||||
&Controls::MODES[..],
|
|
||||||
self.mode,
|
|
||||||
Message::ViewModeSelected,
|
|
||||||
).placeholder("mode")
|
|
||||||
)
|
|
||||||
.push(
|
|
||||||
Button::new(Text::new("<"))
|
|
||||||
.on_press(self.get_msg_prev())
|
|
||||||
.style(button::secondary),
|
|
||||||
)
|
|
||||||
.push(
|
|
||||||
Button::new(Text::new(">"))
|
|
||||||
.on_press(self.get_msg_next())
|
|
||||||
.style(button::secondary),
|
|
||||||
)
|
|
||||||
.push(
|
|
||||||
Text::new(description)
|
|
||||||
.width(Length::Fill)
|
|
||||||
.horizontal_alignment(alignment::Horizontal::Left)
|
|
||||||
.size(40),
|
|
||||||
)
|
|
||||||
.push(
|
|
||||||
Text::new(year.to_string())
|
|
||||||
.width(Length::Fill)
|
|
||||||
.horizontal_alignment(alignment::Horizontal::Right)
|
|
||||||
.size(40),
|
|
||||||
)
|
|
||||||
.into()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_msg_next(&self) -> Message {
|
|
||||||
match self.mode {
|
|
||||||
Some(ViewMode::Week) => Message::NextWeek,
|
|
||||||
Some(ViewMode::Month) => Message::NextMonth,
|
|
||||||
Some(ViewMode::Year) => Message::NextYear,
|
|
||||||
None => todo!(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_msg_prev(&self) -> Message {
|
|
||||||
match self.mode {
|
|
||||||
Some(ViewMode::Week) => Message::PrevWeek,
|
|
||||||
Some(ViewMode::Month) => Message::PrevMonth,
|
|
||||||
Some(ViewMode::Year) => Message::PrevYear,
|
|
||||||
None => todo!(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CalendarApp {
|
impl CalendarApp {
|
||||||
fn with_files(calendar_paths: std::vec::Vec<path::PathBuf>) -> Self {
|
fn with_files(calendar_paths: std::vec::Vec<path::PathBuf>) -> Self {
|
||||||
let month = Utc::now().date_naive();
|
let view_date = Utc::now().date_naive();
|
||||||
let mut app = CalendarApp {
|
let mut app = CalendarApp {
|
||||||
month,
|
view_date,
|
||||||
events: EventsCollection::new(),
|
events: EventsCollection::new(),
|
||||||
..CalendarApp::default()
|
..CalendarApp::default()
|
||||||
};
|
};
|
||||||
|
@ -188,29 +97,25 @@ impl CalendarApp {
|
||||||
app
|
app
|
||||||
}
|
}
|
||||||
|
|
||||||
fn title(&self) -> String {
|
|
||||||
String::from("Calendar")
|
|
||||||
}
|
|
||||||
|
|
||||||
fn update(&mut self, message: Message) -> Command<Message> {
|
fn update(&mut self, message: Message) -> Command<Message> {
|
||||||
match message {
|
match message {
|
||||||
Message::PrevWeek => {
|
Message::PrevWeek => {
|
||||||
self.month = self.month; // TODO
|
self.view_date = self.view_date - Days::new(7);
|
||||||
}
|
}
|
||||||
Message::NextWeek => {
|
Message::NextWeek => {
|
||||||
self.month = self.month; // TODO
|
self.view_date = self.view_date + Days::new(7);
|
||||||
}
|
}
|
||||||
Message::PrevMonth => {
|
Message::PrevMonth => {
|
||||||
self.month = self.month - Months::new(1);
|
self.view_date = self.view_date - Months::new(1);
|
||||||
}
|
}
|
||||||
Message::NextMonth => {
|
Message::NextMonth => {
|
||||||
self.month = self.month + Months::new(1);
|
self.view_date = self.view_date + Months::new(1);
|
||||||
}
|
}
|
||||||
Message::PrevYear => {
|
Message::PrevYear => {
|
||||||
self.month = self.month - Months::new(12);
|
self.view_date = self.view_date - Months::new(12);
|
||||||
}
|
}
|
||||||
Message::NextYear => {
|
Message::NextYear => {
|
||||||
self.month = self.month + Months::new(12);
|
self.view_date = self.view_date + Months::new(12);
|
||||||
}
|
}
|
||||||
Message::ViewModeSelected(mode) => {
|
Message::ViewModeSelected(mode) => {
|
||||||
self.controls.mode = Some(mode);
|
self.controls.mode = Some(mode);
|
||||||
|
@ -222,24 +127,10 @@ impl CalendarApp {
|
||||||
fn view(&self) -> Element<Message> {
|
fn view(&self) -> Element<Message> {
|
||||||
#[cfg(feature = "tracing")]
|
#[cfg(feature = "tracing")]
|
||||||
tracepoints::calendar::view_entry();
|
tracepoints::calendar::view_entry();
|
||||||
const MONTH_NAMES: [&str;12] = [
|
|
||||||
"gennaio",
|
|
||||||
"febbraio",
|
|
||||||
"marzo",
|
|
||||||
"aprile",
|
|
||||||
"maggio",
|
|
||||||
"giugno",
|
|
||||||
"luglio",
|
|
||||||
"agosto",
|
|
||||||
"settembre",
|
|
||||||
"ottobre",
|
|
||||||
"novembre",
|
|
||||||
"dicembre",
|
|
||||||
];
|
|
||||||
let content = Column::new()
|
let content = Column::new()
|
||||||
.align_items(Alignment::Center)
|
.align_items(Alignment::Center)
|
||||||
.push(self.controls.view(MONTH_NAMES[self.month.month0() as usize], self.month.year()))
|
.push(self.controls.view(self.view_date, self.view_date.year()))
|
||||||
.push(CalendarView::new(self.controls.mode.unwrap_or(ViewMode::Year), &CalendarParams::new(), self.month, &self.events))
|
.push(CalendarView::new(self.controls.mode.unwrap_or(ViewMode::Year), &CalendarParams::new(), self.view_date, &self.events))
|
||||||
;
|
;
|
||||||
|
|
||||||
let container = Container::new(content)
|
let container = Container::new(content)
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
pub mod calendar;
|
|
||||||
pub mod basics;
|
pub mod basics;
|
||||||
|
pub mod calendar;
|
||||||
|
pub mod controls;
|
||||||
mod row;
|
mod row;
|
||||||
|
|
|
@ -80,10 +80,10 @@ impl CalendarParams {
|
||||||
day_other_month_fg: Color::from_rgb8(220, 220, 220),
|
day_other_month_fg: Color::from_rgb8(220, 220, 220),
|
||||||
day_weekend_bg: Color::from_rgb8(245, 245, 245),
|
day_weekend_bg: Color::from_rgb8(245, 245, 245),
|
||||||
day_text_margin: 5.0,
|
day_text_margin: 5.0,
|
||||||
ev_height: 20.0,
|
ev_height: 18.0,
|
||||||
ev_margin: 2.0,
|
ev_margin: 2.0,
|
||||||
ev_bg: Color::from_rgb8(200, 245, 200),
|
ev_bg: Color::from_rgb8(200, 245, 200),
|
||||||
ev_fontsize: 16.0,
|
ev_fontsize: 14.0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -218,8 +218,8 @@ fn render_events_in_row<Renderer>(
|
||||||
width: ev_bar.bounds.width,
|
width: ev_bar.bounds.width,
|
||||||
};
|
};
|
||||||
let ev_text_position = Point {
|
let ev_text_position = Point {
|
||||||
x: ev_bar.bounds.x,
|
x: ev_bar.bounds.x + 1.0,
|
||||||
y: ev_bar.bounds.y + 1.0,
|
y: ev_bar.bounds.y,
|
||||||
};
|
};
|
||||||
renderer.fill_text(
|
renderer.fill_text(
|
||||||
Text {
|
Text {
|
||||||
|
|
|
@ -0,0 +1,108 @@
|
||||||
|
use crate::{
|
||||||
|
app::Message,
|
||||||
|
calendar::CalendarViewMode as ViewMode
|
||||||
|
};
|
||||||
|
use chrono::{NaiveDate, Datelike};
|
||||||
|
use iced::{
|
||||||
|
alignment,
|
||||||
|
Alignment,
|
||||||
|
Element,
|
||||||
|
Length,
|
||||||
|
widget::{
|
||||||
|
button,
|
||||||
|
Row,
|
||||||
|
Button,
|
||||||
|
Text,
|
||||||
|
pick_list,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
//#[derive(Default)]
|
||||||
|
pub struct Controls {
|
||||||
|
pub mode: Option<ViewMode>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for Controls {
|
||||||
|
fn default() -> Controls {
|
||||||
|
Controls {
|
||||||
|
mode : Some(ViewMode::Year)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Controls {
|
||||||
|
const MODES : [ViewMode; 3] = [ViewMode::Week, ViewMode::Month, ViewMode::Year];
|
||||||
|
const MONTH_NAMES: [&'static str;12] = [
|
||||||
|
"gennaio",
|
||||||
|
"febbraio",
|
||||||
|
"marzo",
|
||||||
|
"aprile",
|
||||||
|
"maggio",
|
||||||
|
"giugno",
|
||||||
|
"luglio",
|
||||||
|
"agosto",
|
||||||
|
"settembre",
|
||||||
|
"ottobre",
|
||||||
|
"novembre",
|
||||||
|
"dicembre",
|
||||||
|
];
|
||||||
|
|
||||||
|
pub fn view<'a>(&'a self, view_date: NaiveDate, year: i32) -> Element<Message> {
|
||||||
|
let description = match self.mode {
|
||||||
|
Some(ViewMode::Month) => Controls::MONTH_NAMES[view_date.month0() as usize],
|
||||||
|
_ => ""
|
||||||
|
};
|
||||||
|
Row::new()
|
||||||
|
.align_items(Alignment::Center)
|
||||||
|
.padding(5)
|
||||||
|
.spacing(10)
|
||||||
|
.push(
|
||||||
|
pick_list(
|
||||||
|
&Controls::MODES[..],
|
||||||
|
self.mode,
|
||||||
|
Message::ViewModeSelected,
|
||||||
|
).placeholder("mode")
|
||||||
|
)
|
||||||
|
.push(
|
||||||
|
Button::new(Text::new("<"))
|
||||||
|
.on_press(self.get_msg_prev())
|
||||||
|
.style(button::secondary),
|
||||||
|
)
|
||||||
|
.push(
|
||||||
|
Button::new(Text::new(">"))
|
||||||
|
.on_press(self.get_msg_next())
|
||||||
|
.style(button::secondary),
|
||||||
|
)
|
||||||
|
.push(
|
||||||
|
Text::new(description)
|
||||||
|
.width(Length::Fill)
|
||||||
|
.horizontal_alignment(alignment::Horizontal::Left)
|
||||||
|
.size(40),
|
||||||
|
)
|
||||||
|
.push(
|
||||||
|
Text::new(year.to_string())
|
||||||
|
.width(Length::Fill)
|
||||||
|
.horizontal_alignment(alignment::Horizontal::Right)
|
||||||
|
.size(40),
|
||||||
|
)
|
||||||
|
.into()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_msg_next(&self) -> Message {
|
||||||
|
match self.mode {
|
||||||
|
Some(ViewMode::Week) => Message::NextWeek,
|
||||||
|
Some(ViewMode::Month) => Message::NextMonth,
|
||||||
|
Some(ViewMode::Year) => Message::NextYear,
|
||||||
|
None => todo!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_msg_prev(&self) -> Message {
|
||||||
|
match self.mode {
|
||||||
|
Some(ViewMode::Week) => Message::PrevWeek,
|
||||||
|
Some(ViewMode::Month) => Message::PrevMonth,
|
||||||
|
Some(ViewMode::Year) => Message::PrevYear,
|
||||||
|
None => todo!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue