gpui-box-kit 0.1.0

GPUI Box Kit design-system components and interaction primitives
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Two ends of a range over a host-owned calendar.
//!
//! The picker reports and never judges. An incomplete range is a state of its
//! own rather than an error; an end before its start is said out loud instead
//! of quietly swapped; and a blocked day inside a range is named, leaving the
//! host to decide whether that makes the range unusable.

use gpui::{
    App, AppContext as _, Context, Entity, EventEmitter, FocusHandle, Focusable,
    InteractiveElement, IntoElement, ParentElement, Render, SharedString, Styled, Subscription,
    Window, div, px,
};
use gpui_kit_semantics::{NodeSpec, Role, Semantic};
use gpui_kit_theme::{ActiveTheme, Space, TextTone, TypeScale};

use crate::datetime::adapter::{Day, SharedDateAdapter};
use crate::datetime::calendar::{Calendar, CalendarEvent, DayMark};
use crate::display::badge::Tone;
use crate::display::status::StatusLine;
use crate::foundation::{Disableable, Ident, StyledExt, text as foundation_text};
use crate::strings::{ActiveStrings, StringKey};

/// A range as the caller holds it: a start, and an end once there is one.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DayRange {
    pub start: Day,
    pub end: Option<Day>,
}

impl DayRange {
    pub fn starting(start: Day) -> Self {
        Self { start, end: None }
    }

    pub fn new(start: Day, end: Day) -> Self {
        Self {
            start,
            end: Some(end),
        }
    }
}

/// What the picker currently holds.
///
/// Incomplete and inverted are facts, not failures. Naming them separately is
/// what stops a half-finished range being drawn as a broken one, and an end
/// before a start being drawn as a range nobody asked for.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum RangeState {
    #[default]
    Unset,
    Incomplete {
        start: Day,
    },
    Complete {
        start: Day,
        end: Day,
    },
    /// The end sits before the start, exactly as it was given.
    Inverted {
        start: Day,
        end: Day,
    },
}

impl RangeState {
    /// The word the picker publishes for this state.
    pub fn name(self) -> &'static str {
        match self {
            Self::Unset => "unset",
            Self::Incomplete { .. } => "incomplete",
            Self::Complete { .. } => "complete",
            Self::Inverted { .. } => "end before start",
        }
    }

    pub fn from_range(range: Option<DayRange>) -> Self {
        match range {
            None => Self::Unset,
            Some(DayRange { start, end: None }) => Self::Incomplete { start },
            Some(DayRange {
                start,
                end: Some(end),
            }) if end < start => Self::Inverted { start, end },
            Some(DayRange {
                start,
                end: Some(end),
            }) => Self::Complete { start, end },
        }
    }
}

/// One day inside a range the adapter refuses, in the host's own words.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BlockedDay {
    pub day: Day,
    pub reason: SharedString,
}

/// What the picker can say about the days between the two ends.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BlockedReport {
    /// There is no complete range to look at yet.
    NotApplicable,
    /// The host cannot enumerate the days in the range, so nothing was
    /// checked. This is not the same as finding nothing.
    Unchecked,
    Clear,
    Blocked(Vec<BlockedDay>),
}

/// What a range picker reports. The owner decides what any of it means.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RangePickerEvent {
    /// A day was asked for as the start of a new range.
    StartPicked(Day),
    /// A day was asked for as the end of the range in progress.
    EndPicked(Day),
}

impl EventEmitter<RangePickerEvent> for RangePicker {}

/// A calendar with two ends and a running account of what they add up to.
pub struct RangePicker {
    ident: Ident,
    focus_handle: FocusHandle,
    adapter: SharedDateAdapter,
    calendar: Entity<Calendar>,
    range: Option<DayRange>,
    disabled: bool,
    /// Held so the calendar subscription lives as long as the picker does.
    _subscriptions: Vec<Subscription>,
}

