feat(widget): add calendar widget

This commit is contained in:
Michael Aaron Murphy 2024-03-14 16:16:51 +01:00 committed by Michael Murphy
parent f0585c0126
commit d68488de47
6 changed files with 314 additions and 10 deletions

View file

@ -32,7 +32,7 @@ impl Page {
fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
let _ = tracing_log::LogTracer::init();
let input = vec![
(Page::Page1, "🖖 Hello from libcosmic.".into()),
(Page::Page2, "🌟 This is an example application.".into()),
@ -124,8 +124,7 @@ impl cosmic::Application for App {
let page_content = self
.nav_model
.active_data::<String>()
.map(String::as_str)
.unwrap_or("No page selected");
.map_or("No page selected", String::as_str);
let text = cosmic::widget::text(page_content);

View file

@ -0,0 +1,14 @@
[package]
name = "calendar"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
chrono = "0.4.35"
[dependencies.libcosmic]
path = "../../"
default-features = false
features = ["debug", "winit", "tokio", "xdg-portal", "wgpu"]

View file

@ -0,0 +1,136 @@
// Copyright 2024 System76 <info@system76.com>
// SPDX-License-Identifier: MPL-2.0
//! Calendar widget example
use chrono::{Datelike, Days, Local, Months, NaiveDate};
use cosmic::app::{Command, Core, Settings};
use cosmic::{executor, iced, ApplicationExt, Element};
/// Runs application with these settings
#[rustfmt::skip]
fn main() -> Result<(), Box<dyn std::error::Error>> {
cosmic::app::run::<App>(Settings::default(), ())?;
Ok(())
}
/// Messages that are used specifically by our [`App`].
#[derive(Clone, Debug)]
pub enum Message {
PrevMonth,
NextMonth,
DaySelected(u32),
}
/// The [`App`] stores application-specific state.
pub struct App {
core: Core,
date_selected: NaiveDate,
}
/// Implement [`cosmic::Application`] to integrate with COSMIC.
impl cosmic::Application for App {
/// Default async executor to use with the app.
type Executor = executor::Default;
/// Argument received [`cosmic::Application::new`].
type Flags = ();
/// Message type specific to our [`App`].
type Message = Message;
/// The unique application ID to supply to the window manager.
const APP_ID: &'static str = "org.cosmic.AppDemo";
fn core(&self) -> &Core {
&self.core
}
fn core_mut(&mut self) -> &mut Core {
&mut self.core
}
/// Creates the application, and optionally emits command on initialize.
fn init(core: Core, _input: Self::Flags) -> (Self, Command<Self::Message>) {
let now = Local::now();
let mut app = App {
core,
date_selected: NaiveDate::from(now.naive_local()),
};
let command = app.update_title();
(app, command)
}
/// Handle application events here.
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
match message {
Message::DaySelected(day) => {
let current = self.date_selected.day();
let new_date = if current < day {
self.date_selected
.checked_add_days(Days::new((day - current) as u64))
} else if current > day {
self.date_selected
.checked_sub_days(Days::new((current - day) as u64))
} else {
None
};
if let Some(new) = new_date {
self.date_selected = new;
}
}
Message::PrevMonth => {
self.date_selected = self
.date_selected
.checked_sub_months(Months::new(1))
.expect("valid naivedate");
}
Message::NextMonth => {
self.date_selected = self
.date_selected
.checked_add_months(Months::new(1))
.expect("valid naivedate");
}
}
Command::none()
}
/// Creates a view after each update.
fn view(&self) -> Element<Self::Message> {
let mut content = cosmic::widget::column().spacing(12);
let calendar = cosmic::widget::calendar(
&self.date_selected,
Message::PrevMonth,
Message::NextMonth,
|day| Message::DaySelected(day),
);
content = content.push(cosmic::widget::container(calendar).width(350));
let centered = cosmic::widget::container(content)
.width(iced::Length::Fill)
.height(iced::Length::Shrink)
.align_x(iced::alignment::Horizontal::Center)
.align_y(iced::alignment::Vertical::Center);
Element::from(centered)
}
}
impl App
where
Self: cosmic::Application,
{
fn update_title(&mut self) -> Command<Message> {
self.set_header_title(String::from("Calendar Demo"));
self.set_window_title(String::from("Calendar Demo"))
}
}