gpui_material_components/input/
exposed_dropdown.rs1use 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#[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#[derive(Clone, Copy, Debug, PartialEq, Eq)]
34pub enum DropdownAnchor {
35 PrimaryEditable,
37 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#[derive(IntoElement)]
51pub struct ExposedDropdown {
52 id: ElementId,
53 label: SharedString,
54 state: Entity<ExposedDropdownState>,
55 options: Vec<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 {
78 id: id.into(),
79 label: label.into(),
80 state: state.clone(),
81 options: options.into_iter().collect(),
82 primary: None,
83 primary_enabled: true,
84 secondary_enabled: true,
85 secondary: None,
86 editor: None,
87 option_content: None,
88 visibility: Visibility::default(),
89 match_anchor_width: true,
90 popup_width: px(240.),
91 appearance: MenuSurfaceStyle::default(),
92 container_color: None,
93 on_select: None,
94 }
95 }
96
97 pub fn primary_anchor(mut self, anchor: DropdownAnchor) -> Self {
99 self.primary = Some(anchor);
100 self
101 }
102
103 pub fn primary_anchor_enabled(mut self, enabled: bool) -> Self {
104 self.primary_enabled = enabled;
105 self
106 }
107
108 pub fn secondary_anchor<E: IntoElement>(
110 mut self,
111 label: impl Into<SharedString>,
112 content: impl FnOnce(bool) -> E + 'static,
113 ) -> Self {
114 self.secondary = Some((
115 label.into(),
116 Box::new(move |open| content(open).into_any_element()),
117 ));
118 self
119 }
120
121 pub fn secondary_anchor_enabled(mut self, enabled: bool) -> Self {
122 self.secondary_enabled = enabled;
123 self
124 }
125
126 pub fn editor<E: IntoElement>(mut self, render: impl FnOnce(TextField) -> E + 'static) -> Self {
128 self.editor = Some(Box::new(move |field| render(field).into_any_element()));
129 self
130 }
131
132 pub fn option_content<E: IntoElement>(
134 mut self,
135 render: impl Fn(&DropdownOption, bool, &mut Window, &mut App) -> E + 'static,
136 ) -> Self {
137 self.option_content = Some(Rc::new(move |option, highlighted, window, cx| {
138 render(option, highlighted, window, cx).into_any_element()
139 }));
140 self
141 }
142
143 pub fn open(mut self, open: bool) -> Self {
144 self.visibility.controlled = Some(open);
145 self
146 }
147
148 pub fn on_open_change(
149 mut self,
150 callback: impl Fn(bool, &mut Window, &mut App) + 'static,
151 ) -> Self {
152 self.visibility.change = Some(Rc::new(callback));
153 self
154 }
155
156 pub fn on_dismiss(mut self, callback: impl Fn(&mut Window, &mut App) + 'static) -> Self {
157 self.visibility.dismiss = Some(Rc::new(callback));
158 self
159 }
160
161 pub fn match_anchor_width(mut self, matches: bool) -> Self {
162 self.match_anchor_width = matches;
163 self
164 }
165
166 pub fn popup_width(mut self, width: Pixels) -> Self {
168 if f32::from(width).is_finite() && width > px(0.) {
169 self.popup_width = width;
170 }
171 self
172 }
173
174 pub fn appearance(mut self, appearance: MenuSurfaceStyle) -> Self {
175 self.appearance = appearance;
176 self
177 }
178
179 pub fn container_color(mut self, color: Hsla) -> Self {
180 self.container_color = Some(color);
181 self
182 }
183
184 pub fn on_select(
185 mut self,
186 on_select: impl Fn(&DropdownOption, &mut Window, &mut App) + 'static,
187 ) -> Self {
188 self.on_select = Some(Rc::new(on_select));
189 self
190 }
191}