dropdown: Use event instead of callback (#46)

* Rule: use event for `Render`, use callback for `RenderOnce`
This commit is contained in:
Floyd Wang 2024-07-18 18:40:32 +08:00 committed by GitHub
parent bac5392e0a
commit 653222db73
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 57 additions and 57 deletions

View file

@ -6,7 +6,7 @@ use gpui::{
}; };
use ui::{ use ui::{
dropdown::{Dropdown, DropdownItem}, dropdown::{Dropdown, DropdownEvent, DropdownItem},
h_flex, h_flex,
theme::ActiveTheme, theme::ActiveTheme,
v_flex, Selection, v_flex, Selection,
@ -40,7 +40,7 @@ impl DropdownItem for Country {
pub struct DropdownStory { pub struct DropdownStory {
country_dropdown: View<Dropdown<Vec<Country>>>, country_dropdown: View<Dropdown<Vec<Country>>>,
furit_dropdown: View<Dropdown<Vec<SharedString>>>, fruit_dropdown: View<Dropdown<Vec<SharedString>>>,
simple_dropdown1: View<Dropdown<Vec<SharedString>>>, simple_dropdown1: View<Dropdown<Vec<SharedString>>>,
simple_dropdown2: View<Dropdown<Vec<SharedString>>>, simple_dropdown2: View<Dropdown<Vec<SharedString>>>,
} }
@ -61,13 +61,10 @@ impl DropdownStory {
Country::new("Ecuador", "EC"), Country::new("Ecuador", "EC"),
]; ];
let country_dropdown = cx.new_view(|cx| { let country_dropdown =
Dropdown::new("dropdown-country", countries, Some(6), cx).on_change(|value, _cx| { cx.new_view(|cx| Dropdown::new("dropdown-country", countries, Some(6), cx));
println!("Country changed: {:?}", value);
})
});
let furits = vec![ let fruits = vec![
"Apple", "Apple",
"Orange", "Orange",
"Banana", "Banana",
@ -76,37 +73,39 @@ impl DropdownStory {
"Watermelon", "Watermelon",
"Avocado", "Avocado",
]; ];
let furit_dropdown = cx.new_view(|cx| { let fruit_dropdown =
Dropdown::string_list("dropdown-furits", furits, None, cx).on_change(|value, _cx| { cx.new_view(|cx| Dropdown::string_list("dropdown-fruits", fruits, None, cx));
println!("Furit changed: {:?}", value);
})
});
cx.new_view(|cx| Self { cx.new_view(|cx| {
country_dropdown, cx.subscribe(&country_dropdown, Self::on_dropdown_event)
furit_dropdown, .detach();
simple_dropdown1: cx.new_view(|cx| {
Dropdown::string_list( Self {
"string-list1", country_dropdown,
vec!["QPUI", "Iced", "QT", "Cocoa"], fruit_dropdown,
Some(0), simple_dropdown1: cx.new_view(|cx| {
cx, Dropdown::string_list(
) "string-list1",
.size(ui::Size::Small) vec!["QPUI", "Iced", "QT", "Cocoa"],
.placeholder("UI") Some(0),
.title_prefix("UI: ") cx,
}), )
simple_dropdown2: cx.new_view(|cx| { .size(ui::Size::Small)
Dropdown::string_list( .placeholder("UI")
"string-list2", .title_prefix("UI: ")
vec!["Rust", "Go", "C++", "JavaScript"], }),
None, simple_dropdown2: cx.new_view(|cx| {
cx, Dropdown::string_list(
) "string-list2",
.size(ui::Size::Small) vec!["Rust", "Go", "C++", "JavaScript"],
.placeholder("Language") None,
.title_prefix("Language: ") cx,
}), )
.size(ui::Size::Small)
.placeholder("Language")
.title_prefix("Language: ")
}),
}
}) })
} }
@ -114,6 +113,17 @@ impl DropdownStory {
fn on_click(sel: &Selection, cx: &mut WindowContext) { fn on_click(sel: &Selection, cx: &mut WindowContext) {
println!("Check value changed: {}", sel); println!("Check value changed: {}", sel);
} }
fn on_dropdown_event(
&mut self,
_: View<Dropdown<Vec<Country>>>,
event: &DropdownEvent<Vec<Country>>,
_cx: &mut ViewContext<Self>,
) {
match event {
DropdownEvent::Confirm(value) => println!("Selected country: {:?}", value),
}
}
} }
impl Render for DropdownStory { impl Render for DropdownStory {
@ -128,7 +138,7 @@ impl Render for DropdownStory {
.items_center() .items_center()
.gap_4() .gap_4()
.child(self.country_dropdown.clone()) .child(self.country_dropdown.clone())
.child(self.furit_dropdown.clone()), .child(self.fruit_dropdown.clone()),
) )
.child( .child(
v_flex() v_flex()
@ -145,8 +155,8 @@ impl Render for DropdownStory {
self.country_dropdown.read(cx).selected_value() self.country_dropdown.read(cx).selected_value()
)) ))
.child(format!( .child(format!(
"Furit: {:?}", "fruit: {:?}",
self.furit_dropdown.read(cx).selected_value() self.fruit_dropdown.read(cx).selected_value()
)) ))
.child(format!( .child(format!(
"UI: {:?}", "UI: {:?}",

View file

@ -159,10 +159,7 @@ where
.selected_index .selected_index
.and_then(|ix| self.delegate.get(ix)) .and_then(|ix| self.delegate.get(ix))
.map(|item| item.value().clone()); .map(|item| item.value().clone());
cx.emit(DropdownEvent::Confirm(selected_value.clone()));
if let Some(on_change) = &view.on_change {
on_change(&selected_value, cx);
}
view.selected_value = selected_value; view.selected_value = selected_value;
view.open = false; view.open = false;
}); });
@ -174,6 +171,10 @@ where
} }
} }
pub enum DropdownEvent<D: DropdownDelegate + 'static> {
Confirm(Option<<D::Item as DropdownItem>::Value>),
}
pub struct Dropdown<D: DropdownDelegate + 'static> { pub struct Dropdown<D: DropdownDelegate + 'static> {
id: ElementId, id: ElementId,
focus_handle: FocusHandle, focus_handle: FocusHandle,
@ -184,9 +185,6 @@ pub struct Dropdown<D: DropdownDelegate + 'static> {
placeholder: SharedString, placeholder: SharedString,
title_prefix: Option<SharedString>, title_prefix: Option<SharedString>,
selected_value: Option<<D::Item as DropdownItem>::Value>, selected_value: Option<<D::Item as DropdownItem>::Value>,
on_change: Option<
Box<dyn Fn(&Option<<D::Item as DropdownItem>::Value>, &mut WindowContext) + 'static>,
>,
} }
impl<D> Dropdown<D> impl<D> Dropdown<D>
@ -216,7 +214,6 @@ where
open: false, open: false,
cleanable: true, cleanable: true,
title_prefix: None, title_prefix: None,
on_change: None,
}; };
this.update_selected_value(cx); this.update_selected_value(cx);
this this
@ -249,14 +246,6 @@ where
self self
} }
pub fn on_change(
mut self,
on_change: impl Fn(&Option<<D::Item as DropdownItem>::Value>, &mut WindowContext) + 'static,
) -> Self {
self.on_change = Some(Box::new(on_change));
self
}
pub fn set_selected_index( pub fn set_selected_index(
&mut self, &mut self,
selected_index: Option<usize>, selected_index: Option<usize>,
@ -390,6 +379,7 @@ impl Dropdown<Vec<SharedString>> {
} }
} }
impl<D> EventEmitter<DropdownEvent<D>> for Dropdown<D> where D: DropdownDelegate + 'static {}
impl<D> EventEmitter<DismissEvent> for Dropdown<D> where D: DropdownDelegate + 'static {} impl<D> EventEmitter<DismissEvent> for Dropdown<D> where D: DropdownDelegate + 'static {}
impl<D> FocusableView for Dropdown<D> impl<D> FocusableView for Dropdown<D>
where where