gpui-material-extras 0.1.0

Optional Material 3 Expressive component families for GPUI
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
use std::collections::HashMap;

use gpui::{
    Anchor, App, AppContext, Context, ElementId, Entity, EntityInputHandler, EventEmitter,
    FocusHandle, Focusable, InteractiveElement, IntoElement, ListAlignment, ListState,
    ParentElement, RenderOnce, Role, SharedString, StatefulInteractiveElement, Styled,
    Subscription, Window, div, prelude::FluentBuilder, px,
};
use gpui_base::{Dialog, Popup, TestSupportExt};
use gpui_material_components::{
    Button, ButtonVariant, Icon, IconButton, IconName, ListItem, LoadingIndicator, Text, TextField,
    TextFieldEvent, TextFieldState,
};
use gpui_material_foundation::{Surface, logical_anchor, scrim_color, window_material_theme};
use gpui_material_theme::{ColorRole, Direction, Elevation, TypeRole};

/// Identifies one committed query. Complete requests through [SearchState::complete].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SearchRequest {
    revision: u64,
    query: SharedString,
}

impl SearchRequest {
    pub fn query(&self) -> &SharedString {
        &self.query
    }
}

/// A result row whose ID remains stable when results reorder.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct SearchResult {
    id: ElementId,
    label: SharedString,
    supporting: Option<SharedString>,
}

impl SearchResult {
    pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            supporting: None,
        }
    }

    pub fn supporting(mut self, text: impl Into<SharedString>) -> Self {
        self.supporting = Some(text.into());
        self
    }

    pub fn id(&self) -> &ElementId {
        &self.id
    }

    pub fn label(&self) -> &SharedString {
        &self.label
    }
}

/// Result from the application's search provider.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SearchOutcome {
    Results(Vec<SearchResult>),
    Error(SharedString),
}

/// Search providers subscribe to requests. Submissions and result selection
/// are separate commands and the application owns their consequences.
#[derive(Clone, Debug)]
pub enum SearchEvent {
    Request(SearchRequest),
    Submit(SharedString),
    Select(SearchResult),
}

enum Results {
    Idle,
    Loading,
    Ready(SearchOutcome),
}

struct SearchResponse {
    revision: u64,
    pending: Option<SearchRequest>,
    results: Results,
}

impl Default for SearchResponse {
    fn default() -> Self {
        Self {
            revision: 0,
            pending: None,
            results: Results::Idle,
        }
    }
}

impl SearchResponse {
    fn begin(&mut self, query: SharedString) -> Option<SearchRequest> {
        self.revision = self
            .revision
            .checked_add(1)
            .expect("search revision exhausted");
        self.pending = (!query.trim().is_empty()).then_some(SearchRequest {
            revision: self.revision,
            query,
        });
        self.results = if self.pending.is_some() {
            Results::Loading
        } else {
            Results::Idle
        };
        self.pending.clone()
    }

    fn finish(&mut self, request: &SearchRequest, outcome: SearchOutcome) -> bool {
        if self.pending.as_ref() != Some(request) {
            return false;
        }
        self.pending = None;
        self.results = Results::Ready(outcome);
        true
    }
}

/// Retains editing, IME, result and focus state while the search view expands.
/// The application performs asynchronous work and owns resulting navigation.
pub struct SearchState {
    input: Entity<TextFieldState>,
    trigger: FocusHandle,
    scope: FocusHandle,
    restore: Option<FocusHandle>,
    scroll: ListState,
    result_focus: HashMap<ElementId, FocusHandle>,
    result_layout: Option<(gpui_material_theme::Typography, gpui::Pixels, u64)>,
    query: SharedString,
    response: SearchResponse,
    open: bool,
    _input: Subscription,
}

impl EventEmitter<SearchEvent> for SearchState {}

