gpui-material-extras 0.1.2

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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
use std::{cell::Cell, rc::Rc, time::Duration};

use gpui::{
    Anchor, AnyElement, App, Edges, ElementId, Entity, EntityInputHandler, Hsla,
    InteractiveElement, IntoElement, ParentElement, Pixels, RenderOnce, Role, ScrollHandle,
    SharedString, StatefulInteractiveElement, Styled, Window, canvas, deferred, div, point,
    prelude::FluentBuilder, px,
};
use gpui_base::{Dialog, FocusTrapElement, Popup, Positioner, TestSupportExt};
use gpui_material_components::{
    ButtonColors, ButtonShapes, ButtonVariant, ControlSize, Icon, IconButton, IconName, ListItem,
    LoadingIndicator, Text, TextField, TextFieldColors, button_composition::ButtonPart,
};
use gpui_material_foundation::{Scrim, Surface, logical_anchor, window_material_theme};
use gpui_material_theme::{ColorRole, CornerShape, Direction, Elevation, TypeRole};

use super::{Results, SearchOutcome, SearchResult, SearchScrollState, SearchState, SearchValue};

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

/// Independent appearance overrides. Omitted values use the current window theme.
#[derive(Clone, Copy, Debug, Default)]
pub struct SearchStyle {
    pub trigger_colors: Option<ButtonColors>,
    pub trigger_shapes: Option<ButtonShapes>,
    pub input_colors: Option<TextFieldColors>,
    pub container: Option<Hsla>,
    pub content: Option<Hsla>,
    pub divider: Option<Hsla>,
    pub shape: Option<CornerShape>,
    pub results_shape: Option<CornerShape>,
    pub tonal_elevation: Option<Elevation>,
    pub shadow_elevation: Option<Elevation>,
}

/// Optional screen dimming with a window theme fallback.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SearchScrim {
    pub color: Option<Hsla>,
    pub opacity: f32,
}

impl Default for SearchScrim {
    fn default() -> Self {
        Self {
            color: None,
            opacity: 0.32,
        }
    }
}

/// Behavior for a docked popup anchored to the search trigger.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SearchPopupOptions {
    pub logical_anchor: Anchor,
    pub offset: Pixels,
    pub dismiss_on_outside: bool,
    pub dismiss_on_escape: bool,
    pub trap_focus: bool,
    pub scrim: Option<SearchScrim>,
}

impl Default for SearchPopupOptions {
    fn default() -> Self {
        Self {
            logical_anchor: Anchor::TopLeft,
            offset: px(4.),
            dismiss_on_outside: true,
            dismiss_on_escape: true,
            trap_focus: false,
            scrim: None,
        }
    }
}

/// Properties of the full-window search overlay. Insets use left and right as logical start and end.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SearchWindowOptions {
    pub insets: Edges<Pixels>,
    pub dismiss_on_outside: bool,
    pub dismiss_on_escape: bool,
    pub scrim: SearchScrim,
}

impl Default for SearchWindowOptions {
    fn default() -> Self {
        Self {
            insets: Edges::all(px(0.)),
            dismiss_on_outside: true,
            dismiss_on_escape: true,
            scrim: SearchScrim::default(),
        }
    }
}

/// Show and dismissal durations. The default settles immediately.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct SearchMotion {
    pub expand: Duration,
    pub collapse: Duration,
}

type InputBuilder = Box<dyn FnOnce(TextField) -> AnyElement>;
type RowBuilder = Rc<dyn Fn(ListItem, &SearchResult, &mut Window, &mut App) -> ListItem>;
type ResultBuilder = Box<dyn FnOnce(&Entity<SearchState>, &mut Window, &mut App) -> AnyElement>;

/// A Material search entry that can occupy the existing AppBar search slot.
#[derive(IntoElement)]
pub struct SearchBar {
    id: ElementId,
    view: SearchView,
    collapsed_width: Option<Pixels>,
    leading: Option<AnyElement>,
    trailing: Option<AnyElement>,
    trigger_content: Option<AnyElement>,
    scroll: Option<Entity<SearchScrollState>>,
    linked_scroll: Option<ScrollHandle>,
}

