mod agenda;
pub mod editor;
pub mod entry;
mod gesture;
pub mod layout;
mod month;
mod parts;
pub mod repeat;
pub mod rules;
pub mod schedule;
mod state;
pub mod style;
mod time_grid;
mod year;
use std::ops::Range;
use std::rc::Rc;
use iced::widget::{self, column, container, responsive, row, text};
use iced::{Alignment, Element, Length, Theme};
pub use entry::{
Change, Changed, Draft, Emphasis, Entries, Entry, EntryId, Occurrence, Tone, When, instant,
};
pub use jiff;
pub use parts::{
bar_label, day_id, entry_id, is_line, span_label, time_label, weekday_label, when_label,
};
pub use repeat::{Ends, Frequency, Repeat};
pub use state::{
Action, Allow, DOUBLE_CLICK, Event, FOUR_DAYS, Output, SLOT_MINUTES, Selection, State, View,
default_keymap,
};
pub use time_grid::{
GUTTER, SNAP_MINUTES, hour_offset, hours_id, point_at, scroll_id, scroll_to_hour,
};
use crate::forms::select::select;
use crate::lucide;
use crate::primitives::button::{self, Size, button};
use crate::primitives::icon_button::{self, icon_button};
use crate::theme::{self, Tokens, space, text_size};
use parts::{Handler, send};
pub const DEFAULT_HOUR_HEIGHT: f32 = 48.0;
pub const MIN_HOUR_HEIGHT: f32 = 36.0;
pub const HEADER_BREAK: f32 = 520.0;
pub const VIEW_MENU_WIDTH: f32 = 124.0;
pub fn today_button_id() -> widget::Id {
widget::Id::new("iced-cube-calendar-today")
}
pub fn previous_button_id() -> widget::Id {
widget::Id::new("iced-cube-calendar-previous")
}
pub fn next_button_id() -> widget::Id {
widget::Id::new("iced-cube-calendar-next")
}
pub fn view_menu_id() -> widget::Id {
widget::Id::new("iced-cube-calendar-views")
}
pub struct Calendar<'a, Message> {
state: &'a State,
on_event: Option<Box<dyn Fn(Event) -> Message + 'a>>,
width: Length,
height: Length,
header: bool,
leading: Option<Element<'a, Message>>,
trailing: Option<Element<'a, Message>>,
hours: (u8, u8),
hour_height: f32,
start_hour: Option<u8>,
}
pub fn calendar<Message>(state: &State) -> Calendar<'_, Message> {
Calendar {
state,
on_event: None,
width: Length::Fill,
height: Length::Fill,
header: true,
leading: None,
trailing: None,
hours: (0, 24),
hour_height: DEFAULT_HOUR_HEIGHT,
start_hour: None,
}
}
impl<Message> std::fmt::Debug for Calendar<'_, Message> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Calendar")
.field("view", &self.state.view())
.field("cursor", &self.state.cursor())
.field("width", &self.width)
.field("height", &self.height)
.field("header", &self.header)
.field("leading", &self.leading.is_some())
.field("trailing", &self.trailing.is_some())
.field("hours", &self.hours)
.field("hour_height", &self.hour_height)
.field("start_hour", &self.start_hour)
.field("enabled", &self.on_event.is_some())
.finish()
}
}
impl<'a, Message> Calendar<'a, Message> {
pub fn on_event(mut self, on_event: impl Fn(Event) -> Message + 'a) -> Self {
self.on_event = Some(Box::new(on_event));
self
}
pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = width.into();
self
}
pub fn height(mut self, height: impl Into<Length>) -> Self {
self.height = height.into();
self
}
pub fn header(mut self, header: bool) -> Self {
self.header = header;
self
}
pub fn header_leading(mut self, element: impl Into<Element<'a, Message>>) -> Self {
self.leading = Some(element.into());
self
}
pub fn header_trailing(mut self, element: impl Into<Element<'a, Message>>) -> Self {
self.trailing = Some(element.into());
self
}
pub fn start_at_hour(mut self, hour: u8) -> Self {
self.start_hour = Some(hour.min(23));
self
}
pub fn hours(mut self, hours: Range<u8>) -> Self {
let first = hours.start.min(23);
self.hours = (first, hours.end.clamp(first + 1, 24));
self
}
pub fn hour_height(mut self, height: f32) -> Self {
self.hour_height = height.max(MIN_HOUR_HEIGHT);
self
}
}
impl<'a, Message: Clone + 'a> From<Calendar<'a, Message>> for Element<'a, Message> {
fn from(calendar: Calendar<'a, Message>) -> Self {
let Calendar {
state,
on_event,
width,
height,
header,
leading,
trailing,
hours,
hour_height,
start_hour,
} = calendar;
let on_event: Handler<'a, Message> = on_event.map(Rc::from);
let options = time_grid::Options {
hours,
hour_height,
start_hour,
};
let body = match state.view() {
View::Month => month::view(state, &on_event),
View::Year => year::view(state, &on_event),
View::Agenda => agenda::view(state, &on_event),
View::Day | View::Week | View::FourDays => time_grid::view(state, options, &on_event),
};
let framed = container(body)
.width(Length::Fill)
.height(Length::Fill)
.padding(1)
.clip(true)
.style(|theme: &Theme| style::frame(&Tokens::of(theme)));
let mut content = column![].spacing(space::MD).width(width).height(height);
if header {
content = content.push(self::header(state, on_event.clone(), leading, trailing));
}
content.push(framed).into()
}
}
fn header<'a, Message: Clone + 'a>(
state: &'a State,
on_event: Handler<'a, Message>,
leading: Option<Element<'a, Message>>,
trailing: Option<Element<'a, Message>>,
) -> Element<'a, Message> {
let adaptive = responsive(move |size| {
let step = |glyph, label: &'static str, event: Event, id: widget::Id| {
icon_button(glyph)
.label(label)
.id(id)
.variant(icon_button::Variant::Ghost)
.size(Size::Md)
.on_press_maybe(send(&on_event, event))
};
let navigation = row![
container(
button("Today")
.variant(button::Variant::Outline)
.size(Size::Md)
.on_press_maybe(send(&on_event, Event::Today))
)
.id(today_button_id()),
step(
lucide!(ChevronLeft),
"Previous",
Event::Previous,
previous_button_id()
),
step(lucide!(ChevronRight), "Next", Event::Next, next_button_id()),
]
.spacing(space::XS)
.align_y(Alignment::Center);
let narrow = size.width < HEADER_BREAK;
let title = if narrow {
state.short_title()
} else {
state.title()
};
let title = text(title)
.size(if narrow { text_size::SM } else { text_size::MD })
.font(theme::semibold())
.wrapping(text::Wrapping::None)
.style(|theme: &Theme| text::Style {
color: Some(Tokens::of(theme).foreground),
});
let views = container(
select(&View::ALL[..], Some(state.view()))
.width(VIEW_MENU_WIDTH)
.on_select_maybe(
on_event
.clone()
.map(|handler| move |view| handler(Event::Show(view))),
),
)
.id(view_menu_id());
let title = container(title).width(Length::Fill).clip(true);
if narrow {
let lead = row![title, views]
.spacing(space::SM)
.align_y(Alignment::Center);
return column![lead, navigation].spacing(space::SM).into();
}
row![navigation, title, views]
.spacing(space::MD)
.align_y(Alignment::Center)
.into()
})
.width(Length::Fill)
.height(Length::Shrink);
if leading.is_none() && trailing.is_none() {
return adaptive.into();
}
let mut header = row![].spacing(space::XS).align_y(Alignment::Center);
if let Some(leading) = leading {
header = header.push(leading);
}
header = header.push(adaptive);
if let Some(trailing) = trailing {
header = header.push(trailing);
}
header.width(Length::Fill).into()
}
#[cfg(test)]
mod tests {
use super::*;
use jiff::civil::date;
#[test]
fn builder_defaults_and_options() {
let state = State::new(date(2026, 11, 19));
let calendar: Calendar<'_, Event> = calendar(&state);
assert_eq!(calendar.width, Length::Fill);
assert_eq!(calendar.height, Length::Fill);
assert!(calendar.header);
assert_eq!(calendar.hours, (0, 24));
assert_eq!(calendar.hour_height, DEFAULT_HOUR_HEIGHT);
assert!(calendar.on_event.is_none());
let calendar = calendar
.width(400)
.height(300)
.header(false)
.hours(8..18)
.hour_height(60.0)
.on_event(|event| event);
assert_eq!(calendar.width, Length::Fixed(400.0));
assert_eq!(calendar.height, Length::Fixed(300.0));
assert!(!calendar.header);
assert_eq!(calendar.hours, (8, 18));
assert_eq!(calendar.hour_height, 60.0);
assert!(calendar.on_event.is_some());
assert!(format!("{calendar:?}").contains("enabled: true"));
}
#[test]
fn hours_and_hour_height_stay_in_range() {
let state = State::new(date(2026, 11, 19));
let hours = |range: Range<u8>| calendar::<Event>(&state).hours(range).hours;
assert_eq!(hours(0..24), (0, 24));
assert_eq!(hours(9..9), (9, 10));
assert_eq!(hours(20..99), (20, 24));
assert_eq!(hours(40..50), (23, 24));
#[allow(clippy::reversed_empty_ranges)]
let backwards = hours(18..8);
assert_eq!(backwards, (18, 19));
assert_eq!(
calendar::<Event>(&state).hour_height(2.0).hour_height,
MIN_HOUR_HEIGHT
);
}
}