impl SearchState {
    pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
        let input = cx.new(|cx| TextFieldState::new(window, cx));
        let subscription = cx.subscribe_in(&input, window, |state, input, event, window, cx| {
            if !state.open
                || !matches!(
                    event,
                    TextFieldEvent::Change
                        | TextFieldEvent::PressEnter {
                            secondary: false,
                            shift: false
                        }
                )
            {
                return;
            }
            if input.update(cx, |input, cx| {
                input.marked_text_range(window, cx).is_some()
            }) {
                return;
            }
            let query = input.read(cx).value();
            state.update_query(query.clone(), cx);
            if matches!(event, TextFieldEvent::PressEnter { .. }) && !query.trim().is_empty() {
                cx.emit(SearchEvent::Submit(query));
            }
        });
        Self {
            input,
            trigger: cx.focus_handle(),
            scope: cx.focus_handle(),
            restore: None,
            scroll: ListState::new(0, ListAlignment::Top, px(100.)),
            result_focus: HashMap::new(),
            result_layout: None,
            query: "".into(),
            response: SearchResponse::default(),
            open: false,
            _input: subscription,
        }
    }

    pub fn query(&self) -> &SharedString {
        &self.query
    }

    pub fn is_open(&self) -> bool {
        self.open
    }

    /// Returns the stable focus handle of the expanded editor.
    pub fn input_focus(&self, cx: &App) -> FocusHandle {
        self.input.read(cx).focus_handle(cx)
    }

    /// Opens the view and focuses its editor.
    pub fn open(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        if self.open {
            return;
        }
        self.restore = window.focused(cx).or_else(|| Some(self.trigger.clone()));
        self.open = true;
        self.input_focus(cx).focus(window, cx);
        cx.notify();
    }

    /// Dismisses the view and restores its entry focus.
    pub fn close(&mut self, window: &mut Window, cx: &mut Context<Self>) {
        if !self.open {
            return;
        }
        self.open = false;
        if let Some(focus) = self.restore.take() {
            focus.focus(window, cx);
        }
        cx.notify();
    }

    /// Synchronizes the query from the application. Query changes issue a
    /// request, followed by the normal completion path.
    pub fn set_query(
        &mut self,
        query: impl Into<SharedString>,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let query = query.into();
        self.input.update(cx, |input, cx| {
            if input.value() != query {
                input.set_value(query.clone(), window, cx);
            }
        });
        self.update_query(query, cx);
    }

    /// Accepts only the latest pending request. Returns false for superseded
    /// requests and duplicate completions.
    pub fn complete(
        &mut self,
        request: &SearchRequest,
        outcome: SearchOutcome,
        cx: &mut Context<Self>,
    ) -> bool {
        let accepted = self.response.finish(request, outcome);
        if accepted {
            self.sync_result_focus(cx);
            cx.notify();
        }
        accepted
    }

    fn update_query(&mut self, query: SharedString, cx: &mut Context<Self>) {
        if query == self.query {
            return;
        }
        self.query = query.clone();
        if let Some(request) = self.response.begin(query) {
            cx.emit(SearchEvent::Request(request));
        }
        self.scroll.reset(0);
        cx.notify();
    }

    fn rows(&self) -> &[SearchResult] {
        match &self.response.results {
            Results::Ready(SearchOutcome::Results(rows)) => rows,
            _ => &[],
        }
    }

    fn sync_result_focus(&mut self, cx: &App) {
        let mut previous = std::mem::take(&mut self.result_focus);
        self.result_focus = self
            .rows()
            .iter()
            .map(|row| {
                let focus = previous
                    .remove(&row.id)
                    .unwrap_or_else(|| cx.focus_handle());
                (row.id.clone(), focus)
            })
            .collect();
        self.invalidate_result_measurements();
    }

    fn invalidate_result_measurements(&self) {
        let anchor = self.scroll.logical_scroll_top();
        // Focus metadata keeps the focused row mounted when it scrolls out of
        // view; keyboard navigation still addresses the complete result model.
        self.scroll.splice_focusable(
            0..self.scroll.item_count(),
            self.rows()
                .iter()
                .map(|row| self.result_focus.get(&row.id).cloned()),
        );
        self.scroll.scroll_to(anchor);
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum SearchViewVariant {
    #[default]
    Docked,
    FullScreen,
}

/// Collapsed Material search trigger. It can occupy the [AppBar](gpui_material_components::AppBar)
/// search slot or be placed directly in a desktop layout.
#[derive(IntoElement)]
pub struct SearchBar {
    id: ElementId,
    label: SharedString,
    state: Entity<SearchState>,
    variant: SearchViewVariant,
    close_label: SharedString,
    loading_label: SharedString,
    empty_label: SharedString,
    prompt: SharedString,
}

impl SearchBar {
    pub fn new(
        id: impl Into<ElementId>,
        label: impl Into<SharedString>,
        state: &Entity<SearchState>,
    ) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            state: state.clone(),
            variant: SearchViewVariant::default(),
            close_label: "Close search".into(),
            loading_label: "Searching".into(),
            empty_label: "No results".into(),
            prompt: "Enter a search term".into(),
        }
    }

    pub fn variant(mut self, variant: SearchViewVariant) -> Self {
        self.variant = variant;
        self
    }

    /// Overrides the localized labels used inside the view.
    pub fn labels(
        mut self,
        close: impl Into<SharedString>,
        loading: impl Into<SharedString>,
        empty: impl Into<SharedString>,
        prompt: impl Into<SharedString>,
    ) -> Self {
        self.close_label = close.into();
        self.loading_label = loading.into();
        self.empty_label = empty.into();
        self.prompt = prompt.into();
        self
    }
}

