list: Add cx argument to ListDelegate. (#361)

Following #360 TableDelegate changes.
This commit is contained in:
Jason Lee 2024-10-18 10:17:59 +08:00 committed by GitHub
parent 536bb60900
commit c02cb802cb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 16 additions and 16 deletions

View file

@ -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<usize> {
fn confirmed_index(&self, _: &AppContext) -> Option<usize> {
self.confirmed_index
}

View file

@ -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<usize> {
fn confirmed_index(&self, _: &AppContext) -> Option<usize> {
self.confirmed_index
}

View file

@ -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<usize> {
fn confirmed_index(&self, _: &AppContext) -> Option<usize> {
self.selected_index
}

View file

@ -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<usize> {
fn confirmed_index(&self, cx: &AppContext) -> Option<usize> {
None
}
@ -261,7 +261,7 @@ where
}
fn on_action_confirm(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
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<Self>) {
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<Self>) {
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<Self>) -> 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 {