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
+27 -27
View File
@@ -17,19 +17,19 @@ use std::{
use util::ResultExt;
impl Element for &'static str {
type BeforeLayout = TextState;
type AfterLayout = ();
type RequestLayoutState = TextState;
type PrepaintState = ();
fn before_layout(&mut self, cx: &mut ElementContext) -> (LayoutId, Self::BeforeLayout) {
fn request_layout(&mut self, cx: &mut ElementContext) -> (LayoutId, Self::RequestLayoutState) {
let mut state = TextState::default();
let layout_id = state.layout(SharedString::from(*self), None, cx);
(layout_id, state)
}
fn after_layout(
fn prepaint(
&mut self,
_bounds: Bounds<Pixels>,
_text_state: &mut Self::BeforeLayout,
_text_state: &mut Self::RequestLayoutState,
_cx: &mut ElementContext,
) {
}
@@ -62,19 +62,19 @@ impl IntoElement for String {
}
impl Element for SharedString {
type BeforeLayout = TextState;
type AfterLayout = ();
type RequestLayoutState = TextState;
type PrepaintState = ();
fn before_layout(&mut self, cx: &mut ElementContext) -> (LayoutId, Self::BeforeLayout) {
fn request_layout(&mut self, cx: &mut ElementContext) -> (LayoutId, Self::RequestLayoutState) {
let mut state = TextState::default();
let layout_id = state.layout(self.clone(), None, cx);
(layout_id, state)
}
fn after_layout(
fn prepaint(
&mut self,
_bounds: Bounds<Pixels>,
_text_state: &mut Self::BeforeLayout,
_text_state: &mut Self::RequestLayoutState,
_cx: &mut ElementContext,
) {
}
@@ -82,8 +82,8 @@ impl Element for SharedString {
fn paint(
&mut self,
bounds: Bounds<Pixels>,
text_state: &mut Self::BeforeLayout,
_: &mut Self::AfterLayout,
text_state: &mut Self::RequestLayoutState,
_: &mut Self::PrepaintState,
cx: &mut ElementContext,
) {
let text_str: &str = self.as_ref();
@@ -148,19 +148,19 @@ impl StyledText {
}
impl Element for StyledText {
type BeforeLayout = TextState;
type AfterLayout = ();
type RequestLayoutState = TextState;
type PrepaintState = ();
fn before_layout(&mut self, cx: &mut ElementContext) -> (LayoutId, Self::BeforeLayout) {
fn request_layout(&mut self, cx: &mut ElementContext) -> (LayoutId, Self::RequestLayoutState) {
let mut state = TextState::default();
let layout_id = state.layout(self.text.clone(), self.runs.take(), cx);
(layout_id, state)
}
fn after_layout(
fn prepaint(
&mut self,
_bounds: Bounds<Pixels>,
_state: &mut Self::BeforeLayout,
_state: &mut Self::RequestLayoutState,
_cx: &mut ElementContext,
) {
}
@@ -168,8 +168,8 @@ impl Element for StyledText {
fn paint(
&mut self,
bounds: Bounds<Pixels>,
text_state: &mut Self::BeforeLayout,
_: &mut Self::AfterLayout,
text_state: &mut Self::RequestLayoutState,
_: &mut Self::PrepaintState,
cx: &mut ElementContext,
) {
text_state.paint(bounds, &self.text, cx)
@@ -402,17 +402,17 @@ impl InteractiveText {
}
impl Element for InteractiveText {
type BeforeLayout = TextState;
type AfterLayout = Hitbox;
type RequestLayoutState = TextState;
type PrepaintState = Hitbox;
fn before_layout(&mut self, cx: &mut ElementContext) -> (LayoutId, Self::BeforeLayout) {
self.text.before_layout(cx)
fn request_layout(&mut self, cx: &mut ElementContext) -> (LayoutId, Self::RequestLayoutState) {
self.text.request_layout(cx)
}
fn after_layout(
fn prepaint(
&mut self,
bounds: Bounds<Pixels>,
state: &mut Self::BeforeLayout,
state: &mut Self::RequestLayoutState,
cx: &mut ElementContext,
) -> Hitbox {
cx.with_element_state::<InteractiveTextState, _>(
@@ -430,7 +430,7 @@ impl Element for InteractiveText {
}
}
self.text.after_layout(bounds, state, cx);
self.text.prepaint(bounds, state, cx);
let hitbox = cx.insert_hitbox(bounds, false);
(hitbox, interactive_state)
},
@@ -440,7 +440,7 @@ impl Element for InteractiveText {
fn paint(
&mut self,
bounds: Bounds<Pixels>,
text_state: &mut Self::BeforeLayout,
text_state: &mut Self::RequestLayoutState,
hitbox: &mut Hitbox,
cx: &mut ElementContext,
) {