BlockEditor fills its box and scrolls with a scrollbar, bringing the caret into view as it moves; a ScrollArea keeps a wheel it can use, so the page behind stays (#7)
This commit is contained in:
1 parent
ca6d2e219e
commit
5490869f23
9 files changed
+260
-23
No files matched your search
+40
-7
@@ -228,6 +228,7 @@ pub struct ScrollArea {
|
||||
base: Div,
|
||||
shadows: bool,
|
||||
vertical: bool,
|
||||
handle: Option<ScrollHandle>,
|
||||
body: SmallVec<[AnyElement; 2]>,
|
||||
}
|
||||
|
||||
@@ -238,10 +239,17 @@ impl ScrollArea {
|
||||
base: div(),
|
||||
shadows: false,
|
||||
vertical: false,
|
||||
handle: None,
|
||||
body: SmallVec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Scrolls through the owner's handle, so the owner can move it.
|
||||
pub fn track_scroll(mut self, handle: &ScrollHandle) -> Self {
|
||||
self.handle = Some(handle.clone());
|
||||
self
|
||||
}
|
||||
|
||||
/// Soft edges where content continues.
|
||||
pub fn shadows(mut self) -> Self {
|
||||
self.shadows = true;
|
||||
@@ -267,8 +275,13 @@ impl ParentElement for ScrollArea {
|
||||
}
|
||||
}
|
||||
|
||||
fn track_activity(state: &Entity<Activity>, window: &mut Window, cx: &mut App) -> f32 {
|
||||
let offset = state.read(cx).handle.offset();
|
||||
fn track_activity(
|
||||
state: &Entity<Activity>,
|
||||
handle: &ScrollHandle,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) -> f32 {
|
||||
let offset = handle.offset();
|
||||
if state.read(cx).last != offset {
|
||||
state.update(cx, |activity, _| {
|
||||
activity.last = offset;
|
||||
@@ -291,8 +304,11 @@ impl RenderOnce for ScrollArea {
|
||||
active_at: None,
|
||||
hovered: false,
|
||||
});
|
||||
let handle = state.read(cx).handle.clone();
|
||||
let presence = track_activity(&state, window, cx);
|
||||
let handle = match self.handle {
|
||||
Some(handle) => handle,
|
||||
None => state.read(cx).handle.clone(),
|
||||
};
|
||||
let presence = track_activity(&state, &handle, window, cx);
|
||||
self.base
|
||||
.id(self.id)
|
||||
.relative()
|
||||
@@ -308,13 +324,28 @@ impl RenderOnce for ScrollArea {
|
||||
div()
|
||||
.id("scroll-body")
|
||||
.size_full()
|
||||
.when(self.vertical, |body| body.overflow_y_scroll().overflow_x_hidden())
|
||||
.when(self.vertical, |body| {
|
||||
body.overflow_y_scroll().overflow_x_hidden()
|
||||
})
|
||||
.when(!self.vertical, |body| body.overflow_scroll())
|
||||
.track_scroll(&handle)
|
||||
.on_scroll_wheel({
|
||||
let handle = handle.clone();
|
||||
move |event, window, cx| {
|
||||
let delta = event.delta.pixel_delta(window.line_height());
|
||||
let reach = handle.max_offset();
|
||||
let vertical = delta.y != Pixels::ZERO && reach.y > Pixels::ZERO;
|
||||
if vertical || (delta.x != Pixels::ZERO && reach.x > Pixels::ZERO) {
|
||||
cx.stop_propagation();
|
||||
}
|
||||
}
|
||||
})
|
||||
.map(|body| match self.vertical {
|
||||
// Children fill and never widen, so rows wrap.
|
||||
true => body.flex().flex_col().children(
|
||||
self.body.into_iter().map(|child| div().w_full().min_w_0().child(child)),
|
||||
self.body
|
||||
.into_iter()
|
||||
.map(|child| div().w_full().min_w_0().child(child)),
|
||||
),
|
||||
false => body.children(self.body),
|
||||
}),
|
||||
@@ -322,7 +353,9 @@ impl RenderOnce for ScrollArea {
|
||||
.when(self.shadows, |area| area.child(ScrollShadow::new(&handle)))
|
||||
.child(Scrollbar::new("scrollbar-y", &handle, Axis::Vertical).presence(presence))
|
||||
.when(!self.vertical, |area| {
|
||||
area.child(Scrollbar::new("scrollbar-x", &handle, Axis::Horizontal).presence(presence))
|
||||
area.child(
|
||||
Scrollbar::new("scrollbar-x", &handle, Axis::Horizontal).presence(presence),
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
+49
-2
@@ -2,8 +2,8 @@ use std::time::Duration;
|
||||
|
||||
use gpui::{
|
||||
AnyElement, App, AppContext, Axis, Bounds, Context, InteractiveElement, IntoElement, Modifiers,
|
||||
MouseButton, ParentElement, Pixels, Point, Render, Styled, TestAppContext, VisualTestContext,
|
||||
Window, div, point, px,
|
||||
MouseButton, ParentElement, Pixels, Point, Render, StatefulInteractiveElement, Styled,
|
||||
TestAppContext, VisualTestContext, Window, div, point, px,
|
||||
};
|
||||
|
||||
use super::{Drawer, PaneGroup, SplitPane};
|
||||
@@ -254,3 +254,50 @@ fn invalid_sizes_after_a_drag_split_evenly(cx: &mut TestAppContext) {
|
||||
let second = cx.debug_bounds("second").expect("second").size.width;
|
||||
assert_eq!(first, second);
|
||||
}
|
||||
|
||||
/// A ScrollArea of `inner` content, 100px tall, in a page that scrolls.
|
||||
struct Nested(Pixels, gpui::ScrollHandle);
|
||||
|
||||
impl Render for Nested {
|
||||
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
|
||||
div()
|
||||
.id("page")
|
||||
.size_full()
|
||||
.overflow_y_scroll()
|
||||
.track_scroll(&self.1)
|
||||
.child(
|
||||
super::ScrollArea::new("area")
|
||||
.h(px(100.0))
|
||||
.child(div().h(self.0)),
|
||||
)
|
||||
.child(div().h(px(2000.0)))
|
||||
}
|
||||
}
|
||||
|
||||
fn wheel_page(inner: Pixels, cx: &mut TestAppContext) -> Pixels {
|
||||
cx.update(Theme::init);
|
||||
let page = gpui::ScrollHandle::new();
|
||||
let (_, cx) = cx.add_window_view({
|
||||
let page = page.clone();
|
||||
move |_, _| Nested(inner, page)
|
||||
});
|
||||
cx.run_until_parked();
|
||||
cx.simulate_event(gpui::ScrollWheelEvent {
|
||||
position: point(px(50.0), px(50.0)),
|
||||
delta: gpui::ScrollDelta::Pixels(point(px(0.0), px(-40.0))),
|
||||
modifiers: Modifiers::none(),
|
||||
touch_phase: gpui::TouchPhase::Moved,
|
||||
});
|
||||
cx.run_until_parked();
|
||||
page.offset().y
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_scroll_area_keeps_a_wheel_it_can_use(cx: &mut TestAppContext) {
|
||||
assert_eq!(wheel_page(px(500.0), cx), px(0.0));
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn a_scroll_area_with_no_room_passes_the_wheel_on(cx: &mut TestAppContext) {
|
||||
assert_eq!(wheel_page(px(50.0), cx), px(-40.0));
|
||||
}
|
||||
Reference in new issue
Block a user