impl RenderOnce for SearchBar {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let collapsed = self.state.read(cx);
        let trigger = collapsed.trigger.clone();
        let scope = collapsed.scope.clone();
        let open = collapsed.open;
        let label = if collapsed.query.is_empty() {
            self.label.clone()
        } else {
            collapsed.query.clone()
        };
        let state = self.state.clone();
        let focus = trigger.clone();
        let button = Button::new((self.id.clone(), "search-trigger"), label)
            .track_focus(&trigger)
            .size(gpui_material_components::ControlSize::Medium)
            .variant(ButtonVariant::FilledTonal)
            .leading_icon(Icon::new(IconName::Search))
            .on_activate(move |window, cx| {
                focus.focus(window, cx);
                state.update(cx, |state, cx| state.open(window, cx));
            });
        if !open {
            return button.into_any_element();
        }
        let theme = window_material_theme(window, cx);
        let direction = theme.layout.direction;
        let scrim = scrim_color(theme, 0.32);
        let state = self.state.clone();
        let view = SearchView::new((self.id.clone(), "search-view"), self.label.clone(), &state)
            .variant(self.variant)
            .labels(
                self.close_label,
                self.loading_label,
                self.empty_label,
                self.prompt,
            );
        match self.variant {
            SearchViewVariant::Docked => Popup::new(self.id, button)
                .anchor(logical_anchor(Anchor::TopLeft, direction))
                .offset(px(4.))
                .content(
                    div()
                        .id("docked-search")
                        .role(Role::Dialog)
                        .aria_label(self.label)
                        .track_focus(&scope)
                        .occlude()
                        .on_mouse_down_out(move |_, window, cx| {
                            state.update(cx, |state, cx| state.close(window, cx));
                        })
                        .child(view),
                )
                .into_any_element(),
            SearchViewVariant::FullScreen => div()
                .child(button)
                .child(
                    Dialog::new(cx)
                        .accessibility_label(self.label)
                        .focus_handle(scope)
                        .backdrop(div().absolute().inset_0().bg(scrim))
                        .on_ok(|_, _, _| false)
                        .on_cancel(move |_, window, cx| {
                            dismiss_search(&state, window, cx);
                            false
                        })
                        .popup(view),
                )
                .into_any_element(),
        }
    }
}

/// Expanded Material search editor and scrollable result region.
#[derive(IntoElement)]
pub struct SearchView {
    id: ElementId,
    label: SharedString,
    state: Entity<SearchState>,
    variant: SearchViewVariant,
    close_label: SharedString,
    loading_label: SharedString,
    empty_label: SharedString,
    prompt: SharedString,
}

