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

@ -541,12 +541,12 @@ impl TerminalElement {
}
impl Element for TerminalElement {
type BeforeLayout = ();
type AfterLayout = LayoutState;
type RequestLayoutState = ();
type PrepaintState = LayoutState;
fn before_layout(&mut self, cx: &mut ElementContext) -> (LayoutId, Self::BeforeLayout) {
fn request_layout(&mut self, cx: &mut ElementContext) -> (LayoutId, Self::RequestLayoutState) {
self.interactivity.occlude_mouse();
let layout_id = self.interactivity.before_layout(cx, |mut style, cx| {
let layout_id = self.interactivity.request_layout(cx, |mut style, cx| {
style.size.width = relative(1.).into();
style.size.height = relative(1.).into();
let layout_id = cx.request_layout(&style, None);
@ -556,14 +556,14 @@ impl Element for TerminalElement {
(layout_id, ())
}
fn after_layout(
fn prepaint(
&mut self,
bounds: Bounds<Pixels>,
_: &mut Self::BeforeLayout,
_: &mut Self::RequestLayoutState,
cx: &mut ElementContext,
) -> Self::AfterLayout {
) -> Self::PrepaintState {
self.interactivity
.after_layout(bounds, bounds.size, cx, |_, _, hitbox, cx| {
.prepaint(bounds, bounds.size, cx, |_, _, hitbox, cx| {
let hitbox = hitbox.unwrap();
let settings = ThemeSettings::get_global(cx).clone();
@ -669,7 +669,7 @@ impl Element for TerminalElement {
.id("terminal-element")
.tooltip(move |cx| Tooltip::text(hovered_word.word.clone(), cx))
.into_any_element();
element.layout(offset, bounds.size.into(), cx);
element.prepaint_as_root(offset, bounds.size.into(), cx);
element
});
@ -775,8 +775,8 @@ impl Element for TerminalElement {
fn paint(
&mut self,
bounds: Bounds<Pixels>,
_: &mut Self::BeforeLayout,
layout: &mut Self::AfterLayout,
_: &mut Self::RequestLayoutState,
layout: &mut Self::PrepaintState,
cx: &mut ElementContext<'_>,
) {
cx.paint_quad(fill(bounds, layout.background_color));