Skip to main content

gpui_material_components/input/
exposed_dropdown.rs

1use std::rc::Rc;
2
3use crate::MenuSurfaceStyle;
4use gpui::{
5    AnyElement, App, ElementId, Entity, Hsla, IntoElement, Pixels, SharedString, Window, px,
6};
7
8use super::TextField;
9
10mod render;
11mod state;
12
13pub use state::ExposedDropdownState;
14use state::Visibility;
15
16/// A selectable dropdown value. IDs remain stable when options are reordered.
17#[derive(Clone, PartialEq, Eq)]
18pub struct DropdownOption {
19    pub id: ElementId,
20    pub label: SharedString,
21}
22
23impl DropdownOption {
24    pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
25        Self {
26            id: id.into(),
27            label: label.into(),
28        }
29    }
30}
31
32/// Focus and activation behavior for the primary dropdown anchor.
33#[derive(Clone, Copy, Debug, PartialEq, Eq)]
34pub enum DropdownAnchor {
35    /// Allows typing and keeps focus in the field when the menu opens.
36    PrimaryEditable,
37    /// Moves focus to the list when the read-only field opens the menu.
38    PrimaryNotEditable,
39}
40
41type OpenChange = Rc<dyn Fn(bool, &mut Window, &mut App)>;
42type Dismiss = Rc<dyn Fn(&mut Window, &mut App)>;
43type Select = Rc<dyn Fn(&DropdownOption, &mut Window, &mut App)>;
44type Editor = Box<dyn FnOnce(TextField) -> AnyElement>;
45type AnchorContent = Box<dyn FnOnce(bool) -> AnyElement>;
46type OptionContent = Rc<dyn Fn(&DropdownOption, bool, &mut Window, &mut App) -> AnyElement>;
47
48/// A Material field and anchored list in editable and selection-only modes.
49/// Typing filters the list, while only selecting an option calls the owner.
50#[derive(IntoElement)]
51pub struct ExposedDropdown {
52    id: ElementId,
53    label: SharedString,
54    state: Entity<ExposedDropdownState>,
55    options: Rc<[DropdownOption]>,
56    primary: Option<DropdownAnchor>,
57    primary_enabled: bool,
58    secondary_enabled: bool,
59    secondary: Option<(SharedString, AnchorContent)>,
60    editor: Option<Editor>,
61    option_content: Option<OptionContent>,
62    visibility: Visibility,
63    match_anchor_width: bool,
64    popup_width: Pixels,
65    appearance: MenuSurfaceStyle,
66    container_color: Option<Hsla>,
67    on_select: Option<Select>,
68}
69
70impl ExposedDropdown {
71    pub fn new(
72        id: impl Into<ElementId>,
73        label: impl Into<SharedString>,
74        state: &Entity<ExposedDropdownState>,
75        options: impl IntoIterator<Item = DropdownOption>,
76    ) -> Self {
77        Self::with_shared_options(id, label, state, options.into_iter().collect::<Vec<_>>())
78    }
79
80    /// Uses a caller-retained option slice to avoid per-render collection and
81    /// linear slice equality comparisons.
82    pub fn with_shared_options(
83        id: impl Into<ElementId>,
84        label: impl Into<SharedString>,
85        state: &Entity<ExposedDropdownState>,
86        options: impl Into<Rc<[DropdownOption]>>,
87    ) -> Self {
88        Self {
89            id: id.into(),
90            label: label.into(),
91            state: state.clone(),
92            options: options.into(),
93            primary: None,
94            primary_enabled: true,
95            secondary_enabled: true,
96            secondary: None,
97            editor: None,
98            option_content: None,
99            visibility: Visibility::default(),
100            match_anchor_width: true,
101            popup_width: px(240.),
102            appearance: MenuSurfaceStyle::default(),
103            container_color: None,
104            on_select: None,
105        }
106    }
107
108    /// Controls primary activation and focus. Pair the anchor with the state's editing mode.
109    pub fn primary_anchor(mut self, anchor: DropdownAnchor) -> Self {
110        self.primary = Some(anchor);
111        self
112    }
113
114    pub fn primary_anchor_enabled(mut self, enabled: bool) -> Self {
115        self.primary_enabled = enabled;
116        self
117    }
118
119    /// Supplies the name and decorative content of the secondary anchor.
120    pub fn secondary_anchor<E: IntoElement>(
121        mut self,
122        label: impl Into<SharedString>,
123        content: impl FnOnce(bool) -> E + 'static,
124    ) -> Self {
125        self.secondary = Some((
126            label.into(),
127            Box::new(move |open| content(open).into_any_element()),
128        ));
129        self
130    }
131
132    pub fn secondary_anchor_enabled(mut self, enabled: bool) -> Self {
133        self.secondary_enabled = enabled;
134        self
135    }
136
137    /// Styles or wraps the field carrying the dropdown's retained editor and semantics.
138    pub fn editor<E: IntoElement>(mut self, render: impl FnOnce(TextField) -> E + 'static) -> Self {
139        self.editor = Some(Box::new(move |field| render(field).into_any_element()));
140        self
141    }
142
143    /// Supplies repeatable decorative content for each visible option.
144    pub fn option_content<E: IntoElement>(
145        mut self,
146        render: impl Fn(&DropdownOption, bool, &mut Window, &mut App) -> E + 'static,
147    ) -> Self {
148        self.option_content = Some(Rc::new(move |option, highlighted, window, cx| {
149            render(option, highlighted, window, cx).into_any_element()
150        }));
151        self
152    }
153
154    pub fn open(mut self, open: bool) -> Self {
155        self.visibility.controlled = Some(open);
156        self
157    }
158
159    pub fn on_open_change(
160        mut self,
161        callback: impl Fn(bool, &mut Window, &mut App) + 'static,
162    ) -> Self {
163        self.visibility.change = Some(Rc::new(callback));
164        self
165    }
166
167    pub fn on_dismiss(mut self, callback: impl Fn(&mut Window, &mut App) + 'static) -> Self {
168        self.visibility.dismiss = Some(Rc::new(callback));
169        self
170    }
171
172    pub fn match_anchor_width(mut self, matches: bool) -> Self {
173        self.match_anchor_width = matches;
174        self
175    }
176
177    /// Sets the independent width used with match_anchor_width(false).
178    pub fn popup_width(mut self, width: Pixels) -> Self {
179        if f32::from(width).is_finite() && width > px(0.) {
180            self.popup_width = width;
181        }
182        self
183    }
184
185    pub fn appearance(mut self, appearance: MenuSurfaceStyle) -> Self {
186        self.appearance = appearance;
187        self
188    }
189
190    pub fn container_color(mut self, color: Hsla) -> Self {
191        self.container_color = Some(color);
192        self
193    }
194
195    pub fn on_select(
196        mut self,
197        on_select: impl Fn(&DropdownOption, &mut Window, &mut App) + 'static,
198    ) -> Self {
199        self.on_select = Some(Rc::new(on_select));
200        self
201    }
202}