Skip to main content

gpui_material_extras/search/
presentation.rs

1use std::time::Duration;
2
3use gpui::{Anchor, Edges, Hsla, Pixels, px};
4use gpui_material_components::{ButtonColors, ButtonShapes, TextFieldColors};
5use gpui_material_theme::{CornerShape, Elevation};
6
7mod bar;
8mod view;
9
10pub use bar::SearchBar;
11pub use view::SearchView;
12
13/// Presentation layout for an expanded [`SearchView`].
14#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
15pub enum SearchViewVariant {
16    /// Anchors a floating search panel to the [`SearchBar`] trigger.
17    #[default]
18    Docked,
19    /// Fills the window overlay with a full-width search surface.
20    FullScreen,
21    /// Fills the window overlay with a centered pill-shaped search header above results.
22    FullScreenContained,
23}
24
25/// Independent appearance overrides. Omitted values use the current window theme.
26#[derive(Clone, Copy, Debug, Default)]
27pub struct SearchStyle {
28    /// Color overrides for the collapsed [`SearchBar`] trigger button.
29    pub trigger_colors: Option<ButtonColors>,
30    /// Corner shape overrides for the collapsed [`SearchBar`] trigger button.
31    pub trigger_shapes: Option<ButtonShapes>,
32    /// Color overrides for the expanded search input [`gpui_material_components::TextField`].
33    pub input_colors: Option<TextFieldColors>,
34    /// Background fill color for the expanded search surface.
35    pub container: Option<Hsla>,
36    /// Default foreground content color on the expanded search surface.
37    pub content: Option<Hsla>,
38    /// Optional 1px divider color drawn between the input header and results region.
39    pub divider: Option<Hsla>,
40    /// Corner shape override for the search panel (and header when split).
41    pub shape: Option<CornerShape>,
42    /// Optional separate corner shape for the results surface in a split docked view.
43    pub results_shape: Option<CornerShape>,
44    /// Tonal elevation override for the search surface.
45    pub tonal_elevation: Option<Elevation>,
46    /// Shadow elevation override for the search surface.
47    pub shadow_elevation: Option<Elevation>,
48}
49
50/// Optional screen dimming with a window theme fallback.
51#[derive(Clone, Copy, Debug, PartialEq)]
52pub struct SearchScrim {
53    /// Custom scrim color, or `None` to use the theme's scrim role.
54    pub color: Option<Hsla>,
55    /// Target scrim opacity at full expansion (`0.0..=1.0`, defaults to `0.32`).
56    pub opacity: f32,
57}
58
59impl Default for SearchScrim {
60    fn default() -> Self {
61        Self {
62            color: None,
63            opacity: 0.32,
64        }
65    }
66}
67
68/// Behavior for a docked popup anchored to the search trigger.
69#[derive(Clone, Copy, Debug, PartialEq)]
70pub struct SearchPopupOptions {
71    /// Logical anchor corner relative to the trigger (mirrored in RTL). Defaults to [`Anchor::TopLeft`].
72    pub logical_anchor: Anchor,
73    /// Vertical gap in logical pixels between the trigger and the popup. Defaults to `4px`.
74    pub offset: Pixels,
75    /// Dismisses the docked search view when clicking outside its bounds. Defaults to `true`.
76    pub dismiss_on_outside: bool,
77    /// Dismisses the docked search view when pressing Escape. Defaults to `true`.
78    pub dismiss_on_escape: bool,
79    /// Traps keyboard focus inside the docked popup when `true` (also enabled when a scrim is set).
80    pub trap_focus: bool,
81    /// Optional dimmed backdrop rendered behind the docked popup.
82    pub scrim: Option<SearchScrim>,
83}
84
85impl Default for SearchPopupOptions {
86    fn default() -> Self {
87        Self {
88            logical_anchor: Anchor::TopLeft,
89            offset: px(4.),
90            dismiss_on_outside: true,
91            dismiss_on_escape: true,
92            trap_focus: false,
93            scrim: None,
94        }
95    }
96}
97
98/// Properties of the full-window search overlay. Insets use left and right as logical start and end.
99#[derive(Clone, Copy, Debug, PartialEq)]
100pub struct SearchWindowOptions {
101    /// Outer margins around the full-screen overlay (`left`/`right` map to logical start/end).
102    pub insets: Edges<Pixels>,
103    /// Dismisses the overlay when clicking exposed scrim margins. Defaults to `true`.
104    pub dismiss_on_outside: bool,
105    /// Dismisses the overlay when pressing Escape. Defaults to `true`.
106    pub dismiss_on_escape: bool,
107    /// Scrim appearance rendered behind the full-screen overlay.
108    pub scrim: SearchScrim,
109}
110
111impl Default for SearchWindowOptions {
112    fn default() -> Self {
113        Self {
114            insets: Edges::all(px(0.)),
115            dismiss_on_outside: true,
116            dismiss_on_escape: true,
117            scrim: SearchScrim::default(),
118        }
119    }
120}
121
122/// Show and dismissal durations. The default settles immediately.
123#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
124pub struct SearchMotion {
125    /// Duration of the transition from collapsed to expanded.
126    pub expand: Duration,
127    /// Duration of the transition from expanded to collapsed.
128    pub collapse: Duration,
129}
130
131pub(super) fn length(value: Pixels) -> Pixels {
132    if f32::from(value).is_finite() {
133        value.max(px(0.))
134    } else {
135        px(0.)
136    }
137}
138
139fn valid_dimension(value: Pixels) -> Option<Pixels> {
140    let value = length(value);
141    (value > px(0.)).then_some(value)
142}