From c02cb802cbdac43c32792156863e6ab9b0a5e394 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Fri, 18 Oct 2024 10:17:59 +0800 Subject: [PATCH] list: Add `cx` argument to ListDelegate. (#361) Following #360 TableDelegate changes. --- crates/story/src/list_story.rs | 6 +++--- crates/story/src/modal_story.rs | 6 +++--- crates/ui/src/dropdown.rs | 4 ++-- crates/ui/src/list/list.rs | 16 ++++++++-------- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/crates/story/src/list_story.rs b/crates/story/src/list_story.rs index f6ea2352..25fe52cd 100644 --- a/crates/story/src/list_story.rs +++ b/crates/story/src/list_story.rs @@ -2,7 +2,7 @@ use core::time; use fake::Fake; use gpui::{ - actions, div, px, relative, AnyElement, ElementId, FocusHandle, FocusableView, + actions, div, px, relative, AnyElement, AppContext, ElementId, FocusHandle, FocusableView, InteractiveElement, IntoElement, ParentElement, Render, RenderOnce, Styled, Task, Timer, View, ViewContext, VisualContext, WindowContext, }; @@ -140,11 +140,11 @@ struct CompanyListDelegate { impl ListDelegate for CompanyListDelegate { type Item = CompanyListItem; - fn items_count(&self) -> usize { + fn items_count(&self, _: &AppContext) -> usize { self.matched_companies.len() } - fn confirmed_index(&self) -> Option { + fn confirmed_index(&self, _: &AppContext) -> Option { self.confirmed_index } diff --git a/crates/story/src/modal_story.rs b/crates/story/src/modal_story.rs index 5e1955a8..508404b8 100644 --- a/crates/story/src/modal_story.rs +++ b/crates/story/src/modal_story.rs @@ -2,7 +2,7 @@ use std::{sync::Arc, time::Duration}; use fake::Fake; use gpui::{ - actions, div, prelude::FluentBuilder as _, px, FocusHandle, FocusableView, + actions, div, prelude::FluentBuilder as _, px, AppContext, FocusHandle, FocusableView, InteractiveElement as _, IntoElement, ParentElement, Render, SharedString, Styled, Task, Timer, View, ViewContext, VisualContext as _, WeakView, WindowContext, }; @@ -33,11 +33,11 @@ pub struct ListItemDeletegate { impl ListDelegate for ListItemDeletegate { type Item = ListItem; - fn items_count(&self) -> usize { + fn items_count(&self, _: &AppContext) -> usize { self.matches.len() } - fn confirmed_index(&self) -> Option { + fn confirmed_index(&self, _: &AppContext) -> Option { self.confirmed_index } diff --git a/crates/ui/src/dropdown.rs b/crates/ui/src/dropdown.rs index 7539b675..16bfa45c 100644 --- a/crates/ui/src/dropdown.rs +++ b/crates/ui/src/dropdown.rs @@ -118,11 +118,11 @@ where { type Item = ListItem; - fn items_count(&self) -> usize { + fn items_count(&self, _: &AppContext) -> usize { self.delegate.len() } - fn confirmed_index(&self) -> Option { + fn confirmed_index(&self, _: &AppContext) -> Option { self.selected_index } diff --git a/crates/ui/src/list/list.rs b/crates/ui/src/list/list.rs index 22be1971..910a176e 100644 --- a/crates/ui/src/list/list.rs +++ b/crates/ui/src/list/list.rs @@ -40,7 +40,7 @@ pub trait ListDelegate: Sized + 'static { } /// Return the number of items in the list. - fn items_count(&self) -> usize; + fn items_count(&self, cx: &AppContext) -> usize; /// Render the item at the given index. /// @@ -64,7 +64,7 @@ pub trait ListDelegate: Sized + 'static { } /// Return the confirmed index of the selected item. - fn confirmed_index(&self) -> Option { + fn confirmed_index(&self, cx: &AppContext) -> Option { None } @@ -261,7 +261,7 @@ where } fn on_action_confirm(&mut self, _: &Confirm, cx: &mut ViewContext) { - if self.delegate.items_count() == 0 { + if self.delegate.items_count(cx) == 0 { return; } @@ -270,7 +270,7 @@ where } fn on_action_select_prev(&mut self, _: &SelectPrev, cx: &mut ViewContext) { - if self.delegate.items_count() == 0 { + if self.delegate.items_count(cx) == 0 { return; } @@ -278,7 +278,7 @@ where if selected_index > 0 { self.selected_index = Some(selected_index - 1); } else { - self.selected_index = Some(self.delegate.items_count() - 1); + self.selected_index = Some(self.delegate.items_count(cx) - 1); } self.scroll_to_selected_item(cx); @@ -286,12 +286,12 @@ where } fn on_action_select_next(&mut self, _: &SelectNext, cx: &mut ViewContext) { - if self.delegate.items_count() == 0 { + if self.delegate.items_count(cx) == 0 { return; } if let Some(selected_index) = self.selected_index { - if selected_index < self.delegate.items_count() - 1 { + if selected_index < self.delegate.items_count(cx) - 1 { self.selected_index = Some(selected_index + 1); } else { self.selected_index = Some(0); @@ -325,7 +325,7 @@ where fn render(&mut self, cx: &mut ViewContext) -> impl IntoElement { let view = cx.view().clone(); let vertical_scroll_handle = self.vertical_scroll_handle.clone(); - let items_count = self.delegate.items_count(); + let items_count = self.delegate.items_count(cx); let sizing_behavior = if self.max_height.is_some() { ListSizingBehavior::Infer } else {