impl SearchBar {
    pub fn new(
        id: impl Into<ElementId>,
        label: impl Into<SharedString>,
        state: &Entity<SearchState>,
    ) -> Self {
        let id = id.into();
        Self {
            view: SearchView::new((id.clone(), "search-view"), label, state),
            id,
            collapsed_width: None,
            leading: None,
            trailing: None,
            trigger_content: None,
            scroll: None,
            linked_scroll: None,
        }
    }

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

    pub fn labels(
        mut self,
        close: impl Into<SharedString>,
        loading: impl Into<SharedString>,
        empty: impl Into<SharedString>,
        prompt: impl Into<SharedString>,
    ) -> Self {
        self.view = self
            .view
            .labels(close.into(), loading.into(), empty.into(), prompt.into());
        self
    }

    /// Applies explicit widths to the trigger and its focused editor.
    pub fn widths(mut self, collapsed: Pixels, focused: Pixels) -> Self {
        self.collapsed_width = valid_dimension(collapsed);
        self.view.focused_width = valid_dimension(focused);
        self
    }

    pub fn leading(mut self, element: impl IntoElement) -> Self {
        self.leading = Some(element.into_any_element());
        self
    }

    pub fn trailing(mut self, element: impl IntoElement) -> Self {
        self.trailing = Some(element.into_any_element());
        self
    }

    pub fn trigger_content(mut self, element: impl IntoElement) -> Self {
        self.trigger_content = Some(element.into_any_element());
        self
    }

    pub fn style(mut self, style: SearchStyle) -> Self {
        self.view.style = style;
        self
    }

    pub fn motion(mut self, motion: SearchMotion) -> Self {
        self.view.motion = motion;
        self
    }

    pub fn expanded_view(mut self, configure: impl FnOnce(SearchView) -> SearchView) -> Self {
        self.view = configure(self.view);
        self
    }

    pub fn scroll_behavior(mut self, state: &Entity<SearchScrollState>) -> Self {
        self.scroll = Some(state.clone());
        self
    }

    pub fn track_scroll(mut self, handle: &ScrollHandle) -> Self {
        self.linked_scroll = Some(handle.clone());
        self
    }
}

impl RenderOnce for SearchBar {
    fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let view = self.view;
        let state = view.state.clone();
        if state.update(cx, |model, cx| model.sample_animation(window, cx)) {
            window.request_animation_frame();
        }
        let model = state.read(cx);
        let focus = model.trigger.clone();
        let scope = model.scope.clone();
        let visible = model.presented();
        let focused = focus.is_focused(window);
        let progress = model.progress();
        let label = if model.query.is_empty() {
            view.label.clone()
        } else {
            model.query.clone()
        };
        let theme = window_material_theme(window, cx);
        let direction = theme.layout.direction;
        let viewport = window.viewport_size();
        let size_limit = (viewport.width - px(16.)).max(px(1.));
        let width = match (self.collapsed_width, view.focused_width) {
            (Some(rest), Some(active)) if visible => Some(rest + (active - rest) * progress),
            (_, Some(active)) if focused => Some(active),
            (rest, _) => rest,
        };
        let id: ElementId = (self.id.clone(), "search-trigger").into();
        let button = match width {
            Some(width) => ButtonPart::new(id, label, width.min(size_limit)),
            None => ButtonPart::intrinsic(id, label),
        };
        let open = state.clone();
        let trigger_focus = focus.clone();
        let motion = view.motion;
        let button = button
            .track_focus(&focus)
            .size(ControlSize::Medium)
            .variant(ButtonVariant::FilledTonal)
            .leading_icon(Icon::new(IconName::Search))
            .when_some(view.style.trigger_shapes, ButtonPart::shapes)
            .when_some(view.style.trigger_colors, ButtonPart::colors)
            .when_some(self.trigger_content, ButtonPart::content)
            .on_activate(move |window, cx| {
                trigger_focus.focus(window, cx);
                open.update(cx, |state, cx| {
                    state.animate_to(SearchValue::Expanded, motion.expand, window, cx)
                });
            });
        let trigger = div()
            .id((self.id.clone(), "trigger-slots"))
            .flex()
            .items_center()
            .gap_2()
            .when(direction == Direction::Rtl, Styled::flex_row_reverse)
            .children(
                self.leading
                    .map(|element| div().id("leading").child(element)),
            )
            .child(button)
            .children(
                self.trailing
                    .map(|element| div().id("trailing").child(element)),
            );