impl std::fmt::Debug for RangePicker {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("RangePicker")
            .field("ident", &self.ident)
            .field("range", &self.range)
            .field("disabled", &self.disabled)
            .finish()
    }
}

impl RangePicker {
    pub fn new(
        ident: impl Into<Ident>,
        adapter: SharedDateAdapter,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Self {
        let ident = ident.into();
        let calendar =
            cx.new(|cx| Calendar::new(ident.child("calendar"), adapter.clone(), window, cx));
        let subscription = cx.subscribe(&calendar, |picker, _calendar, event, cx| match event {
            CalendarEvent::Picked(day) => picker.take(*day, cx),
            CalendarEvent::Hovered(_) => cx.notify(),
            CalendarEvent::MonthShown(_) => {}
        });

        Self {
            ident,
            focus_handle: cx.focus_handle(),
            adapter,
            calendar,
            range: None,
            disabled: false,
            _subscriptions: vec![subscription],
        }
    }

    /// Seeds the range the caller holds.
    pub fn range(mut self, range: DayRange) -> Self {
        self.range = Some(range);
        self
    }

    /// Marks days with a dot the host supplies, on the calendar underneath.
    pub fn set_overlay(
        &mut self,
        overlay: impl Fn(Day) -> Option<DayMark> + 'static,
        cx: &mut Context<Self>,
    ) {
        self.calendar
            .update(cx, |calendar, cx| calendar.set_overlay(overlay, cx));
    }

    pub fn set_range(&mut self, range: Option<DayRange>, cx: &mut Context<Self>) {
        if self.range == range {
            return;
        }
        self.range = range;
        cx.notify();
    }

    pub fn set_disabled(&mut self, disabled: bool, cx: &mut Context<Self>) {
        if self.disabled == disabled {
            return;
        }
        self.disabled = disabled;
        self.calendar
            .update(cx, |calendar, cx| calendar.set_disabled(disabled, cx));
        cx.notify();
    }

    pub fn calendar(&self) -> &Entity<Calendar> {
        &self.calendar
    }

    pub fn state(&self) -> RangeState {
        RangeState::from_range(self.range)
    }

    /// The days the adapter refuses inside the range, or the reason the
    /// question could not be answered.
    pub fn blocked(&self) -> BlockedReport {
        let (start, end) = match self.state() {
            RangeState::Complete { start, end } => (start, end),
            RangeState::Inverted { start, end } => (end, start),
            _ => return BlockedReport::NotApplicable,
        };
        let Some(days) = self.adapter.days_in(start, end) else {
            return BlockedReport::Unchecked;
        };
        let blocked: Vec<BlockedDay> = days
            .into_iter()
            .filter_map(|day| {
                self.adapter
                    .is_selectable(day)
                    .reason()
                    .cloned()
                    .map(|reason| BlockedDay { day, reason })
            })
            .collect();
        if blocked.is_empty() {
            BlockedReport::Clear
        } else {
            BlockedReport::Blocked(blocked)
        }
    }

    /// Which end the next pick fills. A range that is complete, inverted, or
    /// unset starts a new one.
    fn take(&mut self, day: Day, cx: &mut Context<Self>) {
        if self.disabled {
            return;
        }
        match self.state() {
            RangeState::Incomplete { .. } => cx.emit(RangePickerEvent::EndPicked(day)),
            _ => cx.emit(RangePickerEvent::StartPicked(day)),
        }
    }

    fn summary(&self, cx: &App) -> (SharedString, Tone) {
        let strings = cx.strings();
        match self.state() {
            RangeState::Unset => (strings.text(StringKey::RangeUnset), Tone::Neutral),
            RangeState::Incomplete { start } => (
                strings.format(
                    StringKey::RangeIncomplete,
                    &[self.adapter.format_day(start).as_ref()],
                ),
                Tone::Info,
            ),
            RangeState::Complete { start, end } => (
                strings.format(
                    StringKey::RangeComplete,
                    &[
                        self.adapter.format_day(start).as_ref(),
                        self.adapter.format_day(end).as_ref(),
                    ],
                ),
                Tone::Success,
            ),
            RangeState::Inverted { start, end } => (
                strings.format(
                    StringKey::RangeInverted,
                    &[
                        self.adapter.format_day(end).as_ref(),
                        self.adapter.format_day(start).as_ref(),
                    ],
                ),
                Tone::Warning,
            ),
        }
    }
}

impl Disableable for RangePicker {
    fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }
}

