Simplify ListState API (#35685)

Follow up to: https://github.com/zed-industries/zed/pull/35670,
simplifies the List state APIs so you no longer have to worry about
strong vs. weak pointers when rendering list items.

Release Notes:

- N/A

---------

Co-authored-by: Agus Zubiaga <agus@zed.dev>
This commit is contained in:
Mikayla Maki 2025-08-05 17:02:26 -07:00 committed by GitHub
parent d0de81b0b4
commit 53175263a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 322 additions and 403 deletions

View file

@ -292,7 +292,7 @@ impl<D: PickerDelegate> Picker<D> {
window: &mut Window,
cx: &mut Context<Self>,
) -> Self {
let element_container = Self::create_element_container(container, cx);
let element_container = Self::create_element_container(container);
let scrollbar_state = match &element_container {
ElementContainer::UniformList(scroll_handle) => {
ScrollbarState::new(scroll_handle.clone())
@ -323,31 +323,13 @@ impl<D: PickerDelegate> Picker<D> {
this
}
fn create_element_container(
container: ContainerKind,
cx: &mut Context<Self>,
) -> ElementContainer {
fn create_element_container(container: ContainerKind) -> ElementContainer {
match container {
ContainerKind::UniformList => {
ElementContainer::UniformList(UniformListScrollHandle::new())
}
ContainerKind::List => {
let entity = cx.entity().downgrade();
ElementContainer::List(ListState::new(
0,
gpui::ListAlignment::Top,
px(1000.),
move |ix, window, cx| {
entity
.upgrade()
.map(|entity| {
entity.update(cx, |this, cx| {
this.render_element(window, cx, ix).into_any_element()
})
})
.unwrap_or_else(|| div().into_any_element())
},
))
ElementContainer::List(ListState::new(0, gpui::ListAlignment::Top, px(1000.)))
}
}
}
@ -786,11 +768,16 @@ impl<D: PickerDelegate> Picker<D> {
.py_1()
.track_scroll(scroll_handle.clone())
.into_any_element(),
ElementContainer::List(state) => list(state.clone())
.with_sizing_behavior(sizing_behavior)
.flex_grow()
.py_2()
.into_any_element(),
ElementContainer::List(state) => list(
state.clone(),
cx.processor(|this, ix, window, cx| {
this.render_element(window, cx, ix).into_any_element()
}),
)
.with_sizing_behavior(sizing_behavior)
.flex_grow()
.py_2()
.into_any_element(),
}
}