BlueGit

ict rusty-teams

feat(calendar): open a range without today at work start

Open, span change, navigation and date pick ask the grid to align. With
today in range it follows the clock as before; otherwise it scrolls to
work start minus 30 minutes instead of resting at midnight. Row height
now follows the time scale (15/30/60 min -> 120/96/64 px).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Joachim Mild · pushed by metis

Authored 2026-11-15 · 2 weeks ago

Hash
68fc471ca5763cebd1c46777a0d155cd59d591d6
Parent
55034608
3 files changed +103 −17
crates/app-call-model/src/calendar_now.rs +42 −0
@@ -1,3 +1,4 @@
1 use super::calendar_prefs::TimeScale;
1 2 use super::week_grid::CalendarSpan;
2 3
3 4 /// Fraction of the hour-grid viewport used to keep «now» below the top edge.
@@ -28,6 +29,31 @@ pub fn now_scroll_offset_y(
28 29 (bias - now_y).clamp(min_offset, 0.)
29 30 }
30 31
32 /// Height of one hour row for a time scale; finer scales get taller rows.
33 pub fn hour_height(scale: TimeScale) -> f32 {
34 match scale {
35 TimeScale::Min15 => 120.,
36 TimeScale::Min30 => 96.,
37 TimeScale::Min60 => 64.,
38 }
39 }
40
41 /// Minutes shown above work start when the grid opens on a range without today.
42 pub const WORK_START_LEAD_MINUTES: i64 = 30;
43
44 /// Vertical scroll offset (negative Y) that puts work start, minus a short lead, at the top.
45 pub fn work_start_scroll_offset_y(
46 work_start_minute: i64,
47 hour_height: f32,
48 viewport_height: f32,
49 ) -> f32 {
50 let day_height = 24. * hour_height;
51 let target =
52 (work_start_minute - WORK_START_LEAD_MINUTES).clamp(0, 24 * 60) as f32 / 60. * hour_height;
53 let min_offset = -(day_height - viewport_height.max(0.)).max(0.);
54 (-target).clamp(min_offset, 0.)
55 }
56
31 57 pub fn shows_hour_grid(span: CalendarSpan) -> bool {
32 58 !matches!(span, CalendarSpan::Month)
33 59 }
@@ -70,6 +96,22 @@ mod tests {
70 96 assert!((offset - (bias - now_y)).abs() < 0.01);
71 97 }
72 98
99 #[test]
100 fn work_start_sits_at_the_top_with_a_short_lead() {
101 let hour = hour_height(TimeScale::Min30);
102 assert_eq!(work_start_scroll_offset_y(8 * 60, hour, 480.), -7.5 * hour);
103 assert_eq!(work_start_scroll_offset_y(0, hour, 480.), 0.);
104 // A late start never scrolls past the end of the day.
105 let late = work_start_scroll_offset_y(23 * 60 + 30, hour, 480.);
106 assert_eq!(late, -(24. * hour - 480.));
107 }
108
109 #[test]
110 fn finer_time_scales_get_taller_rows() {
111 assert!(hour_height(TimeScale::Min15) > hour_height(TimeScale::Min30));
112 assert!(hour_height(TimeScale::Min30) > hour_height(TimeScale::Min60));
113 }
114
73 115 #[test]
74 116 fn midnight_stays_at_top() {
75 117 let offset = now_scroll_offset_y(0, 0, 120., 480., NOW_BIAS_FRACTION);
@@ -1,3 +1,4 @@
1use super::calendar_prefs::TimeScale;
1use super::week_grid::CalendarSpan;
2use super::week_grid::CalendarSpan;
2
3
3/// Fraction of the hour-grid viewport used to keep «now» below the top edge.
4/// Fraction of the hour-grid viewport used to keep «now» below the top edge.
@@ -28,6 +29,31 @@ pub fn now_scroll_offset_y(
28 (bias - now_y).clamp(min_offset, 0.)
29 (bias - now_y).clamp(min_offset, 0.)
29}
30}
30
31
32/// Height of one hour row for a time scale; finer scales get taller rows.
33pub fn hour_height(scale: TimeScale) -> f32 {
34 match scale {
35 TimeScale::Min15 => 120.,
36 TimeScale::Min30 => 96.,
37 TimeScale::Min60 => 64.,
38 }
39}
40
41/// Minutes shown above work start when the grid opens on a range without today.
42pub const WORK_START_LEAD_MINUTES: i64 = 30;
43
44/// Vertical scroll offset (negative Y) that puts work start, minus a short lead, at the top.
45pub fn work_start_scroll_offset_y(
46 work_start_minute: i64,
47 hour_height: f32,
48 viewport_height: f32,
49) -> f32 {
50 let day_height = 24. * hour_height;
51 let target =
52 (work_start_minute - WORK_START_LEAD_MINUTES).clamp(0, 24 * 60) as f32 / 60. * hour_height;
53 let min_offset = -(day_height - viewport_height.max(0.)).max(0.);
54 (-target).clamp(min_offset, 0.)
55}
56
31pub fn shows_hour_grid(span: CalendarSpan) -> bool {
57pub fn shows_hour_grid(span: CalendarSpan) -> bool {
32 !matches!(span, CalendarSpan::Month)
58 !matches!(span, CalendarSpan::Month)
33}
59}
@@ -70,6 +96,22 @@ mod tests {
70 assert!((offset - (bias - now_y)).abs() < 0.01);
96 assert!((offset - (bias - now_y)).abs() < 0.01);
71 }
97 }
72
98
99 #[test]
100 fn work_start_sits_at_the_top_with_a_short_lead() {
101 let hour = hour_height(TimeScale::Min30);
102 assert_eq!(work_start_scroll_offset_y(8 * 60, hour, 480.), -7.5 * hour);
103 assert_eq!(work_start_scroll_offset_y(0, hour, 480.), 0.);
104 // A late start never scrolls past the end of the day.
105 let late = work_start_scroll_offset_y(23 * 60 + 30, hour, 480.);
106 assert_eq!(late, -(24. * hour - 480.));
107 }
108
109 #[test]
110 fn finer_time_scales_get_taller_rows() {
111 assert!(hour_height(TimeScale::Min15) > hour_height(TimeScale::Min30));
112 assert!(hour_height(TimeScale::Min30) > hour_height(TimeScale::Min60));
113 }
114
73 #[test]
115 #[test]
74 fn midnight_stays_at_top() {
116 fn midnight_stays_at_top() {
75 let offset = now_scroll_offset_y(0, 0, 120., 480., NOW_BIAS_FRACTION);
117 let offset = now_scroll_offset_y(0, 0, 120., 480., NOW_BIAS_FRACTION);
crates/app-state/src/call/week.rs +18 −0
@@ -526,6 +526,24 @@ mod tests {
526 526 });
527 527 }
528 528
529 #[gpui_kit::test]
530 fn navigating_asks_the_grid_to_align_again(cx: &mut TestAppContext) {
531 cx.update(gpui_kit::init);
532 let store = Arc::new(Store::open_in_memory().unwrap());
533 let app = cx.update(|cx| cx.new(|_| AppState::new(store, Mode::default())));
534 cx.update(|cx| {
535 app.update(cx, |state, cx| {
536 state.open_calendar(cx);
537 let opened = state.calendar_scroll_to_now;
538 state.shift_calendar_week(1, cx);
539 assert_ne!(state.calendar_scroll_to_now, opened);
540 let shifted = state.calendar_scroll_to_now;
541 state.show_calendar_date(chrono::Local::now().date_naive(), cx);
542 assert_ne!(state.calendar_scroll_to_now, shifted);
543 })
544 });
545 }
546
529 547 #[test]
530 548 fn a_failed_source_keeps_its_previous_events() {
531 549 let previous = vec![
@@ -526,6 +526,24 @@ mod tests {
526 });
526 });
527 }
527 }
528
528
529 #[gpui_kit::test]
530 fn navigating_asks_the_grid_to_align_again(cx: &mut TestAppContext) {
531 cx.update(gpui_kit::init);
532 let store = Arc::new(Store::open_in_memory().unwrap());
533 let app = cx.update(|cx| cx.new(|_| AppState::new(store, Mode::default())));
534 cx.update(|cx| {
535 app.update(cx, |state, cx| {
536 state.open_calendar(cx);
537 let opened = state.calendar_scroll_to_now;
538 state.shift_calendar_week(1, cx);
539 assert_ne!(state.calendar_scroll_to_now, opened);
540 let shifted = state.calendar_scroll_to_now;
541 state.show_calendar_date(chrono::Local::now().date_naive(), cx);
542 assert_ne!(state.calendar_scroll_to_now, shifted);
543 })
544 });
545 }
546
529 #[test]
547 #[test]
530 fn a_failed_source_keeps_its_previous_events() {
548 fn a_failed_source_keeps_its_previous_events() {
531 let previous = vec![
549 let previous = vec![
crates/app/src/views/calendar_view.rs +43 −17
@@ -22,8 +22,8 @@ use super::rsvp_menu::rsvp_menu;
22 22 use super::scroll_edge::scroll_offset;
23 23 use crate::app_state::AppState;
24 24 use crate::call::calendar_now::{
25 DEFAULT_GRID_VIEWPORT, FollowNow, MANUAL_SCROLL_EPSILON, NOW_BIAS_FRACTION,
26 now_scroll_offset_y, offset_differs, shows_hour_grid,
25 DEFAULT_GRID_VIEWPORT, FollowNow, MANUAL_SCROLL_EPSILON, NOW_BIAS_FRACTION, hour_height,
26 now_scroll_offset_y, offset_differs, shows_hour_grid, work_start_scroll_offset_y,
27 27 };
28 28 use crate::call::week::CalendarLoadFailure;
29 29 use crate::call::week_grid::{
@@ -33,8 +33,6 @@ use crate::call::week_grid::{
33 33 use crate::i18n::tr;
34 34 use crate::theme;
35 35
36 const HOUR_HEIGHT: f32 = 120.;
37 const QUARTERS_PER_HOUR: usize = 4;
38 36 const QUARTER_LINE_OPACITY: f32 = 0.45;
39 37 const GUTTER_WIDTH: f32 = 50.;
40 38 const HEADER_HEIGHT: f32 = 52.;
@@ -63,6 +61,9 @@ pub struct CalendarView {
63 61 last_programmatic_offset: f32,
64 62 skip_manual_check: bool,
65 63 clock_running: bool,
64 /// Row height of one hour and minor lines per hour, from the time scale.
65 hour_height: f32,
66 steps_per_hour: usize,
66 67 _observation: Subscription,
67 68 _agent_observation: Option<Subscription>,
68 69 }
@@ -80,6 +81,8 @@ impl CalendarView {
80 81 last_programmatic_offset: 0.,
81 82 skip_manual_check: false,
82 83 clock_running: false,
84 hour_height: hour_height(Default::default()),
85 steps_per_hour: 2,
83 86 _observation: observation,
84 87 _agent_observation: agent_observation,
85 88 }
@@ -94,23 +97,41 @@ impl CalendarView {
94 97 let offset = now_scroll_offset_y(
95 98 local.hour(),
96 99 local.minute(),
97 HOUR_HEIGHT,
100 self.hour_height,
98 101 Self::grid_viewport_height(window),
99 102 NOW_BIAS_FRACTION,
100 103 );
104 self.set_programmatic_offset(offset);
105 }
106
107 /// A range without today opens at work start instead of midnight.
108 fn align_scroll_to_work_start(&mut self, work_start_minute: i64, window: &Window) {
109 let offset = work_start_scroll_offset_y(
110 work_start_minute,
111 self.hour_height,
112 Self::grid_viewport_height(window),
113 );
114 self.set_programmatic_offset(offset);
115 }
116
117 fn set_programmatic_offset(&mut self, offset: f32) {
101 118 self.scroll.set_offset(point(px(0.), px(offset)));
102 119 self.last_programmatic_offset = offset;
103 120 self.skip_manual_check = true;
104 121 }
105 122
106 123 fn sync_follow_from_state(&mut self, window: &Window, cx: &mut Context<Self>) {
107 let (scroll_to_now, span, today_visible) = {
124 let (scroll_to_now, span, today_visible, work_start) = {
108 125 let state = self.state.read(cx);
109 126 let today_visible = state.calendar_shows_today();
127 let prefs = &state.calendar_prefs;
128 self.hour_height = hour_height(prefs.time_scale);
129 self.steps_per_hour = prefs.time_scale.steps_per_hour() as usize;
110 130 (
111 131 state.calendar_scroll_to_now,
112 132 state.calendar_week.span,
113 133 today_visible,
134 prefs.work_hours.minute_range().0,
114 135 )
115 136 };
116 137 if !shows_hour_grid(span) {
@@ -120,7 +141,12 @@ impl CalendarView {
120 141 self.ensure_clock(cx);
121 142 if scroll_to_now != self.seen_scroll_to_now {
122 143 self.seen_scroll_to_now = scroll_to_now;
123 self.follow_now.request_align();
144 if today_visible {
145 self.follow_now.request_align();
146 } else {
147 self.align_scroll_to_work_start(work_start, window);
148 return;
149 }
124 150 }
125 151 if self.skip_manual_check {
126 152 self.skip_manual_check = false;
@@ -426,8 +452,8 @@ impl CalendarView {
426 452 day: NaiveDate,
427 453 send_response: bool,
428 454 ) -> impl IntoElement {
429 let top = block.start_minute as f32 / 60. * HOUR_HEIGHT;
430 let height = ((block.end_minute - block.start_minute) as f32 / 60. * HOUR_HEIGHT
455 let top = block.start_minute as f32 / 60. * self.hour_height;
456 let height = ((block.end_minute - block.start_minute) as f32 / 60. * self.hour_height
431 457 - BLOCK_GAP)
432 458 .max(14.);
433 459 let width = 1. / block.lanes as f32;
@@ -548,7 +574,9 @@ impl CalendarView {
548 574 send_response: bool,
549 575 cx: &mut Context<Self>,
550 576 ) -> impl IntoElement {
551 let total_height = 24. * HOUR_HEIGHT;
577 let row = self.hour_height;
578 let steps = self.steps_per_hour;
579 let total_height = 24. * row;
552 580 let twelve_hour = clock_format().contains("%p") || clock_format().contains("%P");
553 581 let now_minute = {
554 582 let local = now.with_timezone(&Local);
@@ -571,7 +599,7 @@ impl CalendarView {
571 599 .flex_none()
572 600 .children((0..24).map(|hour| {
573 601 div()
574 .h(px(HOUR_HEIGHT))
602 .h(px(row))
575 603 .pt(px(4.))
576 604 .flex()
577 605 .justify_center()
@@ -590,15 +618,13 @@ impl CalendarView {
590 618 .h_full()
591 619 .border_l_1()
592 620 .border_color(theme::border())
593 .children((1..24 * QUARTERS_PER_HOUR).map(|quarter| {
621 .children((1..24 * steps).map(|quarter| {
594 622 let line = div()
595 623 .absolute()
596 .top(
597 px(quarter as f32 * HOUR_HEIGHT / QUARTERS_PER_HOUR as f32),
598 )
624 .top(px(quarter as f32 * row / steps as f32))
599 625 .left_0()
600 626 .right_0();
601 if quarter % QUARTERS_PER_HOUR == 0 {
627 if quarter % steps == 0 {
602 628 line.h(px(1.)).bg(theme::border())
603 629 } else {
604 630 line.h(px(1.))
@@ -614,7 +640,7 @@ impl CalendarView {
614 640 )
615 641 }))
616 642 .when(day.date == today, |column| {
617 let now_top = now_minute as f32 / 60. * HOUR_HEIGHT;
643 let now_top = now_minute as f32 / 60. * row;
618 644 column
619 645 .child(
620 646 div()
@@ -22,8 +22,8 @@ use super::rsvp_menu::rsvp_menu;
22use super::scroll_edge::scroll_offset;
22use super::scroll_edge::scroll_offset;
23use crate::app_state::AppState;
23use crate::app_state::AppState;
24use crate::call::calendar_now::{
24use crate::call::calendar_now::{
25 DEFAULT_GRID_VIEWPORT, FollowNow, MANUAL_SCROLL_EPSILON, NOW_BIAS_FRACTION,
25 DEFAULT_GRID_VIEWPORT, FollowNow, MANUAL_SCROLL_EPSILON, NOW_BIAS_FRACTION, hour_height,
26 now_scroll_offset_y, offset_differs, shows_hour_grid,
26 now_scroll_offset_y, offset_differs, shows_hour_grid, work_start_scroll_offset_y,
27};
27};
28use crate::call::week::CalendarLoadFailure;
28use crate::call::week::CalendarLoadFailure;
29use crate::call::week_grid::{
29use crate::call::week_grid::{
@@ -33,8 +33,6 @@ use crate::call::week_grid::{
33use crate::i18n::tr;
33use crate::i18n::tr;
34use crate::theme;
34use crate::theme;
35
35
36const HOUR_HEIGHT: f32 = 120.;
37const QUARTERS_PER_HOUR: usize = 4;
38const QUARTER_LINE_OPACITY: f32 = 0.45;
36const QUARTER_LINE_OPACITY: f32 = 0.45;
39const GUTTER_WIDTH: f32 = 50.;
37const GUTTER_WIDTH: f32 = 50.;
40const HEADER_HEIGHT: f32 = 52.;
38const HEADER_HEIGHT: f32 = 52.;
@@ -63,6 +61,9 @@ pub struct CalendarView {
63 last_programmatic_offset: f32,
61 last_programmatic_offset: f32,
64 skip_manual_check: bool,
62 skip_manual_check: bool,
65 clock_running: bool,
63 clock_running: bool,
64 /// Row height of one hour and minor lines per hour, from the time scale.
65 hour_height: f32,
66 steps_per_hour: usize,
66 _observation: Subscription,
67 _observation: Subscription,
67 _agent_observation: Option<Subscription>,
68 _agent_observation: Option<Subscription>,
68}
69}
@@ -80,6 +81,8 @@ impl CalendarView {
80 last_programmatic_offset: 0.,
81 last_programmatic_offset: 0.,
81 skip_manual_check: false,
82 skip_manual_check: false,
82 clock_running: false,
83 clock_running: false,
84 hour_height: hour_height(Default::default()),
85 steps_per_hour: 2,
83 _observation: observation,
86 _observation: observation,
84 _agent_observation: agent_observation,
87 _agent_observation: agent_observation,
85 }
88 }
@@ -94,23 +97,41 @@ impl CalendarView {
94 let offset = now_scroll_offset_y(
97 let offset = now_scroll_offset_y(
95 local.hour(),
98 local.hour(),
96 local.minute(),
99 local.minute(),
97 HOUR_HEIGHT,
100 self.hour_height,
98 Self::grid_viewport_height(window),
101 Self::grid_viewport_height(window),
99 NOW_BIAS_FRACTION,
102 NOW_BIAS_FRACTION,
100 );
103 );
104 self.set_programmatic_offset(offset);
105 }
106
107 /// A range without today opens at work start instead of midnight.
108 fn align_scroll_to_work_start(&mut self, work_start_minute: i64, window: &Window) {
109 let offset = work_start_scroll_offset_y(
110 work_start_minute,
111 self.hour_height,
112 Self::grid_viewport_height(window),
113 );
114 self.set_programmatic_offset(offset);
115 }
116
117 fn set_programmatic_offset(&mut self, offset: f32) {
101 self.scroll.set_offset(point(px(0.), px(offset)));
118 self.scroll.set_offset(point(px(0.), px(offset)));
102 self.last_programmatic_offset = offset;
119 self.last_programmatic_offset = offset;
103 self.skip_manual_check = true;
120 self.skip_manual_check = true;
104 }
121 }
105
122
106 fn sync_follow_from_state(&mut self, window: &Window, cx: &mut Context<Self>) {
123 fn sync_follow_from_state(&mut self, window: &Window, cx: &mut Context<Self>) {
107 let (scroll_to_now, span, today_visible) = {
124 let (scroll_to_now, span, today_visible, work_start) = {
108 let state = self.state.read(cx);
125 let state = self.state.read(cx);
109 let today_visible = state.calendar_shows_today();
126 let today_visible = state.calendar_shows_today();
127 let prefs = &state.calendar_prefs;
128 self.hour_height = hour_height(prefs.time_scale);
129 self.steps_per_hour = prefs.time_scale.steps_per_hour() as usize;
110 (
130 (
111 state.calendar_scroll_to_now,
131 state.calendar_scroll_to_now,
112 state.calendar_week.span,
132 state.calendar_week.span,
113 today_visible,
133 today_visible,
134 prefs.work_hours.minute_range().0,
114 )
135 )
115 };
136 };
116 if !shows_hour_grid(span) {
137 if !shows_hour_grid(span) {
@@ -120,7 +141,12 @@ impl CalendarView {
120 self.ensure_clock(cx);
141 self.ensure_clock(cx);
121 if scroll_to_now != self.seen_scroll_to_now {
142 if scroll_to_now != self.seen_scroll_to_now {
122 self.seen_scroll_to_now = scroll_to_now;
143 self.seen_scroll_to_now = scroll_to_now;
123 self.follow_now.request_align();
144 if today_visible {
145 self.follow_now.request_align();
146 } else {
147 self.align_scroll_to_work_start(work_start, window);
148 return;
149 }
124 }
150 }
125 if self.skip_manual_check {
151 if self.skip_manual_check {
126 self.skip_manual_check = false;
152 self.skip_manual_check = false;
@@ -426,8 +452,8 @@ impl CalendarView {
426 day: NaiveDate,
452 day: NaiveDate,
427 send_response: bool,
453 send_response: bool,
428 ) -> impl IntoElement {
454 ) -> impl IntoElement {
429 let top = block.start_minute as f32 / 60. * HOUR_HEIGHT;
455 let top = block.start_minute as f32 / 60. * self.hour_height;
430 let height = ((block.end_minute - block.start_minute) as f32 / 60. * HOUR_HEIGHT
456 let height = ((block.end_minute - block.start_minute) as f32 / 60. * self.hour_height
431 - BLOCK_GAP)
457 - BLOCK_GAP)
432 .max(14.);
458 .max(14.);
433 let width = 1. / block.lanes as f32;
459 let width = 1. / block.lanes as f32;
@@ -548,7 +574,9 @@ impl CalendarView {
548 send_response: bool,
574 send_response: bool,
549 cx: &mut Context<Self>,
575 cx: &mut Context<Self>,
550 ) -> impl IntoElement {
576 ) -> impl IntoElement {
551 let total_height = 24. * HOUR_HEIGHT;
577 let row = self.hour_height;
578 let steps = self.steps_per_hour;
579 let total_height = 24. * row;
552 let twelve_hour = clock_format().contains("%p") || clock_format().contains("%P");
580 let twelve_hour = clock_format().contains("%p") || clock_format().contains("%P");
553 let now_minute = {
581 let now_minute = {
554 let local = now.with_timezone(&Local);
582 let local = now.with_timezone(&Local);
@@ -571,7 +599,7 @@ impl CalendarView {
571 .flex_none()
599 .flex_none()
572 .children((0..24).map(|hour| {
600 .children((0..24).map(|hour| {
573 div()
601 div()
574 .h(px(HOUR_HEIGHT))
602 .h(px(row))
575 .pt(px(4.))
603 .pt(px(4.))
576 .flex()
604 .flex()
577 .justify_center()
605 .justify_center()
@@ -590,15 +618,13 @@ impl CalendarView {
590 .h_full()
618 .h_full()
591 .border_l_1()
619 .border_l_1()
592 .border_color(theme::border())
620 .border_color(theme::border())
593 .children((1..24 * QUARTERS_PER_HOUR).map(|quarter| {
621 .children((1..24 * steps).map(|quarter| {
594 let line = div()
622 let line = div()
595 .absolute()
623 .absolute()
596 .top(
624 .top(px(quarter as f32 * row / steps as f32))
597 px(quarter as f32 * HOUR_HEIGHT / QUARTERS_PER_HOUR as f32),
598 )
599 .left_0()
625 .left_0()
600 .right_0();
626 .right_0();
601 if quarter % QUARTERS_PER_HOUR == 0 {
627 if quarter % steps == 0 {
602 line.h(px(1.)).bg(theme::border())
628 line.h(px(1.)).bg(theme::border())
603 } else {
629 } else {
604 line.h(px(1.))
630 line.h(px(1.))
@@ -614,7 +640,7 @@ impl CalendarView {
614 )
640 )
615 }))
641 }))
616 .when(day.date == today, |column| {
642 .when(day.date == today, |column| {
617 let now_top = now_minute as f32 / 60. * HOUR_HEIGHT;
643 let now_top = now_minute as f32 / 60. * row;
618 column
644 column
619 .child(
645 .child(
620 div()
646 div()