Introduce autoscroll support for elements (#10889)

This pull request introduces the new
`ElementContext::request_autoscroll(bounds)` and
`ElementContext::take_autoscroll()` methods in GPUI. These new APIs
enable container elements such as `List` to change their scroll position
if one of their children requested an autoscroll. We plan to use this in
the revamped assistant.

As a drive-by, we also:

- Renamed `Element::before_layout` to `Element::request_layout`
- Renamed `Element::after_layout` to `Element::prepaint`
- Introduced a new `List::splice_focusable` method to splice focusable
elements into the list, which enables rendering offscreen elements that
are focused.

Release Notes:

- N/A

---------

Co-authored-by: Nathan <nathan@zed.dev>
This commit is contained in:
Antonio Scandurra
2024-04-23 15:14:22 +02:00
committed by GitHub
co-authored by Nathan
parent efcd31c254
commit bcbf2f2fd3
31 changed files with 780 additions and 513 deletions
+14 -13
View File
@@ -104,13 +104,13 @@ impl Styled for UniformList {
}
impl Element for UniformList {
type BeforeLayout = UniformListFrameState;
type AfterLayout = Option<Hitbox>;
type RequestLayoutState = UniformListFrameState;
type PrepaintState = Option<Hitbox>;
fn before_layout(&mut self, cx: &mut ElementContext) -> (LayoutId, Self::BeforeLayout) {
fn request_layout(&mut self, cx: &mut ElementContext) -> (LayoutId, Self::RequestLayoutState) {
let max_items = self.item_count;
let item_size = self.measure_item(None, cx);
let layout_id = self.interactivity.before_layout(cx, |style, cx| {
let layout_id = self.interactivity.request_layout(cx, |style, cx| {
cx.request_measured_layout(style, move |known_dimensions, available_space, _cx| {
let desired_height = item_size.height * max_items;
let width = known_dimensions
@@ -137,10 +137,10 @@ impl Element for UniformList {
)
}
fn after_layout(
fn prepaint(
&mut self,
bounds: Bounds<Pixels>,
before_layout: &mut Self::BeforeLayout,
frame_state: &mut Self::RequestLayoutState,
cx: &mut ElementContext,
) -> Option<Hitbox> {
let style = self.interactivity.compute_style(None, cx);
@@ -155,7 +155,7 @@ impl Element for UniformList {
let content_size = Size {
width: padded_bounds.size.width,
height: before_layout.item_size.height * self.item_count + padding.top + padding.bottom,
height: frame_state.item_size.height * self.item_count + padding.top + padding.bottom,
};
let shared_scroll_offset = self.interactivity.scroll_offset.clone().unwrap();
@@ -166,7 +166,7 @@ impl Element for UniformList {
.as_mut()
.and_then(|handle| handle.deferred_scroll_to_item.take());
self.interactivity.after_layout(
self.interactivity.prepaint(
bounds,
content_size,
cx,
@@ -222,8 +222,9 @@ impl Element for UniformList {
AvailableSpace::Definite(padded_bounds.size.width),
AvailableSpace::Definite(item_height),
);
item.layout(item_origin, available_space, cx);
before_layout.items.push(item);
item.layout_as_root(available_space, cx);
item.prepaint_at(item_origin, cx);
frame_state.items.push(item);
}
});
}
@@ -236,13 +237,13 @@ impl Element for UniformList {
fn paint(
&mut self,
bounds: Bounds<crate::Pixels>,
before_layout: &mut Self::BeforeLayout,
request_layout: &mut Self::RequestLayoutState,
hitbox: &mut Option<Hitbox>,
cx: &mut ElementContext,
) {
self.interactivity
.paint(bounds, hitbox.as_ref(), cx, |_, cx| {
for item in &mut before_layout.items {
for item in &mut request_layout.items {
item.paint(cx);
}
})
@@ -278,7 +279,7 @@ impl UniformList {
}),
AvailableSpace::MinContent,
);
item_to_measure.measure(available_space, cx)
item_to_measure.layout_as_root(available_space, cx)
}
/// Track and render scroll state of this list with reference to the given scroll handle.