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

View file

@ -159,10 +159,7 @@ where
.selected_index
.and_then(|ix| self.delegate.get(ix))
.map(|item| item.value().clone());
if let Some(on_change) = &view.on_change {
on_change(&selected_value, cx);
}
cx.emit(DropdownEvent::Confirm(selected_value.clone()));
view.selected_value = selected_value;
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> {
id: ElementId,
focus_handle: FocusHandle,
@ -184,9 +185,6 @@ pub struct Dropdown<D: DropdownDelegate + 'static> {
placeholder: SharedString,
title_prefix: Option<SharedString>,
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>
@ -216,7 +214,6 @@ where
open: false,
cleanable: true,
title_prefix: None,
on_change: None,
};
this.update_selected_value(cx);
this
@ -249,14 +246,6 @@ where
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(
&mut self,
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> FocusableView for Dropdown<D>
where