1use gpui::Corners;
2use gpui::{
3 Anchor, App, Context, Edges, ElementId, InteractiveElement as _, IntoElement, ParentElement,
4 RenderOnce, StyleRefinement, Styled, Window, div, prelude::FluentBuilder,
5};
6
7use crate::{
8 Disableable, Selectable, Sizable, Size, StyledExt as _,
9 menu::{DropdownMenu, PopupMenu},
10};
11
12use super::{Button, ButtonVariant, ButtonVariants};
13
14#[derive(IntoElement)]
21pub struct DropdownButton {
22 id: ElementId,
23 style: StyleRefinement,
24 button: Option<Button>,
25 menu:
26 Option<Box<dyn Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu + 'static>>,
27 selected: bool,
28 disabled: bool,
29 outline: bool,
32 variant: Option<ButtonVariant>,
33 size: Option<Size>,
34 anchor: Anchor,
35}
36
37impl DropdownButton {
38 pub fn new(id: impl Into<ElementId>) -> Self {
40 Self {
41 id: id.into(),
42 style: StyleRefinement::default(),
43 button: None,
44 menu: None,
45 selected: false,
46 disabled: false,
47 outline: false,
48 variant: None,
49 size: None,
50 anchor: Anchor::TopRight,
51 }
52 }
53
54 fn effective_variant(&self) -> ButtonVariant {
55 self.variant
56 .or_else(|| self.button.as_ref().map(Button::variant))
57 .unwrap_or_default()
58 }
59
60 fn effective_size(&self) -> Size {
61 self.size
62 .or_else(|| self.button.as_ref().map(Button::button_size))
63 .unwrap_or_default()
64 }
65
66 pub fn button(mut self, button: Button) -> Self {
73 self.button = Some(button);
74 self
75 }
76
77 pub fn dropdown_menu(
79 mut self,
80 menu: impl Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu + 'static,
81 ) -> Self {
82 self.menu = Some(Box::new(menu));
83 self
84 }
85
86 pub fn dropdown_menu_with_anchor(
88 mut self,
89 anchor: impl Into<Anchor>,
90 menu: impl Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu + 'static,
91 ) -> Self {
92 self.menu = Some(Box::new(menu));
93 self.anchor = anchor.into();
94 self
95 }
96
97 pub fn outline(mut self) -> Self {
101 self.outline = true;
102 self
103 }
104}
105
106impl Disableable for DropdownButton {
107 fn disabled(mut self, disabled: bool) -> Self {
108 self.disabled = disabled;
109 self
110 }
111}
112
113impl Styled for DropdownButton {
114 fn style(&mut self) -> &mut gpui::StyleRefinement {
115 &mut self.style
116 }
117}
118
119impl Sizable for DropdownButton {
120 fn with_size(mut self, size: impl Into<Size>) -> Self {
121 self.size = Some(size.into());
122 self
123 }
124}
125
126impl ButtonVariants for DropdownButton {
127 fn with_variant(mut self, variant: ButtonVariant) -> Self {
128 self.variant = Some(variant);
129 self
130 }
131}
132
133impl Selectable for DropdownButton {
134 fn selected(mut self, selected: bool) -> Self {
135 self.selected = selected;
136 self
137 }
138
139 fn is_selected(&self) -> bool {
140 self.selected
141 }
142}
143
144impl RenderOnce for DropdownButton {
145 fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
146 debug_assert!(
147 self.button.is_some() || self.menu.is_some(),
148 "a DropdownButton needs a `button`, a `dropdown_menu`, or both"
149 );
150
151 let variant = self.effective_variant();
152 let size = self.effective_size();
153 let selected = self.selected || self.button.as_ref().is_some_and(Selectable::is_selected);
154 let is_ghost = variant.is_ghost();
155 let attached = !(is_ghost && !selected);
156
157 div()
158 .id(self.id)
159 .h_flex()
160 .refine_style(&self.style)
161 .when_some(self.button, |this, button| {
162 let disabled = self.disabled || button.is_disabled();
163 this.child(
164 button
165 .border_corners(Corners {
166 top_left: true,
167 top_right: !attached,
168 bottom_left: true,
169 bottom_right: !attached,
170 })
171 .border_edges(Edges::all(true))
172 .selected(selected)
173 .disabled(disabled)
174 .when(self.outline, |this| this.outline())
175 .with_size(size)
176 .with_variant(variant),
177 )
178 })
179 .when_some(self.menu, |this, menu| {
180 this.child(
181 Button::new("popup")
182 .dropdown_caret(true)
183 .border_corners(Corners {
184 top_left: !attached,
185 top_right: true,
186 bottom_left: !attached,
187 bottom_right: true,
188 })
189 .border_edges(Edges {
190 left: !attached,
191 top: true,
192 right: true,
193 bottom: true,
194 })
195 .selected(selected)
196 .disabled(self.disabled)
197 .when(self.outline, |this| this.outline())
198 .with_size(size)
199 .with_variant(variant)
200 .dropdown_menu_with_anchor(self.anchor, menu),
201 )
202 })
203 }
204}
205
206#[cfg(test)]
207mod tests {
208 use super::*;
209
210 #[gpui::test]
211 fn test_dropdown_button_builder(_cx: &mut gpui::TestAppContext) {
212 let button = Button::new("inner").label("Action");
213 let dropdown = DropdownButton::new("complex-dropdown")
214 .button(button)
215 .primary()
216 .outline()
217 .large()
218 .disabled(false)
219 .selected(false)
220 .dropdown_menu_with_anchor(Anchor::BottomLeft, |menu, _, _| menu);
221
222 assert!(dropdown.button.is_some());
223 assert_eq!(dropdown.variant, Some(ButtonVariant::Primary));
224 assert!(dropdown.outline);
225 assert_eq!(dropdown.size, Some(Size::Large));
226 assert!(!dropdown.disabled);
227 assert!(!dropdown.selected);
228 assert!(dropdown.menu.is_some());
229 assert_eq!(dropdown.anchor, Anchor::BottomLeft);
230 }
231
232 #[gpui::test]
235 fn inner_button_keeps_its_own_variant_and_size(_cx: &mut gpui::TestAppContext) {
236 let dropdown = DropdownButton::new("dropdown")
237 .button(Button::new("inner").label("Action").danger().small())
238 .dropdown_menu(|menu, _, _| menu);
239
240 assert_eq!(dropdown.variant, None);
241 assert_eq!(dropdown.size, None);
242 }
243
244 #[gpui::test]
245 fn inner_ghost_becomes_the_split_variant(_cx: &mut gpui::TestAppContext) {
246 let dropdown = DropdownButton::new("dropdown")
247 .button(Button::new("inner").label("Action").ghost())
248 .dropdown_menu(|menu, _, _| menu);
249
250 assert_eq!(dropdown.effective_variant(), ButtonVariant::Ghost);
251 }
252
253 #[gpui::test]
254 fn inner_size_becomes_the_split_size(_cx: &mut gpui::TestAppContext) {
255 let dropdown = DropdownButton::new("dropdown")
256 .button(Button::new("inner").label("Action").small())
257 .dropdown_menu(|menu, _, _| menu);
258
259 assert_eq!(dropdown.effective_size(), Size::Small);
260 }
261}