From 000c0bec5792ead3e11976a09d6dbbd453aafd2d Mon Sep 17 00:00:00 2001 From: Floyd Wang Date: Wed, 16 Oct 2024 17:33:46 +0800 Subject: [PATCH] dropdown: Add `set_disabled` for `&mut self` (#351) --- crates/story/src/dropdown_story.rs | 38 +++++++++++++++++++++++++++++- crates/ui/src/dropdown.rs | 10 ++++---- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/crates/story/src/dropdown_story.rs b/crates/story/src/dropdown_story.rs index 9a73b153..1e451829 100644 --- a/crates/story/src/dropdown_story.rs +++ b/crates/story/src/dropdown_story.rs @@ -4,10 +4,12 @@ use gpui::{ }; use ui::{ + button::{Button, ButtonStyled}, + button_group::ButtonGroup, dropdown::{Dropdown, DropdownEvent, DropdownItem, SearchableVec}, h_flex, theme::ActiveTheme, - v_flex, FocusableCycle, IconName, Sizable, + v_flex, FocusableCycle, IconName, Selectable, Sizable, }; actions!(dropdown_story, [Tab, TabPrev]); @@ -47,6 +49,7 @@ impl DropdownItem for Country { } pub struct DropdownStory { + disabled: bool, country_dropdown: View>>, fruit_dropdown: View>>, simple_dropdown1: View>>, @@ -115,6 +118,7 @@ impl DropdownStory { .detach(); Self { + disabled: false, country_dropdown, fruit_dropdown, simple_dropdown1: cx.new_view(|cx| { @@ -188,6 +192,20 @@ impl DropdownStory { self.cycle_focus(false, cx); cx.notify(); } + + fn toggle_disabled(&mut self, disabled: bool, cx: &mut ViewContext) { + self.disabled = disabled; + self.country_dropdown + .update(cx, |this, _| this.set_disabled(disabled)); + self.fruit_dropdown + .update(cx, |this, _| this.set_disabled(disabled)); + self.simple_dropdown1 + .update(cx, |this, _| this.set_disabled(disabled)); + self.simple_dropdown2 + .update(cx, |this, _| this.set_disabled(disabled)); + self.simple_dropdown3 + .update(cx, |this, _| this.set_disabled(disabled)); + } } impl FocusableCycle for DropdownStory { @@ -213,6 +231,24 @@ impl Render for DropdownStory { .on_action(cx.listener(Self::on_key_shift_tab)) .size_full() .gap_4() + .child( + ButtonGroup::new("button-group") + .primary() + .small() + .on_click(cx.listener(|this, index: &Vec, cx| { + this.toggle_disabled(index.contains(&1), cx); + })) + .child( + Button::new("Enable") + .label("Enable") + .selected(!self.disabled), + ) + .child( + Button::new("Disable") + .label("Disable") + .selected(self.disabled), + ), + ) .child( h_flex() .w_full() diff --git a/crates/ui/src/dropdown.rs b/crates/ui/src/dropdown.rs index dba9f36e..82713d20 100644 --- a/crates/ui/src/dropdown.rs +++ b/crates/ui/src/dropdown.rs @@ -393,6 +393,10 @@ where self } + pub fn set_disabled(&mut self, disabled: bool) { + self.disabled = disabled; + } + pub fn empty(mut self, f: F) -> Self where E: IntoElement, @@ -519,11 +523,7 @@ where .unwrap_or_default(); h_flex() - .children(self.title_prefix.clone().map(|prefix| { - div() - .text_color(cx.theme().accent_foreground) - .child(prefix.clone()) - })) + .when_some(self.title_prefix.clone(), |this, prefix| this.child(prefix)) .child(title.clone()) } else { div().text_color(cx.theme().accent_foreground).child(