//! Simple calendar applications. mod calendar; use calendar::{CalendarMonthView, CalendarParams }; use iced::{ Alignment, Column, Row, Container, Element, Length, Sandbox, Settings, Button, Text, }; use iced::button; use iced::theme; pub fn main() -> iced::Result { CalendarApp::run(Settings::default()) } #[derive(Default)] struct CalendarApp { month: i32, controls: Controls, } #[derive(Debug, Clone, Copy)] enum Message { NextMonth, PrevMonth, } #[derive(Default)] struct Controls { prev_button: button::State, next_button: button::State, } impl Controls { fn view(&mut self, month_name: &str) -> Element { Row::new() .align_items(Alignment::Center) .padding(5) .spacing(10) .push( Button::new(&mut self.prev_button, Text::new("<<")) .on_press(Message::PrevMonth) .style(theme::Button::Secondary), ) .push( Text::new(month_name) .width(Length::Shrink) .size(50), ) .push( Button::new(&mut self.next_button, Text::new(">>")) .on_press(Message::NextMonth) .style(theme::Button::Secondary), ) .into() } } impl Sandbox for CalendarApp { type Message = Message; fn new() -> Self { CalendarApp { month: 7, ..CalendarApp::default() } } fn title(&self) -> String { String::from("Calendar") } fn update(&mut self, message: Message) { match message { Message::PrevMonth => { self.month = (self.month + 12 - 1) % 12; } Message::NextMonth => { self.month = (self.month + 12 + 1) % 12; } } println!("month={}", self.month); } fn view(&mut self) -> Element { const MONTH_NAMES: [&str;12] = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "Novemeber", "December", ]; let content = Column::new() .align_items(Alignment::Fill) .push(self.controls.view(MONTH_NAMES[self.month as usize])) .push(CalendarMonthView::new(&CalendarParams::new(), self.month)) ; // let area = Row::new() // .align_items(Alignment::Fill) // .push(Column::new() // .align_items(Alignment::Start) // .push(CalendarMonthView::new(6)) // .push(CalendarMonthView::new(6)) // ) // .push(content) // ; Container::new(content) .width(Length::Fill) .height(Length::Fill) .center_x() .center_y() .into() } }