feat(calendar): scroll day/week view to local clock time (#14)
@@ -0,0 +1,112 @@
1
use super::week_grid::CalendarSpan;
2
3
/// Fraction of the hour-grid viewport used to keep «now» below the top edge.
4
pub const NOW_BIAS_FRACTION: f32 = 1. / 3.;
5
6
/// Fallback grid height before layout reports a real viewport.
7
pub const DEFAULT_GRID_VIEWPORT: f32 = 480.;
8
9
/// Pixels of scroll-offset slack before a change counts as manual scroll.
10
pub const MANUAL_SCROLL_EPSILON: f32 = 2.;
11
12
/// Vertical scroll offset (negative Y) so local wall time sits near `bias_fraction`
13
/// down the visible hour grid. Clamped to the day extent.
14
pub fn now_scroll_offset_y(
15
hour: u32,
16
minute: u32,
17
hour_height: f32,
18
viewport_height: f32,
19
bias_fraction: f32,
20
) -> f32 {
21
let hour = hour.min(23);
22
let minute = minute.min(59);
23
let day_height = 24. * hour_height;
24
let now_y = (hour as f32 + minute as f32 / 60.) * hour_height;
25
let viewport = viewport_height.max(0.);
26
let bias = (viewport * bias_fraction.clamp(0., 1.)).clamp(0., viewport);
27
let min_offset = -(day_height - viewport).max(0.);
28
(bias - now_y).clamp(min_offset, 0.)
29
}
30
31
pub fn shows_hour_grid(span: CalendarSpan) -> bool {
32
!matches!(span, CalendarSpan::Month)
33
}
34
35
/// Whether auto-follow should keep aligning while today is on screen.
36
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
37
pub struct FollowNow {
38
pub active: bool,
39
}
40
41
impl FollowNow {
42
pub fn request_align(&mut self) {
43
self.active = true;
44
}
45
46
pub fn note_manual_scroll(&mut self) {
47
self.active = false;
48
}
49
50
pub fn should_follow(self, today_visible: bool) -> bool {
51
self.active && today_visible
52
}
53
}
54
55
pub fn offset_differs(actual: f32, expected: f32, epsilon: f32) -> bool {
56
(actual - expected).abs() > epsilon
57
}
58
59
#[cfg(test)]
60
mod tests {
61
use super::*;
62
63
#[test]
64
fn noon_centers_near_bias_without_clamp() {
65
let hour_height = 120.;
66
let viewport = 480.;
67
let offset = now_scroll_offset_y(12, 0, hour_height, viewport, NOW_BIAS_FRACTION);
68
let now_y = 12. * hour_height;
69
let bias = viewport * NOW_BIAS_FRACTION;
70
assert!((offset - (bias - now_y)).abs() < 0.01);
71
}
72
73
#[test]
74
fn midnight_stays_at_top() {
75
let offset = now_scroll_offset_y(0, 0, 120., 480., NOW_BIAS_FRACTION);
76
assert_eq!(offset, 0.);
77
}
78
79
#[test]
80
fn late_evening_clamps_to_day_end() {
81
let hour_height = 120.;
82
let viewport = 480.;
83
let day_height = 24. * hour_height;
84
let min_offset = -(day_height - viewport);
85
let offset = now_scroll_offset_y(23, 50, hour_height, viewport, NOW_BIAS_FRACTION);
86
assert!((offset - min_offset).abs() < 0.01);
87
}
88
89
#[test]
90
fn month_has_no_hour_grid() {
91
assert!(!shows_hour_grid(CalendarSpan::Month));
92
assert!(shows_hour_grid(CalendarSpan::Day));
93
assert!(shows_hour_grid(CalendarSpan::WorkWeek));
94
}
95
96
#[test]
97
fn follow_turns_off_on_manual_scroll_and_back_on_align() {
98
let mut follow = FollowNow { active: true };
99
assert!(follow.should_follow(true));
100
follow.note_manual_scroll();
101
assert!(!follow.should_follow(true));
102
follow.request_align();
103
assert!(follow.should_follow(true));
104
assert!(!follow.should_follow(false));
105
}
106
107
#[test]
108
fn offset_diff_uses_epsilon() {
109
assert!(!offset_differs(-100., -100.5, MANUAL_SCROLL_EPSILON));
110
assert!(offset_differs(-100., -110., MANUAL_SCROLL_EPSILON));
111
}
112
}
@@ -0,0 +1,112 @@1
use super::week_grid::CalendarSpan;2
3
/// Fraction of the hour-grid viewport used to keep «now» below the top edge.4
pub const NOW_BIAS_FRACTION: f32 = 1. / 3.;5
6
/// Fallback grid height before layout reports a real viewport.7
pub const DEFAULT_GRID_VIEWPORT: f32 = 480.;8
9
/// Pixels of scroll-offset slack before a change counts as manual scroll.10
pub const MANUAL_SCROLL_EPSILON: f32 = 2.;11
12
/// Vertical scroll offset (negative Y) so local wall time sits near `bias_fraction`13
/// down the visible hour grid. Clamped to the day extent.14
pub fn now_scroll_offset_y(15
hour: u32,16
minute: u32,17
hour_height: f32,18
viewport_height: f32,19
bias_fraction: f32,20
) -> f32 {21
let hour = hour.min(23);22
let minute = minute.min(59);23
let day_height = 24. * hour_height;24
let now_y = (hour as f32 + minute as f32 / 60.) * hour_height;25
let viewport = viewport_height.max(0.);26
let bias = (viewport * bias_fraction.clamp(0., 1.)).clamp(0., viewport);27
let min_offset = -(day_height - viewport).max(0.);28
(bias - now_y).clamp(min_offset, 0.)29
}30
31
pub fn shows_hour_grid(span: CalendarSpan) -> bool {32
!matches!(span, CalendarSpan::Month)33
}34
35
/// Whether auto-follow should keep aligning while today is on screen.36
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]37
pub struct FollowNow {38
pub active: bool,39
}40
41
impl FollowNow {42
pub fn request_align(&mut self) {43
self.active = true;44
}45
46
pub fn note_manual_scroll(&mut self) {47
self.active = false;48
}49
50
pub fn should_follow(self, today_visible: bool) -> bool {51
self.active && today_visible52
}53
}54
55
pub fn offset_differs(actual: f32, expected: f32, epsilon: f32) -> bool {56
(actual - expected).abs() > epsilon57
}58
59
#[cfg(test)]60
mod tests {61
use super::*;62
63
#[test]64
fn noon_centers_near_bias_without_clamp() {65
let hour_height = 120.;66
let viewport = 480.;67
let offset = now_scroll_offset_y(12, 0, hour_height, viewport, NOW_BIAS_FRACTION);68
let now_y = 12. * hour_height;69
let bias = viewport * NOW_BIAS_FRACTION;70
assert!((offset - (bias - now_y)).abs() < 0.01);71
}72
73
#[test]74
fn midnight_stays_at_top() {75
let offset = now_scroll_offset_y(0, 0, 120., 480., NOW_BIAS_FRACTION);76
assert_eq!(offset, 0.);77
}78
79
#[test]80
fn late_evening_clamps_to_day_end() {81
let hour_height = 120.;82
let viewport = 480.;83
let day_height = 24. * hour_height;84
let min_offset = -(day_height - viewport);85
let offset = now_scroll_offset_y(23, 50, hour_height, viewport, NOW_BIAS_FRACTION);86
assert!((offset - min_offset).abs() < 0.01);87
}88
89
#[test]90
fn month_has_no_hour_grid() {91
assert!(!shows_hour_grid(CalendarSpan::Month));92
assert!(shows_hour_grid(CalendarSpan::Day));93
assert!(shows_hour_grid(CalendarSpan::WorkWeek));94
}95
96
#[test]97
fn follow_turns_off_on_manual_scroll_and_back_on_align() {98
let mut follow = FollowNow { active: true };99
assert!(follow.should_follow(true));100
follow.note_manual_scroll();101
assert!(!follow.should_follow(true));102
follow.request_align();103
assert!(follow.should_follow(true));104
assert!(!follow.should_follow(false));105
}106
107
#[test]108
fn offset_diff_uses_epsilon() {109
assert!(!offset_differs(-100., -100.5, MANUAL_SCROLL_EPSILON));110
assert!(offset_differs(-100., -110., MANUAL_SCROLL_EPSILON));111
}112
}@@ -1,6 +1,7 @@
1
1
pub mod agenda;
2
2
pub mod background;
3
3
pub mod bubbles;
4
pub mod calendar_now;
4
5
pub mod caption_style;
5
6
pub mod demo;
6
7
pub mod demo_scene;
@@ -1,6 +1,7 @@1
pub mod agenda;1
pub mod agenda;2
pub mod background;2
pub mod background;3
pub mod bubbles;3
pub mod bubbles;4
pub mod calendar_now;4
pub mod caption_style;5
pub mod caption_style;5
pub mod demo;6
pub mod demo;6
pub mod demo_scene;7
pub mod demo_scene;@@ -209,6 +209,8 @@ pub struct AppState {
209
209
pub meeting_toasts: crate::call::MeetingToasts,
210
210
pub demo_meetings: Vec<chatsvc::CalendarEvent>,
211
211
pub calendar_week: crate::call::week::CalendarWeek,
212
/// Bumped when the hour grid should align to local «now» (open / Today / day-week span).
213
pub calendar_scroll_to_now: u64,
212
214
pub call_backgrounds: crate::call::BackgroundLibrary,
213
215
pub transfer_picker: Option<AnyView>,
214
216
pub transcript_panel: Option<AnyView>,
@@ -369,6 +371,7 @@ impl AppState {
369
371
meeting_toasts: crate::call::MeetingToasts::default(),
370
372
demo_meetings: Vec::new(),
371
373
calendar_week: crate::call::week::CalendarWeek::default(),
374
calendar_scroll_to_now: 0,
372
375
call_backgrounds: if mode.demo {
373
376
crate::call::demo_library()
374
377
} else {
@@ -209,6 +209,8 @@ pub struct AppState {209
pub meeting_toasts: crate::call::MeetingToasts,209
pub meeting_toasts: crate::call::MeetingToasts,210
pub demo_meetings: Vec<chatsvc::CalendarEvent>,210
pub demo_meetings: Vec<chatsvc::CalendarEvent>,211
pub calendar_week: crate::call::week::CalendarWeek,211
pub calendar_week: crate::call::week::CalendarWeek,212
/// Bumped when the hour grid should align to local «now» (open / Today / day-week span).213
pub calendar_scroll_to_now: u64,212
pub call_backgrounds: crate::call::BackgroundLibrary,214
pub call_backgrounds: crate::call::BackgroundLibrary,213
pub transfer_picker: Option<AnyView>,215
pub transfer_picker: Option<AnyView>,214
pub transcript_panel: Option<AnyView>,216
pub transcript_panel: Option<AnyView>,@@ -369,6 +371,7 @@ impl AppState {369
meeting_toasts: crate::call::MeetingToasts::default(),371
meeting_toasts: crate::call::MeetingToasts::default(),370
demo_meetings: Vec::new(),372
demo_meetings: Vec::new(),371
calendar_week: crate::call::week::CalendarWeek::default(),373
calendar_week: crate::call::week::CalendarWeek::default(),374
calendar_scroll_to_now: 0,372
call_backgrounds: if mode.demo {375
call_backgrounds: if mode.demo {373
crate::call::demo_library()376
crate::call::demo_library()374
} else {377
} else {@@ -12,34 +12,32 @@ pub mod overlay_layout;
12
12
pub mod people_actions;
13
13
pub mod prejoin;
14
14
pub mod record_load;
15
pub mod remote_command;
16
15
pub mod reminder;
16
pub mod remote_command;
17
pub mod schedule;
17
18
pub mod share_actions;
18
19
pub mod share_overlay;
19
20
pub mod speed_dial;
20
pub mod schedule;
21
21
pub mod target;
22
22
pub mod view_actions;
23
23
pub mod week;
24
24
25
25
pub use app_call_model::{
26
agenda, background, bubbles, caption_style, demo, end_screen, fullscreen, health, media, mic,
27
model, options_dialog, people, preview, record_rights,
28
recording_menu, report, ring, share_view, stage_layout, stage_split, transcript, view_settings,
29
week_grid,
26
agenda, background, bubbles, calendar_now, caption_style, demo, end_screen, fullscreen, health,
27
media, mic, model, options_dialog, people, preview, record_rights, recording_menu, report, ring,
28
share_view, stage_layout, stage_split, transcript, view_settings, week_grid,
30
29
};
31
30
32
31
pub use actions::{
33
32
apply_sound_output, load_camera, load_camera_effects, load_input, load_output, load_share_sound,
34
33
};
35
pub use mic::load_mic;
36
34
pub use agenda::{AgendaRow, fetch_window, join_available, response_label};
37
pub use caption_style::{CaptionPosition, CaptionSize};
38
35
pub use background::{
39
36
BackgroundLibrary, BackgroundPick, default_cache_directory, demo_library, load_background,
40
37
load_custom_backgrounds,
41
38
};
42
39
pub use bubbles::{BUBBLE_SHOWN, shown_bubbles};
40
pub use caption_style::{CaptionPosition, CaptionSize};
43
41
pub use demo::DemoScene;
44
42
pub use features::TransferCandidate;
45
43
pub use health::{HealthBanner, level_text, media_text, network_rows, transport_text};
@@ -47,29 +45,33 @@ pub use history::CallRow;
47
45
pub use launcher::CallLauncher;
48
46
pub use list::{CALL_ROW_HEIGHT, CallsList, EMPTY_HEIGHT, HistoryFilter, ListEntry};
49
47
pub use media::CallMedia;
48
pub use mic::load_mic;
49
pub use model::ConsultCall;
50
50
pub use model::{
51
51
ActiveCall, CAPTION_SHOWN, CallKind, CallModel, Focus, REACTION_SHOWN, Tile, TileSize,
52
52
TileState,
53
53
};
54
pub use model::ConsultCall;
55
54
pub use options_dialog::OptionsDialog;
56
pub use prejoin::{AudioChoice, JoinOptions, PendingJoin, load_join_options};
57
55
pub use people::{PeopleSections, PersonRow, people_sections};
58
pub use share_actions::ZoomStep;
59
pub use speed_dial::SpeedDialEntry;
60
pub use share_view::{SHARE_MOTION_META_KEY, SHARE_STANDOUT_META_KEY, ShareStage, load_flag};
56
pub use prejoin::{AudioChoice, JoinOptions, PendingJoin, load_join_options};
61
57
pub use preview::{CameraPreview, preview_backgrounds};
58
pub use record_rights::{RecordBlock, RecordFeature};
59
pub use recording_menu::{RecordAction, RecordingChip, record_actions, recording_chip};
62
60
pub use reminder::{MeetingToasts, ReminderPhase, subtitle as meeting_subtitle};
61
pub use report::load_enabled as load_report_enabled;
63
62
pub use ring::{MissedCall, RingEntry, Rings};
64
pub use stage_split::{
65
GRID_PADDING, STRIP_GAP, GalleryPage, SpeakerSplit, StripSplit, TILE_GAP, gallery_page,
66
measured_stage, speaker_split, strip_split, video_wants,
63
pub use schedule::{FormValues, Invitee, default_window, draft_from_form};
64
pub use share_actions::ZoomStep;
65
pub use share_view::{
66
SHARE_MOTION_META_KEY, SHARE_STANDOUT_META_KEY, STRIP_DEFAULT_WIDTH, ShareStage, load_flag,
67
load_strip_width,
67
68
};
69
pub use speed_dial::SpeedDialEntry;
68
70
pub use stage_layout::{contain_size, focus_stage_size, gallery_grid};
69
pub use schedule::{FormValues, Invitee, default_window, draft_from_form};
70
pub use record_rights::{RecordBlock, RecordFeature};
71
pub use recording_menu::{RecordAction, RecordingChip, record_actions, recording_chip};
72
pub use report::load_enabled as load_report_enabled;
71
pub use stage_split::{
72
GRID_PADDING, STRIP_GAP, SpeakerSplit, StripSplit, TILE_GAP, gallery_split, measured_stage,
73
speaker_split, strip_split, video_wants,
74
};
73
75
pub use target::{is_echo_chat, plan_for_chat};
74
76
pub use transcript::clock_label;
75
77
pub use view_settings::{CallLayout, ViewSettings};
@@ -12,34 +12,32 @@ pub mod overlay_layout;12
pub mod people_actions;12
pub mod people_actions;13
pub mod prejoin;13
pub mod prejoin;14
pub mod record_load;14
pub mod record_load;15
pub mod remote_command;16
pub mod reminder;15
pub mod reminder;16
pub mod remote_command;17
pub mod schedule;17
pub mod share_actions;18
pub mod share_actions;18
pub mod share_overlay;19
pub mod share_overlay;19
pub mod speed_dial;20
pub mod speed_dial;20
pub mod schedule;21
pub mod target;21
pub mod target;22
pub mod view_actions;22
pub mod view_actions;23
pub mod week;23
pub mod week;24
24
25
pub use app_call_model::{25
pub use app_call_model::{26
agenda, background, bubbles, caption_style, demo, end_screen, fullscreen, health, media, mic,26
agenda, background, bubbles, calendar_now, caption_style, demo, end_screen, fullscreen, health,27
model, options_dialog, people, preview, record_rights,27
media, mic, model, options_dialog, people, preview, record_rights, recording_menu, report, ring,28
recording_menu, report, ring, share_view, stage_layout, stage_split, transcript, view_settings,28
share_view, stage_layout, stage_split, transcript, view_settings, week_grid,29
week_grid,30
};29
};31
30
32
pub use actions::{31
pub use actions::{33
apply_sound_output, load_camera, load_camera_effects, load_input, load_output, load_share_sound,32
apply_sound_output, load_camera, load_camera_effects, load_input, load_output, load_share_sound,34
};33
};35
pub use mic::load_mic;36
pub use agenda::{AgendaRow, fetch_window, join_available, response_label};34
pub use agenda::{AgendaRow, fetch_window, join_available, response_label};37
pub use caption_style::{CaptionPosition, CaptionSize};38
pub use background::{35
pub use background::{39
BackgroundLibrary, BackgroundPick, default_cache_directory, demo_library, load_background,36
BackgroundLibrary, BackgroundPick, default_cache_directory, demo_library, load_background,40
load_custom_backgrounds,37
load_custom_backgrounds,41
};38
};42
pub use bubbles::{BUBBLE_SHOWN, shown_bubbles};39
pub use bubbles::{BUBBLE_SHOWN, shown_bubbles};40
pub use caption_style::{CaptionPosition, CaptionSize};43
pub use demo::DemoScene;41
pub use demo::DemoScene;44
pub use features::TransferCandidate;42
pub use features::TransferCandidate;45
pub use health::{HealthBanner, level_text, media_text, network_rows, transport_text};43
pub use health::{HealthBanner, level_text, media_text, network_rows, transport_text};@@ -47,29 +45,33 @@ pub use history::CallRow;47
pub use launcher::CallLauncher;45
pub use launcher::CallLauncher;48
pub use list::{CALL_ROW_HEIGHT, CallsList, EMPTY_HEIGHT, HistoryFilter, ListEntry};46
pub use list::{CALL_ROW_HEIGHT, CallsList, EMPTY_HEIGHT, HistoryFilter, ListEntry};49
pub use media::CallMedia;47
pub use media::CallMedia;48
pub use mic::load_mic;49
pub use model::ConsultCall;50
pub use model::{50
pub use model::{51
ActiveCall, CAPTION_SHOWN, CallKind, CallModel, Focus, REACTION_SHOWN, Tile, TileSize,51
ActiveCall, CAPTION_SHOWN, CallKind, CallModel, Focus, REACTION_SHOWN, Tile, TileSize,52
TileState,52
TileState,53
};53
};54
pub use model::ConsultCall;55
pub use options_dialog::OptionsDialog;54
pub use options_dialog::OptionsDialog;56
pub use prejoin::{AudioChoice, JoinOptions, PendingJoin, load_join_options};57
pub use people::{PeopleSections, PersonRow, people_sections};55
pub use people::{PeopleSections, PersonRow, people_sections};58
pub use share_actions::ZoomStep;56
pub use prejoin::{AudioChoice, JoinOptions, PendingJoin, load_join_options};59
pub use speed_dial::SpeedDialEntry;60
pub use share_view::{SHARE_MOTION_META_KEY, SHARE_STANDOUT_META_KEY, ShareStage, load_flag};61
pub use preview::{CameraPreview, preview_backgrounds};57
pub use preview::{CameraPreview, preview_backgrounds};58
pub use record_rights::{RecordBlock, RecordFeature};59
pub use recording_menu::{RecordAction, RecordingChip, record_actions, recording_chip};62
pub use reminder::{MeetingToasts, ReminderPhase, subtitle as meeting_subtitle};60
pub use reminder::{MeetingToasts, ReminderPhase, subtitle as meeting_subtitle};61
pub use report::load_enabled as load_report_enabled;63
pub use ring::{MissedCall, RingEntry, Rings};62
pub use ring::{MissedCall, RingEntry, Rings};64
pub use stage_split::{63
pub use schedule::{FormValues, Invitee, default_window, draft_from_form};65
GRID_PADDING, STRIP_GAP, GalleryPage, SpeakerSplit, StripSplit, TILE_GAP, gallery_page,64
pub use share_actions::ZoomStep;66
measured_stage, speaker_split, strip_split, video_wants,65
pub use share_view::{66
SHARE_MOTION_META_KEY, SHARE_STANDOUT_META_KEY, STRIP_DEFAULT_WIDTH, ShareStage, load_flag,67
load_strip_width,67
};68
};69
pub use speed_dial::SpeedDialEntry;68
pub use stage_layout::{contain_size, focus_stage_size, gallery_grid};70
pub use stage_layout::{contain_size, focus_stage_size, gallery_grid};69
pub use schedule::{FormValues, Invitee, default_window, draft_from_form};71
pub use stage_split::{70
pub use record_rights::{RecordBlock, RecordFeature};72
GRID_PADDING, STRIP_GAP, SpeakerSplit, StripSplit, TILE_GAP, gallery_split, measured_stage,71
pub use recording_menu::{RecordAction, RecordingChip, record_actions, recording_chip};73
speaker_split, strip_split, video_wants,72
pub use report::load_enabled as load_report_enabled;74
};73
pub use target::{is_echo_chat, plan_for_chat};75
pub use target::{is_echo_chat, plan_for_chat};74
pub use transcript::clock_label;76
pub use transcript::clock_label;75
pub use view_settings::{CallLayout, ViewSettings};77
pub use view_settings::{CallLayout, ViewSettings};@@ -27,6 +27,7 @@ impl AppState {
27
27
28
28
pub fn open_calendar(&mut self, cx: &mut Context<Self>) {
29
29
self.calendar_week.start = Some(self.calendar_start());
30
self.request_calendar_scroll_to_now(cx);
30
31
self.load_calendar_week(cx);
31
32
}
32
33
@@ -41,6 +42,14 @@ impl AppState {
41
42
self.open_calendar(cx);
42
43
}
43
44
45
pub fn request_calendar_scroll_to_now(&mut self, cx: &mut Context<Self>) {
46
if !crate::call::calendar_now::shows_hour_grid(self.calendar_week.span) {
47
return;
48
}
49
self.calendar_scroll_to_now = self.calendar_scroll_to_now.wrapping_add(1);
50
cx.notify();
51
}
52
44
53
pub fn set_calendar_span(&mut self, span: CalendarSpan, cx: &mut Context<Self>) {
45
54
let today = Local::now().date_naive();
46
55
let start = self.calendar_start();
@@ -48,6 +57,9 @@ impl AppState {
48
57
start <= today && today < start + Duration::days(self.calendar_week.span.days());
49
58
self.calendar_week.span = span;
50
59
self.calendar_week.start = Some(span.start_for(if shown { today } else { start }));
60
if crate::call::calendar_now::shows_hour_grid(span) {
61
self.request_calendar_scroll_to_now(cx);
62
}
51
63
self.load_calendar_week(cx);
52
64
}
53
65
@@ -27,6 +27,7 @@ impl AppState {27
27
28
pub fn open_calendar(&mut self, cx: &mut Context<Self>) {28
pub fn open_calendar(&mut self, cx: &mut Context<Self>) {29
self.calendar_week.start = Some(self.calendar_start());29
self.calendar_week.start = Some(self.calendar_start());30
self.request_calendar_scroll_to_now(cx);30
self.load_calendar_week(cx);31
self.load_calendar_week(cx);31
}32
}32
33
@@ -41,6 +42,14 @@ impl AppState {41
self.open_calendar(cx);42
self.open_calendar(cx);42
}43
}43
44
45
pub fn request_calendar_scroll_to_now(&mut self, cx: &mut Context<Self>) {46
if !crate::call::calendar_now::shows_hour_grid(self.calendar_week.span) {47
return;48
}49
self.calendar_scroll_to_now = self.calendar_scroll_to_now.wrapping_add(1);50
cx.notify();51
}52
44
pub fn set_calendar_span(&mut self, span: CalendarSpan, cx: &mut Context<Self>) {53
pub fn set_calendar_span(&mut self, span: CalendarSpan, cx: &mut Context<Self>) {45
let today = Local::now().date_naive();54
let today = Local::now().date_naive();46
let start = self.calendar_start();55
let start = self.calendar_start();@@ -48,6 +57,9 @@ impl AppState {48
start <= today && today < start + Duration::days(self.calendar_week.span.days());57
start <= today && today < start + Duration::days(self.calendar_week.span.days());49
self.calendar_week.span = span;58
self.calendar_week.span = span;50
self.calendar_week.start = Some(span.start_for(if shown { today } else { start }));59
self.calendar_week.start = Some(span.start_for(if shown { today } else { start }));60
if crate::call::calendar_now::shows_hour_grid(span) {61
self.request_calendar_scroll_to_now(cx);62
}51
self.load_calendar_week(cx);63
self.load_calendar_week(cx);52
}64
}53
65
@@ -1,3 +1,5 @@
1
use std::time::Duration;
2
1
3
use app_base::format::{self, clock_format};
2
4
use chatsvc::{CalendarEvent, ResponseType, join_info_text};
3
5
use chrono::{DateTime, Local, NaiveDate, Offset, Timelike, Utc};
@@ -11,11 +13,15 @@ use gpui_kit::*;
11
13
12
14
use super::calendar_date_picker::date_picker_button;
13
15
use super::calendar_month::month_grid;
14
use super::meeting_proposal_card::meeting_proposal_cards;
15
use super::rsvp_menu::rsvp_menu;
16
16
use super::join_dialog::open_join_with_id;
17
17
use super::new_meeting_dialog::open_new_meeting;
18
use super::rsvp_menu::rsvp_menu;
19
use super::scroll_edge::scroll_offset;
18
20
use crate::app_state::AppState;
21
use crate::call::calendar_now::{
22
DEFAULT_GRID_VIEWPORT, FollowNow, MANUAL_SCROLL_EPSILON, NOW_BIAS_FRACTION,
23
now_scroll_offset_y, offset_differs, shows_hour_grid,
24
};
19
25
use crate::call::join_available;
20
26
use crate::call::week_grid::{
21
27
CalendarSpan, EventLook, WeekBlock, WeekDay, event_look, hour_label, month_shown, week_days,
@@ -30,7 +36,6 @@ const HEADER_HEIGHT: f32 = 52.;
30
36
const DAY_HEADER_HEIGHT: f32 = 56.;
31
37
const ALL_DAY_HEIGHT: f32 = 26.;
32
38
const BLOCK_GAP: f32 = 2.;
33
const FIRST_VISIBLE_HOUR: f32 = 8.;
34
39
const CANCELED_PREFIX: &str = "Canceled: ";
35
40
const COPY_JOIN_INFO: &str = "Copy join info";
36
41
const COMPACT_HEADER_WIDTH: f32 = 960.;
@@ -42,28 +47,132 @@ const HATCH_GAP: f32 = 2.5;
42
47
const SMALL_TITLE_HEIGHT: f32 = 20.;
43
48
const TODAY_MARK_HEIGHT: f32 = 3.;
44
49
const NOW_DOT_SIZE: f32 = 8.;
50
const CLOCK_TICK: Duration = Duration::from_secs(30);
51
const CHROME_HEIGHT: f32 = HEADER_HEIGHT + DAY_HEADER_HEIGHT + ALL_DAY_HEIGHT;
45
52
46
53
pub struct CalendarView {
47
54
state: Entity<AppState>,
48
55
scroll: ScrollHandle,
49
scrolled_to_morning: bool,
56
follow_now: FollowNow,
57
seen_scroll_to_now: u64,
58
last_programmatic_offset: f32,
59
skip_manual_check: bool,
60
clock_running: bool,
50
61
_observation: Subscription,
51
_agent_observation: Option<Subscription>,
52
62
}
53
63
54
64
impl CalendarView {
55
65
pub fn new(state: Entity<AppState>, cx: &mut Context<Self>) -> Self {
56
66
let observation = cx.observe(&state, |_, _, cx| cx.notify());
57
let agent_observation = crate::agent_api::handle(cx).map(|api| cx.observe(&api, |_, _, cx| cx.notify()));
58
67
CalendarView {
59
68
state,
60
69
scroll: ScrollHandle::new(),
61
scrolled_to_morning: false,
70
follow_now: FollowNow { active: true },
71
seen_scroll_to_now: 0,
72
last_programmatic_offset: 0.,
73
skip_manual_check: false,
74
clock_running: false,
62
75
_observation: observation,
63
_agent_observation: agent_observation,
64
76
}
65
77
}
66
78
79
fn grid_viewport_height(window: &Window) -> f32 {
80
(f32::from(window.viewport_size().height) - CHROME_HEIGHT).max(DEFAULT_GRID_VIEWPORT * 0.5)
81
}
82
83
fn align_scroll_to_now(&mut self, window: &Window) {
84
let local = Local::now();
85
let offset = now_scroll_offset_y(
86
local.hour(),
87
local.minute(),
88
HOUR_HEIGHT,
89
Self::grid_viewport_height(window),
90
NOW_BIAS_FRACTION,
91
);
92
self.scroll.set_offset(point(px(0.), px(offset)));
93
self.last_programmatic_offset = offset;
94
self.skip_manual_check = true;
95
}
96
97
fn sync_follow_from_state(&mut self, window: &Window, cx: &mut Context<Self>) {
98
let (scroll_to_now, span, today_visible) = {
99
let state = self.state.read(cx);
100
let start = state.calendar_start();
101
let today = Local::now().date_naive();
102
let today_visible = start <= today
103
&& today < start + chrono::Duration::days(state.calendar_week.span.days());
104
(
105
state.calendar_scroll_to_now,
106
state.calendar_week.span,
107
today_visible,
108
)
109
};
110
if !shows_hour_grid(span) {
111
self.stop_clock();
112
return;
113
}
114
self.ensure_clock(cx);
115
if scroll_to_now != self.seen_scroll_to_now {
116
self.seen_scroll_to_now = scroll_to_now;
117
self.follow_now.request_align();
118
}
119
if self.skip_manual_check {
120
self.skip_manual_check = false;
121
} else if self.follow_now.active
122
&& offset_differs(
123
f32::from(scroll_offset(&self.scroll).y),
124
self.last_programmatic_offset,
125
MANUAL_SCROLL_EPSILON,
126
)
127
{
128
self.follow_now.note_manual_scroll();
129
}
130
if self.follow_now.should_follow(today_visible) {
131
self.align_scroll_to_now(window);
132
}
133
}
134
135
fn ensure_clock(&mut self, cx: &mut Context<Self>) {
136
if self.clock_running {
137
return;
138
}
139
self.clock_running = true;
140
cx.spawn(async move |this, cx| {
141
loop {
142
cx.background_executor().timer(CLOCK_TICK).await;
143
let keep = this
144
.update(cx, |view, cx| {
145
let span = view.state.read(cx).calendar_week.span;
146
if !shows_hour_grid(span) {
147
view.clock_running = false;
148
return false;
149
}
150
cx.notify();
151
true
152
})
153
.unwrap_or(false);
154
if !keep {
155
break;
156
}
157
}
158
})
159
.detach();
160
}
161
162
fn stop_clock(&mut self) {
163
self.clock_running = false;
164
}
165
166
fn on_grid_scroll_wheel(
167
&mut self,
168
_event: &ScrollWheelEvent,
169
_window: &mut Window,
170
cx: &mut Context<Self>,
171
) {
172
self.follow_now.note_manual_scroll();
173
cx.notify();
174
}
175
67
176
fn header(&self, start: NaiveDate, span: CalendarSpan, compact: bool) -> impl IntoElement {
68
177
let span_state = self.state.clone();
69
178
let label = |text: &'static str| (!compact).then_some(text);
@@ -123,19 +232,17 @@ impl CalendarView {
123
232
forward.update(cx, |state, cx| state.shift_calendar_week(1, cx))
124
233
}),
125
234
)
126
.child(
127
h_flex().flex_1().min_w_0().child(date_picker_button(
128
&self.state,
129
span.title(start),
130
start,
131
span.days(),
132
if span == CalendarSpan::Month {
133
month_shown(start)
134
} else {
135
start
136
},
137
)),
138
)
235
.child(h_flex().flex_1().min_w_0().child(date_picker_button(
236
&self.state,
237
span.title(start),
238
start,
239
span.days(),
240
if span == CalendarSpan::Month {
241
month_shown(start)
242
} else {
243
start
244
},
245
)))
139
246
.child(
140
247
Button::new("calendar-span")
141
248
.small()
@@ -396,6 +503,7 @@ impl CalendarView {
396
503
today: NaiveDate,
397
504
now: DateTime<Utc>,
398
505
send_response: bool,
506
cx: &mut Context<Self>,
399
507
) -> impl IntoElement {
400
508
let total_height = 24. * HOUR_HEIGHT;
401
509
let twelve_hour = clock_format().contains("%p") || clock_format().contains("%P");
@@ -409,6 +517,7 @@ impl CalendarView {
409
517
.min_h_0()
410
518
.overflow_y_scroll()
411
519
.track_scroll(&self.scroll)
520
.on_scroll_wheel(cx.listener(Self::on_grid_scroll_wheel))
412
521
.child(
413
522
h_flex()
414
523
.h(px(total_height))
@@ -576,11 +685,7 @@ fn event_title(event: &CalendarEvent) -> String {
576
685
impl Render for CalendarView {
577
686
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
578
687
let compact = f32::from(window.viewport_size().width) < COMPACT_HEADER_WIDTH;
579
if !self.scrolled_to_morning {
580
self.scrolled_to_morning = true;
581
self.scroll
582
.set_offset(point(px(0.), px(-FIRST_VISIBLE_HOUR * HOUR_HEIGHT)));
583
}
688
self.sync_follow_from_state(window, cx);
584
689
let state = self.state.read(cx);
585
690
let start = state.calendar_start();
586
691
let events = state.calendar_week.events.clone();
@@ -600,15 +705,21 @@ impl Render for CalendarView {
600
705
.bg(theme::surface())
601
706
.overflow_hidden()
602
707
.child(self.header(start, span, compact))
603
.children(meeting_proposal_cards(cx))
604
708
.map(|calendar| {
605
709
if span == CalendarSpan::Month {
606
calendar.child(month_grid(&self.state, start, &days, &events, today, offset))
710
calendar.child(month_grid(
711
&self.state,
712
start,
713
&days,
714
&events,
715
today,
716
offset,
717
))
607
718
} else {
608
719
calendar
609
720
.child(self.day_headers(&days, today))
610
721
.children(self.all_day_row(&days, &events))
611
.child(self.grid(&days, &events, today, now, send_response))
722
.child(self.grid(&days, &events, today, now, send_response, cx))
612
723
}
613
724
})
614
725
}
@@ -1,3 +1,5 @@1
use std::time::Duration;2
1
use app_base::format::{self, clock_format};3
use app_base::format::{self, clock_format};2
use chatsvc::{CalendarEvent, ResponseType, join_info_text};4
use chatsvc::{CalendarEvent, ResponseType, join_info_text};3
use chrono::{DateTime, Local, NaiveDate, Offset, Timelike, Utc};5
use chrono::{DateTime, Local, NaiveDate, Offset, Timelike, Utc};@@ -11,11 +13,15 @@ use gpui_kit::*;11
13
12
use super::calendar_date_picker::date_picker_button;14
use super::calendar_date_picker::date_picker_button;13
use super::calendar_month::month_grid;15
use super::calendar_month::month_grid;14
use super::meeting_proposal_card::meeting_proposal_cards;15
use super::rsvp_menu::rsvp_menu;16
use super::join_dialog::open_join_with_id;16
use super::join_dialog::open_join_with_id;17
use super::new_meeting_dialog::open_new_meeting;17
use super::new_meeting_dialog::open_new_meeting;18
use super::rsvp_menu::rsvp_menu;19
use super::scroll_edge::scroll_offset;18
use crate::app_state::AppState;20
use crate::app_state::AppState;21
use crate::call::calendar_now::{22
DEFAULT_GRID_VIEWPORT, FollowNow, MANUAL_SCROLL_EPSILON, NOW_BIAS_FRACTION,23
now_scroll_offset_y, offset_differs, shows_hour_grid,24
};19
use crate::call::join_available;25
use crate::call::join_available;20
use crate::call::week_grid::{26
use crate::call::week_grid::{21
CalendarSpan, EventLook, WeekBlock, WeekDay, event_look, hour_label, month_shown, week_days,27
CalendarSpan, EventLook, WeekBlock, WeekDay, event_look, hour_label, month_shown, week_days,@@ -30,7 +36,6 @@ const HEADER_HEIGHT: f32 = 52.;30
const DAY_HEADER_HEIGHT: f32 = 56.;36
const DAY_HEADER_HEIGHT: f32 = 56.;31
const ALL_DAY_HEIGHT: f32 = 26.;37
const ALL_DAY_HEIGHT: f32 = 26.;32
const BLOCK_GAP: f32 = 2.;38
const BLOCK_GAP: f32 = 2.;33
const FIRST_VISIBLE_HOUR: f32 = 8.;34
const CANCELED_PREFIX: &str = "Canceled: ";39
const CANCELED_PREFIX: &str = "Canceled: ";35
const COPY_JOIN_INFO: &str = "Copy join info";40
const COPY_JOIN_INFO: &str = "Copy join info";36
const COMPACT_HEADER_WIDTH: f32 = 960.;41
const COMPACT_HEADER_WIDTH: f32 = 960.;@@ -42,28 +47,132 @@ const HATCH_GAP: f32 = 2.5;42
const SMALL_TITLE_HEIGHT: f32 = 20.;47
const SMALL_TITLE_HEIGHT: f32 = 20.;43
const TODAY_MARK_HEIGHT: f32 = 3.;48
const TODAY_MARK_HEIGHT: f32 = 3.;44
const NOW_DOT_SIZE: f32 = 8.;49
const NOW_DOT_SIZE: f32 = 8.;50
const CLOCK_TICK: Duration = Duration::from_secs(30);51
const CHROME_HEIGHT: f32 = HEADER_HEIGHT + DAY_HEADER_HEIGHT + ALL_DAY_HEIGHT;45
52
46
pub struct CalendarView {53
pub struct CalendarView {47
state: Entity<AppState>,54
state: Entity<AppState>,48
scroll: ScrollHandle,55
scroll: ScrollHandle,49
scrolled_to_morning: bool,56
follow_now: FollowNow,57
seen_scroll_to_now: u64,58
last_programmatic_offset: f32,59
skip_manual_check: bool,60
clock_running: bool,50
_observation: Subscription,61
_observation: Subscription,51
_agent_observation: Option<Subscription>,52
}62
}53
63
54
impl CalendarView {64
impl CalendarView {55
pub fn new(state: Entity<AppState>, cx: &mut Context<Self>) -> Self {65
pub fn new(state: Entity<AppState>, cx: &mut Context<Self>) -> Self {56
let observation = cx.observe(&state, |_, _, cx| cx.notify());66
let observation = cx.observe(&state, |_, _, cx| cx.notify());57
let agent_observation = crate::agent_api::handle(cx).map(|api| cx.observe(&api, |_, _, cx| cx.notify()));58
CalendarView {67
CalendarView {59
state,68
state,60
scroll: ScrollHandle::new(),69
scroll: ScrollHandle::new(),61
scrolled_to_morning: false,70
follow_now: FollowNow { active: true },71
seen_scroll_to_now: 0,72
last_programmatic_offset: 0.,73
skip_manual_check: false,74
clock_running: false,62
_observation: observation,75
_observation: observation,63
_agent_observation: agent_observation,64
}76
}65
}77
}66
78
79
fn grid_viewport_height(window: &Window) -> f32 {80
(f32::from(window.viewport_size().height) - CHROME_HEIGHT).max(DEFAULT_GRID_VIEWPORT * 0.5)81
}82
83
fn align_scroll_to_now(&mut self, window: &Window) {84
let local = Local::now();85
let offset = now_scroll_offset_y(86
local.hour(),87
local.minute(),88
HOUR_HEIGHT,89
Self::grid_viewport_height(window),90
NOW_BIAS_FRACTION,91
);92
self.scroll.set_offset(point(px(0.), px(offset)));93
self.last_programmatic_offset = offset;94
self.skip_manual_check = true;95
}96
97
fn sync_follow_from_state(&mut self, window: &Window, cx: &mut Context<Self>) {98
let (scroll_to_now, span, today_visible) = {99
let state = self.state.read(cx);100
let start = state.calendar_start();101
let today = Local::now().date_naive();102
let today_visible = start <= today103
&& today < start + chrono::Duration::days(state.calendar_week.span.days());104
(105
state.calendar_scroll_to_now,106
state.calendar_week.span,107
today_visible,108
)109
};110
if !shows_hour_grid(span) {111
self.stop_clock();112
return;113
}114
self.ensure_clock(cx);115
if scroll_to_now != self.seen_scroll_to_now {116
self.seen_scroll_to_now = scroll_to_now;117
self.follow_now.request_align();118
}119
if self.skip_manual_check {120
self.skip_manual_check = false;121
} else if self.follow_now.active122
&& offset_differs(123
f32::from(scroll_offset(&self.scroll).y),124
self.last_programmatic_offset,125
MANUAL_SCROLL_EPSILON,126
)127
{128
self.follow_now.note_manual_scroll();129
}130
if self.follow_now.should_follow(today_visible) {131
self.align_scroll_to_now(window);132
}133
}134
135
fn ensure_clock(&mut self, cx: &mut Context<Self>) {136
if self.clock_running {137
return;138
}139
self.clock_running = true;140
cx.spawn(async move |this, cx| {141
loop {142
cx.background_executor().timer(CLOCK_TICK).await;143
let keep = this144
.update(cx, |view, cx| {145
let span = view.state.read(cx).calendar_week.span;146
if !shows_hour_grid(span) {147
view.clock_running = false;148
return false;149
}150
cx.notify();151
true152
})153
.unwrap_or(false);154
if !keep {155
break;156
}157
}158
})159
.detach();160
}161
162
fn stop_clock(&mut self) {163
self.clock_running = false;164
}165
166
fn on_grid_scroll_wheel(167
&mut self,168
_event: &ScrollWheelEvent,169
_window: &mut Window,170
cx: &mut Context<Self>,171
) {172
self.follow_now.note_manual_scroll();173
cx.notify();174
}175
67
fn header(&self, start: NaiveDate, span: CalendarSpan, compact: bool) -> impl IntoElement {176
fn header(&self, start: NaiveDate, span: CalendarSpan, compact: bool) -> impl IntoElement {68
let span_state = self.state.clone();177
let span_state = self.state.clone();69
let label = |text: &'static str| (!compact).then_some(text);178
let label = |text: &'static str| (!compact).then_some(text);@@ -123,19 +232,17 @@ impl CalendarView {123
forward.update(cx, |state, cx| state.shift_calendar_week(1, cx))232
forward.update(cx, |state, cx| state.shift_calendar_week(1, cx))124
}),233
}),125
)234
)126
.child(235
.child(h_flex().flex_1().min_w_0().child(date_picker_button(127
h_flex().flex_1().min_w_0().child(date_picker_button(236
&self.state,128
&self.state,237
span.title(start),129
span.title(start),238
start,130
start,239
span.days(),131
span.days(),240
if span == CalendarSpan::Month {132
if span == CalendarSpan::Month {241
month_shown(start)133
month_shown(start)242
} else {134
} else {243
start135
start244
},136
},245
)))137
)),138
)139
.child(246
.child(140
Button::new("calendar-span")247
Button::new("calendar-span")141
.small()248
.small()@@ -396,6 +503,7 @@ impl CalendarView {396
today: NaiveDate,503
today: NaiveDate,397
now: DateTime<Utc>,504
now: DateTime<Utc>,398
send_response: bool,505
send_response: bool,506
cx: &mut Context<Self>,399
) -> impl IntoElement {507
) -> impl IntoElement {400
let total_height = 24. * HOUR_HEIGHT;508
let total_height = 24. * HOUR_HEIGHT;401
let twelve_hour = clock_format().contains("%p") || clock_format().contains("%P");509
let twelve_hour = clock_format().contains("%p") || clock_format().contains("%P");@@ -409,6 +517,7 @@ impl CalendarView {409
.min_h_0()517
.min_h_0()410
.overflow_y_scroll()518
.overflow_y_scroll()411
.track_scroll(&self.scroll)519
.track_scroll(&self.scroll)520
.on_scroll_wheel(cx.listener(Self::on_grid_scroll_wheel))412
.child(521
.child(413
h_flex()522
h_flex()414
.h(px(total_height))523
.h(px(total_height))@@ -576,11 +685,7 @@ fn event_title(event: &CalendarEvent) -> String {576
impl Render for CalendarView {685
impl Render for CalendarView {577
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {686
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {578
let compact = f32::from(window.viewport_size().width) < COMPACT_HEADER_WIDTH;687
let compact = f32::from(window.viewport_size().width) < COMPACT_HEADER_WIDTH;579
if !self.scrolled_to_morning {688
self.sync_follow_from_state(window, cx);580
self.scrolled_to_morning = true;581
self.scroll582
.set_offset(point(px(0.), px(-FIRST_VISIBLE_HOUR * HOUR_HEIGHT)));583
}584
let state = self.state.read(cx);689
let state = self.state.read(cx);585
let start = state.calendar_start();690
let start = state.calendar_start();586
let events = state.calendar_week.events.clone();691
let events = state.calendar_week.events.clone();@@ -600,15 +705,21 @@ impl Render for CalendarView {600
.bg(theme::surface())705
.bg(theme::surface())601
.overflow_hidden()706
.overflow_hidden()602
.child(self.header(start, span, compact))707
.child(self.header(start, span, compact))603
.children(meeting_proposal_cards(cx))604
.map(|calendar| {708
.map(|calendar| {605
if span == CalendarSpan::Month {709
if span == CalendarSpan::Month {606
calendar.child(month_grid(&self.state, start, &days, &events, today, offset))710
calendar.child(month_grid(711
&self.state,712
start,713
&days,714
&events,715
today,716
offset,717
))607
} else {718
} else {608
calendar719
calendar609
.child(self.day_headers(&days, today))720
.child(self.day_headers(&days, today))610
.children(self.all_day_row(&days, &events))721
.children(self.all_day_row(&days, &events))611
.child(self.grid(&days, &events, today, now, send_response))722
.child(self.grid(&days, &events, today, now, send_response, cx))612
}723
}613
})724
})614
}725
}