assistant2: Add scrollbar to active thread (#27534)

This required adding scrollbar support to `list`. Since `list` is
virtualized, the scrollbar height will change as more items are
measured. When the user manually drags the scrollbar, we'll persist the
initial height and offset calculations accordingly to prevent the
scrollbar from moving away from the cursor as new items are measured.

We're not doing this yet, but in the future, it'd be nice to budget some
time each frame to layout unmeasured items so that the scrollbar height
is as accurate as possible.

Release Notes:

- N/A
This commit is contained in:
Agus Zubiaga 2025-03-26 18:01:13 -03:00 committed by GitHub
parent 0a3c8a6790
commit 7b40ab30d7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 189 additions and 15 deletions

View file

@ -4,8 +4,8 @@ use crate::{prelude::*, px, relative, IntoElement};
use gpui::{
point, quad, Along, App, Axis as ScrollbarAxis, BorderStyle, Bounds, ContentMask, Corners,
Edges, Element, ElementId, Entity, EntityId, GlobalElementId, Hitbox, Hsla, LayoutId,
MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels, Point, ScrollHandle, ScrollWheelEvent,
Size, Style, UniformListScrollHandle, Window,
ListState, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels, Point, ScrollHandle,
ScrollWheelEvent, Size, Style, UniformListScrollHandle, Window,
};
pub struct Scrollbar {
@ -39,6 +39,39 @@ impl ScrollableHandle for UniformListScrollHandle {
}
}
impl ScrollableHandle for ListState {
fn content_size(&self) -> Option<ContentSize> {
Some(ContentSize {
size: self.content_size_for_scrollbar(),
scroll_adjustment: None,
})
}
fn set_offset(&self, point: Point<Pixels>) {
self.set_offset_from_scrollbar(point);
}
fn offset(&self) -> Point<Pixels> {
self.scroll_px_offset_for_scrollbar()
}
fn drag_started(&self) {
self.scrollbar_drag_started();
}
fn drag_ended(&self) {
self.scrollbar_drag_ended();
}
fn viewport(&self) -> Bounds<Pixels> {
self.viewport_bounds()
}
fn as_any(&self) -> &dyn Any {
self
}
}
impl ScrollableHandle for ScrollHandle {
fn content_size(&self) -> Option<ContentSize> {
let last_children_index = self.children_count().checked_sub(1)?;
@ -92,6 +125,8 @@ pub trait ScrollableHandle: Debug + 'static {
fn offset(&self) -> Point<Pixels>;
fn viewport(&self) -> Bounds<Pixels>;
fn as_any(&self) -> &dyn Any;
fn drag_started(&self) {}
fn drag_ended(&self) {}
}
/// A scrollbar state that should be persisted across frames.
@ -300,6 +335,8 @@ impl Element for Scrollbar {
return;
}
scroll.drag_started();
if thumb_bounds.contains(&event.position) {
let offset = event.position.along(axis) - thumb_bounds.origin.along(axis);
state.drag.set(Some(offset));
@ -349,7 +386,7 @@ impl Element for Scrollbar {
});
let state = self.state.clone();
let axis = self.kind;
window.on_mouse_event(move |event: &MouseMoveEvent, _, _, cx| {
window.on_mouse_event(move |event: &MouseMoveEvent, _, window, cx| {
if let Some(drag_state) = state.drag.get().filter(|_| event.dragging()) {
if let Some(ContentSize {
size: item_size, ..
@ -381,6 +418,7 @@ impl Element for Scrollbar {
scroll.set_offset(point(scroll.offset().x, drag_offset));
}
};
window.refresh();
if let Some(id) = state.parent_id {
cx.notify(id);
}
@ -390,9 +428,11 @@ impl Element for Scrollbar {
}
});
let state = self.state.clone();
let scroll = self.state.scroll_handle.clone();
window.on_mouse_event(move |_event: &MouseUpEvent, phase, _, cx| {
if phase.bubble() {
state.drag.take();
scroll.drag_ended();
if let Some(id) = state.parent_id {
cx.notify(id);
}