        if !visible {
            return match self.scroll {
                Some(scroll) => scroll_trigger(
                    self.id,
                    trigger,
                    scroll,
                    self.linked_scroll,
                    focused,
                    window,
                    cx,
                ),
                None => trigger.into_any_element(),
            };
        }
        if let Some(scroll) = self.scroll {
            scroll.update(cx, |scroll, _| {
                scroll.measure(
                    self.linked_scroll.as_ref().map(|handle| -handle.offset().y),
                    px(56.),
                    true,
                );
            });
        }

        let label = view.label.clone();
        let close_label = view.close_label.clone();
        match view.variant {
            SearchViewVariant::Docked => {
                let options = view.popup;
                let outside = state.clone();
                let mount = div()
                    .id("docked-search")
                    .role(Role::Dialog)
                    .aria_label(label)
                    .track_focus(&scope)
                    .occlude()
                    .when(
                        options.dismiss_on_outside && options.scrim.is_none(),
                        |dock| {
                            dock.on_mouse_down_out(move |_, window, cx| {
                                dismiss_search(&outside, motion.collapse, window, cx);
                            })
                        },
                    )
                    .child(view);
                let trap = options.trap_focus || options.scrim.is_some();
                let content = if trap {
                    let focus = window
                        .use_keyed_state((self.id.clone(), "dock-trap"), cx, |_, cx| {
                            cx.focus_handle().tab_stop(false)
                        })
                        .read(cx)
                        .clone();
                    mount.focus_trap("docked-search", &focus).into_any_element()
                } else {
                    mount.into_any_element()
                };
                let popup = Popup::new(self.id.clone(), trigger)
                    .anchor(logical_anchor(options.logical_anchor, direction))
                    .offset(length(options.offset))
                    .content(content);
                div()
                    .relative()
                    .children(options.scrim.map(|style| {
                        let backdrop = backdrop(
                            self.id,
                            style,
                            close_label,
                            progress,
                            options.dismiss_on_outside,
                            state,
                            motion.collapse,
                        );
                        deferred(
                            Positioner::corner(Anchor::TopLeft, point(px(0.), px(0.)))
                                .margin(px(0.))
                                .occlude()
                                .child(
                                    div()
                                        .relative()
                                        .w(viewport.width)
                                        .h(viewport.height)
                                        .child(backdrop),
                                ),
                        )
                        .with_priority(99)
                    }))
                    .child(popup)
                    .into_any_element()
            }
            SearchViewVariant::FullScreen | SearchViewVariant::FullScreenContained => {
                let options = view.window;
                let insets = options.insets;
                let exposed = [insets.left, insets.right, insets.top, insets.bottom]
                    .into_iter()
                    .any(|inset| length(inset) > px(0.));
                let backdrop = backdrop(
                    self.id,
                    options.scrim,
                    close_label,
                    progress,
                    options.dismiss_on_outside && exposed,
                    state.clone(),
                    motion.collapse,
                );
                div()
                    .child(trigger)
                    .child(
                        Dialog::new(cx)
                            .accessibility_label(label)
                            .focus_handle(scope)
                            .close_on_backdrop_press(false)
                            .backdrop(backdrop)
                            .on_ok(|_, _, _| false)
                            .on_cancel(move |_, window, cx| {
                                if options.dismiss_on_escape {
                                    dismiss_search(&state, motion.collapse, window, cx);
                                }
                                false
                            })
                            .popup(view),
                    )
                    .into_any_element()
            }
        }
    }
}

fn backdrop(
    id: ElementId,
    style: SearchScrim,
    label: SharedString,
    progress: f32,
    dismissible: bool,
    state: Entity<SearchState>,
    duration: Duration,
) -> impl IntoElement {
    let opacity = if style.opacity.is_nan() {
        0.32
    } else {
        style.opacity.clamp(0., 1.)
    };
    Scrim::new((id, "search-scrim"))
        .opacity(opacity * progress)
        .when_some(style.color, Scrim::color)
        .when(dismissible, |scrim| {
            scrim
                .accessibility_label(label)
                .on_click(move |window, cx| {
                    dismiss_search(&state, duration, window, cx);
                })
        })
}

