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
parent efcd31c254
commit bcbf2f2fd3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
31 changed files with 780 additions and 513 deletions

View file

@ -1,8 +1,8 @@
use crate::{
seal::Sealed, AfterLayoutIndex, AnyElement, AnyModel, AnyWeakModel, AppContext, Bounds,
ContentMask, Element, ElementContext, ElementId, Entity, EntityId, Flatten, FocusHandle,
FocusableView, IntoElement, LayoutId, Model, PaintIndex, Pixels, Render, Style,
StyleRefinement, TextStyle, ViewContext, VisualContext, WeakModel,
seal::Sealed, AnyElement, AnyModel, AnyWeakModel, AppContext, Bounds, ContentMask, Element,
ElementContext, ElementId, Entity, EntityId, Flatten, FocusHandle, FocusableView, IntoElement,
LayoutId, Model, PaintIndex, Pixels, PrepaintStateIndex, Render, Style, StyleRefinement,
TextStyle, ViewContext, VisualContext, WeakModel,
};
use anyhow::{Context, Result};
use refineable::Refineable;
@ -23,7 +23,7 @@ pub struct View<V> {
impl<V> Sealed for View<V> {}
struct AnyViewState {
after_layout_range: Range<AfterLayoutIndex>,
prepaint_range: Range<PrepaintStateIndex>,
paint_range: Range<PaintIndex>,
cache_key: ViewCacheKey,
}
@ -90,34 +90,34 @@ impl<V: 'static> View<V> {
}
impl<V: Render> Element for View<V> {
type BeforeLayout = AnyElement;
type AfterLayout = ();
type RequestLayoutState = AnyElement;
type PrepaintState = ();
fn before_layout(&mut self, cx: &mut ElementContext) -> (LayoutId, Self::BeforeLayout) {
fn request_layout(&mut self, cx: &mut ElementContext) -> (LayoutId, Self::RequestLayoutState) {
cx.with_element_id(Some(ElementId::View(self.entity_id())), |cx| {
let mut element = self.update(cx, |view, cx| view.render(cx).into_any_element());
let layout_id = element.before_layout(cx);
let layout_id = element.request_layout(cx);
(layout_id, element)
})
}
fn after_layout(
fn prepaint(
&mut self,
_: Bounds<Pixels>,
element: &mut Self::BeforeLayout,
element: &mut Self::RequestLayoutState,
cx: &mut ElementContext,
) {
cx.set_view_id(self.entity_id());
cx.with_element_id(Some(ElementId::View(self.entity_id())), |cx| {
element.after_layout(cx)
element.prepaint(cx)
})
}
fn paint(
&mut self,
_: Bounds<Pixels>,
element: &mut Self::BeforeLayout,
_: &mut Self::AfterLayout,
element: &mut Self::RequestLayoutState,
_: &mut Self::PrepaintState,
cx: &mut ElementContext,
) {
cx.with_element_id(Some(ElementId::View(self.entity_id())), |cx| {
@ -276,10 +276,10 @@ impl<V: Render> From<View<V>> for AnyView {
}
impl Element for AnyView {
type BeforeLayout = Option<AnyElement>;
type AfterLayout = Option<AnyElement>;
type RequestLayoutState = Option<AnyElement>;
type PrepaintState = Option<AnyElement>;
fn before_layout(&mut self, cx: &mut ElementContext) -> (LayoutId, Self::BeforeLayout) {
fn request_layout(&mut self, cx: &mut ElementContext) -> (LayoutId, Self::RequestLayoutState) {
if let Some(style) = self.cached_style.as_ref() {
let mut root_style = Style::default();
root_style.refine(style);
@ -288,16 +288,16 @@ impl Element for AnyView {
} else {
cx.with_element_id(Some(ElementId::View(self.entity_id())), |cx| {
let mut element = (self.render)(self, cx);
let layout_id = element.before_layout(cx);
let layout_id = element.request_layout(cx);
(layout_id, Some(element))
})
}
}
fn after_layout(
fn prepaint(
&mut self,
bounds: Bounds<Pixels>,
element: &mut Self::BeforeLayout,
element: &mut Self::RequestLayoutState,
cx: &mut ElementContext,
) -> Option<AnyElement> {
cx.set_view_id(self.entity_id());
@ -317,23 +317,24 @@ impl Element for AnyView {
&& !cx.window.dirty_views.contains(&self.entity_id())
&& !cx.window.refreshing
{
let after_layout_start = cx.after_layout_index();
cx.reuse_after_layout(element_state.after_layout_range.clone());
let after_layout_end = cx.after_layout_index();
element_state.after_layout_range = after_layout_start..after_layout_end;
let prepaint_start = cx.prepaint_index();
cx.reuse_prepaint(element_state.prepaint_range.clone());
let prepaint_end = cx.prepaint_index();
element_state.prepaint_range = prepaint_start..prepaint_end;
return (None, Some(element_state));
}
}
let after_layout_start = cx.after_layout_index();
let prepaint_start = cx.prepaint_index();
let mut element = (self.render)(self, cx);
element.layout(bounds.origin, bounds.size.into(), cx);
let after_layout_end = cx.after_layout_index();
element.layout_as_root(bounds.size.into(), cx);
element.prepaint_at(bounds.origin, cx);
let prepaint_end = cx.prepaint_index();
(
Some(element),
Some(AnyViewState {
after_layout_range: after_layout_start..after_layout_end,
prepaint_range: prepaint_start..prepaint_end,
paint_range: PaintIndex::default()..PaintIndex::default(),
cache_key: ViewCacheKey {
bounds,
@ -347,7 +348,7 @@ impl Element for AnyView {
} else {
cx.with_element_id(Some(ElementId::View(self.entity_id())), |cx| {
let mut element = element.take().unwrap();
element.after_layout(cx);
element.prepaint(cx);
Some(element)
})
}
@ -356,8 +357,8 @@ impl Element for AnyView {
fn paint(
&mut self,
_bounds: Bounds<Pixels>,
_: &mut Self::BeforeLayout,
element: &mut Self::AfterLayout,
_: &mut Self::RequestLayoutState,
element: &mut Self::PrepaintState,
cx: &mut ElementContext,
) {
if self.cached_style.is_some() {