dropdown: Add set_disabled for &mut self (#351)

This commit is contained in:
Floyd Wang 2024-10-16 17:33:46 +08:00 committed by GitHub
parent c91028b756
commit 000c0bec57
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 42 additions and 6 deletions

View file

@ -4,10 +4,12 @@ use gpui::{
}; };
use ui::{ use ui::{
button::{Button, ButtonStyled},
button_group::ButtonGroup,
dropdown::{Dropdown, DropdownEvent, DropdownItem, SearchableVec}, dropdown::{Dropdown, DropdownEvent, DropdownItem, SearchableVec},
h_flex, h_flex,
theme::ActiveTheme, theme::ActiveTheme,
v_flex, FocusableCycle, IconName, Sizable, v_flex, FocusableCycle, IconName, Selectable, Sizable,
}; };
actions!(dropdown_story, [Tab, TabPrev]); actions!(dropdown_story, [Tab, TabPrev]);
@ -47,6 +49,7 @@ impl DropdownItem for Country {
} }
pub struct DropdownStory { pub struct DropdownStory {
disabled: bool,
country_dropdown: View<Dropdown<Vec<Country>>>, country_dropdown: View<Dropdown<Vec<Country>>>,
fruit_dropdown: View<Dropdown<SearchableVec<SharedString>>>, fruit_dropdown: View<Dropdown<SearchableVec<SharedString>>>,
simple_dropdown1: View<Dropdown<Vec<SharedString>>>, simple_dropdown1: View<Dropdown<Vec<SharedString>>>,
@ -115,6 +118,7 @@ impl DropdownStory {
.detach(); .detach();
Self { Self {
disabled: false,
country_dropdown, country_dropdown,
fruit_dropdown, fruit_dropdown,
simple_dropdown1: cx.new_view(|cx| { simple_dropdown1: cx.new_view(|cx| {
@ -188,6 +192,20 @@ impl DropdownStory {
self.cycle_focus(false, cx); self.cycle_focus(false, cx);
cx.notify(); cx.notify();
} }
fn toggle_disabled(&mut self, disabled: bool, cx: &mut ViewContext<Self>) {
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 { impl FocusableCycle for DropdownStory {
@ -213,6 +231,24 @@ impl Render for DropdownStory {
.on_action(cx.listener(Self::on_key_shift_tab)) .on_action(cx.listener(Self::on_key_shift_tab))
.size_full() .size_full()
.gap_4() .gap_4()
.child(
ButtonGroup::new("button-group")
.primary()
.small()
.on_click(cx.listener(|this, index: &Vec<usize>, 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( .child(
h_flex() h_flex()
.w_full() .w_full()

View file

@ -393,6 +393,10 @@ where
self self
} }
pub fn set_disabled(&mut self, disabled: bool) {
self.disabled = disabled;
}
pub fn empty<E, F>(mut self, f: F) -> Self pub fn empty<E, F>(mut self, f: F) -> Self
where where
E: IntoElement, E: IntoElement,
@ -519,11 +523,7 @@ where
.unwrap_or_default(); .unwrap_or_default();
h_flex() h_flex()
.children(self.title_prefix.clone().map(|prefix| { .when_some(self.title_prefix.clone(), |this, prefix| this.child(prefix))
div()
.text_color(cx.theme().accent_foreground)
.child(prefix.clone())
}))
.child(title.clone()) .child(title.clone())
} else { } else {
div().text_color(cx.theme().accent_foreground).child( div().text_color(cx.theme().accent_foreground).child(