diff --git a/crates/story/src/list_story.rs b/crates/story/src/list_story.rs index b6556e98..dca166f1 100644 --- a/crates/story/src/list_story.rs +++ b/crates/story/src/list_story.rs @@ -332,6 +332,8 @@ pub struct ListStory { focus_handle: FocusHandle, company_list: Entity>, selected_company: Option>, + selectable: bool, + searchable: bool, _subscriptions: Vec, } @@ -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.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.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") diff --git a/crates/ui/src/list/list.rs b/crates/ui/src/list/list.rs index 6ea6c5f9..f30698d0 100644 --- a/crates/ui/src/list/list.rs +++ b/crates/ui/src/list/list.rs @@ -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.selectable = selectable; + cx.notify(); + } + pub fn delegate(&self) -> &D { &self.delegate } @@ -163,6 +169,10 @@ where window: &mut Window, cx: &mut Context, ) { + 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) { + 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);