impl Focusable for RangePicker {
    fn focus_handle(&self, _cx: &App) -> FocusHandle {
        self.focus_handle.clone()
    }
}

impl Render for RangePicker {
    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        let theme = cx.theme().clone();
        let range = self.range;
        self.calendar
            .update(cx, |calendar, cx| calendar.set_range(range, cx));
        let (summary, tone) = self.summary(cx);
        let state = self.state();
        let blocked = self.blocked();

        let blocked_line = match &blocked {
            BlockedReport::Unchecked => Some(
                foundation_text(
                    &theme,
                    TypeScale::Caption,
                    cx.strings().text(StringKey::RangeUncheckable),
                )
                .text_tone(&theme, TextTone::Muted)
                .semantic_in(
                    cx,
                    NodeSpec::new(self.ident.child("blocked").semantic_id(), Role::Status)
                        .parent(self.ident.semantic_id())
                        .value("unchecked")
                        .text(cx.strings().text(StringKey::RangeUncheckable)),
                ),
            ),
            _ => None,
        };

        let named: Vec<gpui::AnyElement> = match &blocked {
            BlockedReport::Blocked(days) => days
                .iter()
                .map(|blocked| {
                    let blocked_id = format!("blocked-{}", blocked.day.0);
                    let ident = self.ident.child(blocked_id);
                    let text = cx.strings().format(
                        StringKey::RangeBlockedDay,
                        &[
                            self.adapter.format_day(blocked.day).as_ref(),
                            blocked.reason.as_ref(),
                        ],
                    );
                    foundation_text(&theme, TypeScale::Caption, text.clone())
                        .text_color(theme.colors.warning)
                        .semantic_in(
                            cx,
                            NodeSpec::new(ident.semantic_id(), Role::Status)
                                .parent(self.ident.semantic_id())
                                .text(text)
                                .value(blocked.reason.clone()),
                        )
                        .into_any_element()
                })
                .collect(),
            _ => Vec::new(),
        };

        div()
            .id(self.ident.element_id())
            .column()
            .flex_none()
            .gap_token(&theme, Space::Sm)
            .track_focus(&self.focus_handle)
            .child(self.calendar.clone())
            .child(
                div()
                    .column()
                    .gap(px(theme.space(Space::Xs)))
                    .child(StatusLine::new(summary.clone(), tone).id(self.ident.child("summary")))
                    .children(blocked_line)
                    .children(named),
            )
            .semantic_in(
                cx,
                NodeSpec::new(self.ident.semantic_id(), Role::Group)
                    .disabled(self.disabled)
                    .value(state.name()),
            )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn an_incomplete_range_is_its_own_state_rather_than_an_error() {
        assert_eq!(
            RangeState::from_range(Some(DayRange::starting(Day(4)))),
            RangeState::Incomplete { start: Day(4) }
        );
        assert_eq!(RangeState::from_range(None), RangeState::Unset);
    }

    #[test]
    fn an_end_before_the_start_is_reported_rather_than_swapped() {
        let state = RangeState::from_range(Some(DayRange::new(Day(9), Day(4))));
        assert_eq!(
            state,
            RangeState::Inverted {
                start: Day(9),
                end: Day(4)
            }
        );
        assert_eq!(state.name(), "end before start");
    }

    #[test]
    fn a_range_that_runs_forwards_is_complete() {
        assert_eq!(
            RangeState::from_range(Some(DayRange::new(Day(4), Day(9)))),
            RangeState::Complete {
                start: Day(4),
                end: Day(9)
            }
        );
    }
}