list.rs

   1//! A list element that can be used to render a large number of differently sized elements
   2//! efficiently. Clients of this API need to ensure that elements outside of the scrolled
   3//! area do not change their height for this element to function correctly. If your elements
   4//! do change height, notify the list element via [`ListState::splice`] or [`ListState::reset`].
   5//! In order to minimize re-renders, this element's state is stored intrusively
   6//! on your own views, so that your code can coordinate directly with the list element's cached state.
   7//!
   8//! If all of your elements are the same height, see [`UniformList`] for a simpler API
   9
  10use crate::{
  11    AnyElement, App, AvailableSpace, Bounds, ContentMask, DispatchPhase, Edges, Element, EntityId,
  12    FocusHandle, GlobalElementId, Hitbox, HitboxBehavior, InspectorElementId, IntoElement,
  13    Overflow, Pixels, Point, ScrollWheelEvent, Size, Style, StyleRefinement, Styled, Window, point,
  14    px, size,
  15};
  16use collections::VecDeque;
  17use refineable::Refineable as _;
  18use std::{cell::RefCell, ops::Range, rc::Rc};
  19use sum_tree::{Bias, SumTree};
  20
  21/// Construct a new list element
  22pub fn list(state: ListState) -> List {
  23    List {
  24        state,
  25        style: StyleRefinement::default(),
  26        sizing_behavior: ListSizingBehavior::default(),
  27    }
  28}
  29
  30/// A list element
  31pub struct List {
  32    state: ListState,
  33    style: StyleRefinement,
  34    sizing_behavior: ListSizingBehavior,
  35}
  36
  37impl List {
  38    /// Set the sizing behavior for the list.
  39    pub fn with_sizing_behavior(mut self, behavior: ListSizingBehavior) -> Self {
  40        self.sizing_behavior = behavior;
  41        self
  42    }
  43}
  44
  45/// The list state that views must hold on behalf of the list element.
  46#[derive(Clone)]
  47pub struct ListState(Rc<RefCell<StateInner>>);
  48
  49impl std::fmt::Debug for ListState {
  50    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  51        f.write_str("ListState")
  52    }
  53}
  54
  55struct StateInner {
  56    last_layout_bounds: Option<Bounds<Pixels>>,
  57    last_padding: Option<Edges<Pixels>>,
  58    render_item: Box<dyn FnMut(usize, &mut Window, &mut App) -> AnyElement>,
  59    items: SumTree<ListItem>,
  60    logical_scroll_top: Option<ListOffset>,
  61    alignment: ListAlignment,
  62    overdraw: Pixels,
  63    reset: bool,
  64    #[allow(clippy::type_complexity)]
  65    scroll_handler: Option<Box<dyn FnMut(&ListScrollEvent, &mut Window, &mut App)>>,
  66    scrollbar_drag_start_height: Option<Pixels>,
  67}
  68
  69/// Whether the list is scrolling from top to bottom or bottom to top.
  70#[derive(Clone, Copy, Debug, Eq, PartialEq)]
  71pub enum ListAlignment {
  72    /// The list is scrolling from top to bottom, like most lists.
  73    Top,
  74    /// The list is scrolling from bottom to top, like a chat log.
  75    Bottom,
  76}
  77
  78/// A scroll event that has been converted to be in terms of the list's items.
  79pub struct ListScrollEvent {
  80    /// The range of items currently visible in the list, after applying the scroll event.
  81    pub visible_range: Range<usize>,
  82
  83    /// The number of items that are currently visible in the list, after applying the scroll event.
  84    pub count: usize,
  85
  86    /// Whether the list has been scrolled.
  87    pub is_scrolled: bool,
  88}
  89
  90/// The sizing behavior to apply during layout.
  91#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
  92pub enum ListSizingBehavior {
  93    /// The list should calculate its size based on the size of its items.
  94    Infer,
  95    /// The list should not calculate a fixed size.
  96    #[default]
  97    Auto,
  98}
  99
 100/// The horizontal sizing behavior to apply during layout.
 101#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
 102pub enum ListHorizontalSizingBehavior {
 103    /// List items' width can never exceed the width of the list.
 104    #[default]
 105    FitList,
 106    /// List items' width may go over the width of the list, if any item is wider.
 107    Unconstrained,
 108}
 109
 110struct LayoutItemsResponse {
 111    max_item_width: Pixels,
 112    scroll_top: ListOffset,
 113    item_layouts: VecDeque<ItemLayout>,
 114}
 115
 116struct ItemLayout {
 117    index: usize,
 118    element: AnyElement,
 119    size: Size<Pixels>,
 120}
 121
 122/// Frame state used by the [List] element after layout.
 123pub struct ListPrepaintState {
 124    hitbox: Hitbox,
 125    layout: LayoutItemsResponse,
 126}
 127
 128#[derive(Clone)]
 129enum ListItem {
 130    Unmeasured {
 131        focus_handle: Option<FocusHandle>,
 132    },
 133    Measured {
 134        size: Size<Pixels>,
 135        focus_handle: Option<FocusHandle>,
 136    },
 137}
 138
 139impl ListItem {
 140    fn size(&self) -> Option<Size<Pixels>> {
 141        if let ListItem::Measured { size, .. } = self {
 142            Some(*size)
 143        } else {
 144            None
 145        }
 146    }
 147
 148    fn focus_handle(&self) -> Option<FocusHandle> {
 149        match self {
 150            ListItem::Unmeasured { focus_handle } | ListItem::Measured { focus_handle, .. } => {
 151                focus_handle.clone()
 152            }
 153        }
 154    }
 155
 156    fn contains_focused(&self, window: &Window, cx: &App) -> bool {
 157        match self {
 158            ListItem::Unmeasured { focus_handle } | ListItem::Measured { focus_handle, .. } => {
 159                focus_handle
 160                    .as_ref()
 161                    .is_some_and(|handle| handle.contains_focused(window, cx))
 162            }
 163        }
 164    }
 165}
 166
 167#[derive(Clone, Debug, Default, PartialEq)]
 168struct ListItemSummary {
 169    count: usize,
 170    rendered_count: usize,
 171    unrendered_count: usize,
 172    height: Pixels,
 173    has_focus_handles: bool,
 174}
 175
 176#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
 177struct Count(usize);
 178
 179#[derive(Clone, Debug, Default)]
 180struct Height(Pixels);
 181
 182impl ListState {
 183    /// Construct a new list state, for storage on a view.
 184    ///
 185    /// The overdraw parameter controls how much extra space is rendered
 186    /// above and below the visible area. Elements within this area will
 187    /// be measured even though they are not visible. This can help ensure
 188    /// that the list doesn't flicker or pop in when scrolling.
 189    pub fn new<R>(
 190        item_count: usize,
 191        alignment: ListAlignment,
 192        overdraw: Pixels,
 193        render_item: R,
 194    ) -> Self
 195    where
 196        R: 'static + FnMut(usize, &mut Window, &mut App) -> AnyElement,
 197    {
 198        let this = Self(Rc::new(RefCell::new(StateInner {
 199            last_layout_bounds: None,
 200            last_padding: None,
 201            render_item: Box::new(render_item),
 202            items: SumTree::default(),
 203            logical_scroll_top: None,
 204            alignment,
 205            overdraw,
 206            scroll_handler: None,
 207            reset: false,
 208            scrollbar_drag_start_height: None,
 209        })));
 210        this.splice(0..0, item_count);
 211        this
 212    }
 213
 214    /// Reset this instantiation of the list state.
 215    ///
 216    /// Note that this will cause scroll events to be dropped until the next paint.
 217    pub fn reset(&self, element_count: usize) {
 218        let old_count = {
 219            let state = &mut *self.0.borrow_mut();
 220            state.reset = true;
 221            state.logical_scroll_top = None;
 222            state.scrollbar_drag_start_height = None;
 223            state.items.summary().count
 224        };
 225
 226        self.splice(0..old_count, element_count);
 227    }
 228
 229    /// The number of items in this list.
 230    pub fn item_count(&self) -> usize {
 231        self.0.borrow().items.summary().count
 232    }
 233
 234    /// Inform the list state that the items in `old_range` have been replaced
 235    /// by `count` new items that must be recalculated.
 236    pub fn splice(&self, old_range: Range<usize>, count: usize) {
 237        self.splice_focusable(old_range, (0..count).map(|_| None))
 238    }
 239
 240    /// Register with the list state that the items in `old_range` have been replaced
 241    /// by new items. As opposed to [`splice`], this method allows an iterator of optional focus handles
 242    /// to be supplied to properly integrate with items in the list that can be focused. If a focused item
 243    /// is scrolled out of view, the list will continue to render it to allow keyboard interaction.
 244    pub fn splice_focusable(
 245        &self,
 246        old_range: Range<usize>,
 247        focus_handles: impl IntoIterator<Item = Option<FocusHandle>>,
 248    ) {
 249        let state = &mut *self.0.borrow_mut();
 250
 251        let mut old_items = state.items.cursor::<Count>(&());
 252        let mut new_items = old_items.slice(&Count(old_range.start), Bias::Right, &());
 253        old_items.seek_forward(&Count(old_range.end), Bias::Right, &());
 254
 255        let mut spliced_count = 0;
 256        new_items.extend(
 257            focus_handles.into_iter().map(|focus_handle| {
 258                spliced_count += 1;
 259                ListItem::Unmeasured { focus_handle }
 260            }),
 261            &(),
 262        );
 263        new_items.append(old_items.suffix(&()), &());
 264        drop(old_items);
 265        state.items = new_items;
 266
 267        if let Some(ListOffset {
 268            item_ix,
 269            offset_in_item,
 270        }) = state.logical_scroll_top.as_mut()
 271        {
 272            if old_range.contains(item_ix) {
 273                *item_ix = old_range.start;
 274                *offset_in_item = px(0.);
 275            } else if old_range.end <= *item_ix {
 276                *item_ix = *item_ix - (old_range.end - old_range.start) + spliced_count;
 277            }
 278        }
 279    }
 280
 281    /// Set a handler that will be called when the list is scrolled.
 282    pub fn set_scroll_handler(
 283        &self,
 284        handler: impl FnMut(&ListScrollEvent, &mut Window, &mut App) + 'static,
 285    ) {
 286        self.0.borrow_mut().scroll_handler = Some(Box::new(handler))
 287    }
 288
 289    /// Get the current scroll offset, in terms of the list's items.
 290    pub fn logical_scroll_top(&self) -> ListOffset {
 291        self.0.borrow().logical_scroll_top()
 292    }
 293
 294    /// Scroll the list to the given offset
 295    pub fn scroll_to(&self, mut scroll_top: ListOffset) {
 296        let state = &mut *self.0.borrow_mut();
 297        let item_count = state.items.summary().count;
 298        if scroll_top.item_ix >= item_count {
 299            scroll_top.item_ix = item_count;
 300            scroll_top.offset_in_item = px(0.);
 301        }
 302
 303        state.logical_scroll_top = Some(scroll_top);
 304    }
 305
 306    /// Scroll the list to the given item, such that the item is fully visible.
 307    pub fn scroll_to_reveal_item(&self, ix: usize) {
 308        let state = &mut *self.0.borrow_mut();
 309
 310        let mut scroll_top = state.logical_scroll_top();
 311        let height = state
 312            .last_layout_bounds
 313            .map_or(px(0.), |bounds| bounds.size.height);
 314        let padding = state.last_padding.unwrap_or_default();
 315
 316        if ix <= scroll_top.item_ix {
 317            scroll_top.item_ix = ix;
 318            scroll_top.offset_in_item = px(0.);
 319        } else {
 320            let mut cursor = state.items.cursor::<ListItemSummary>(&());
 321            cursor.seek(&Count(ix + 1), Bias::Right, &());
 322            let bottom = cursor.start().height + padding.top;
 323            let goal_top = px(0.).max(bottom - height + padding.bottom);
 324
 325            cursor.seek(&Height(goal_top), Bias::Left, &());
 326            let start_ix = cursor.start().count;
 327            let start_item_top = cursor.start().height;
 328
 329            if start_ix >= scroll_top.item_ix {
 330                scroll_top.item_ix = start_ix;
 331                scroll_top.offset_in_item = goal_top - start_item_top;
 332            }
 333        }
 334
 335        state.logical_scroll_top = Some(scroll_top);
 336    }
 337
 338    /// Get the bounds for the given item in window coordinates, if it's
 339    /// been rendered.
 340    pub fn bounds_for_item(&self, ix: usize) -> Option<Bounds<Pixels>> {
 341        let state = &*self.0.borrow();
 342
 343        let bounds = state.last_layout_bounds.unwrap_or_default();
 344        let scroll_top = state.logical_scroll_top();
 345        if ix < scroll_top.item_ix {
 346            return None;
 347        }
 348
 349        let mut cursor = state.items.cursor::<(Count, Height)>(&());
 350        cursor.seek(&Count(scroll_top.item_ix), Bias::Right, &());
 351
 352        let scroll_top = cursor.start().1.0 + scroll_top.offset_in_item;
 353
 354        cursor.seek_forward(&Count(ix), Bias::Right, &());
 355        if let Some(&ListItem::Measured { size, .. }) = cursor.item() {
 356            let &(Count(count), Height(top)) = cursor.start();
 357            if count == ix {
 358                let top = bounds.top() + top - scroll_top;
 359                return Some(Bounds::from_corners(
 360                    point(bounds.left(), top),
 361                    point(bounds.right(), top + size.height),
 362                ));
 363            }
 364        }
 365        None
 366    }
 367
 368    /// Call this method when the user starts dragging the scrollbar.
 369    ///
 370    /// This will prevent the height reported to the scrollbar from changing during the drag
 371    /// as items in the overdraw get measured, and help offset scroll position changes accordingly.
 372    pub fn scrollbar_drag_started(&self) {
 373        let mut state = self.0.borrow_mut();
 374        state.scrollbar_drag_start_height = Some(state.items.summary().height);
 375    }
 376
 377    /// Called when the user stops dragging the scrollbar.
 378    ///
 379    /// See `scrollbar_drag_started`.
 380    pub fn scrollbar_drag_ended(&self) {
 381        self.0.borrow_mut().scrollbar_drag_start_height.take();
 382    }
 383
 384    /// Set the offset from the scrollbar
 385    pub fn set_offset_from_scrollbar(&self, point: Point<Pixels>) {
 386        self.0.borrow_mut().set_offset_from_scrollbar(point);
 387    }
 388
 389    /// Returns the size of items we have measured.
 390    /// This value remains constant while dragging to prevent the scrollbar from moving away unexpectedly.
 391    pub fn content_size_for_scrollbar(&self) -> Size<Pixels> {
 392        let state = self.0.borrow();
 393        let bounds = state.last_layout_bounds.unwrap_or_default();
 394
 395        let height = state
 396            .scrollbar_drag_start_height
 397            .unwrap_or_else(|| state.items.summary().height);
 398
 399        Size::new(bounds.size.width, height)
 400    }
 401
 402    /// Returns the current scroll offset adjusted for the scrollbar
 403    pub fn scroll_px_offset_for_scrollbar(&self) -> Point<Pixels> {
 404        let state = &self.0.borrow();
 405        let logical_scroll_top = state.logical_scroll_top();
 406
 407        let mut cursor = state.items.cursor::<ListItemSummary>(&());
 408        let summary: ListItemSummary =
 409            cursor.summary(&Count(logical_scroll_top.item_ix), Bias::Right, &());
 410        let content_height = state.items.summary().height;
 411        let drag_offset =
 412            // if dragging the scrollbar, we want to offset the point if the height changed
 413            content_height - state.scrollbar_drag_start_height.unwrap_or(content_height);
 414        let offset = summary.height + logical_scroll_top.offset_in_item - drag_offset;
 415
 416        Point::new(px(0.), -offset)
 417    }
 418
 419    /// Return the bounds of the viewport in pixels.
 420    pub fn viewport_bounds(&self) -> Bounds<Pixels> {
 421        self.0.borrow().last_layout_bounds.unwrap_or_default()
 422    }
 423}
 424
 425impl StateInner {
 426    fn visible_range(&self, height: Pixels, scroll_top: &ListOffset) -> Range<usize> {
 427        let mut cursor = self.items.cursor::<ListItemSummary>(&());
 428        cursor.seek(&Count(scroll_top.item_ix), Bias::Right, &());
 429        let start_y = cursor.start().height + scroll_top.offset_in_item;
 430        cursor.seek_forward(&Height(start_y + height), Bias::Left, &());
 431        scroll_top.item_ix..cursor.start().count + 1
 432    }
 433
 434    fn scroll(
 435        &mut self,
 436        scroll_top: &ListOffset,
 437        height: Pixels,
 438        delta: Point<Pixels>,
 439        current_view: EntityId,
 440        window: &mut Window,
 441        cx: &mut App,
 442    ) {
 443        // Drop scroll events after a reset, since we can't calculate
 444        // the new logical scroll top without the item heights
 445        if self.reset {
 446            return;
 447        }
 448
 449        let padding = self.last_padding.unwrap_or_default();
 450        let scroll_max =
 451            (self.items.summary().height + padding.top + padding.bottom - height).max(px(0.));
 452        let new_scroll_top = (self.scroll_top(scroll_top) - delta.y)
 453            .max(px(0.))
 454            .min(scroll_max);
 455
 456        if self.alignment == ListAlignment::Bottom && new_scroll_top == scroll_max {
 457            self.logical_scroll_top = None;
 458        } else {
 459            let mut cursor = self.items.cursor::<ListItemSummary>(&());
 460            cursor.seek(&Height(new_scroll_top), Bias::Right, &());
 461            let item_ix = cursor.start().count;
 462            let offset_in_item = new_scroll_top - cursor.start().height;
 463            self.logical_scroll_top = Some(ListOffset {
 464                item_ix,
 465                offset_in_item,
 466            });
 467        }
 468
 469        if self.scroll_handler.is_some() {
 470            let visible_range = self.visible_range(height, scroll_top);
 471            self.scroll_handler.as_mut().unwrap()(
 472                &ListScrollEvent {
 473                    visible_range,
 474                    count: self.items.summary().count,
 475                    is_scrolled: self.logical_scroll_top.is_some(),
 476                },
 477                window,
 478                cx,
 479            );
 480        }
 481
 482        cx.notify(current_view);
 483    }
 484
 485    fn logical_scroll_top(&self) -> ListOffset {
 486        self.logical_scroll_top
 487            .unwrap_or_else(|| match self.alignment {
 488                ListAlignment::Top => ListOffset {
 489                    item_ix: 0,
 490                    offset_in_item: px(0.),
 491                },
 492                ListAlignment::Bottom => ListOffset {
 493                    item_ix: self.items.summary().count,
 494                    offset_in_item: px(0.),
 495                },
 496            })
 497    }
 498
 499    fn scroll_top(&self, logical_scroll_top: &ListOffset) -> Pixels {
 500        let mut cursor = self.items.cursor::<ListItemSummary>(&());
 501        cursor.seek(&Count(logical_scroll_top.item_ix), Bias::Right, &());
 502        cursor.start().height + logical_scroll_top.offset_in_item
 503    }
 504
 505    fn layout_items(
 506        &mut self,
 507        available_width: Option<Pixels>,
 508        available_height: Pixels,
 509        padding: &Edges<Pixels>,
 510        window: &mut Window,
 511        cx: &mut App,
 512    ) -> LayoutItemsResponse {
 513        let old_items = self.items.clone();
 514        let mut measured_items = VecDeque::new();
 515        let mut item_layouts = VecDeque::new();
 516        let mut rendered_height = padding.top;
 517        let mut max_item_width = px(0.);
 518        let mut scroll_top = self.logical_scroll_top();
 519        let mut rendered_focused_item = false;
 520
 521        let available_item_space = size(
 522            available_width.map_or(AvailableSpace::MinContent, |width| {
 523                AvailableSpace::Definite(width)
 524            }),
 525            AvailableSpace::MinContent,
 526        );
 527
 528        let mut cursor = old_items.cursor::<Count>(&());
 529
 530        // Render items after the scroll top, including those in the trailing overdraw
 531        cursor.seek(&Count(scroll_top.item_ix), Bias::Right, &());
 532        for (ix, item) in cursor.by_ref().enumerate() {
 533            let visible_height = rendered_height - scroll_top.offset_in_item;
 534            if visible_height >= available_height + self.overdraw {
 535                break;
 536            }
 537
 538            // Use the previously cached height and focus handle if available
 539            let mut size = item.size();
 540
 541            // If we're within the visible area or the height wasn't cached, render and measure the item's element
 542            if visible_height < available_height || size.is_none() {
 543                let item_index = scroll_top.item_ix + ix;
 544                let mut element = (self.render_item)(item_index, window, cx);
 545                let element_size = element.layout_as_root(available_item_space, window, cx);
 546                size = Some(element_size);
 547                if visible_height < available_height {
 548                    item_layouts.push_back(ItemLayout {
 549                        index: item_index,
 550                        element,
 551                        size: element_size,
 552                    });
 553                    if item.contains_focused(window, cx) {
 554                        rendered_focused_item = true;
 555                    }
 556                }
 557            }
 558
 559            let size = size.unwrap();
 560            rendered_height += size.height;
 561            max_item_width = max_item_width.max(size.width);
 562            measured_items.push_back(ListItem::Measured {
 563                size,
 564                focus_handle: item.focus_handle(),
 565            });
 566        }
 567        rendered_height += padding.bottom;
 568
 569        // Prepare to start walking upward from the item at the scroll top.
 570        cursor.seek(&Count(scroll_top.item_ix), Bias::Right, &());
 571
 572        // If the rendered items do not fill the visible region, then adjust
 573        // the scroll top upward.
 574        if rendered_height - scroll_top.offset_in_item < available_height {
 575            while rendered_height < available_height {
 576                cursor.prev(&());
 577                if let Some(item) = cursor.item() {
 578                    let item_index = cursor.start().0;
 579                    let mut element = (self.render_item)(item_index, window, cx);
 580                    let element_size = element.layout_as_root(available_item_space, window, cx);
 581                    let focus_handle = item.focus_handle();
 582                    rendered_height += element_size.height;
 583                    measured_items.push_front(ListItem::Measured {
 584                        size: element_size,
 585                        focus_handle,
 586                    });
 587                    item_layouts.push_front(ItemLayout {
 588                        index: item_index,
 589                        element,
 590                        size: element_size,
 591                    });
 592                    if item.contains_focused(window, cx) {
 593                        rendered_focused_item = true;
 594                    }
 595                } else {
 596                    break;
 597                }
 598            }
 599
 600            scroll_top = ListOffset {
 601                item_ix: cursor.start().0,
 602                offset_in_item: rendered_height - available_height,
 603            };
 604
 605            match self.alignment {
 606                ListAlignment::Top => {
 607                    scroll_top.offset_in_item = scroll_top.offset_in_item.max(px(0.));
 608                    self.logical_scroll_top = Some(scroll_top);
 609                }
 610                ListAlignment::Bottom => {
 611                    scroll_top = ListOffset {
 612                        item_ix: cursor.start().0,
 613                        offset_in_item: rendered_height - available_height,
 614                    };
 615                    self.logical_scroll_top = None;
 616                }
 617            };
 618        }
 619
 620        // Measure items in the leading overdraw
 621        let mut leading_overdraw = scroll_top.offset_in_item;
 622        while leading_overdraw < self.overdraw {
 623            cursor.prev(&());
 624            if let Some(item) = cursor.item() {
 625                let size = if let ListItem::Measured { size, .. } = item {
 626                    *size
 627                } else {
 628                    let mut element = (self.render_item)(cursor.start().0, window, cx);
 629                    element.layout_as_root(available_item_space, window, cx)
 630                };
 631
 632                leading_overdraw += size.height;
 633                measured_items.push_front(ListItem::Measured {
 634                    size,
 635                    focus_handle: item.focus_handle(),
 636                });
 637            } else {
 638                break;
 639            }
 640        }
 641
 642        let measured_range = cursor.start().0..(cursor.start().0 + measured_items.len());
 643        let mut cursor = old_items.cursor::<Count>(&());
 644        let mut new_items = cursor.slice(&Count(measured_range.start), Bias::Right, &());
 645        new_items.extend(measured_items, &());
 646        cursor.seek(&Count(measured_range.end), Bias::Right, &());
 647        new_items.append(cursor.suffix(&()), &());
 648        self.items = new_items;
 649
 650        // If none of the visible items are focused, check if an off-screen item is focused
 651        // and include it to be rendered after the visible items so keyboard interaction continues
 652        // to work for it.
 653        if !rendered_focused_item {
 654            let mut cursor = self
 655                .items
 656                .filter::<_, Count>(&(), |summary| summary.has_focus_handles);
 657            cursor.next(&());
 658            while let Some(item) = cursor.item() {
 659                if item.contains_focused(window, cx) {
 660                    let item_index = cursor.start().0;
 661                    let mut element = (self.render_item)(cursor.start().0, window, cx);
 662                    let size = element.layout_as_root(available_item_space, window, cx);
 663                    item_layouts.push_back(ItemLayout {
 664                        index: item_index,
 665                        element,
 666                        size,
 667                    });
 668                    break;
 669                }
 670                cursor.next(&());
 671            }
 672        }
 673
 674        LayoutItemsResponse {
 675            max_item_width,
 676            scroll_top,
 677            item_layouts,
 678        }
 679    }
 680
 681    fn prepaint_items(
 682        &mut self,
 683        bounds: Bounds<Pixels>,
 684        padding: Edges<Pixels>,
 685        autoscroll: bool,
 686        window: &mut Window,
 687        cx: &mut App,
 688    ) -> Result<LayoutItemsResponse, ListOffset> {
 689        window.transact(|window| {
 690            let mut layout_response = self.layout_items(
 691                Some(bounds.size.width),
 692                bounds.size.height,
 693                &padding,
 694                window,
 695                cx,
 696            );
 697
 698            // Avoid honoring autoscroll requests from elements other than our children.
 699            window.take_autoscroll();
 700
 701            // Only paint the visible items, if there is actually any space for them (taking padding into account)
 702            if bounds.size.height > padding.top + padding.bottom {
 703                let mut item_origin = bounds.origin + Point::new(px(0.), padding.top);
 704                item_origin.y -= layout_response.scroll_top.offset_in_item;
 705                for item in &mut layout_response.item_layouts {
 706                    window.with_content_mask(Some(ContentMask { bounds }), |window| {
 707                        item.element.prepaint_at(item_origin, window, cx);
 708                    });
 709
 710                    if let Some(autoscroll_bounds) = window.take_autoscroll() {
 711                        if autoscroll {
 712                            if autoscroll_bounds.top() < bounds.top() {
 713                                return Err(ListOffset {
 714                                    item_ix: item.index,
 715                                    offset_in_item: autoscroll_bounds.top() - item_origin.y,
 716                                });
 717                            } else if autoscroll_bounds.bottom() > bounds.bottom() {
 718                                let mut cursor = self.items.cursor::<Count>(&());
 719                                cursor.seek(&Count(item.index), Bias::Right, &());
 720                                let mut height = bounds.size.height - padding.top - padding.bottom;
 721
 722                                // Account for the height of the element down until the autoscroll bottom.
 723                                height -= autoscroll_bounds.bottom() - item_origin.y;
 724
 725                                // Keep decreasing the scroll top until we fill all the available space.
 726                                while height > Pixels::ZERO {
 727                                    cursor.prev(&());
 728                                    let Some(item) = cursor.item() else { break };
 729
 730                                    let size = item.size().unwrap_or_else(|| {
 731                                        let mut item =
 732                                            (self.render_item)(cursor.start().0, window, cx);
 733                                        let item_available_size = size(
 734                                            bounds.size.width.into(),
 735                                            AvailableSpace::MinContent,
 736                                        );
 737                                        item.layout_as_root(item_available_size, window, cx)
 738                                    });
 739                                    height -= size.height;
 740                                }
 741
 742                                return Err(ListOffset {
 743                                    item_ix: cursor.start().0,
 744                                    offset_in_item: if height < Pixels::ZERO {
 745                                        -height
 746                                    } else {
 747                                        Pixels::ZERO
 748                                    },
 749                                });
 750                            }
 751                        }
 752                    }
 753
 754                    item_origin.y += item.size.height;
 755                }
 756            } else {
 757                layout_response.item_layouts.clear();
 758            }
 759
 760            Ok(layout_response)
 761        })
 762    }
 763
 764    // Scrollbar support
 765
 766    fn set_offset_from_scrollbar(&mut self, point: Point<Pixels>) {
 767        let Some(bounds) = self.last_layout_bounds else {
 768            return;
 769        };
 770        let height = bounds.size.height;
 771
 772        let padding = self.last_padding.unwrap_or_default();
 773        let content_height = self.items.summary().height;
 774        let scroll_max = (content_height + padding.top + padding.bottom - height).max(px(0.));
 775        let drag_offset =
 776            // if dragging the scrollbar, we want to offset the point if the height changed
 777            content_height - self.scrollbar_drag_start_height.unwrap_or(content_height);
 778        let new_scroll_top = (point.y - drag_offset).abs().max(px(0.)).min(scroll_max);
 779
 780        if self.alignment == ListAlignment::Bottom && new_scroll_top == scroll_max {
 781            self.logical_scroll_top = None;
 782        } else {
 783            let mut cursor = self.items.cursor::<ListItemSummary>(&());
 784            cursor.seek(&Height(new_scroll_top), Bias::Right, &());
 785
 786            let item_ix = cursor.start().count;
 787            let offset_in_item = new_scroll_top - cursor.start().height;
 788            self.logical_scroll_top = Some(ListOffset {
 789                item_ix,
 790                offset_in_item,
 791            });
 792        }
 793    }
 794}
 795
 796impl std::fmt::Debug for ListItem {
 797    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 798        match self {
 799            Self::Unmeasured { .. } => write!(f, "Unrendered"),
 800            Self::Measured { size, .. } => f.debug_struct("Rendered").field("size", size).finish(),
 801        }
 802    }
 803}
 804
 805/// An offset into the list's items, in terms of the item index and the number
 806/// of pixels off the top left of the item.
 807#[derive(Debug, Clone, Copy, Default)]
 808pub struct ListOffset {
 809    /// The index of an item in the list
 810    pub item_ix: usize,
 811    /// The number of pixels to offset from the item index.
 812    pub offset_in_item: Pixels,
 813}
 814
 815impl Element for List {
 816    type RequestLayoutState = ();
 817    type PrepaintState = ListPrepaintState;
 818
 819    fn id(&self) -> Option<crate::ElementId> {
 820        None
 821    }
 822
 823    fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
 824        None
 825    }
 826
 827    fn request_layout(
 828        &mut self,
 829        _id: Option<&GlobalElementId>,
 830        _inspector_id: Option<&InspectorElementId>,
 831        window: &mut Window,
 832        cx: &mut App,
 833    ) -> (crate::LayoutId, Self::RequestLayoutState) {
 834        let layout_id = match self.sizing_behavior {
 835            ListSizingBehavior::Infer => {
 836                let mut style = Style::default();
 837                style.overflow.y = Overflow::Scroll;
 838                style.refine(&self.style);
 839                window.with_text_style(style.text_style().cloned(), |window| {
 840                    let state = &mut *self.state.0.borrow_mut();
 841
 842                    let available_height = if let Some(last_bounds) = state.last_layout_bounds {
 843                        last_bounds.size.height
 844                    } else {
 845                        // If we don't have the last layout bounds (first render),
 846                        // we might just use the overdraw value as the available height to layout enough items.
 847                        state.overdraw
 848                    };
 849                    let padding = style.padding.to_pixels(
 850                        state.last_layout_bounds.unwrap_or_default().size.into(),
 851                        window.rem_size(),
 852                    );
 853
 854                    let layout_response =
 855                        state.layout_items(None, available_height, &padding, window, cx);
 856                    let max_element_width = layout_response.max_item_width;
 857
 858                    let summary = state.items.summary();
 859                    let total_height = summary.height;
 860
 861                    window.request_measured_layout(
 862                        style,
 863                        move |known_dimensions, available_space, _window, _cx| {
 864                            let width =
 865                                known_dimensions
 866                                    .width
 867                                    .unwrap_or(match available_space.width {
 868                                        AvailableSpace::Definite(x) => x,
 869                                        AvailableSpace::MinContent | AvailableSpace::MaxContent => {
 870                                            max_element_width
 871                                        }
 872                                    });
 873                            let height = match available_space.height {
 874                                AvailableSpace::Definite(height) => total_height.min(height),
 875                                AvailableSpace::MinContent | AvailableSpace::MaxContent => {
 876                                    total_height
 877                                }
 878                            };
 879                            size(width, height)
 880                        },
 881                    )
 882                })
 883            }
 884            ListSizingBehavior::Auto => {
 885                let mut style = Style::default();
 886                style.refine(&self.style);
 887                window.with_text_style(style.text_style().cloned(), |window| {
 888                    window.request_layout(style, None, cx)
 889                })
 890            }
 891        };
 892        (layout_id, ())
 893    }
 894
 895    fn prepaint(
 896        &mut self,
 897        _id: Option<&GlobalElementId>,
 898        _inspector_id: Option<&InspectorElementId>,
 899        bounds: Bounds<Pixels>,
 900        _: &mut Self::RequestLayoutState,
 901        window: &mut Window,
 902        cx: &mut App,
 903    ) -> ListPrepaintState {
 904        let state = &mut *self.state.0.borrow_mut();
 905        state.reset = false;
 906
 907        let mut style = Style::default();
 908        style.refine(&self.style);
 909
 910        let hitbox = window.insert_hitbox(bounds, HitboxBehavior::Normal);
 911
 912        // If the width of the list has changed, invalidate all cached item heights
 913        if state.last_layout_bounds.map_or(true, |last_bounds| {
 914            last_bounds.size.width != bounds.size.width
 915        }) {
 916            let new_items = SumTree::from_iter(
 917                state.items.iter().map(|item| ListItem::Unmeasured {
 918                    focus_handle: item.focus_handle(),
 919                }),
 920                &(),
 921            );
 922
 923            state.items = new_items;
 924        }
 925
 926        let padding = style
 927            .padding
 928            .to_pixels(bounds.size.into(), window.rem_size());
 929        let layout = match state.prepaint_items(bounds, padding, true, window, cx) {
 930            Ok(layout) => layout,
 931            Err(autoscroll_request) => {
 932                state.logical_scroll_top = Some(autoscroll_request);
 933                state
 934                    .prepaint_items(bounds, padding, false, window, cx)
 935                    .unwrap()
 936            }
 937        };
 938
 939        state.last_layout_bounds = Some(bounds);
 940        state.last_padding = Some(padding);
 941        ListPrepaintState { hitbox, layout }
 942    }
 943
 944    fn paint(
 945        &mut self,
 946        _id: Option<&GlobalElementId>,
 947        _inspector_id: Option<&InspectorElementId>,
 948        bounds: Bounds<crate::Pixels>,
 949        _: &mut Self::RequestLayoutState,
 950        prepaint: &mut Self::PrepaintState,
 951        window: &mut Window,
 952        cx: &mut App,
 953    ) {
 954        let current_view = window.current_view();
 955        window.with_content_mask(Some(ContentMask { bounds }), |window| {
 956            for item in &mut prepaint.layout.item_layouts {
 957                item.element.paint(window, cx);
 958            }
 959        });
 960
 961        let list_state = self.state.clone();
 962        let height = bounds.size.height;
 963        let scroll_top = prepaint.layout.scroll_top;
 964        let hitbox_id = prepaint.hitbox.id;
 965        window.on_mouse_event(move |event: &ScrollWheelEvent, phase, window, cx| {
 966            if phase == DispatchPhase::Bubble && hitbox_id.should_handle_scroll(window) {
 967                list_state.0.borrow_mut().scroll(
 968                    &scroll_top,
 969                    height,
 970                    event.delta.pixel_delta(px(20.)),
 971                    current_view,
 972                    window,
 973                    cx,
 974                )
 975            }
 976        });
 977    }
 978}
 979
 980impl IntoElement for List {
 981    type Element = Self;
 982
 983    fn into_element(self) -> Self::Element {
 984        self
 985    }
 986}
 987
 988impl Styled for List {
 989    fn style(&mut self) -> &mut StyleRefinement {
 990        &mut self.style
 991    }
 992}
 993
 994impl sum_tree::Item for ListItem {
 995    type Summary = ListItemSummary;
 996
 997    fn summary(&self, _: &()) -> Self::Summary {
 998        match self {
 999            ListItem::Unmeasured { focus_handle } => ListItemSummary {
1000                count: 1,
1001                rendered_count: 0,
1002                unrendered_count: 1,
1003                height: px(0.),
1004                has_focus_handles: focus_handle.is_some(),
1005            },
1006            ListItem::Measured {
1007                size, focus_handle, ..
1008            } => ListItemSummary {
1009                count: 1,
1010                rendered_count: 1,
1011                unrendered_count: 0,
1012                height: size.height,
1013                has_focus_handles: focus_handle.is_some(),
1014            },
1015        }
1016    }
1017}
1018
1019impl sum_tree::Summary for ListItemSummary {
1020    type Context = ();
1021
1022    fn zero(_cx: &()) -> Self {
1023        Default::default()
1024    }
1025
1026    fn add_summary(&mut self, summary: &Self, _: &()) {
1027        self.count += summary.count;
1028        self.rendered_count += summary.rendered_count;
1029        self.unrendered_count += summary.unrendered_count;
1030        self.height += summary.height;
1031        self.has_focus_handles |= summary.has_focus_handles;
1032    }
1033}
1034
1035impl<'a> sum_tree::Dimension<'a, ListItemSummary> for Count {
1036    fn zero(_cx: &()) -> Self {
1037        Default::default()
1038    }
1039
1040    fn add_summary(&mut self, summary: &'a ListItemSummary, _: &()) {
1041        self.0 += summary.count;
1042    }
1043}
1044
1045impl<'a> sum_tree::Dimension<'a, ListItemSummary> for Height {
1046    fn zero(_cx: &()) -> Self {
1047        Default::default()
1048    }
1049
1050    fn add_summary(&mut self, summary: &'a ListItemSummary, _: &()) {
1051        self.0 += summary.height;
1052    }
1053}
1054
1055impl sum_tree::SeekTarget<'_, ListItemSummary, ListItemSummary> for Count {
1056    fn cmp(&self, other: &ListItemSummary, _: &()) -> std::cmp::Ordering {
1057        self.0.partial_cmp(&other.count).unwrap()
1058    }
1059}
1060
1061impl sum_tree::SeekTarget<'_, ListItemSummary, ListItemSummary> for Height {
1062    fn cmp(&self, other: &ListItemSummary, _: &()) -> std::cmp::Ordering {
1063        self.0.partial_cmp(&other.height).unwrap()
1064    }
1065}
1066
1067#[cfg(test)]
1068mod test {
1069
1070    use gpui::{ScrollDelta, ScrollWheelEvent};
1071
1072    use crate::{self as gpui, TestAppContext};
1073
1074    #[gpui::test]
1075    fn test_reset_after_paint_before_scroll(cx: &mut TestAppContext) {
1076        use crate::{
1077            AppContext, Context, Element, IntoElement, ListState, Render, Styled, Window, div,
1078            list, point, px, size,
1079        };
1080
1081        let cx = cx.add_empty_window();
1082
1083        let state = ListState::new(5, crate::ListAlignment::Top, px(10.), |_, _, _| {
1084            div().h(px(10.)).w_full().into_any()
1085        });
1086
1087        // Ensure that the list is scrolled to the top
1088        state.scroll_to(gpui::ListOffset {
1089            item_ix: 0,
1090            offset_in_item: px(0.0),
1091        });
1092
1093        struct TestView(ListState);
1094        impl Render for TestView {
1095            fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
1096                list(self.0.clone()).w_full().h_full()
1097            }
1098        }
1099
1100        // Paint
1101        cx.draw(point(px(0.), px(0.)), size(px(100.), px(20.)), |_, cx| {
1102            cx.new(|_| TestView(state.clone()))
1103        });
1104
1105        // Reset
1106        state.reset(5);
1107
1108        // And then receive a scroll event _before_ the next paint
1109        cx.simulate_event(ScrollWheelEvent {
1110            position: point(px(1.), px(1.)),
1111            delta: ScrollDelta::Pixels(point(px(0.), px(-500.))),
1112            ..Default::default()
1113        });
1114
1115        // Scroll position should stay at the top of the list
1116        assert_eq!(state.logical_scroll_top().item_ix, 0);
1117        assert_eq!(state.logical_scroll_top().offset_in_item, px(0.));
1118    }
1119}