list: Avoid select item when selectable(false). (#1508)

Close #1500
This commit is contained in:
Jason Lee 2025-11-04 22:12:31 +08:00 committed by GitHub
parent d037f074dc
commit 5f39b99fe4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 48 additions and 0 deletions

View file

@ -332,6 +332,8 @@ pub struct ListStory {
focus_handle: FocusHandle,
company_list: Entity<ListState<CompanyListDelegate>>,
selected_company: Option<Rc<Company>>,
selectable: bool,
searchable: bool,
_subscriptions: Vec<Subscription>,
}
@ -408,6 +410,8 @@ impl ListStory {
Self {
focus_handle: cx.focus_handle(),
searchable: true,
selectable: true,
company_list,
selected_company: None,
_subscriptions,
@ -420,6 +424,20 @@ impl ListStory {
self.selected_company = Some(company);
}
}
fn toggle_selectable(&mut self, selectable: bool, _: &mut Window, cx: &mut Context<Self>) {
self.selectable = selectable;
self.company_list.update(cx, |list, cx| {
list.set_selectable(self.selectable, cx);
})
}
fn toggle_searchable(&mut self, searchable: bool, _: &mut Window, cx: &mut Context<Self>) {
self.searchable = searchable;
self.company_list.update(cx, |list, cx| {
list.set_searchable(self.searchable, cx);
})
}
}
fn random_company() -> Company {
@ -522,6 +540,22 @@ impl Render for ListStory {
})
})),
)
.child(
Checkbox::new("selectable")
.label("Selectable")
.checked(self.selectable)
.on_click(cx.listener(|this, check: &bool, window, cx| {
this.toggle_selectable(*check, window, cx)
})),
)
.child(
Checkbox::new("searchable")
.label("Searchable")
.checked(self.searchable)
.on_click(cx.listener(|this, check: &bool, window, cx| {
this.toggle_searchable(*check, window, cx)
})),
)
.child(
Checkbox::new("loading")
.label("Loading")

View file

@ -138,6 +138,12 @@ where
self
}
/// Sets whether the list is selectable, default is true.
pub fn set_selectable(&mut self, selectable: bool, cx: &mut Context<Self>) {
self.selectable = selectable;
cx.notify();
}
pub fn delegate(&self) -> &D {
&self.delegate
}
@ -163,6 +169,10 @@ where
window: &mut Window,
cx: &mut Context<Self>,
) {
if !self.selectable {
return;
}
self.selected_index = ix;
self.delegate.set_selected_index(ix, window, cx);
self.scroll_to_selected_item(window, cx);
@ -347,6 +357,10 @@ where
}
fn select_item(&mut self, ix: IndexPath, window: &mut Window, cx: &mut Context<Self>) {
if !self.selectable {
return;
}
self.selected_index = Some(ix);
self.delegate.set_selected_index(Some(ix), window, cx);
self.scroll_to_selected_item(window, cx);