Skip to content

Truly independent web browser

LibCompositing+Compositor: Stretch a zoomed page by whole screen pixels

Aliaksandr Kalenik78f9c9aae64a
diff --git a/Libraries/LibCompositing/Scrolling/ElasticOverscroll.cpp b/Libraries/LibCompositing/Scrolling/ElasticOverscroll.cpp
index 0c04c6cc753..76736c22905 100644
--- a/Libraries/LibCompositing/Scrolling/ElasticOverscroll.cpp
+++ b/Libraries/LibCompositing/Scrolling/ElasticOverscroll.cpp
@@ -17,7 +17,7 @@ static constexpr double rubber_band_period = 1.6;
static constexpr double rebound_decay_per_second = rubber_band_stiffness / rubber_band_period;
static constexpr double largest_bounce_as_share_of_scrollport = 0.2;
-static constexpr float stretch_that_counts_as_sprung_back = 1;
+static constexpr float screen_pixels_of_stretch_that_count_as_sprung_back = 1;
// A scroller released at its edge with speed V peaks at amplitude * V / (decay * e). Wheel events carry no
// timestamps, so the speed measured for events that were handled in a burst can be far above any real one.
@@ -26,8 +26,9 @@ static float fastest_release_speed_for(float scrollport_length)
return static_cast<float>(largest_bounce_as_share_of_scrollport * scrollport_length * rebound_decay_per_second * AK::E<double> / rubber_band_amplitude);
}
-ElasticOverscroll::ElasticOverscroll(Gfx::FloatSize scrollport_size)
+ElasticOverscroll::ElasticOverscroll(Gfx::FloatSize scrollport_size, float screen_pixels_per_pixel)
: m_scrollport_size(scrollport_size)
+ , m_screen_pixels_per_pixel(screen_pixels_per_pixel)
{
}
@@ -36,7 +37,10 @@ Gfx::FloatPoint ElasticOverscroll::stretch() const
auto unrounded_stretch = m_rebound.has_value()
? m_rebound->current_stretch
: m_accumulated_overscroll.scaled(static_cast<float>(1 / rubber_band_stiffness));
- return { AK::trunc(unrounded_stretch.x()), AK::trunc(unrounded_stretch.y()) };
+ return {
+ AK::trunc(unrounded_stretch.x() * m_screen_pixels_per_pixel) / m_screen_pixels_per_pixel,
+ AK::trunc(unrounded_stretch.y() * m_screen_pixels_per_pixel) / m_screen_pixels_per_pixel,
+ };
}
static float relax_axis_by(float& accumulated_overscroll, float delta)
@@ -112,7 +116,7 @@ struct ReboundAxisSample {
bool has_sprung_back { true };
};
-static ReboundAxisSample sample_rebound_axis(float initial_stretch, float initial_velocity, float scrollport_length, double elapsed_seconds)
+static ReboundAxisSample sample_rebound_axis(float initial_stretch, float initial_velocity, float scrollport_length, float stretch_that_counts_as_sprung_back, double elapsed_seconds)
{
if (initial_stretch == 0)
return {};
@@ -142,8 +146,9 @@ bool ElasticOverscroll::advance_rebound_to(MonotonicTime frame_time)
// The time of a display tick can be earlier than the event that released the stretch.
auto elapsed_seconds = max(0.0, (frame_time - m_rebound->started_at).to_seconds_f64());
- auto horizontal = sample_rebound_axis(m_rebound->initial_stretch.x(), m_rebound->initial_velocity.x(), m_scrollport_size.width(), elapsed_seconds);
- auto vertical = sample_rebound_axis(m_rebound->initial_stretch.y(), m_rebound->initial_velocity.y(), m_scrollport_size.height(), elapsed_seconds);
+ auto stretch_that_counts_as_sprung_back = screen_pixels_of_stretch_that_count_as_sprung_back / m_screen_pixels_per_pixel;
+ auto horizontal = sample_rebound_axis(m_rebound->initial_stretch.x(), m_rebound->initial_velocity.x(), m_scrollport_size.width(), stretch_that_counts_as_sprung_back, elapsed_seconds);
+ auto vertical = sample_rebound_axis(m_rebound->initial_stretch.y(), m_rebound->initial_velocity.y(), m_scrollport_size.height(), stretch_that_counts_as_sprung_back, elapsed_seconds);
if (horizontal.has_sprung_back && vertical.has_sprung_back) {
m_rebound.clear();
return false;
diff --git a/Libraries/LibCompositing/Scrolling/ElasticOverscroll.h b/Libraries/LibCompositing/Scrolling/ElasticOverscroll.h
index c1986df5deb..69de0615e82 100644
--- a/Libraries/LibCompositing/Scrolling/ElasticOverscroll.h
+++ b/Libraries/LibCompositing/Scrolling/ElasticOverscroll.h
@@ -19,9 +19,10 @@ namespace Compositing {
// Deltas, stretches and the scrollport are in the same pixels. Velocities are in those pixels per second.
class COMPOSITING_API ElasticOverscroll {
public:
- explicit ElasticOverscroll(Gfx::FloatSize scrollport_size);
+ // A scroller that is shown magnified takes up `screen_pixels_per_pixel` pixels of the screen with each of its own.
+ explicit ElasticOverscroll(Gfx::FloatSize scrollport_size, float screen_pixels_per_pixel = 1);
- // Whole pixels, like the scroll offsets the stretch is presented with.
+ // Whole pixels of the screen, like the scroll offsets the stretch is presented with.
Gfx::FloatPoint stretch() const;
bool is_at_rest() const { return !m_rebound.has_value() && m_accumulated_overscroll.is_zero(); }
bool is_rebounding() const { return m_rebound.has_value(); }
@@ -48,6 +49,7 @@ private:
};
Gfx::FloatSize m_scrollport_size;
+ float m_screen_pixels_per_pixel { 1 };
Gfx::FloatPoint m_accumulated_overscroll;
Optional<Rebound> m_rebound;
};
diff --git a/Services/Compositor/ContextState.cpp b/Services/Compositor/ContextState.cpp
index e3bd2fb4dd3..1f77cfa8692 100644
--- a/Services/Compositor/ContextState.cpp
+++ b/Services/Compositor/ContextState.cpp
@@ -1424,10 +1424,11 @@ bool ContextState::pull_elastic_overscroll_by(Compositing::AsyncScrollNode const
if (!active) {
// A zoomed page shows only the part of the viewport's scrollport that its visual viewport is on, and that part
// is what bounds how far the viewport stretches.
+ auto magnification = visual_viewport_magnification();
auto scrollport_size = node.scrollport_rect.size().to_type<float>();
if (node.is_viewport)
- scrollport_size.scale_by(1.0f / visual_viewport_magnification());
- m_elastic_overscrolls.append({ node.stable_node_id, Compositing::ElasticOverscroll { scrollport_size } });
+ scrollport_size.scale_by(1.0f / magnification);
+ m_elastic_overscrolls.append({ node.stable_node_id, Compositing::ElasticOverscroll { scrollport_size, magnification } });
active = &m_elastic_overscrolls.last();
}
active->overscroll.pull_by(overscroll_delta);
diff --git a/Tests/LibCompositing/TestElasticOverscroll.cpp b/Tests/LibCompositing/TestElasticOverscroll.cpp
index 849074a8c2d..74ba4ec3327 100644
--- a/Tests/LibCompositing/TestElasticOverscroll.cpp
+++ b/Tests/LibCompositing/TestElasticOverscroll.cpp
@@ -132,3 +132,26 @@ TEST_CASE(fingers_take_a_rebound_over_from_where_it_has_got_to)
overscroll.pull_by({ 0, -40 });
EXPECT(overscroll.stretch().y() < stretch_when_taken_over.y());
}
+
+TEST_CASE(magnified_scroller_stretches_and_springs_back_in_whole_pixels_of_the_screen)
+{
+ ElasticOverscroll overscroll { scrollport_size, 2 };
+ overscroll.pull_by({ 0, -150 });
+ EXPECT_EQ(overscroll.stretch(), Gfx::FloatPoint(0, -7.5f));
+
+ overscroll.pull_by({ 0, -1850 });
+ auto released_at = MonotonicTime::now();
+ overscroll.release({}, released_at);
+
+ float smallest_stretch_presented = 100;
+ bool rebounding = true;
+ for (i64 milliseconds = 1; milliseconds <= 1000 && rebounding; ++milliseconds) {
+ rebounding = overscroll.advance_rebound_to(time_after_release(released_at, milliseconds));
+ auto stretch = AK::abs(overscroll.stretch().y());
+ EXPECT_EQ(stretch * 2, AK::trunc(stretch * 2));
+ if (stretch != 0)
+ smallest_stretch_presented = min(smallest_stretch_presented, stretch);
+ }
+ EXPECT(!rebounding);
+ EXPECT_EQ(smallest_stretch_presented, 0.5f);
+}