dropdown_menu.rs

  1use gpui::{ClickEvent, CursorStyle, MouseButton, View};
  2
  3use crate::{prelude::*, ContextMenu, PopoverMenu};
  4
  5#[derive(IntoElement)]
  6pub struct DropdownMenu {
  7    id: ElementId,
  8    label: SharedString,
  9    menu: View<ContextMenu>,
 10    full_width: bool,
 11    disabled: bool,
 12}
 13
 14impl DropdownMenu {
 15    pub fn new(
 16        id: impl Into<ElementId>,
 17        label: impl Into<SharedString>,
 18        menu: View<ContextMenu>,
 19    ) -> Self {
 20        Self {
 21            id: id.into(),
 22            label: label.into(),
 23            menu,
 24            full_width: false,
 25            disabled: false,
 26        }
 27    }
 28
 29    pub fn full_width(mut self, full_width: bool) -> Self {
 30        self.full_width = full_width;
 31        self
 32    }
 33}
 34
 35impl Disableable for DropdownMenu {
 36    fn disabled(mut self, disabled: bool) -> Self {
 37        self.disabled = disabled;
 38        self
 39    }
 40}
 41
 42impl RenderOnce for DropdownMenu {
 43    fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
 44        PopoverMenu::new(self.id)
 45            .full_width(self.full_width)
 46            .menu(move |_cx| Some(self.menu.clone()))
 47            .trigger(DropdownMenuTrigger::new(self.label).full_width(self.full_width))
 48    }
 49}
 50
 51#[derive(IntoElement)]
 52struct DropdownMenuTrigger {
 53    label: SharedString,
 54    full_width: bool,
 55    selected: bool,
 56    disabled: bool,
 57    cursor_style: CursorStyle,
 58    on_click: Option<Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>>,
 59}
 60
 61impl DropdownMenuTrigger {
 62    pub fn new(label: impl Into<SharedString>) -> Self {
 63        Self {
 64            label: label.into(),
 65            full_width: false,
 66            selected: false,
 67            disabled: false,
 68            cursor_style: CursorStyle::default(),
 69            on_click: None,
 70        }
 71    }
 72
 73    pub fn full_width(mut self, full_width: bool) -> Self {
 74        self.full_width = full_width;
 75        self
 76    }
 77}
 78
 79impl Disableable for DropdownMenuTrigger {
 80    fn disabled(mut self, disabled: bool) -> Self {
 81        self.disabled = disabled;
 82        self
 83    }
 84}
 85
 86impl Selectable for DropdownMenuTrigger {
 87    fn selected(mut self, selected: bool) -> Self {
 88        self.selected = selected;
 89        self
 90    }
 91}
 92
 93impl Clickable for DropdownMenuTrigger {
 94    fn on_click(mut self, handler: impl Fn(&ClickEvent, &mut WindowContext) + 'static) -> Self {
 95        self.on_click = Some(Box::new(handler));
 96        self
 97    }
 98
 99    fn cursor_style(mut self, cursor_style: CursorStyle) -> Self {
100        self.cursor_style = cursor_style;
101        self
102    }
103}
104
105impl RenderOnce for DropdownMenuTrigger {
106    fn render(self, cx: &mut WindowContext) -> impl IntoElement {
107        let disabled = self.disabled;
108
109        h_flex()
110            .id("dropdown-menu-trigger")
111            .justify_between()
112            .rounded_md()
113            .bg(cx.theme().colors().editor_background)
114            .pl_2()
115            .pr_1p5()
116            .py_0p5()
117            .gap_2()
118            .min_w_20()
119            .map(|el| {
120                if self.full_width {
121                    el.w_full()
122                } else {
123                    el.flex_none().w_auto()
124                }
125            })
126            .map(|el| {
127                if disabled {
128                    el.cursor_not_allowed()
129                } else {
130                    el.cursor_pointer()
131                }
132            })
133            .child(Label::new(self.label).color(if disabled {
134                Color::Disabled
135            } else {
136                Color::Default
137            }))
138            .child(
139                Icon::new(IconName::ChevronUpDown)
140                    .size(IconSize::XSmall)
141                    .color(if disabled {
142                        Color::Disabled
143                    } else {
144                        Color::Muted
145                    }),
146            )
147            .when_some(self.on_click.filter(|_| !disabled), |el, on_click| {
148                el.on_mouse_down(MouseButton::Left, |_, cx| cx.prevent_default())
149                    .on_click(move |event, cx| {
150                        cx.stop_propagation();
151                        (on_click)(event, cx)
152                    })
153            })
154    }
155}