impl SearchView {
    pub fn new(
        id: impl Into<ElementId>,
        label: impl Into<SharedString>,
        state: &Entity<SearchState>,
    ) -> Self {
        Self {
            id: id.into(),
            label: label.into(),
            state: state.clone(),
            variant: SearchViewVariant::default(),
            close_label: "Close search".into(),
            loading_label: "Searching".into(),
            empty_label: "No results".into(),
            prompt: "Enter a search term".into(),
        }
    }

    pub fn variant(mut self, variant: SearchViewVariant) -> Self {
        self.variant = variant;
        self
    }

    pub fn labels(
        mut self,
        close: SharedString,
        loading: SharedString,
        empty: SharedString,
        prompt: SharedString,
    ) -> Self {
        self.close_label = close;
        self.loading_label = loading;
        self.empty_label = empty;
        self.prompt = prompt;
        self
    }
}

impl RenderOnce for SearchView {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let layout = (
            window_material_theme(window, cx).typography.clone(),
            window.rem_size(),
            gpui_material_foundation::font_cache_revision(cx),
        );
        self.state.update(cx, |state, _| {
            if state.result_layout.as_ref() != Some(&layout) {
                state.result_layout = Some(layout);
                state.invalidate_result_measurements();
            }
        });
        let model = self.state.read(cx);
        let input = model.input.clone();
        let status = match &model.response.results {
            Results::Idle => Some((self.prompt, false, false)),
            Results::Loading => Some((self.loading_label, true, false)),
            Results::Ready(SearchOutcome::Error(error)) => Some((error.clone(), false, true)),
            Results::Ready(SearchOutcome::Results(results)) if results.is_empty() => {
                Some((self.empty_label, false, false))
            }
            Results::Ready(SearchOutcome::Results(_)) => None,
        };
        let size = window.viewport_size();
        let direction = window_material_theme(window, cx).layout.direction;
        let fullscreen = self.variant == SearchViewVariant::FullScreen;
        let close = self.state.clone();
        let dismiss = self.state.clone();
        let mut results = div()
            .id((self.id.clone(), "results"))
            .occlude()
            .flex()
            .flex_col()
            .flex_1()
            .min_h_0()
            .overflow_hidden();
        if let Some((message, loading, error)) = status {
            results = results.child(
                div()
                    .id((self.id.clone(), "search-status"))
                    .test_support()
                    .role(if error { Role::Alert } else { Role::Status })
                    .aria_label(message.clone())
                    .a11y_synthetic_children(move |tree| {
                        let node = tree.parent_node();
                        node.set_live(if error {
                            gpui::accesskit::Live::Assertive
                        } else {
                            gpui::accesskit::Live::Polite
                        });
                        node.set_live_atomic();
                    })
                    .p_4()
                    .flex()
                    .items_center()
                    .gap_3()
                    .when(loading, |status| {
                        status.child(LoadingIndicator::new("search-loading", message.clone()))
                    })
                    .child(Text::new(message).role(TypeRole::BodyMedium)),
            );
        }
        let scroll = model.scroll.clone();
        let entry = scroll.logical_scroll_top().item_ix;
        let row_state = self.state.clone();
        let keys = self.state.clone();
        if !model.rows().is_empty() {
            results = results.child(
                div()
                    .id((self.id.clone(), "result-list"))
                    .role(Role::List)
                    .flex_1()
                    .min_h_0()
                    .overflow_hidden()
                    .on_key_down(move |event, window, cx| {
                        let model = keys.read(cx);
                        let rows = model.rows();
                        let Some(current) = rows
                            .iter()
                            .position(|row| model.result_focus[&row.id].is_focused(window))
                        else {
                            return;
                        };
                        let next = match event.keystroke.key.as_str() {
                            "up" => (current + rows.len() - 1) % rows.len(),
                            "down" => (current + 1) % rows.len(),
                            _ => return,
                        };
                        let focus = model.result_focus[&rows[next].id].clone();
                        if model.scroll.bounds_for_item(next).is_some() {
                            model.scroll.scroll_to_reveal_item(next);
                        } else {
                            // Unmeasured rows have no physical offset yet. A
                            // logical anchor reveals them without measuring the
                            // entire prefix merely to navigate to the last row.
                            model.scroll.scroll_to(gpui::ListOffset {
                                item_ix: next,
                                offset_in_item: px(0.),
                            });
                        }
                        focus.focus(window, cx);
                        window.refresh();
                        cx.stop_propagation();
                    })
                    .child(
                        gpui::list(scroll, move |ix, window, cx| {
                            let model = row_state.read(cx);
                            let result = model.rows()[ix].clone();
                            let focus = model.result_focus[&result.id].clone();
                            let tab_stop = focus.is_focused(window)
                                || ix == entry
                                    && !model
                                        .result_focus
                                        .values()
                                        .any(|handle| handle.is_focused(window));
                            let state = row_state.clone();
                            ListItem::new(result.id.clone(), result.label.clone())
                                .track_focus(&focus, tab_stop)
                                .when_some(result.supporting.clone(), ListItem::supporting_text)
                                .on_activate(move |window, cx| {
                                    state.update(cx, |state, cx| {
                                        state.close(window, cx);
                                        cx.emit(SearchEvent::Select(result.clone()));
                                    });
                                })
                                .into_any_element()
                        })
                        .size_full(),
                    ),
            );
        }
        div()
            .id(self.id.clone())
            .test_support()
            .capture_key_down(move |event, window, cx| {
                if event.keystroke.key == "escape" && dismiss_search(&dismiss, window, cx) {
                    cx.stop_propagation();
                }
            })
            .child(
                Surface::new(ColorRole::SurfaceContainerHigh)
                    .flex()
                    .flex_col()
                    .w(if fullscreen {
                        size.width
                    } else {
                        px(440.).min(size.width - px(16.))
                    })
                    .h(if fullscreen {
                        size.height
                    } else {
                        px(424.).min(size.height * 0.65)
                    })
                    .when(!fullscreen, |surface| {
                        surface.rounded(px(28.)).shadow_elevation(Elevation::Level3)
                    })
                    .child(
                        div()
                            .flex()
                            .flex_none()
                            .items_center()
                            .p_2()
                            .gap_2()
                            .when(direction == Direction::Rtl, Styled::flex_row_reverse)
                            .child(
                                IconButton::new(
                                    (self.id.clone(), "close-search"),
                                    self.close_label,
                                    Icon::new(IconName::Close),
                                )
                                .on_activate(move |window, cx| {
                                    close.update(cx, |state, cx| state.close(window, cx));
                                }),
                            )
                            .child(div().flex_1().min_w_0().child(TextField::new(
                                (self.id.clone(), "editor"),
                                self.label,
                                &input,
                            ))),
                    )
                    .child(results),
            )
    }
}

fn dismiss_search(state: &Entity<SearchState>, window: &mut Window, cx: &mut App) -> bool {
    state.update(cx, |state, cx| {
        let composing = state.input.update(cx, |input, cx| {
            input.marked_text_range(window, cx).is_some()
        });
        if !composing {
            state.close(window, cx);
        }
        !composing
    })
}

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

    #[test]
    fn query_revision_rejects_stale_duplicate_and_cleared_results() {
        let mut state = SearchResponse::default();
        let first = state.begin("alpha".into()).unwrap();
        let second = state.begin("beta".into()).unwrap();
        assert_eq!(second.query(), "beta");
        assert!(!state.finish(&first, SearchOutcome::Error("stale".into())));
        assert!(state.finish(&second, SearchOutcome::Results(vec![])));
        assert!(!state.finish(&second, SearchOutcome::Error("duplicate".into())));
        let third = state.begin("alpha".into()).unwrap();
        assert!(state.begin(" ".into()).is_none());
        assert!(!state.finish(&third, SearchOutcome::Error("cleared".into())));
        assert!(matches!(state.results, Results::Idle));
    }
}