173 lines
4.3 KiB
Rust
173 lines
4.3 KiB
Rust
//! Simple calendar applications.
|
|
mod ui;
|
|
use ui::calendar;
|
|
|
|
use chrono::{Datelike, NaiveDate, Months, Utc};
|
|
use calendar::{CalendarMonthView, CalendarYearView, CalendarParams };
|
|
|
|
use iced::{
|
|
Alignment,
|
|
Element, Length, Sandbox, Settings,
|
|
};
|
|
use iced::widget::{
|
|
Column, Row,
|
|
Container,
|
|
Button, Text,
|
|
pick_list,
|
|
};
|
|
//use iced::button;
|
|
use iced::theme;
|
|
|
|
pub fn main() -> iced::Result {
|
|
CalendarApp::run(Settings::default())
|
|
}
|
|
|
|
#[derive(Default)]
|
|
struct CalendarApp {
|
|
month: NaiveDate,
|
|
controls: Controls,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
enum Message {
|
|
NextMonth,
|
|
PrevMonth,
|
|
ViewModeSelected(ViewMode),
|
|
}
|
|
|
|
//#[derive(Default)]
|
|
struct Controls {
|
|
mode: Option<ViewMode>,
|
|
}
|
|
|
|
impl Default for Controls {
|
|
fn default() -> Controls {
|
|
Controls {
|
|
mode : Some(ViewMode::Year)
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub enum ViewMode {
|
|
Month,
|
|
Year,
|
|
}
|
|
|
|
impl std::fmt::Display for ViewMode {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
write!(
|
|
f,
|
|
"{}",
|
|
match self {
|
|
ViewMode::Month => "Month",
|
|
ViewMode::Year => "Year",
|
|
// ViewMode::Day => "Day",
|
|
}
|
|
)
|
|
}
|
|
}
|
|
impl Controls {
|
|
const MODES : [ViewMode; 2] = [ViewMode::Month, ViewMode::Year];
|
|
|
|
fn view<'a>(&'a self, month_name: &'a str, year: i32) -> Element<Message> {
|
|
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(Message::PrevMonth)
|
|
.style(theme::Button::Secondary),
|
|
)
|
|
.push(
|
|
Button::new(Text::new(">"))
|
|
.on_press(Message::NextMonth)
|
|
.style(theme::Button::Secondary),
|
|
)
|
|
.push(
|
|
Text::new(month_name)
|
|
.width(Length::Fill)
|
|
.horizontal_alignment(iced_native::alignment::Horizontal::Left)
|
|
.size(40),
|
|
)
|
|
.push(
|
|
Text::new(year.to_string())
|
|
.width(Length::Fill)
|
|
.horizontal_alignment(iced_native::alignment::Horizontal::Right)
|
|
.size(40),
|
|
)
|
|
.into()
|
|
}
|
|
}
|
|
|
|
impl Sandbox for CalendarApp {
|
|
type Message = Message;
|
|
|
|
fn new() -> Self {
|
|
let month = Utc::today().naive_local();
|
|
CalendarApp {
|
|
month,
|
|
..CalendarApp::default()
|
|
}
|
|
}
|
|
|
|
fn title(&self) -> String {
|
|
String::from("Calendar")
|
|
}
|
|
|
|
fn update(&mut self, message: Message) {
|
|
match message {
|
|
Message::PrevMonth => {
|
|
self.month = self.month - Months::new(1);
|
|
}
|
|
Message::NextMonth => {
|
|
self.month = self.month + Months::new(1);
|
|
}
|
|
Message::ViewModeSelected(mode) => {
|
|
self.controls.mode = Some(mode);
|
|
}
|
|
}
|
|
}
|
|
|
|
fn view(&self) -> Element<Message> {
|
|
const MONTH_NAMES: [&str;12] = [
|
|
"gennaio",
|
|
"febbraio",
|
|
"marzo",
|
|
"aprile",
|
|
"maggio",
|
|
"giugno",
|
|
"luglio",
|
|
"agosto",
|
|
"settembre",
|
|
"ottobre",
|
|
"novembre",
|
|
"dicembre",
|
|
];
|
|
let mut content = Column::new()
|
|
.align_items(Alignment::Fill)
|
|
.push(self.controls.view(MONTH_NAMES[self.month.month0() as usize], self.month.year()))
|
|
;
|
|
|
|
match self.controls.mode {
|
|
Some(ViewMode::Year) => content = content.push(CalendarYearView::new(&CalendarParams::new(), self.month)),
|
|
Some(ViewMode::Month) | None => content = content.push(CalendarMonthView::new(&CalendarParams::new(), self.month)),
|
|
};
|
|
|
|
Container::new(content)
|
|
.width(Length::Fill)
|
|
.height(Length::Fill)
|
|
.center_x()
|
|
.center_y()
|
|
.into()
|
|
}
|
|
}
|