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

@ -792,13 +792,13 @@ mod element {
}
impl Element for PaneAxisElement {
type BeforeLayout = ();
type AfterLayout = PaneAxisLayout;
type RequestLayoutState = ();
type PrepaintState = PaneAxisLayout;
fn before_layout(
fn request_layout(
&mut self,
cx: &mut ui::prelude::ElementContext,
) -> (gpui::LayoutId, Self::BeforeLayout) {
) -> (gpui::LayoutId, Self::RequestLayoutState) {
let mut style = Style::default();
style.flex_grow = 1.;
style.flex_shrink = 1.;
@ -808,10 +808,10 @@ mod element {
(cx.request_layout(&style, None), ())
}
fn after_layout(
fn prepaint(
&mut self,
bounds: Bounds<Pixels>,
_state: &mut Self::BeforeLayout,
_state: &mut Self::RequestLayoutState,
cx: &mut ElementContext,
) -> PaneAxisLayout {
let dragged_handle = cx.with_element_state::<Rc<RefCell<Option<usize>>>, _>(
@ -872,7 +872,8 @@ mod element {
size: child_size,
};
bounding_boxes.push(Some(child_bounds));
child.layout(origin, child_size.into(), cx);
child.layout_as_root(child_size.into(), cx);
child.prepaint_at(origin, cx);
origin = origin.apply_along(self.axis, |val| val + child_size.along(self.axis));
layout.children.push(PaneAxisChildLayout {
@ -897,8 +898,8 @@ mod element {
fn paint(
&mut self,
bounds: gpui::Bounds<ui::prelude::Pixels>,
_: &mut Self::BeforeLayout,
layout: &mut Self::AfterLayout,
_: &mut Self::RequestLayoutState,
layout: &mut Self::PrepaintState,
cx: &mut ui::prelude::ElementContext,
) {
for child in &mut layout.children {

View file

@ -4971,10 +4971,10 @@ fn parse_pixel_size_env_var(value: &str) -> Option<Size<DevicePixels>> {
struct DisconnectedOverlay;
impl Element for DisconnectedOverlay {
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) {
let mut background = cx.theme().colors().elevated_surface_background;
background.fade_out(0.2);
let mut overlay = div()
@ -4992,24 +4992,24 @@ impl Element for DisconnectedOverlay {
"Your connection to the remote project has been lost.",
))
.into_any();
(overlay.before_layout(cx), overlay)
(overlay.request_layout(cx), overlay)
}
fn after_layout(
fn prepaint(
&mut self,
bounds: Bounds<Pixels>,
overlay: &mut Self::BeforeLayout,
overlay: &mut Self::RequestLayoutState,
cx: &mut ElementContext,
) {
cx.insert_hitbox(bounds, true);
overlay.after_layout(cx);
overlay.prepaint(cx);
}
fn paint(
&mut self,
_: Bounds<Pixels>,
overlay: &mut Self::BeforeLayout,
_: &mut Self::AfterLayout,
overlay: &mut Self::RequestLayoutState,
_: &mut Self::PrepaintState,
cx: &mut ElementContext,
) {
overlay.paint(cx)