fn scroll_trigger(
    id: ElementId,
    trigger: impl IntoElement,
    state: Entity<SearchScrollState>,
    linked: Option<ScrollHandle>,
    held_open: bool,
    window: &mut Window,
    cx: &mut App,
) -> AnyElement {
    let size = window
        .use_keyed_state((id.clone(), "search-scroll-height"), cx, |_, _| {
            Rc::new(Cell::new(px(56.)))
        })
        .read(cx)
        .clone();
    let linked_offset = linked.as_ref().map(|handle| -handle.offset().y);
    let distance = state.update(cx, |state, _| {
        state.measure(linked_offset, size.get(), held_open)
    });
    div()
        .id((id, "search-scroll"))
        .test_support()
        .relative()
        .h(size.get() - distance)
        .overflow_hidden()
        .child(
            div().relative().top(-distance).child(trigger).child(
                canvas(
                    move |bounds, window, _| {
                        if size.replace(bounds.size.height) != bounds.size.height
                            || linked
                                .as_ref()
                                .is_some_and(|handle| Some(-handle.offset().y) != linked_offset)
                        {
                            window.refresh();
                        }
                    },
                    |_, _, _, _| {},
                )
                .absolute()
                .inset_0(),
            ),
        )
        .into_any_element()
}

/// Expanded Material search editor and result region, also usable as an independent view.
#[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,
    input: Option<InputBuilder>,
    filters: Option<AnyElement>,
    row: Option<RowBuilder>,
    result_content: Option<ResultBuilder>,
    focused_width: Option<Pixels>,
    docked_dimensions: (Pixels, Pixels),
    gap: Pixels,
    style: SearchStyle,
    motion: SearchMotion,
    popup: SearchPopupOptions,
    window: SearchWindowOptions,
}

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(),
            input: None,
            filters: None,
            row: None,
            result_content: None,
            focused_width: None,
            docked_dimensions: (px(440.), px(424.)),
            gap: px(0.),
            style: SearchStyle::default(),
            motion: SearchMotion::default(),
            popup: SearchPopupOptions::default(),
            window: SearchWindowOptions::default(),
        }
    }

    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
    }

    /// Configures the editor. Keep the provided field in the returned content.
    pub fn input<E: IntoElement + 'static>(
        mut self,
        build: impl FnOnce(TextField) -> E + 'static,
    ) -> Self {
        self.input = Some(Box::new(move |field| build(field).into_any_element()));
        self
    }

    pub fn filters(mut self, content: impl IntoElement) -> Self {
        self.filters = Some(content.into_any_element());
        self
    }

    /// Decorates a provider row. Search installs the stable ID's focus and selection after this callback.
    pub fn result_row(
        mut self,
        build: impl Fn(ListItem, &SearchResult, &mut Window, &mut App) -> ListItem + 'static,
    ) -> Self {
        self.row = Some(Rc::new(build));
        self
    }

    /// Takes over the entire result region while still using the SearchState provider.
    pub fn result_content<E: IntoElement + 'static>(
        mut self,
        build: impl FnOnce(&Entity<SearchState>, &mut Window, &mut App) -> E + 'static,
    ) -> Self {
        self.result_content = Some(Box::new(move |state, window, cx| {
            build(state, window, cx).into_any_element()
        }));
        self
    }

    pub fn focused_width(mut self, width: Pixels) -> Self {
        self.focused_width = valid_dimension(width);
        self
    }

    pub fn docked_size(mut self, width: Pixels, height: Pixels) -> Self {
        self.docked_dimensions = (
            valid_dimension(width).unwrap_or(px(440.)),
            valid_dimension(height).unwrap_or(px(424.)),
        );
        self
    }

    pub fn result_gap(mut self, gap: Pixels) -> Self {
        self.gap = length(gap);
        self
    }

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

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

    pub fn popup_options(mut self, options: SearchPopupOptions) -> Self {
        self.popup = options;
        self
    }

    pub fn window_options(mut self, options: SearchWindowOptions) -> Self {
        self.window = options;
        self
    }
}

