feat(calendar): Day, Work week and Week views
- View picker in the calendar header (Day / Work week / Week); back/forward step one day or one week - Switching keeps today in view when it was shown, otherwise the shown date Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@@ -24,6 +24,53 @@ pub fn week_start(date: NaiveDate) -> NaiveDate {
24
24
date - Duration::days(i64::from(date.weekday().num_days_from_monday()))
25
25
}
26
26
27
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
28
pub enum CalendarSpan {
29
Day,
30
#[default]
31
WorkWeek,
32
Week,
33
}
34
35
impl CalendarSpan {
36
pub const ALL: [CalendarSpan; 3] = [
37
CalendarSpan::Day,
38
CalendarSpan::WorkWeek,
39
CalendarSpan::Week,
40
];
41
42
pub fn label(self) -> &'static str {
43
match self {
44
CalendarSpan::Day => "Day",
45
CalendarSpan::WorkWeek => "Work week",
46
CalendarSpan::Week => "Week",
47
}
48
}
49
50
pub fn days(self) -> i64 {
51
match self {
52
CalendarSpan::Day => 1,
53
CalendarSpan::WorkWeek => WORK_WEEK_DAYS,
54
CalendarSpan::Week => 7,
55
}
56
}
57
58
pub fn start_for(self, date: NaiveDate) -> NaiveDate {
59
match self {
60
CalendarSpan::Day => date,
61
CalendarSpan::WorkWeek => work_week_for(date),
62
CalendarSpan::Week => week_start(date),
63
}
64
}
65
66
pub fn step(self) -> Duration {
67
match self {
68
CalendarSpan::Day => Duration::days(1),
69
CalendarSpan::WorkWeek | CalendarSpan::Week => Duration::weeks(1),
70
}
71
}
72
}
73
27
74
/// On a weekend the work week view shows the coming week, as Outlook does.
28
75
pub fn work_week_for(today: NaiveDate) -> NaiveDate {
29
76
match today.weekday() {
@@ -237,6 +284,19 @@ mod tests {
237
284
);
238
285
}
239
286
287
#[test]
288
fn spans_start_and_step_like_teams() {
289
let wednesday = NaiveDate::from_ymd_opt(2026, 11, 11).unwrap();
290
let monday = NaiveDate::from_ymd_opt(2026, 11, 9).unwrap();
291
assert_eq!(CalendarSpan::Day.start_for(wednesday), wednesday);
292
assert_eq!(CalendarSpan::WorkWeek.start_for(wednesday), monday);
293
assert_eq!(CalendarSpan::Week.start_for(wednesday), monday);
294
let sunday = NaiveDate::from_ymd_opt(2026, 11, 15).unwrap();
295
assert_eq!(CalendarSpan::Week.start_for(sunday), monday);
296
assert_eq!(CalendarSpan::Day.step(), Duration::days(1));
297
assert_eq!(CalendarSpan::Week.days(), 7);
298
}
299
240
300
#[test]
241
301
fn labels_read_like_teams() {
242
302
let monday = NaiveDate::from_ymd_opt(2026, 11, 30).unwrap();
@@ -24,6 +24,53 @@ pub fn week_start(date: NaiveDate) -> NaiveDate {24
date - Duration::days(i64::from(date.weekday().num_days_from_monday()))24
date - Duration::days(i64::from(date.weekday().num_days_from_monday()))25
}25
}26
26
27
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]28
pub enum CalendarSpan {29
Day,30
#[default]31
WorkWeek,32
Week,33
}34
35
impl CalendarSpan {36
pub const ALL: [CalendarSpan; 3] = [37
CalendarSpan::Day,38
CalendarSpan::WorkWeek,39
CalendarSpan::Week,40
];41
42
pub fn label(self) -> &'static str {43
match self {44
CalendarSpan::Day => "Day",45
CalendarSpan::WorkWeek => "Work week",46
CalendarSpan::Week => "Week",47
}48
}49
50
pub fn days(self) -> i64 {51
match self {52
CalendarSpan::Day => 1,53
CalendarSpan::WorkWeek => WORK_WEEK_DAYS,54
CalendarSpan::Week => 7,55
}56
}57
58
pub fn start_for(self, date: NaiveDate) -> NaiveDate {59
match self {60
CalendarSpan::Day => date,61
CalendarSpan::WorkWeek => work_week_for(date),62
CalendarSpan::Week => week_start(date),63
}64
}65
66
pub fn step(self) -> Duration {67
match self {68
CalendarSpan::Day => Duration::days(1),69
CalendarSpan::WorkWeek | CalendarSpan::Week => Duration::weeks(1),70
}71
}72
}73
27
/// On a weekend the work week view shows the coming week, as Outlook does.74
/// On a weekend the work week view shows the coming week, as Outlook does.28
pub fn work_week_for(today: NaiveDate) -> NaiveDate {75
pub fn work_week_for(today: NaiveDate) -> NaiveDate {29
match today.weekday() {76
match today.weekday() {@@ -237,6 +284,19 @@ mod tests {237
);284
);238
}285
}239
286
287
#[test]288
fn spans_start_and_step_like_teams() {289
let wednesday = NaiveDate::from_ymd_opt(2026, 11, 11).unwrap();290
let monday = NaiveDate::from_ymd_opt(2026, 11, 9).unwrap();291
assert_eq!(CalendarSpan::Day.start_for(wednesday), wednesday);292
assert_eq!(CalendarSpan::WorkWeek.start_for(wednesday), monday);293
assert_eq!(CalendarSpan::Week.start_for(wednesday), monday);294
let sunday = NaiveDate::from_ymd_opt(2026, 11, 15).unwrap();295
assert_eq!(CalendarSpan::Week.start_for(sunday), monday);296
assert_eq!(CalendarSpan::Day.step(), Duration::days(1));297
assert_eq!(CalendarSpan::Week.days(), 7);298
}299
240
#[test]300
#[test]241
fn labels_read_like_teams() {301
fn labels_read_like_teams() {242
let monday = NaiveDate::from_ymd_opt(2026, 11, 30).unwrap();302
let monday = NaiveDate::from_ymd_opt(2026, 11, 30).unwrap();@@ -4,7 +4,7 @@ use gpui_kit::Context;
4
4
5
5
use super::agenda::demo_events;
6
6
use super::calendar::demo_anchor;
7
use super::week_grid::{WORK_WEEK_DAYS, local_midnight, week_range, work_week_for};
7
use super::week_grid::{CalendarSpan, local_midnight, week_range, week_start};
8
8
use crate::app_state::AppState;
9
9
10
10
pub const CALENDAR_LOAD_FAILURE: &str = "We couldn't load your calendar.";
@@ -12,6 +12,7 @@ pub const CALENDAR_LOAD_FAILURE: &str = "We couldn't load your calendar.";
12
12
#[derive(Debug, Clone, Default)]
13
13
pub struct CalendarWeek {
14
14
pub start: Option<NaiveDate>,
15
pub span: CalendarSpan,
15
16
pub events: Vec<CalendarEvent>,
16
17
pub loading: bool,
17
18
request: u64,
@@ -21,33 +22,42 @@ impl AppState {
21
22
pub fn calendar_start(&self) -> NaiveDate {
22
23
self.calendar_week
23
24
.start
24
.unwrap_or_else(|| work_week_for(Local::now().date_naive()))
25
.unwrap_or_else(|| self.calendar_week.span.start_for(Local::now().date_naive()))
25
26
}
26
27
27
28
pub fn open_calendar(&mut self, cx: &mut Context<Self>) {
28
if self.calendar_week.start.is_none() {
29
self.calendar_week.start = Some(work_week_for(Local::now().date_naive()));
30
}
29
self.calendar_week.start = Some(self.calendar_start());
31
30
self.load_calendar_week(cx);
32
31
}
33
32
34
pub fn shift_calendar_week(&mut self, weeks: i64, cx: &mut Context<Self>) {
35
self.calendar_week.start = Some(self.calendar_start() + Duration::weeks(weeks));
33
pub fn shift_calendar_week(&mut self, steps: i32, cx: &mut Context<Self>) {
34
let step = self.calendar_week.span.step() * steps;
35
self.calendar_week.start = Some(self.calendar_start() + step);
36
36
self.load_calendar_week(cx);
37
37
}
38
38
39
39
pub fn calendar_to_today(&mut self, cx: &mut Context<Self>) {
40
self.calendar_week.start = Some(work_week_for(Local::now().date_naive()));
40
self.calendar_week.start = None;
41
self.open_calendar(cx);
42
}
43
44
pub fn set_calendar_span(&mut self, span: CalendarSpan, cx: &mut Context<Self>) {
45
let today = Local::now().date_naive();
46
let start = self.calendar_start();
47
let shown =
48
start <= today && today < start + Duration::days(self.calendar_week.span.days());
49
self.calendar_week.span = span;
50
self.calendar_week.start = Some(span.start_for(if shown { today } else { start }));
41
51
self.load_calendar_week(cx);
42
52
}
43
53
44
54
fn load_calendar_week(&mut self, cx: &mut Context<Self>) {
45
55
let start = self.calendar_start();
46
56
let offset = Local::now().offset().fix();
47
let (from, to) = week_range(start, WORK_WEEK_DAYS, offset);
57
let (from, to) = week_range(start, self.calendar_week.span.days(), offset);
48
58
if self.mode.demo {
49
59
let mut events = demo_events(demo_anchor());
50
events.extend(demo_week(start, offset));
60
events.extend(demo_week(week_start(start), offset));
51
61
events.extend(self.demo_meetings.iter().cloned());
52
62
events.retain(|event| event.end > from && event.start < to);
53
63
events.sort_by_key(|event| (event.start, event.end));
@@ -4,7 +4,7 @@ use gpui_kit::Context;4
4
5
use super::agenda::demo_events;5
use super::agenda::demo_events;6
use super::calendar::demo_anchor;6
use super::calendar::demo_anchor;7
use super::week_grid::{WORK_WEEK_DAYS, local_midnight, week_range, work_week_for};7
use super::week_grid::{CalendarSpan, local_midnight, week_range, week_start};8
use crate::app_state::AppState;8
use crate::app_state::AppState;9
9
10
pub const CALENDAR_LOAD_FAILURE: &str = "We couldn't load your calendar.";10
pub const CALENDAR_LOAD_FAILURE: &str = "We couldn't load your calendar.";@@ -12,6 +12,7 @@ pub const CALENDAR_LOAD_FAILURE: &str = "We couldn't load your calendar.";12
#[derive(Debug, Clone, Default)]12
#[derive(Debug, Clone, Default)]13
pub struct CalendarWeek {13
pub struct CalendarWeek {14
pub start: Option<NaiveDate>,14
pub start: Option<NaiveDate>,15
pub span: CalendarSpan,15
pub events: Vec<CalendarEvent>,16
pub events: Vec<CalendarEvent>,16
pub loading: bool,17
pub loading: bool,17
request: u64,18
request: u64,@@ -21,33 +22,42 @@ impl AppState {21
pub fn calendar_start(&self) -> NaiveDate {22
pub fn calendar_start(&self) -> NaiveDate {22
self.calendar_week23
self.calendar_week23
.start24
.start24
.unwrap_or_else(|| work_week_for(Local::now().date_naive()))25
.unwrap_or_else(|| self.calendar_week.span.start_for(Local::now().date_naive()))25
}26
}26
27
27
pub fn open_calendar(&mut self, cx: &mut Context<Self>) {28
pub fn open_calendar(&mut self, cx: &mut Context<Self>) {28
if self.calendar_week.start.is_none() {29
self.calendar_week.start = Some(self.calendar_start());29
self.calendar_week.start = Some(work_week_for(Local::now().date_naive()));30
}31
self.load_calendar_week(cx);30
self.load_calendar_week(cx);32
}31
}33
32
34
pub fn shift_calendar_week(&mut self, weeks: i64, cx: &mut Context<Self>) {33
pub fn shift_calendar_week(&mut self, steps: i32, cx: &mut Context<Self>) {35
self.calendar_week.start = Some(self.calendar_start() + Duration::weeks(weeks));34
let step = self.calendar_week.span.step() * steps;35
self.calendar_week.start = Some(self.calendar_start() + step);36
self.load_calendar_week(cx);36
self.load_calendar_week(cx);37
}37
}38
38
39
pub fn calendar_to_today(&mut self, cx: &mut Context<Self>) {39
pub fn calendar_to_today(&mut self, cx: &mut Context<Self>) {40
self.calendar_week.start = Some(work_week_for(Local::now().date_naive()));40
self.calendar_week.start = None;41
self.open_calendar(cx);42
}43
44
pub fn set_calendar_span(&mut self, span: CalendarSpan, cx: &mut Context<Self>) {45
let today = Local::now().date_naive();46
let start = self.calendar_start();47
let shown =48
start <= today && today < start + Duration::days(self.calendar_week.span.days());49
self.calendar_week.span = span;50
self.calendar_week.start = Some(span.start_for(if shown { today } else { start }));41
self.load_calendar_week(cx);51
self.load_calendar_week(cx);42
}52
}43
53
44
fn load_calendar_week(&mut self, cx: &mut Context<Self>) {54
fn load_calendar_week(&mut self, cx: &mut Context<Self>) {45
let start = self.calendar_start();55
let start = self.calendar_start();46
let offset = Local::now().offset().fix();56
let offset = Local::now().offset().fix();47
let (from, to) = week_range(start, WORK_WEEK_DAYS, offset);57
let (from, to) = week_range(start, self.calendar_week.span.days(), offset);48
if self.mode.demo {58
if self.mode.demo {49
let mut events = demo_events(demo_anchor());59
let mut events = demo_events(demo_anchor());50
events.extend(demo_week(start, offset));60
events.extend(demo_week(week_start(start), offset));51
events.extend(self.demo_meetings.iter().cloned());61
events.extend(self.demo_meetings.iter().cloned());52
events.retain(|event| event.end > from && event.start < to);62
events.retain(|event| event.end > from && event.start < to);53
events.sort_by_key(|event| (event.start, event.end));63
events.sort_by_key(|event| (event.start, event.end));@@ -3,7 +3,7 @@ use chrono::{DateTime, Local, NaiveDate, Offset, Timelike, Utc};
3
3
use gpui_kit::assets::IconName;
4
4
use gpui_kit::component::Sizable as _;
5
5
use gpui_kit::component::button::{Button, ButtonVariants as _};
6
use gpui_kit::component::menu::{ContextMenuExt as _, PopupMenu, PopupMenuItem};
6
use gpui_kit::component::menu::{ContextMenuExt as _, DropdownMenu as _, PopupMenu, PopupMenuItem};
7
7
use gpui_kit::component::{h_flex, v_flex};
8
8
use gpui_kit::prelude::FluentBuilder as _;
9
9
use gpui_kit::*;
@@ -14,7 +14,7 @@ use super::new_meeting_dialog::open_new_meeting;
14
14
use crate::app_state::AppState;
15
15
use crate::call::join_available;
16
16
use crate::call::week_grid::{
17
WORK_WEEK_DAYS, WeekBlock, WeekDay, hour_label, range_label, week_days,
17
CalendarSpan, WeekBlock, WeekDay, hour_label, range_label, week_days,
18
18
};
19
19
use crate::theme;
20
20
@@ -47,7 +47,8 @@ impl CalendarView {
47
47
}
48
48
}
49
49
50
fn header(&self, start: NaiveDate, compact: bool) -> impl IntoElement {
50
fn header(&self, start: NaiveDate, span: CalendarSpan, compact: bool) -> impl IntoElement {
51
let span_state = self.state.clone();
51
52
let label = |text: &'static str| (!compact).then_some(text);
52
53
let (today, back, forward) = (self.state.clone(), self.state.clone(), self.state.clone());
53
54
let (join_state, meet_state, new_state) =
@@ -82,7 +83,11 @@ impl CalendarView {
82
83
.small()
83
84
.ghost()
84
85
.icon(IconName::ChevronLeft)
85
.tooltip("Go to previous week")
86
.tooltip(if span == CalendarSpan::Day {
87
"Go to previous day"
88
} else {
89
"Go to previous week"
90
})
86
91
.on_click(move |_, _, cx| {
87
92
back.update(cx, |state, cx| state.shift_calendar_week(-1, cx))
88
93
}),
@@ -92,7 +97,11 @@ impl CalendarView {
92
97
.small()
93
98
.ghost()
94
99
.icon(IconName::ChevronRight)
95
.tooltip("Go to next week")
100
.tooltip(if span == CalendarSpan::Day {
101
"Go to next day"
102
} else {
103
"Go to next week"
104
})
96
105
.on_click(move |_, _, cx| {
97
106
forward.update(cx, |state, cx| state.shift_calendar_week(1, cx))
98
107
}),
@@ -104,7 +113,28 @@ impl CalendarView {
104
113
.truncate()
105
114
.text_size(px(14.))
106
115
.font_weight(FontWeight::SEMIBOLD)
107
.child(range_label(start, WORK_WEEK_DAYS)),
116
.child(range_label(start, span.days())),
117
)
118
.child(
119
Button::new("calendar-span")
120
.small()
121
.ghost()
122
.label(span.label())
123
.dropdown_caret(true)
124
.dropdown_menu(move |menu, _, _| {
125
CalendarSpan::ALL.iter().fold(menu, |menu, &choice| {
126
let state = span_state.clone();
127
menu.item(
128
PopupMenuItem::new(choice.label())
129
.checked(choice == span)
130
.on_click(move |_, _, cx| {
131
state.update(cx, |state, cx| {
132
state.set_calendar_span(choice, cx)
133
})
134
}),
135
)
136
})
137
}),
108
138
)
109
139
.child(
110
140
Button::new("calendar-join-id")
@@ -474,7 +504,8 @@ impl Render for CalendarView {
474
504
let events = state.calendar_week.events.clone();
475
505
let send_response = state.rsvp_send_response;
476
506
let offset = Local::now().offset().fix();
477
let days = week_days(&events, start, WORK_WEEK_DAYS, offset);
507
let span = state.calendar_week.span;
508
let days = week_days(&events, start, span.days(), offset);
478
509
let today = Local::now().date_naive();
479
510
let now = Utc::now();
480
511
v_flex()
@@ -486,7 +517,7 @@ impl Render for CalendarView {
486
517
.rounded(px(10.))
487
518
.bg(theme::surface())
488
519
.overflow_hidden()
489
.child(self.header(start, compact))
520
.child(self.header(start, span, compact))
490
521
.child(self.day_headers(&days, today))
491
522
.children(self.all_day_row(&days, &events))
492
523
.child(self.grid(&days, &events, today, now, send_response))
@@ -3,7 +3,7 @@ use chrono::{DateTime, Local, NaiveDate, Offset, Timelike, Utc};3
use gpui_kit::assets::IconName;3
use gpui_kit::assets::IconName;4
use gpui_kit::component::Sizable as _;4
use gpui_kit::component::Sizable as _;5
use gpui_kit::component::button::{Button, ButtonVariants as _};5
use gpui_kit::component::button::{Button, ButtonVariants as _};6
use gpui_kit::component::menu::{ContextMenuExt as _, PopupMenu, PopupMenuItem};6
use gpui_kit::component::menu::{ContextMenuExt as _, DropdownMenu as _, PopupMenu, PopupMenuItem};7
use gpui_kit::component::{h_flex, v_flex};7
use gpui_kit::component::{h_flex, v_flex};8
use gpui_kit::prelude::FluentBuilder as _;8
use gpui_kit::prelude::FluentBuilder as _;9
use gpui_kit::*;9
use gpui_kit::*;@@ -14,7 +14,7 @@ use super::new_meeting_dialog::open_new_meeting;14
use crate::app_state::AppState;14
use crate::app_state::AppState;15
use crate::call::join_available;15
use crate::call::join_available;16
use crate::call::week_grid::{16
use crate::call::week_grid::{17
WORK_WEEK_DAYS, WeekBlock, WeekDay, hour_label, range_label, week_days,17
CalendarSpan, WeekBlock, WeekDay, hour_label, range_label, week_days,18
};18
};19
use crate::theme;19
use crate::theme;20
20
@@ -47,7 +47,8 @@ impl CalendarView {47
}47
}48
}48
}49
49
50
fn header(&self, start: NaiveDate, compact: bool) -> impl IntoElement {50
fn header(&self, start: NaiveDate, span: CalendarSpan, compact: bool) -> impl IntoElement {51
let span_state = self.state.clone();51
let label = |text: &'static str| (!compact).then_some(text);52
let label = |text: &'static str| (!compact).then_some(text);52
let (today, back, forward) = (self.state.clone(), self.state.clone(), self.state.clone());53
let (today, back, forward) = (self.state.clone(), self.state.clone(), self.state.clone());53
let (join_state, meet_state, new_state) =54
let (join_state, meet_state, new_state) =@@ -82,7 +83,11 @@ impl CalendarView {82
.small()83
.small()83
.ghost()84
.ghost()84
.icon(IconName::ChevronLeft)85
.icon(IconName::ChevronLeft)85
.tooltip("Go to previous week")86
.tooltip(if span == CalendarSpan::Day {87
"Go to previous day"88
} else {89
"Go to previous week"90
})86
.on_click(move |_, _, cx| {91
.on_click(move |_, _, cx| {87
back.update(cx, |state, cx| state.shift_calendar_week(-1, cx))92
back.update(cx, |state, cx| state.shift_calendar_week(-1, cx))88
}),93
}),@@ -92,7 +97,11 @@ impl CalendarView {92
.small()97
.small()93
.ghost()98
.ghost()94
.icon(IconName::ChevronRight)99
.icon(IconName::ChevronRight)95
.tooltip("Go to next week")100
.tooltip(if span == CalendarSpan::Day {101
"Go to next day"102
} else {103
"Go to next week"104
})96
.on_click(move |_, _, cx| {105
.on_click(move |_, _, cx| {97
forward.update(cx, |state, cx| state.shift_calendar_week(1, cx))106
forward.update(cx, |state, cx| state.shift_calendar_week(1, cx))98
}),107
}),@@ -104,7 +113,28 @@ impl CalendarView {104
.truncate()113
.truncate()105
.text_size(px(14.))114
.text_size(px(14.))106
.font_weight(FontWeight::SEMIBOLD)115
.font_weight(FontWeight::SEMIBOLD)107
.child(range_label(start, WORK_WEEK_DAYS)),116
.child(range_label(start, span.days())),117
)118
.child(119
Button::new("calendar-span")120
.small()121
.ghost()122
.label(span.label())123
.dropdown_caret(true)124
.dropdown_menu(move |menu, _, _| {125
CalendarSpan::ALL.iter().fold(menu, |menu, &choice| {126
let state = span_state.clone();127
menu.item(128
PopupMenuItem::new(choice.label())129
.checked(choice == span)130
.on_click(move |_, _, cx| {131
state.update(cx, |state, cx| {132
state.set_calendar_span(choice, cx)133
})134
}),135
)136
})137
}),108
)138
)109
.child(139
.child(110
Button::new("calendar-join-id")140
Button::new("calendar-join-id")@@ -474,7 +504,8 @@ impl Render for CalendarView {474
let events = state.calendar_week.events.clone();504
let events = state.calendar_week.events.clone();475
let send_response = state.rsvp_send_response;505
let send_response = state.rsvp_send_response;476
let offset = Local::now().offset().fix();506
let offset = Local::now().offset().fix();477
let days = week_days(&events, start, WORK_WEEK_DAYS, offset);507
let span = state.calendar_week.span;508
let days = week_days(&events, start, span.days(), offset);478
let today = Local::now().date_naive();509
let today = Local::now().date_naive();479
let now = Utc::now();510
let now = Utc::now();480
v_flex()511
v_flex()@@ -486,7 +517,7 @@ impl Render for CalendarView {486
.rounded(px(10.))517
.rounded(px(10.))487
.bg(theme::surface())518
.bg(theme::surface())488
.overflow_hidden()519
.overflow_hidden()489
.child(self.header(start, compact))520
.child(self.header(start, span, compact))490
.child(self.day_headers(&days, today))521
.child(self.day_headers(&days, today))491
.children(self.all_day_row(&days, &events))522
.children(self.all_day_row(&days, &events))492
.child(self.grid(&days, &events, today, now, send_response))523
.child(self.grid(&days, &events, today, now, send_response))