Skip to content

Truly independent web browser

LibCompositing+LibWeb+Compositor: Clamp each step of a smooth scroll

Andreas Klingc52c471c115e
diff --git a/Libraries/LibCompositing/Types.cpp b/Libraries/LibCompositing/Types.cpp
index 09a31cf1f6a..2c02444725a 100644
--- a/Libraries/LibCompositing/Types.cpp
+++ b/Libraries/LibCompositing/Types.cpp
@@ -215,6 +215,8 @@ ErrorOr<void> encode(Encoder& encoder, Compositing::RequestedSmoothScroll const&
TRY(encoder.encode(smooth_scroll.operation_id));
TRY(encoder.encode(smooth_scroll.destination_offset));
TRY(encoder.encode(smooth_scroll.destination_is_relative));
+ TRY(encoder.encode(smooth_scroll.relative_lower_bound));
+ TRY(encoder.encode(smooth_scroll.relative_upper_bound));
TRY(encoder.encode(smooth_scroll.animation_kind));
TRY(encoder.encode(smooth_scroll.initiator));
TRY(encoder.encode(smooth_scroll.retargets_operation));
@@ -225,15 +227,20 @@ ErrorOr<void> encode(Encoder& encoder, Compositing::RequestedSmoothScroll const&
template<>
ErrorOr<Compositing::RequestedSmoothScroll> decode(Decoder& decoder)
{
- return Compositing::RequestedSmoothScroll {
+ Compositing::RequestedSmoothScroll smooth_scroll {
.operation_id = TRY(decoder.decode<Compositing::AsyncScrollOperationID>()),
.destination_offset = TRY(decoder.decode<Gfx::FloatPoint>()),
.destination_is_relative = TRY(decoder.decode<bool>()),
+ .relative_lower_bound = TRY(decoder.decode<Gfx::FloatPoint>()),
+ .relative_upper_bound = TRY(decoder.decode<Gfx::FloatPoint>()),
.animation_kind = TRY(decoder.decode<Compositing::ScrollAnimationKind>()),
.initiator = TRY(decoder.decode<Compositing::SmoothScrollInitiator>()),
.retargets_operation = TRY(decoder.decode<bool>()),
.settled_user_input_scroll_count = TRY(decoder.decode<Optional<u64>>()),
};
+ if (!(smooth_scroll.relative_lower_bound.x() <= smooth_scroll.relative_upper_bound.x() && smooth_scroll.relative_lower_bound.y() <= smooth_scroll.relative_upper_bound.y()))
+ return Error::from_string_literal("Smooth scroll with an empty range of relative destinations");
+ return smooth_scroll;
}
template<>
diff --git a/Libraries/LibCompositing/Types.h b/Libraries/LibCompositing/Types.h
index 66a2f55de29..a19eafbb5dc 100644
--- a/Libraries/LibCompositing/Types.h
+++ b/Libraries/LibCompositing/Types.h
@@ -204,6 +204,10 @@ struct RequestedSmoothScroll {
// Where the scroll goes, or how far it goes from wherever the box is.
Gfx::FloatPoint destination_offset;
bool destination_is_relative { false };
+ // What a relative scroll clamps the box to, as each scroll folded into it clamps the box to the scroll range it
+ // had.
+ Gfx::FloatPoint relative_lower_bound { -AK::Infinity<float>, -AK::Infinity<float> };
+ Gfx::FloatPoint relative_upper_bound { AK::Infinity<float>, AK::Infinity<float> };
ScrollAnimationKind animation_kind { ScrollAnimationKind::SmoothScroll };
SmoothScrollInitiator initiator { SmoothScrollInitiator::Programmatic };
// User input that heads the user input scroll of the same operation somewhere else, rather than starting another.
@@ -212,19 +216,31 @@ struct RequestedSmoothScroll {
// that the main thread had heard of. User input the compositor took since carries the gesture on instead.
Optional<u64> settled_user_input_scroll_count;
+ RequestedRelativeScroll relative_scroll() const
+ {
+ VERIFY(destination_is_relative);
+ return { destination_offset, relative_lower_bound, relative_upper_bound };
+ }
+
// A later scroll of the same operation goes on from where this one was headed.
void fold_later(RequestedSmoothScroll const& later)
{
VERIFY(later.operation_id == operation_id);
- auto earlier_destination_offset = destination_offset;
- auto earlier_destination_is_relative = destination_is_relative;
- auto earlier_retargets_operation = retargets_operation;
+ auto earlier = *this;
*this = later;
- retargets_operation |= earlier_retargets_operation;
- if (later.destination_is_relative) {
- destination_offset.translate_by(earlier_destination_offset);
- destination_is_relative = earlier_destination_is_relative;
+ retargets_operation |= earlier.retargets_operation;
+ if (!later.destination_is_relative)
+ return;
+ if (!earlier.destination_is_relative) {
+ destination_offset = later.relative_scroll().applied_to(earlier.destination_offset);
+ destination_is_relative = false;
+ return;
}
+ auto folded = earlier.relative_scroll();
+ folded.then(later.relative_scroll());
+ destination_offset = folded.delta;
+ relative_lower_bound = folded.lower_bound;
+ relative_upper_bound = folded.upper_bound;
}
};
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index cea3fe9c800..6291c38d921 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -7283,6 +7283,12 @@ void LocalNavigable::request_scroll_jump(Web::AsyncScrollNodeStableID stable_nod
void LocalNavigable::request_smooth_scroll(Web::AsyncScrollNodeStableID stable_node_id, Compositing::RequestedSmoothScroll smooth_scroll)
{
+ if (smooth_scroll.destination_is_relative) {
+ if (auto const* scrolling_box = committed_scrolling_box_for_async_scroll_node(*active_document(), stable_node_id)) {
+ smooth_scroll.relative_lower_bound = device_scroll_offset(Painting::minimum_scroll_offset(*scrolling_box));
+ smooth_scroll.relative_upper_bound = device_scroll_offset(Painting::maximum_scroll_offset(*scrolling_box));
+ }
+ }
auto& scroll_request = unsent_scroll_request_for(stable_node_id);
// A scroll that continues the same operation folds into the request for it, whose promises carry over.
if (scroll_request.smooth_scroll.has_value() && scroll_request.smooth_scroll->operation_id == smooth_scroll.operation_id) {
diff --git a/Services/Compositor/ContextState.cpp b/Services/Compositor/ContextState.cpp
index 7552b69c48e..2156b725269 100644
--- a/Services/Compositor/ContextState.cpp
+++ b/Services/Compositor/ContextState.cpp
@@ -1136,7 +1136,7 @@ Optional<PendingFrame> ContextState::apply_scroll_requests(ReadonlySpan<Composit
auto relative_scroll_start = m_async_scroll_tree.scroll_offset_for_node(scroll_node_index.value(), m_scroll_state_snapshot).value();
if (smooth_scroll->initiator == Compositing::SmoothScrollInitiator::UserInput && destination_of_running_user_scroll.has_value())
relative_scroll_start = destination_of_running_user_scroll.value();
- destination_offset.translate_by(relative_scroll_start);
+ destination_offset = smooth_scroll->relative_scroll().applied_to(relative_scroll_start);
}
destination_offset = m_async_scroll_tree.clamped_scroll_offset_for_node(scroll_node_index.value(), destination_offset);
if (retargets_running_animation) {
diff --git a/Tests/LibCompositing/TestScrollRequest.cpp b/Tests/LibCompositing/TestScrollRequest.cpp
index 762e5126408..0dafe94afb9 100644
--- a/Tests/LibCompositing/TestScrollRequest.cpp
+++ b/Tests/LibCompositing/TestScrollRequest.cpp
@@ -94,3 +94,25 @@ TEST_CASE(later_step_of_an_unsent_operation_goes_on_from_where_it_was_headed)
EXPECT(!then_to_an_offset.destination_is_relative);
EXPECT(then_to_an_offset.retargets_operation);
}
+
+TEST_CASE(step_past_the_end_of_the_scroll_range_takes_nothing_from_the_later_step_of_an_unsent_operation)
+{
+ auto step = [](float delta) {
+ auto scroll = smooth_scroll({ 0, delta }, true, true);
+ scroll.relative_lower_bound = { 0, 0 };
+ scroll.relative_upper_bound = { 0, 1000 };
+ return scroll;
+ };
+
+ auto from_the_start = step(-100);
+ from_the_start.fold_later(step(100));
+ EXPECT_EQ(from_the_start.relative_scroll().applied_to({ 0, 0 }), Gfx::FloatPoint(0, 100));
+ EXPECT_EQ(from_the_start.relative_scroll().applied_to({ 0, 500 }), Gfx::FloatPoint(0, 500));
+ EXPECT_EQ(from_the_start.relative_scroll().applied_to({ 0, 1000 }), Gfx::FloatPoint(0, 1000));
+
+ auto to_an_offset = smooth_scroll({ 0, 950 }, false, false);
+ to_an_offset.fold_later(step(100));
+ EXPECT_EQ(to_an_offset.destination_offset, Gfx::FloatPoint(0, 1000));
+ to_an_offset.fold_later(step(-100));
+ EXPECT_EQ(to_an_offset.destination_offset, Gfx::FloatPoint(0, 900));
+}
diff --git a/Tests/LibWeb/Text/expected/async-scrolling/key-on-the-main-thread-after-a-key-with-nothing-to-scroll.txt b/Tests/LibWeb/Text/expected/async-scrolling/key-on-the-main-thread-after-a-key-with-nothing-to-scroll.txt
index 4cd715cd5a0..707c31eb150 100644
--- a/Tests/LibWeb/Text/expected/async-scrolling/key-on-the-main-thread-after-a-key-with-nothing-to-scroll.txt
+++ b/Tests/LibWeb/Text/expected/async-scrolling/key-on-the-main-thread-after-a-key-with-nothing-to-scroll.txt
@@ -1,2 +1,2 @@
keys the page heard of: ArrowUp, ArrowDown
-main thread offset: 0
+main thread offset: 100