impl RenderOnce for SearchView {
    fn render(mut self, window: &mut Window, cx: &mut App) -> impl IntoElement {
        let measurement = (
            window_material_theme(window, cx).typography.clone(),
            window.rem_size(),
            gpui_material_foundation::font_cache_revision(cx),
        );
        let animate = self.state.update(cx, |state, cx| {
            let animate = state.sample_animation(window, cx);
            if state.result_layout.as_ref() != Some(&measurement) {
                state.result_layout = Some(measurement);
                state.invalidate_result_measurements();
            }
            animate
        });
        if animate {
            window.request_animation_frame();
        }
        let model = self.state.read(cx);
        let progress = if model.presented() {
            model.progress()
        } else {
            1.
        };
        let editor = model.input.clone();
        let layout = window_material_theme(window, cx).layout;
        let direction = layout.direction;
        let size = window.viewport_size();
        let docked = self.variant == SearchViewVariant::Docked;
        let contained = self.variant == SearchViewVariant::FullScreenContained;
        let split = docked && (self.gap > px(0.) || self.style.results_shape.is_some());
        let geometry = self.window.insets;
        let (left, right) = layout.horizontal(length(geometry.left), length(geometry.right));
        let width = if docked {
            self.docked_dimensions
                .0
                .min((size.width - px(16.)).max(px(0.)))
        } else {
            (size.width - left - right).max(px(0.))
        };
        let height = if docked {
            self.docked_dimensions.1.min(size.height * 0.65)
        } else {
            (size.height - length(geometry.top) - length(geometry.bottom)).max(px(0.))
        };
        let editor_limit = if contained {
            (width - px(24.)).max(px(0.))
        } else {
            width
        };
        let editor_width = self
            .focused_width
            .or(contained.then_some(px(720.)))
            .map(|width| width.min(editor_limit));
        let close = self.state.clone();
        let dismiss = self.state.clone();
        let duration = self.motion.collapse;
        let close_on_escape = if docked {
            self.popup.dismiss_on_escape
        } else {
            self.window.dismiss_on_escape
        };
        let field = TextField::new((self.id.clone(), "editor"), self.label.clone(), &editor)
            .when_some(self.style.input_colors, TextField::colors);
        let field = match self.input.take() {
            Some(builder) => builder(field),
            None => field.into_any_element(),
        };
        let input = div()
            .id((self.id.clone(), "input-region"))
            .test_support()
            .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.clone(),
                    Icon::new(IconName::Close),
                )
                .on_activate(move |window, cx| {
                    dismiss_search(&close, duration, window, cx);
                }),
            )
            .child(
                div()
                    .flex_1()
                    .min_w_0()
                    .when_some(editor_width, |editor, width| editor.max_w(width))
                    .child(field),
            );

        let mut results = div()
            .id((self.id.clone(), "results"))
            .test_support()
            .occlude()
            .flex()
            .flex_col()
            .flex_1()
            .min_h_0()
            .overflow_hidden()
            .children(
                self.style
                    .divider
                    .map(|color| div().flex_none().h(px(1.)).bg(color)),
            )
            .children(
                self.filters
                    .take()
                    .map(|filters| div().id("filters").flex_none().child(filters)),
            );
        if let Some(builder) = self.result_content.take() {
            results = results.child(builder(&self.state, window, cx));
        } else {
            let status = status_info(
                self.state.read(cx),
                &self.prompt,
                &self.loading_label,
                &self.empty_label,
            );
            if let Some((message, loading, error)) = status {
                results = results.child(search_status(&self.id, message, loading, error));
            } else {
                results = results.child(provider_results(
                    &self.id,
                    &self.state,
                    self.row.take(),
                    duration,
                    window,
                    cx,
                ));
            }
        }

        let panel = panel_surface(self.style, docked, false).w(width).h(height);
        let panel = if split {
            div()
                .flex()
                .flex_col()
                .w(width)
                .h(height)
                .child(panel_surface(self.style, true, false).child(input))
                .child(div().flex_none().h(self.gap.min(height * 0.35)))
                .child(
                    panel_surface(self.style, true, true)
                        .flex_1()
                        .min_h_0()
                        .child(results),
                )
                .into_any_element()
        } else {
            panel
                .child(if contained {
                    div()
                        .flex()
                        .justify_center()
                        .p_3()
                        .child(
                            panel_surface(self.style, false, false)
                                .shape(self.style.shape.unwrap_or(CornerShape::Full))
                                .w(editor_width.unwrap_or(width))
                                .child(input),
                        )
                        .into_any_element()
                } else {
                    input.into_any_element()
                })
                .child(results)
                .into_any_element()
        };
        div()
            .id(self.id)
            .test_support()
            .when(!docked, |view| {
                view.pl(left)
                    .pr(right)
                    .pt(length(geometry.top))
                    .pb(length(geometry.bottom))
            })
            .opacity(progress)
            .capture_key_down(move |event, window, cx| {
                if event.keystroke.key == "escape"
                    && close_on_escape
                    && dismiss_search(&dismiss, duration, window, cx)
                {
                    cx.stop_propagation();
                }
            })
            .child(panel)
    }
}

