Skip to content

Truly independent web browser

Compositor: Keep hovered overlay scrollbars expanded until they fade out

Andreas Klingc7ec543a9f90
diff --git a/Services/Compositor/ContextState.cpp b/Services/Compositor/ContextState.cpp
index 417fbd0bae5..b554d4b4394 100644
--- a/Services/Compositor/ContextState.cpp
+++ b/Services/Compositor/ContextState.cpp
@@ -1534,27 +1534,35 @@ ContextPresenter::FrameContent ContextState::frame_content(Compositing::ScrollSt
}
// Each frame shows the scrollbars as of when it is painted. An elastic overscroll moves the presented scroll offsets
-// only, and shows the scrollbars of the stretched scroller as a scroll does.
-void ContextState::update_scrollbar_visibility()
+// only, and shows the scrollbars of the stretched scroller as a scroll does. Returns the damage of the scrollbars that
+// the display list does not damage itself.
+Gfx::IntRect ContextState::update_scrollbar_visibility()
{
auto stretched_scroll_state_snapshot = scroll_state_snapshot_stretched_by_elastic_overscrolls();
auto const& presented_scroll_state_snapshot = stretched_scroll_state_snapshot.has_value() ? *stretched_scroll_state_snapshot : m_scroll_state_snapshot;
- m_scrollbar_controller.update_visibility(presented_scroll_state_snapshot, MonotonicTime::now());
+ auto const* visual_context_tree = m_scene.visual_context_tree().has_value() ? &m_scene.visual_context_tree_for_compositing() : nullptr;
+ auto now = MonotonicTime::now();
+ auto damage_rect = m_scrollbar_controller.update_visibility(visual_context_tree, presented_scroll_state_snapshot, now);
+
+ // A hovered scrollbar starts to grow here, which needs display ticks before anything else asks for a frame.
+ if (m_request_scrollbar_fade_frames && m_scrollbar_controller.fades_need_frame(now))
+ m_request_scrollbar_fade_frames();
// The fade out runs on display ticks, which the wait before it does not need.
auto fade_out_time = m_scrollbar_controller.next_fade_out_time();
if (!fade_out_time.has_value() || !m_request_scrollbar_fade_frames) {
if (m_scrollbar_fade_out_timer)
m_scrollbar_fade_out_timer->stop();
- return;
+ return damage_rect;
}
if (!m_scrollbar_fade_out_timer) {
m_scrollbar_fade_out_timer = Core::Timer::create_single_shot(AK::Duration::zero(), [this] {
m_request_scrollbar_fade_frames();
});
}
- auto delay = max(AK::Duration::zero(), *fade_out_time - MonotonicTime::now());
+ auto delay = max(AK::Duration::zero(), *fade_out_time - now);
m_scrollbar_fade_out_timer->restart(delay);
+ return damage_rect;
}
Optional<PendingFrame> ContextState::advance_scrollbar_fades(MonotonicTime frame_time)
@@ -1572,7 +1580,7 @@ Optional<PendingFrame> ContextState::advance_scrollbar_fades(MonotonicTime frame
Optional<PreparedFrame> ContextState::prepare_frame(DisplayListPlayerSkia& display_list_player, PendingFrame pending_frame, CompositedContextResolver const* composited_context_resolver)
{
- update_scrollbar_visibility();
+ pending_frame.forced_damage_rect.unite(update_scrollbar_visibility());
auto stretched_scroll_state_snapshot = scroll_state_snapshot_stretched_by_elastic_overscrolls();
auto const& presented_scroll_state_snapshot = stretched_scroll_state_snapshot.has_value() ? *stretched_scroll_state_snapshot : m_scroll_state_snapshot;
return m_presenter.prepare_frame(display_list_player, pending_frame, frame_content(presented_scroll_state_snapshot, composited_context_resolver));
diff --git a/Services/Compositor/ContextState.h b/Services/Compositor/ContextState.h
index 64b91d041a0..db6f0c0b5e3 100644
--- a/Services/Compositor/ContextState.h
+++ b/Services/Compositor/ContextState.h
@@ -333,7 +333,7 @@ private:
void rebuild_wheel_hit_test_targets();
ContextPresenter::FrameContent frame_content(Compositing::ScrollStateSnapshot const& presented_scroll_state_snapshot, CompositedContextResolver const*) const;
Gfx::IntRect damage_rect_of_blinking_carets();
- void update_scrollbar_visibility();
+ Gfx::IntRect update_scrollbar_visibility();
CompositorStateWebContentClient& m_web_content_client;
Web::CompositorContextId m_context_id;
diff --git a/Services/Compositor/DisplayListPlayerSkia.cpp b/Services/Compositor/DisplayListPlayerSkia.cpp
index faed192d2dc..b4d18bf2893 100644
--- a/Services/Compositor/DisplayListPlayerSkia.cpp
+++ b/Services/Compositor/DisplayListPlayerSkia.cpp
@@ -190,43 +190,31 @@ void DisplayListPlayerSkia::flush_async(Gfx::PaintingSurface& surface, Function<
surface.flush();
}
-// A scroll offset outside the scroll range moves the thumb past an end of its track, where it is cut short instead.
-static Gfx::IntRect thumb_rect_kept_inside_track(PaintScrollBar const& command)
-{
- auto thumb_rect = command.thumb_rect;
- auto const& track_rect = command.track_rect;
- if (command.vertical) {
- auto top = max(thumb_rect.top(), track_rect.top());
- auto bottom = min(thumb_rect.bottom(), track_rect.bottom());
- thumb_rect.set_y(top);
- thumb_rect.set_height(max(0, bottom - top));
- } else {
- auto left = max(thumb_rect.left(), track_rect.left());
- auto right = min(thumb_rect.right(), track_rect.right());
- thumb_rect.set_x(left);
- thumb_rect.set_width(max(0, right - left));
- }
- return thumb_rect;
+static bool rect_is_pixel_aligned(SkRect const& rect)
+{
+ return rect.left() == roundf(rect.left()) && rect.top() == roundf(rect.top()) && rect.right() == roundf(rect.right()) && rect.bottom() == roundf(rect.bottom());
}
-static void paint_scrollbar_into_surface(Gfx::PaintingSurface& surface, PaintScrollBar const& command, float opacity, Optional<SkRRect> const& corner_clip = {})
+static void paint_scrollbar_into_surface(Gfx::PaintingSurface& surface, ScrollbarController::PresentedScrollbar const& scrollbar, Optional<SkRRect> const& corner_clip = {})
{
+ auto opacity = scrollbar.opacity;
if (opacity <= 0)
return;
- auto gutter_rect = to_skia_rect(command.gutter_rect);
+ auto gutter_rect = to_skia_rect(scrollbar.gutter_rect);
- auto thumb_rect = to_skia_rect(thumb_rect_kept_inside_track(command));
+ auto thumb_rect = to_skia_rect(scrollbar.thumb_rect);
auto radius = thumb_rect.width() / 2;
auto thumb_rrect = SkRRect::MakeRectXY(thumb_rect, radius, radius);
auto& canvas = surface.canvas();
- auto gutter_fill_color = command.track_color;
SkPaint gutter_fill_paint;
- gutter_fill_paint.setColor(to_skia_color(gutter_fill_color));
+ gutter_fill_paint.setColor(to_skia_color(scrollbar.track_color));
gutter_fill_paint.setAlphaf(gutter_fill_paint.getAlphaf() * opacity);
- canvas.drawRect(gutter_rect, gutter_fill_paint);
+ gutter_fill_paint.setAntiAlias(!rect_is_pixel_aligned(gutter_rect));
+ if (gutter_fill_paint.getAlphaf() > 0)
+ canvas.drawRect(gutter_rect, gutter_fill_paint);
if (thumb_rect.isEmpty())
return;
@@ -248,12 +236,13 @@ static void paint_scrollbar_into_surface(Gfx::PaintingSurface& surface, PaintScr
};
SkPaint thumb_fill_paint;
- thumb_fill_paint.setColor(to_skia_color(command.thumb_color));
+ thumb_fill_paint.setColor(to_skia_color(scrollbar.thumb_color));
thumb_fill_paint.setAlphaf(thumb_fill_paint.getAlphaf() * opacity);
- thumb_fill_paint.setAntiAlias(clipped_thumb.has_value());
+ // A thumb that is growing or shrinking has edges between pixels.
+ thumb_fill_paint.setAntiAlias(clipped_thumb.has_value() || !rect_is_pixel_aligned(thumb_rect));
draw_thumb(thumb_fill_paint);
- auto stroke_color = command.thumb_color.lightened();
+ auto stroke_color = scrollbar.thumb_color.lightened();
SkPaint stroke_paint;
stroke_paint.setStroke(true);
stroke_paint.setStrokeWidth(1);
@@ -263,14 +252,14 @@ static void paint_scrollbar_into_surface(Gfx::PaintingSurface& surface, PaintScr
draw_thumb(stroke_paint);
}
-void DisplayListPlayerSkia::paint_scrollbar(Gfx::PaintingSurface& surface, PaintScrollBar const& command, float opacity, Gfx::IntRect const& corner_clip_rect, int corner_clip_radius)
+void DisplayListPlayerSkia::paint_scrollbar(Gfx::PaintingSurface& surface, ScrollbarController::PresentedScrollbar const& scrollbar, Gfx::IntRect const& corner_clip_rect, int corner_clip_radius)
{
Optional<SkRRect> corner_clip;
if (corner_clip_radius > 0) {
auto radius = static_cast<SkScalar>(corner_clip_radius);
corner_clip = SkRRect::MakeRectXY(to_skia_rect(corner_clip_rect), radius, radius);
}
- paint_scrollbar_into_surface(surface, command, opacity, corner_clip);
+ paint_scrollbar_into_surface(surface, scrollbar, corner_clip);
}
static SkMatrix vertical_glyph_run_rotation(Gfx::IntRect const& rect)
@@ -1468,6 +1457,8 @@ void DisplayListPlayerSkia::play_command(PaintNestedDisplayList const& command)
canvas.scale(command.rect.width() / command.list_size.width(), command.rect.height() / command.list_size.height());
}
ScrollStateSnapshot scroll_state_snapshot;
+ // The scrollbars of this context do not belong to the scroll nodes of a nested display list.
+ TemporaryChange<ScrollbarController const*> scrollbar_controller_change { m_scrollbar_controller, nullptr };
execute_nested_display_list(*nested_display_list.display_list, nested_display_list.visual_context_tree, scroll_state_snapshot);
canvas.restore();
}
@@ -1514,8 +1505,19 @@ void DisplayListPlayerSkia::play_command(CompositorNestedContextWheelRegion cons
void DisplayListPlayerSkia::play_command(PaintScrollBar const& command)
{
- auto opacity = m_scrollbar_controller ? m_scrollbar_controller->opacity_of_scrollbar_painted_by_display_list(command.scroll_node_index, command.vertical) : 1.0f;
- paint_scrollbar_into_surface(surface(), command, opacity);
+ if (m_scrollbar_controller) {
+ if (auto scrollbar = m_scrollbar_controller->presented_scrollbar_painted_by_display_list(command.scroll_node_index, command.vertical, command.thumb_color, command.track_color, active_scroll_state()); scrollbar.has_value()) {
+ paint_scrollbar_into_surface(surface(), *scrollbar);
+ return;
+ }
+ }
+ ScrollbarController::PresentedScrollbar scrollbar {
+ .gutter_rect = command.gutter_rect.to_type<float>(),
+ .thumb_rect = ScrollbarController::thumb_rect_kept_inside_track(command.thumb_rect, command.track_rect, command.vertical).to_type<float>(),
+ .thumb_color = command.thumb_color,
+ .track_color = command.track_color,
+ };
+ paint_scrollbar_into_surface(surface(), scrollbar);
}
void DisplayListPlayerSkia::push_clip(ReplayClip const& clip)
diff --git a/Services/Compositor/DisplayListPlayerSkia.h b/Services/Compositor/DisplayListPlayerSkia.h
index cd5fa33a535..df5ccf94d29 100644
--- a/Services/Compositor/DisplayListPlayerSkia.h
+++ b/Services/Compositor/DisplayListPlayerSkia.h
@@ -12,6 +12,7 @@
#include <AK/RefPtr.h>
#include <Compositor/DisplayListPlayer.h>
#include <Compositor/Forward.h>
+#include <Compositor/ScrollbarController.h>
#include <LibCompositing/DisplayList/CompositedContext.h>
#include <LibCompositing/DisplayList/DisplayList.h>
#include <LibCompositing/DisplayList/DisplayListCommand.h>
@@ -27,8 +28,6 @@ class sk_sp;
namespace Compositor {
-class ScrollbarController;
-
// The color filter force-dark runs classified images through: the same Oklab lightness inversion the solid colors
// take — as a Skia runtime effect. Exposed so a benchmark can time the shipped effect, rather than a copy of it.
sk_sp<SkColorFilter> force_dark_image_color_filter();
@@ -55,7 +54,7 @@ public:
void flush(Gfx::PaintingSurface&) override;
void flush_async(Gfx::PaintingSurface&, Function<void()>&&);
// A corner clip radius that is not zero shapes the thumb to the corner clip rect with that radius.
- void paint_scrollbar(Gfx::PaintingSurface&, Compositing::PaintScrollBar const&, float opacity, Gfx::IntRect const& corner_clip_rect = {}, int corner_clip_radius = 0);
+ void paint_scrollbar(Gfx::PaintingSurface&, ScrollbarController::PresentedScrollbar const&, Gfx::IntRect const& corner_clip_rect = {}, int corner_clip_radius = 0);
private:
#define DECLARE_PLAY_COMMAND(command_type) \
diff --git a/Services/Compositor/ScrollbarController.cpp b/Services/Compositor/ScrollbarController.cpp
index c187ed6a7d3..7f42438a9b8 100644
--- a/Services/Compositor/ScrollbarController.cpp
+++ b/Services/Compositor/ScrollbarController.cpp
@@ -182,7 +182,7 @@ Optional<size_t> ScrollbarController::hit_test_scrollbar_painted_by_display_list
auto position_in_scrollbar_space = position_in_space_of_scrollbar(scrollbar, visual_context_tree, scroll_state_snapshot, position, Compositing::AccumulatedVisualContextTree::ClipBehavior::Respect);
if (!position_in_scrollbar_space.has_value())
continue;
- auto const& painted_scrollbar_rect = scrollbar.display_list_paints_enlarged_scrollbar ? scrollbar.expanded_gutter_rect : scrollbar.track_rect;
+ auto const& painted_scrollbar_rect = scrollbar.display_list_paints_enlarged_scrollbar || expansion(i) > 0 ? scrollbar.expanded_gutter_rect : scrollbar.track_rect;
if (!painted_scrollbar_rect.to_type<float>().contains(*position_in_scrollbar_space))
continue;
@@ -310,19 +310,29 @@ Optional<ScrollbarController::ScrollOffset> ScrollbarController::scroll_offset_f
return ScrollOffset { scrollbar.scroll_node_id, scroll_offset };
}
-void ScrollbarController::update_visibility(Compositing::ScrollStateSnapshot const& scroll_state_snapshot, MonotonicTime now)
+Gfx::IntRect ScrollbarController::update_visibility(Compositing::AccumulatedVisualContextTree const* visual_context_tree, Compositing::ScrollStateSnapshot const& scroll_state_snapshot, MonotonicTime now)
{
+ Gfx::IntRect damage_rect;
for (size_t i = 0; i < m_scrollbars.size(); ++i) {
auto& visibility = m_visibilities[i];
- auto scroll_offset = scroll_state_snapshot.device_offset_for_index(m_scrollbars[i].scroll_node_index);
+ auto const& scrollbar = m_scrollbars[i];
+ auto scroll_offset = scroll_state_snapshot.device_offset_for_index(scrollbar.scroll_node_index);
auto scrolled = visibility.scroll_offset.has_value() && *visibility.scroll_offset != scroll_offset;
visibility.scroll_offset = scroll_offset;
+ // The display list damages only the rect of the scrollbar as it gives it when it scrolls.
+ if (scrolled && visual_context_tree && !scrollbar.is_painted_by_compositor && expansion(i) != (scrollbar.display_list_paints_enlarged_scrollbar ? 1 : 0)) {
+ if (auto rect = rect_in_viewport(i, *visual_context_tree, scroll_state_snapshot); rect.has_value())
+ damage_rect.unite(*rect);
+ }
+
auto was_held_shown = visibility.is_held_shown;
- visibility.is_held_shown = is_held_shown(i);
+ // Hovering a hidden scrollbar does not show it. Only a scroll does.
+ visibility.is_held_shown = is_held_shown(i) && (scrolled || visibility.fade.opacity() > 0);
if (visibility.is_held_shown) {
visibility.fade.jump_to(1);
visibility.fade_out_time.clear();
+ visibility.expansion.fade_to(1, now);
continue;
}
if (scrolled)
@@ -330,6 +340,7 @@ void ScrollbarController::update_visibility(Compositing::ScrollStateSnapshot con
if (scrolled || was_held_shown)
visibility.fade_out_time = now + shown_duration_after_activity;
}
+ return damage_rect;
}
Optional<MonotonicTime> ScrollbarController::next_fade_out_time() const
@@ -345,7 +356,7 @@ Optional<MonotonicTime> ScrollbarController::next_fade_out_time() const
bool ScrollbarController::fades_need_frame(MonotonicTime now) const
{
for (auto const& visibility : m_visibilities) {
- if (visibility.fade.is_fading() || (visibility.fade_out_time.has_value() && *visibility.fade_out_time <= now))
+ if (visibility.fade.is_fading() || visibility.expansion.is_fading() || (visibility.fade_out_time.has_value() && *visibility.fade_out_time <= now))
return true;
}
return false;
@@ -360,33 +371,95 @@ Gfx::IntRect ScrollbarController::advance_fades(Compositing::AccumulatedVisualCo
visibility.fade_out_time.clear();
visibility.fade.fade_to(0, frame_time);
}
- if (!visibility.fade.advance_to(frame_time))
+ auto changed = visibility.fade.advance_to(frame_time);
+ // A scrollbar that has faded out is thin again when it is next shown.
+ if (visibility.fade.opacity() <= 0)
+ visibility.expansion.jump_to(0);
+ changed |= visibility.expansion.advance_to(frame_time);
+ if (!changed)
continue;
- auto const& scrollbar = m_scrollbars[i];
- auto rect = scrollbar.gutter_rect.united(scrollbar.track_rect).united(scrollbar.expanded_gutter_rect);
- if (scrollbar.is_painted_by_compositor) {
- damage_rect.unite(rect.inflated(2, 2));
- continue;
- }
- if (!visual_context_tree.context_is_valid(scrollbar.context))
- continue;
- auto rect_in_viewport = visual_context_tree.transform_rect_to_viewport(scrollbar.context.spatial, rect.to_type<float>(), scroll_state_snapshot);
- if (!isfinite(rect_in_viewport.x()) || !isfinite(rect_in_viewport.y()) || !isfinite(rect_in_viewport.width()) || !isfinite(rect_in_viewport.height()))
- continue;
- damage_rect.unite(Gfx::enclosing_int_rect(rect_in_viewport).inflated(2, 2));
+ if (auto rect = rect_in_viewport(i, visual_context_tree, scroll_state_snapshot); rect.has_value())
+ damage_rect.unite(*rect);
}
return damage_rect;
}
-float ScrollbarController::opacity_of_scrollbar_painted_by_display_list(Compositing::SpatialNodeIndex scroll_node_index, bool vertical) const
+Optional<Gfx::IntRect> ScrollbarController::rect_in_viewport(size_t scrollbar_index, Compositing::AccumulatedVisualContextTree const& visual_context_tree, Compositing::ScrollStateSnapshot const& scroll_state_snapshot) const
+{
+ auto const& scrollbar = m_scrollbars[scrollbar_index];
+ auto rect = scrollbar.gutter_rect.united(scrollbar.track_rect).united(scrollbar.expanded_gutter_rect);
+ if (scrollbar.is_painted_by_compositor)
+ return rect.inflated(2, 2);
+ if (!visual_context_tree.context_is_valid(scrollbar.context))
+ return {};
+ auto rect_in_viewport = visual_context_tree.transform_rect_to_viewport(scrollbar.context.spatial, rect.to_type<float>(), scroll_state_snapshot);
+ if (!isfinite(rect_in_viewport.x()) || !isfinite(rect_in_viewport.y()) || !isfinite(rect_in_viewport.width()) || !isfinite(rect_in_viewport.height()))
+ return {};
+ return Gfx::enclosing_int_rect(rect_in_viewport).inflated(2, 2);
+}
+
+Gfx::IntRect ScrollbarController::thumb_rect_kept_inside_track(Gfx::IntRect thumb_rect, Gfx::IntRect const& track_rect, bool vertical)
+{
+ if (vertical) {
+ auto top = max(thumb_rect.top(), track_rect.top());
+ auto bottom = min(thumb_rect.bottom(), track_rect.bottom());
+ thumb_rect.set_y(top);
+ thumb_rect.set_height(max(0, bottom - top));
+ } else {
+ auto left = max(thumb_rect.left(), track_rect.left());
+ auto right = min(thumb_rect.right(), track_rect.right());
+ thumb_rect.set_x(left);
+ thumb_rect.set_width(max(0, right - left));
+ }
+ return thumb_rect;
+}
+
+static Gfx::FloatRect interpolated_rect(Gfx::IntRect const& from, Gfx::IntRect const& to, float progress)
+{
+ auto interpolate = [progress](int from, int to) {
+ return static_cast<float>(from) + static_cast<float>(to - from) * progress;
+ };
+ return {
+ interpolate(from.x(), to.x()),
+ interpolate(from.y(), to.y()),
+ interpolate(from.width(), to.width()),
+ interpolate(from.height(), to.height()),
+ };
+}
+
+ScrollbarController::PresentedScrollbar ScrollbarController::presented_scrollbar(size_t scrollbar_index, Compositing::ScrollStateSnapshot const& scroll_state_snapshot, Color thumb_color, Color track_color) const
+{
+ auto const& scrollbar = m_scrollbars[scrollbar_index];
+ auto progress = expansion(scrollbar_index);
+
+ auto thin_thumb_rect = thumb_rect_kept_inside_track(translated_thumb_rect(scrollbar, scroll_state_snapshot, false), scrollbar.track_rect, scrollbar.vertical);
+ auto expanded_thumb_rect = thumb_rect_kept_inside_track(translated_thumb_rect(scrollbar, scroll_state_snapshot, true), scrollbar.expanded_gutter_rect, scrollbar.vertical);
+ // An empty thumb has nothing to grow from or to.
+ if (thin_thumb_rect.is_empty())
+ thin_thumb_rect = expanded_thumb_rect;
+ if (expanded_thumb_rect.is_empty())
+ expanded_thumb_rect = thin_thumb_rect;
+
+ // The gutter of a thin scrollbar is not painted, so the gutter grows out of the thin track as it fades in.
+ auto track_alpha = roundf(static_cast<float>(track_color.alpha()) * progress);
+ return PresentedScrollbar {
+ .gutter_rect = interpolated_rect(scrollbar.track_rect, scrollbar.expanded_gutter_rect, progress),
+ .thumb_rect = interpolated_rect(thin_thumb_rect, expanded_thumb_rect, progress),
+ .thumb_color = thumb_color,
+ .track_color = track_color.with_alpha(static_cast<u8>(track_alpha)),
+ .opacity = opacity(scrollbar_index),
+ };
+}
+
+Optional<ScrollbarController::PresentedScrollbar> ScrollbarController::presented_scrollbar_painted_by_display_list(Compositing::SpatialNodeIndex scroll_node_index, bool vertical, Color thumb_color, Color track_color, Compositing::ScrollStateSnapshot const& scroll_state_snapshot) const
{
for (size_t i = 0; i < m_scrollbars.size(); ++i) {
auto const& scrollbar = m_scrollbars[i];
if (!scrollbar.is_painted_by_compositor && scrollbar.scroll_node_index == scroll_node_index && scrollbar.vertical == vertical)
- return opacity(i);
+ return presented_scrollbar(i, scroll_state_snapshot, thumb_color, track_color);
}
- return 1;
+ return {};
}
bool ScrollbarController::paint(Gfx::PaintingSurface& surface, DisplayListPlayerSkia& display_list_player, Compositing::ScrollStateSnapshot const& scroll_state_snapshot) const
@@ -396,21 +469,9 @@ bool ScrollbarController::paint(Gfx::PaintingSurface& surface, DisplayListPlayer
auto const& scrollbar = m_scrollbars[i];
if (!scrollbar.is_painted_by_compositor)
continue;
- auto scrollbar_opacity = opacity(i);
- if (scrollbar_opacity <= 0)
+ if (opacity(i) <= 0)
continue;
- auto expanded = is_expanded(i);
- Compositing::PaintScrollBar paint_scrollbar {
- .scroll_node_index = scrollbar.scroll_node_index,
- .gutter_rect = scrollbar_gutter_rect(scrollbar, expanded),
- .thumb_rect = translated_thumb_rect(scrollbar, scroll_state_snapshot, expanded),
- .track_rect = scrollbar_gutter_rect(scrollbar, true),
- .scroll_size = scrollbar_scroll_size(scrollbar, expanded),
- .thumb_color = scrollbar.thumb_color,
- .track_color = scrollbar.track_color,
- .vertical = scrollbar.vertical,
- };
- display_list_player.paint_scrollbar(surface, paint_scrollbar, scrollbar_opacity, scrollbar.corner_clip_rect, scrollbar.corner_clip_radius);
+ display_list_player.paint_scrollbar(surface, presented_scrollbar(i, scroll_state_snapshot, scrollbar.thumb_color, scrollbar.track_color), scrollbar.corner_clip_rect, scrollbar.corner_clip_radius);
painted_a_scrollbar = true;
}
return painted_a_scrollbar;
@@ -437,4 +498,11 @@ float ScrollbarController::opacity(size_t scrollbar_index) const
return m_visibilities[scrollbar_index].fade.opacity();
}
+float ScrollbarController::expansion(size_t scrollbar_index) const
+{
+ if (s_scrollbars_always_shown)
+ return is_held_shown(scrollbar_index) ? 1 : 0;
+ return m_visibilities[scrollbar_index].expansion.opacity();
+}
+
}
diff --git a/Services/Compositor/ScrollbarController.h b/Services/Compositor/ScrollbarController.h
index 0e0b63c9186..39ed135d691 100644
--- a/Services/Compositor/ScrollbarController.h
+++ b/Services/Compositor/ScrollbarController.h
@@ -12,8 +12,10 @@
#include <Compositor/Forward.h>
#include <LibCompositing/OverlayFade.h>
#include <LibCompositing/Scrolling/AsyncScrollingState.h>
+#include <LibGfx/Color.h>
#include <LibGfx/Forward.h>
#include <LibGfx/Point.h>
+#include <LibGfx/Rect.h>
namespace Compositing {
@@ -43,8 +45,20 @@ public:
Gfx::FloatPoint scroll_offset;
};
- // Overlay scrollbars are hidden until their scroller scrolls or the pointer hovers them. A scrollbar stays shown
- // for this long after its scroller last scrolled or the pointer left it, and then fades out.
+ // A scrollbar as it is painted, somewhere between its thin and its expanded geometry.
+ struct PresentedScrollbar {
+ Gfx::FloatRect gutter_rect;
+ Gfx::FloatRect thumb_rect;
+ Color thumb_color;
+ Color track_color;
+ float opacity { 1 };
+ };
+
+ // A scroll offset outside the scroll range moves the thumb past an end of its track, where it is cut short instead.
+ static Gfx::IntRect thumb_rect_kept_inside_track(Gfx::IntRect thumb_rect, Gfx::IntRect const& track_rect, bool vertical);
+
+ // Overlay scrollbars are hidden until their scroller scrolls. A scrollbar stays shown for this long after its
+ // scroller last scrolled or the pointer left it, and then fades out.
static constexpr auto shown_duration_after_activity = AK::Duration::from_milliseconds(1000);
// Tests see every scrollbar at all times, as when it is shown depends on the time.
@@ -53,15 +67,19 @@ public:
void clear();
void set_scrollbars(Vector<Compositing::AsyncScrollbar> const&);
- // Shows each scrollbar that is hovered or dragged, or whose scroller scrolled since the last call, and starts the
- // wait before a scrollbar that is no longer hovered or dragged fades out.
- void update_visibility(Compositing::ScrollStateSnapshot const&, MonotonicTime now);
+ // Shows each scrollbar whose scroller scrolled since the last call, keeps a shown scrollbar that is hovered or
+ // dragged shown, and starts the wait before a scrollbar that is no longer hovered or dragged fades out. A hovered or
+ // dragged scrollbar that is shown grows to its expanded geometry, and keeps it until it has faded out. Returns the rect in the viewport of the scrollbars the
+ // display list paints that scrolled while they are presented in another geometry than the display list gives them.
+ Gfx::IntRect update_visibility(Compositing::AccumulatedVisualContextTree const*, Compositing::ScrollStateSnapshot const&, MonotonicTime now);
// When the next scrollbar starts to fade out.
Optional<MonotonicTime> next_fade_out_time() const;
bool fades_need_frame(MonotonicTime now) const;
- // Returns the rect in the viewport of the scrollbars whose opacity changed.
+ // Returns the rect in the viewport of the scrollbars whose opacity or geometry changed.
Gfx::IntRect advance_fades(Compositing::AccumulatedVisualContextTree const&, Compositing::ScrollStateSnapshot const&, MonotonicTime frame_time);
- float opacity_of_scrollbar_painted_by_display_list(Compositing::SpatialNodeIndex scroll_node_index, bool vertical) const;
+ // A scrollbar command of the display list with no scrollbar here is painted as the command gives it. The colors are
+ // the ones the display list resolved for the page, which it may have darkened.
+ Optional<PresentedScrollbar> presented_scrollbar_painted_by_display_list(Compositing::SpatialNodeIndex scroll_node_index, bool vertical, Color thumb_color, Color track_color, Compositing::ScrollStateSnapshot const&) const;
bool is_empty() const { return m_scrollbars.is_empty(); }
Vector<Compositing::AsyncScrollbar> const& scrollbars() const { return m_scrollbars; }
@@ -85,11 +103,16 @@ private:
Optional<Gfx::FloatPoint> scroll_offset;
bool is_held_shown { false };
Optional<MonotonicTime> fade_out_time;
+ // How far the scrollbar has grown from its thin geometry to its expanded one.
+ Compositing::OverlayFade expansion;
};
bool is_expanded(size_t scrollbar_index) const;
bool is_held_shown(size_t scrollbar_index) const;
float opacity(size_t scrollbar_index) const;
+ float expansion(size_t scrollbar_index) const;
+ PresentedScrollbar presented_scrollbar(size_t scrollbar_index, Compositing::ScrollStateSnapshot const&, Color thumb_color, Color track_color) const;
+ Optional<Gfx::IntRect> rect_in_viewport(size_t scrollbar_index, Compositing::AccumulatedVisualContextTree const&, Compositing::ScrollStateSnapshot const&) const;
Optional<size_t> hit_test_scrollbar_painted_by_display_list(Compositing::AsyncScrollTree const&, Compositing::AccumulatedVisualContextTree const&, Compositing::ScrollStateSnapshot const&, Gfx::FloatPoint position) const;
float primary_position_of_captured_drag(Compositing::AccumulatedVisualContextTree const&, Compositing::ScrollStateSnapshot const&, Gfx::FloatPoint position);
diff --git a/Tests/Compositor/TestScrollbarController.cpp b/Tests/Compositor/TestScrollbarController.cpp
index 284dd78ee47..0d9360f99bf 100644
--- a/Tests/Compositor/TestScrollbarController.cpp
+++ b/Tests/Compositor/TestScrollbarController.cpp
@@ -142,9 +142,16 @@ static Compositing::AsyncScrollbar scrollbar_painted_by_display_list(bool displa
};
}
+static Compositor::ScrollbarController::PresentedScrollbar presented_scrollbar(Compositor::ScrollbarController const& controller, Compositing::ScrollStateSnapshot const& scroll_state_snapshot = {})
+{
+ auto scrollbar = controller.presented_scrollbar_painted_by_display_list(Compositing::SpatialNodeIndex { 1 }, true, Gfx::Color(0, 0, 0, 100), Gfx::Color(0, 0, 0, 20), scroll_state_snapshot);
+ VERIFY(scrollbar.has_value());
+ return *scrollbar;
+}
+
static float opacity_of_scrollbar(Compositor::ScrollbarController const& controller)
{
- return controller.opacity_of_scrollbar_painted_by_display_list(Compositing::SpatialNodeIndex { 1 }, true);
+ return presented_scrollbar(controller).opacity;
}
static auto const half_fade_duration = AK::Duration::from_milliseconds(Compositing::OverlayFade::duration.to_milliseconds() / 2);
@@ -163,12 +170,12 @@ TEST_CASE(scrollbar_is_hidden_until_its_scroller_scrolls)
Compositing::ScrollStateSnapshot scroll_state_snapshot;
auto start = MonotonicTime::now();
- controller.update_visibility(scroll_state_snapshot, start);
+ (void)controller.update_visibility(nullptr, scroll_state_snapshot, start);
EXPECT_EQ(opacity_of_scrollbar(controller), 0);
EXPECT(!controller.next_fade_out_time().has_value());
scroll_state_snapshot.set_device_offset_for_index(Compositing::SpatialNodeIndex { 1 }, { 0, 10 });
- controller.update_visibility(scroll_state_snapshot, start);
+ (void)controller.update_visibility(nullptr, scroll_state_snapshot, start);
EXPECT_EQ(opacity_of_scrollbar(controller), 1);
EXPECT(controller.next_fade_out_time() == start + Compositor::ScrollbarController::shown_duration_after_activity);
}
@@ -180,9 +187,9 @@ TEST_CASE(scrollbar_fades_out_after_its_scroller_stops_scrolling)
Compositing::ScrollStateSnapshot scroll_state_snapshot;
auto visual_context_tree = visual_context_tree_with_scroller();
auto start = MonotonicTime::now();
- controller.update_visibility(scroll_state_snapshot, start);
+ (void)controller.update_visibility(nullptr, scroll_state_snapshot, start);
scroll_state_snapshot.set_device_offset_for_index(Compositing::SpatialNodeIndex { 1 }, { 0, 10 });
- controller.update_visibility(scroll_state_snapshot, start);
+ (void)controller.update_visibility(nullptr, scroll_state_snapshot, start);
auto shown_until = start + Compositor::ScrollbarController::shown_duration_after_activity;
EXPECT(!controller.fades_need_frame(shown_until - AK::Duration::from_milliseconds(1)));
@@ -209,9 +216,9 @@ TEST_CASE(scroll_while_fading_out_shows_scrollbar_again)
Compositing::ScrollStateSnapshot scroll_state_snapshot;
auto visual_context_tree = visual_context_tree_with_scroller();
auto start = MonotonicTime::now();
- controller.update_visibility(scroll_state_snapshot, start);
+ (void)controller.update_visibility(nullptr, scroll_state_snapshot, start);
scroll_state_snapshot.set_device_offset_for_index(Compositing::SpatialNodeIndex { 1 }, { 0, 10 });
- controller.update_visibility(scroll_state_snapshot, start);
+ (void)controller.update_visibility(nullptr, scroll_state_snapshot, start);
auto shown_until = start + Compositor::ScrollbarController::shown_duration_after_activity;
(void)controller.advance_fades(visual_context_tree, scroll_state_snapshot, shown_until);
@@ -220,30 +227,128 @@ TEST_CASE(scroll_while_fading_out_shows_scrollbar_again)
auto scrolled_again_at = shown_until + half_fade_duration;
scroll_state_snapshot.set_device_offset_for_index(Compositing::SpatialNodeIndex { 1 }, { 0, 20 });
- controller.update_visibility(scroll_state_snapshot, scrolled_again_at);
+ (void)controller.update_visibility(nullptr, scroll_state_snapshot, scrolled_again_at);
EXPECT_EQ(opacity_of_scrollbar(controller), 1);
EXPECT(!controller.fades_need_frame(scrolled_again_at));
EXPECT(controller.next_fade_out_time() == scrolled_again_at + Compositor::ScrollbarController::shown_duration_after_activity);
}
-TEST_CASE(hovered_scrollbar_stays_shown_until_the_pointer_leaves_it)
+TEST_CASE(hovering_hidden_scrollbar_does_not_show_it)
{
Compositor::ScrollbarController controller;
+ controller.set_scrollbars({ scrollbar_painted_by_display_list() });
+ Compositing::ScrollStateSnapshot scroll_state_snapshot;
+ auto start = MonotonicTime::now();
+ (void)controller.update_visibility(nullptr, scroll_state_snapshot, start);
+
controller.set_scrollbars({ scrollbar_painted_by_display_list(true) });
+ (void)controller.update_visibility(nullptr, scroll_state_snapshot, start);
+ EXPECT_EQ(opacity_of_scrollbar(controller), 0);
+ EXPECT(!controller.fades_need_frame(start));
+
+ // A scroll shows it, and the hover then keeps it shown and grows it.
+ scroll_state_snapshot.set_device_offset_for_index(Compositing::SpatialNodeIndex { 1 }, { 0, 10 });
+ (void)controller.update_visibility(nullptr, scroll_state_snapshot, start);
+ EXPECT_EQ(opacity_of_scrollbar(controller), 1);
+ EXPECT(!controller.next_fade_out_time().has_value());
+ EXPECT(controller.fades_need_frame(start));
+}
+
+TEST_CASE(hovered_scrollbar_stays_shown_until_the_pointer_leaves_it)
+{
+ Compositor::ScrollbarController controller;
+ controller.set_scrollbars({ scrollbar_painted_by_display_list() });
Compositing::ScrollStateSnapshot scroll_state_snapshot;
auto start = MonotonicTime::now();
+ (void)controller.update_visibility(nullptr, scroll_state_snapshot, start);
+ scroll_state_snapshot.set_device_offset_for_index(Compositing::SpatialNodeIndex { 1 }, { 0, 10 });
+ (void)controller.update_visibility(nullptr, scroll_state_snapshot, start);
- controller.update_visibility(scroll_state_snapshot, start);
+ controller.set_scrollbars({ scrollbar_painted_by_display_list(true) });
+ (void)controller.update_visibility(nullptr, scroll_state_snapshot, start);
EXPECT_EQ(opacity_of_scrollbar(controller), 1);
EXPECT(!controller.next_fade_out_time().has_value());
auto much_later = start + AK::Duration::from_seconds(10);
- controller.update_visibility(scroll_state_snapshot, much_later);
+ (void)controller.update_visibility(nullptr, scroll_state_snapshot, much_later);
+ (void)controller.advance_fades(visual_context_tree_with_scroller(), scroll_state_snapshot, much_later);
EXPECT_EQ(opacity_of_scrollbar(controller), 1);
EXPECT(!controller.fades_need_frame(much_later));
controller.set_scrollbars({ scrollbar_painted_by_display_list(false) });
- controller.update_visibility(scroll_state_snapshot, much_later);
+ (void)controller.update_visibility(nullptr, scroll_state_snapshot, much_later);
EXPECT_EQ(opacity_of_scrollbar(controller), 1);
EXPECT(controller.next_fade_out_time() == much_later + Compositor::ScrollbarController::shown_duration_after_activity);
}
+
+TEST_CASE(hovered_scrollbar_grows_and_stays_expanded_until_it_has_faded_out)
+{
+ Compositor::ScrollbarController controller;
+ controller.set_scrollbars({ scrollbar_painted_by_display_list() });
+ Compositing::ScrollStateSnapshot scroll_state_snapshot;
+ auto visual_context_tree = visual_context_tree_with_scroller();
+ auto start = MonotonicTime::now();
+ (void)controller.update_visibility(nullptr, scroll_state_snapshot, start);
+
+ // A scroll shows the thin scrollbar.
+ scroll_state_snapshot.set_device_offset_for_index(Compositing::SpatialNodeIndex { 1 }, { 0, 10 });
+ (void)controller.update_visibility(nullptr, scroll_state_snapshot, start);
+ EXPECT_EQ(presented_scrollbar(controller).thumb_rect.width(), 2);
+ EXPECT_EQ(presented_scrollbar(controller).thumb_color.alpha(), 100);
+ EXPECT_EQ(presented_scrollbar(controller).track_color.alpha(), 0);
+
+ // Hovering it grows it over the duration of a fade.
+ controller.set_scrollbars({ scrollbar_painted_by_display_list(true) });
+ (void)controller.update_visibility(nullptr, scroll_state_snapshot, start);
+ EXPECT(controller.fades_need_frame(start));
+ auto damage_rect = controller.advance_fades(visual_context_tree, scroll_state_snapshot, start + half_fade_duration);
+ EXPECT(damage_rect.contains(Gfx::IntRect { 92, 0, 8, 100 }));
+ EXPECT(presented_scrollbar(controller).thumb_rect.width() > 2);
+ EXPECT(presented_scrollbar(controller).thumb_rect.width() < 6);
+ auto grown_at = start + Compositing::OverlayFade::duration;
+ (void)controller.advance_fades(visual_context_tree, scroll_state_snapshot, grown_at);
+ EXPECT_EQ(presented_scrollbar(controller, scroll_state_snapshot).thumb_rect, Gfx::FloatRect(94, 12, 6, 20));
+ EXPECT_EQ(presented_scrollbar(controller).gutter_rect, Gfx::FloatRect(92, 0, 8, 100));
+ EXPECT_EQ(presented_scrollbar(controller).thumb_color.alpha(), 100);
+ EXPECT_EQ(presented_scrollbar(controller).track_color.alpha(), 20);
+ EXPECT(!controller.fades_need_frame(grown_at));
+
+ // It stays expanded after the pointer leaves it, while it is shown and while it fades out.
+ controller.set_scrollbars({ scrollbar_painted_by_display_list(false) });
+ (void)controller.update_visibility(nullptr, scroll_state_snapshot, grown_at);
+ auto shown_until = grown_at + Compositor::ScrollbarController::shown_duration_after_activity;
+ (void)controller.advance_fades(visual_context_tree, scroll_state_snapshot, shown_until);
+ (void)controller.advance_fades(visual_context_tree, scroll_state_snapshot, shown_until + half_fade_duration);
+ EXPECT(opacity_of_scrollbar(controller) > 0);
+ EXPECT_EQ(presented_scrollbar(controller).thumb_rect.width(), 6);
+
+ // Once it has faded out, it is thin again.
+ (void)controller.advance_fades(visual_context_tree, scroll_state_snapshot, shown_until + Compositing::OverlayFade::duration);
+ EXPECT_EQ(opacity_of_scrollbar(controller), 0);
+ EXPECT_EQ(presented_scrollbar(controller).thumb_rect.width(), 2);
+ EXPECT(!controller.fades_need_frame(shown_until + Compositing::OverlayFade::duration));
+}
+
+TEST_CASE(scrolling_expanded_scrollbar_the_display_list_paints_thin_damages_it)
+{
+ Compositor::ScrollbarController controller;
+ controller.set_scrollbars({ scrollbar_painted_by_display_list() });
+ Compositing::ScrollStateSnapshot scroll_state_snapshot;
+ auto visual_context_tree = visual_context_tree_with_scroller();
+ auto start = MonotonicTime::now();
+ (void)controller.update_visibility(&visual_context_tree, scroll_state_snapshot, start);
+ scroll_state_snapshot.set_device_offset_for_index(Compositing::SpatialNodeIndex { 1 }, { 0, 5 });
+ (void)controller.update_visibility(&visual_context_tree, scroll_state_snapshot, start);
+ controller.set_scrollbars({ scrollbar_painted_by_display_list(true) });
+ (void)controller.update_visibility(&visual_context_tree, scroll_state_snapshot, start);
+ (void)controller.advance_fades(visual_context_tree, scroll_state_snapshot, start + Compositing::OverlayFade::duration);
+
+ // While the display list paints the scrollbar expanded, it damages it on a scroll itself.
+ scroll_state_snapshot.set_device_offset_for_index(Compositing::SpatialNodeIndex { 1 }, { 0, 10 });
+ EXPECT(controller.update_visibility(&visual_context_tree, scroll_state_snapshot, start).is_empty());
+
+ controller.set_scrollbars({ scrollbar_painted_by_display_list(false) });
+ scroll_state_snapshot.set_device_offset_for_index(Compositing::SpatialNodeIndex { 1 }, { 0, 20 });
+ auto damage_rect = controller.update_visibility(&visual_context_tree, scroll_state_snapshot, start);
+ EXPECT(damage_rect.contains(Gfx::IntRect { 92, 0, 8, 100 }));
+}