BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / calendar_date_picker.rs

calendar_date_picker.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 3 weeks ago
a6592765 History
calendar_date_picker.rs 7.0 KiB Download Raw
1use std::cell::RefCell;
2use std::rc::Rc;
3
4use chrono::{Datelike, Duration, Local, Months, NaiveDate};
5use gpui_kit::assets::IconName;
6use gpui_kit::component::Sizable as _;
7use gpui_kit::component::button::{Button, ButtonVariants as _};
8use gpui_kit::component::popover::{Popover, PopoverState};
9use gpui_kit::component::{h_flex, v_flex};
10use gpui_kit::prelude::FluentBuilder as _;
11use gpui_kit::*;
12
13use crate::app_state::AppState;
14use crate::call::week_grid::{MONTH_GRID_DAYS, first_of_month, month_grid_start};
15use crate::theme;
16
17pub const DATE_PICKER_TAG: &str = "shell/islands/calendar/date-picker";
18const WEEKDAY_LETTERS: [&str; 7] = ["M", "T", "W", "T", "F", "S", "S"];
19const CELL_SIZE: f32 = 32.;
20const PICKER_PADDING: f32 = 12.;
21
22pub struct DatePicker {
23 app: Entity<AppState>,
24 month: NaiveDate,
25 shown_start: NaiveDate,
26 shown_days: i64,
27 popover: WeakEntity<PopoverState>,
28}
29
30impl DatePicker {
31 fn new(
32 app: Entity<AppState>,
33 shown_start: NaiveDate,
34 shown_days: i64,
35 month: NaiveDate,
36 popover: WeakEntity<PopoverState>,
37 ) -> Self {
38 DatePicker {
39 app,
40 month: first_of_month(month),
41 shown_start,
42 shown_days,
43 popover,
44 }
45 }
46
47 fn move_month(&mut self, forward: bool, cx: &mut Context<Self>) {
48 let moved = if forward {
49 self.month.checked_add_months(Months::new(1))
50 } else {
51 self.month.checked_sub_months(Months::new(1))
52 };
53 self.month = moved.unwrap_or(self.month);
54 cx.notify();
55 }
56
57 fn pick(&mut self, date: NaiveDate, window: &mut Window, cx: &mut Context<Self>) {
58 self.app
59 .update(cx, |state, cx| state.show_calendar_date(date, cx));
60 self.popover
61 .update(cx, |popover, cx| popover.dismiss(window, cx))
62 .ok();
63 }
64
65 fn day_cell(&self, date: NaiveDate, today: NaiveDate, cx: &mut Context<Self>) -> Stateful<Div> {
66 let in_month = date.month() == self.month.month();
67 let shown =
68 self.shown_start <= date && date < self.shown_start + Duration::days(self.shown_days);
69 let is_today = date == today;
70 div()
71 .id(SharedString::from(format!("date-picker-{date}")))
72 .size(px(CELL_SIZE))
73 .flex()
74 .items_center()
75 .justify_center()
76 .rounded_full()
77 .cursor_pointer()
78 .text_size(px(12.))
79 .text_color(if in_month {
80 theme::text()
81 } else {
82 theme::text_faint()
83 })
84 .when(shown && !is_today, |cell| cell.bg(theme::popup_hover()))
85 .when(is_today, |cell| {
86 cell.bg(theme::accent()).text_color(theme::on_accent())
87 })
88 .when(!is_today, |cell| {
89 cell.hover(|cell| cell.bg(theme::border_strong()))
90 })
91 .child(date.day().to_string())
92 .on_click(cx.listener(move |this, _, window, cx| this.pick(date, window, cx)))
93 }
94}
95
96impl Render for DatePicker {
97 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
98 let today = Local::now().date_naive();
99 let start = month_grid_start(self.month);
100 let days: Vec<NaiveDate> = (0..MONTH_GRID_DAYS)
101 .map(|offset| start + Duration::days(offset))
102 .collect();
103 let weeks = days
104 .chunks(7)
105 .map(|week| {
106 h_flex().children(
107 week.iter()
108 .map(|date| self.day_cell(*date, today, cx))
109 .collect::<Vec<_>>(),
110 )
111 })
112 .collect::<Vec<_>>();
113 v_flex()
114 .debug_selector(|| DATE_PICKER_TAG.into())
115 .p(px(PICKER_PADDING))
116 .gap(px(4.))
117 .child(
118 h_flex()
119 .items_center()
120 .justify_between()
121 .pb(px(4.))
122 .child(
123 div()
124 .pl(px(6.))
125 .text_size(px(14.))
126 .font_weight(FontWeight::SEMIBOLD)
127 .text_color(theme::text())
128 .child(self.month.format("%B %Y").to_string()),
129 )
130 .child(
131 h_flex()
132 .child(
133 Button::new("date-picker-back")
134 .small()
135 .ghost()
136 .icon(IconName::ChevronLeft)
137 .tooltip("Previous month")
138 .on_click(
139 cx.listener(|this, _, _, cx| this.move_month(false, cx)),
140 ),
141 )
142 .child(
143 Button::new("date-picker-forward")
144 .small()
145 .ghost()
146 .icon(IconName::ChevronRight)
147 .tooltip("Next month")
148 .on_click(
149 cx.listener(|this, _, _, cx| this.move_month(true, cx)),
150 ),
151 ),
152 ),
153 )
154 .child(h_flex().children(WEEKDAY_LETTERS.iter().map(|letter| {
155 div()
156 .size(px(CELL_SIZE))
157 .flex()
158 .items_center()
159 .justify_center()
160 .text_size(px(11.))
161 .text_color(theme::text_muted())
162 .child(*letter)
163 })))
164 .children(weeks)
165 }
166}
167
168pub fn date_picker_button(
169 app: &Entity<AppState>,
170 title: String,
171 shown_start: NaiveDate,
172 shown_days: i64,
173 month: NaiveDate,
174) -> AnyElement {
175 let app = app.clone();
176 let picker: Rc<RefCell<Option<Entity<DatePicker>>>> = Rc::default();
177 let reset = picker.clone();
178 Popover::new("calendar-date-picker")
179 .anchor(Anchor::TopLeft)
180 .p(px(0.))
181 .trigger(
182 Button::new("calendar-date-title")
183 .small()
184 .ghost()
185 .label(title)
186 .dropdown_caret(true),
187 )
188 .on_open_change(move |open, _, _| {
189 if !*open {
190 reset.borrow_mut().take();
191 }
192 })
193 .content(move |_, _, cx| {
194 if let Some(existing) = picker.borrow().clone() {
195 return existing;
196 }
197 let popover = cx.entity().downgrade();
198 let app = app.clone();
199 let created = cx.new(|_| DatePicker::new(app, shown_start, shown_days, month, popover));
200 *picker.borrow_mut() = Some(created.clone());
201 created
202 })
203 .into_any_element()
204}