fn panel_surface(style: SearchStyle, docked: bool, results: bool) -> Surface {
    Surface::new(ColorRole::SurfaceContainerHigh)
        .flex()
        .flex_col()
        .when_some(style.container, Surface::container_color)
        .when_some(style.content, Surface::content_color)
        .when_some(
            if results {
                style.results_shape.or(style.shape)
            } else {
                style.shape
            }
            .or(docked.then_some(CornerShape::Radii([28.; 4]))),
            Surface::shape,
        )
        .when_some(style.tonal_elevation, Surface::tonal_elevation)
        .when_some(
            style
                .shadow_elevation
                .or(docked.then_some(Elevation::Level3)),
            Surface::shadow_elevation,
        )
}

fn status_info(
    model: &SearchState,
    prompt: &SharedString,
    loading: &SharedString,
    empty: &SharedString,
) -> Option<(SharedString, bool, bool)> {
    match &model.response.results {
        Results::Idle => Some((prompt.clone(), false, false)),
        Results::Loading => Some((loading.clone(), true, false)),
        Results::Ready(SearchOutcome::Error(error)) => Some((error.clone(), false, true)),
        Results::Ready(SearchOutcome::Results(results)) if results.is_empty() => {
            Some((empty.clone(), false, false))
        }
        Results::Ready(SearchOutcome::Results(_)) => None,
    }
}

fn search_status(
    id: &ElementId,
    message: SharedString,
    loading: bool,
    error: bool,
) -> impl IntoElement + use<> {
    div()
        .id((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))
}

fn provider_results(
    id: &ElementId,
    state: &Entity<SearchState>,
    custom_row: Option<RowBuilder>,
    duration: Duration,
    window: &Window,
    cx: &App,
) -> impl IntoElement + use<> {
    let model = state.read(cx);
    let scroll = model.scroll.clone();
    let entry = scroll.logical_scroll_top().item_ix;
    let roving = model
        .result_focus
        .values()
        .any(|focus| focus.is_focused(window));
    let in_overlay = model.presented();
    let keys = state.clone();
    let row_state = state.clone();
    div()
        .id((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 {
                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 |index, window, cx| {
                let model = row_state.read(cx);
                let Some(result) = model.rows().get(index).cloned() else {
                    return div().into_any_element();
                };
                let focus = model.result_focus[&result.id].clone();
                let group = result.group.clone().filter(|group| {
                    index == 0 || model.rows()[index - 1].group.as_ref() != Some(group)
                });
                let tab_stop = focus.is_focused(window) || index == entry && !roving;
                let row = ListItem::new(result.id.clone(), result.label.clone())
                    .when_some(result.supporting.clone(), ListItem::supporting_text)
                    .when_some(result.icon, |item, icon| {
                        item.leading(Icon::new(icon).size(px(24.)))
                    });
                let row = match &custom_row {
                    Some(builder) => builder(row, &result, window, cx),
                    None => row,
                };
                let state = row_state.clone();
                let cell_id = (result.id.clone(), "search-result-cell");
                let row = row
                    .track_focus(&focus, tab_stop)
                    .on_activate(move |window, cx| {
                        state.update(cx, |state, cx| {
                            if !in_overlay || state.is_open() {
                                state.select_with_motion(result.clone(), duration, window, cx);
                            }
                        });
                    });
                div()
                    .id(cell_id)
                    .children(group.map(|group| {
                        div()
                            .id("search-group-title")
                            .test_support()
                            .role(Role::Heading)
                            .aria_label(group.clone())
                            .px_4()
                            .pt_3()
                            .child(Text::new(group).role(TypeRole::LabelMedium))
                    }))
                    .child(row)
                    .into_any_element()
            })
            .size_full(),
        )
}

pub(super) fn length(value: Pixels) -> Pixels {
    if f32::from(value).is_finite() {
        value.max(px(0.))
    } else {
        px(0.)
    }
}

fn valid_dimension(value: Pixels) -> Option<Pixels> {
    let value = length(value);
    (value > px(0.)).then_some(value)
}

fn dismiss_search(
    state: &Entity<SearchState>,
    duration: Duration,
    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.animate_to(SearchValue::Collapsed, duration, window, cx);
        }
        !composing
    })
}