Skip to content

Truly independent web browser

Compositor: Hide overlay scrollbars until they are used

Andreas Kling2751d4ca18db
diff --git a/Libraries/LibCompositing/OverlayFade.h b/Libraries/LibCompositing/OverlayFade.h
index 93df8d8f6b6..9a1cc2ca828 100644
--- a/Libraries/LibCompositing/OverlayFade.h
+++ b/Libraries/LibCompositing/OverlayFade.h
@@ -26,6 +26,15 @@ public:
m_started_at = now;
}
+ // Ends any fade, with the opacity at once where it would end.
+ void jump_to(float opacity)
+ {
+ m_opacity = opacity;
+ m_initial_opacity = opacity;
+ m_target_opacity = opacity;
+ m_started_at.clear();
+ }
+
// Returns whether the opacity changed.
bool advance_to(MonotonicTime frame_time)
{
diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cpp
index f29aa4f8dd9..55e7eff2486 100644
--- a/Services/Compositor/CompositorState.cpp
+++ b/Services/Compositor/CompositorState.cpp
@@ -192,9 +192,14 @@ void CompositorState::create_context(Web::CompositorContextId context_id, Option
VERIFY(context_id == Web::compositor_context_id_for_page(*page_id));
auto& context = *m_contexts.ensure(context_id, [&] {
- return make<ContextState>(context_id, page_id, web_content_client, Compositing::OwnedCanvasSurfaces { m_canvas_surface_registry, web_content_client.canvas_surface_owner_id() }, [this, context_id](Gfx::IntRect damage_rect) {
- schedule_caret_repaint(context_id, damage_rect);
- });
+ auto schedule_caret_repaint = [this, context_id](Gfx::IntRect damage_rect) {
+ this->schedule_caret_repaint(context_id, damage_rect);
+ };
+ auto request_scrollbar_fade_frames = [this, context_id] {
+ if (auto* context = context_if_present(context_id))
+ schedule_animation_frames_if_needed(*context);
+ };
+ return make<ContextState>(context_id, page_id, web_content_client, Compositing::OwnedCanvasSurfaces { m_canvas_surface_registry, web_content_client.canvas_surface_owner_id() }, move(schedule_caret_repaint), move(request_scrollbar_fade_frames));
});
resize_backing_stores_if_needed(context_id, context);
}
@@ -1216,6 +1221,8 @@ void CompositorState::present_pending_frames_on_vsync(Optional<u64> display_id,
context.presenter().queue_present_frame(PendingFrame::repainting_changes(*rebound_frame));
if (auto fade_frame = context.presenter().advance_ui_overlay_fades(frame_time); fade_frame.has_value())
context.presenter().queue_present_frame(*fade_frame);
+ if (auto fade_frame = context.advance_scrollbar_fades(frame_time); fade_frame.has_value())
+ context.presenter().queue_present_frame(*fade_frame);
}
// A render clock tick goes to the process's render clock thread, beside what its main thread is doing, with
@@ -1264,7 +1271,8 @@ bool CompositorState::request_screenshot(Web::CompositorContextId context_id, Gf
return false;
auto composited_context_resolver = resolver_for(context_id);
- context->paint_screenshot(*m_display_list_player, target_bitmap, &composited_context_resolver);
+ if (auto scrollbar_fade_frame = context->paint_screenshot(*m_display_list_player, target_bitmap, &composited_context_resolver); scrollbar_fade_frame.has_value())
+ schedule_present_frame(context_id, *context, *scrollbar_fade_frame);
return true;
}
diff --git a/Services/Compositor/ContextPresenter.cpp b/Services/Compositor/ContextPresenter.cpp
index dd9450f1346..f397119ead6 100644
--- a/Services/Compositor/ContextPresenter.cpp
+++ b/Services/Compositor/ContextPresenter.cpp
@@ -536,7 +536,8 @@ void ContextPresenter::paint_current_display_list(DisplayListPlayerSkia& display
frame_content.presented_scroll_state_snapshot,
target_surface,
&m_canvas_surfaces,
- frame_content.composited_context_resolver);
+ frame_content.composited_context_resolver,
+ &frame_content.scrollbar_controller);
frame_content.scrollbar_controller.paint(target_surface, display_list_player, frame_content.presented_scroll_state_snapshot);
if (paint_ui_overlay == PaintUIOverlay::No)
return;
diff --git a/Services/Compositor/ContextState.cpp b/Services/Compositor/ContextState.cpp
index 6aaefb03e07..417fbd0bae5 100644
--- a/Services/Compositor/ContextState.cpp
+++ b/Services/Compositor/ContextState.cpp
@@ -47,16 +47,23 @@ static void clamp_visual_viewport_transform_to_viewport(Compositing::TransformWi
transform.matrix[1, 3] = clamp(transform.matrix[1, 3], min_y, 0.0f);
}
-ContextState::ContextState(Web::CompositorContextId context_id, Optional<u64> page_id, CompositorStateWebContentClient& web_content_client, Compositing::OwnedCanvasSurfaces canvas_surfaces, Function<void(Gfx::IntRect)> schedule_caret_repaint)
+ContextState::ContextState(Web::CompositorContextId context_id, Optional<u64> page_id, CompositorStateWebContentClient& web_content_client, Compositing::OwnedCanvasSurfaces canvas_surfaces, Function<void(Gfx::IntRect)> schedule_caret_repaint, Function<void()> request_scrollbar_fade_frames)
: m_web_content_client(web_content_client)
, m_context_id(context_id)
, m_page_id(page_id)
, m_presenter(m_scene, canvas_surfaces, page_id.has_value())
, m_caret_blink_timer([this] { return damage_rect_of_blinking_carets(); }, move(schedule_caret_repaint))
+ , m_request_scrollbar_fade_frames(move(request_scrollbar_fade_frames))
{
}
-ContextState::~ContextState() = default;
+ContextState::~ContextState()
+{
+ if (m_scrollbar_fade_out_timer) {
+ m_scrollbar_fade_out_timer->on_timeout = {};
+ m_scrollbar_fade_out_timer->stop();
+ }
+}
bool ContextState::is_owned_by(CompositorStateWebContentClient const& web_content_client) const
{
@@ -1526,8 +1533,46 @@ 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()
+{
+ 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());
+
+ // 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;
+ }
+ 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());
+ m_scrollbar_fade_out_timer->restart(delay);
+}
+
+Optional<PendingFrame> ContextState::advance_scrollbar_fades(MonotonicTime frame_time)
+{
+ if (!m_scene.visual_context_tree().has_value())
+ return {};
+ 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;
+ auto damage_rect = m_scrollbar_controller.advance_fades(m_scene.visual_context_tree_for_compositing(), presented_scroll_state_snapshot, frame_time);
+ auto viewport_rect = m_presenter.viewport_rect_for_ui_overlay();
+ if (damage_rect.is_empty() || !viewport_rect.has_value())
+ return {};
+ return PendingFrame { *viewport_rect, damage_rect };
+}
+
Optional<PreparedFrame> ContextState::prepare_frame(DisplayListPlayerSkia& display_list_player, PendingFrame pending_frame, CompositedContextResolver const* composited_context_resolver)
{
+ 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));
@@ -1535,21 +1580,27 @@ Optional<PreparedFrame> ContextState::prepare_frame(DisplayListPlayerSkia& displ
bool ContextState::present_synchronously(DisplayListPlayerSkia& display_list_player, CompositedContextResolver const* composited_context_resolver)
{
+ 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.present_synchronously(display_list_player, frame_content(presented_scroll_state_snapshot, composited_context_resolver));
}
-void ContextState::paint_screenshot(DisplayListPlayerSkia& display_list_player, Gfx::ShareableBitmap& target_bitmap, CompositedContextResolver const* composited_context_resolver)
+Optional<PendingFrame> ContextState::paint_screenshot(DisplayListPlayerSkia& display_list_player, Gfx::ShareableBitmap& target_bitmap, CompositedContextResolver const* composited_context_resolver)
{
VERIFY(can_paint_screenshot(target_bitmap));
- // Screenshot requests can arrive between display ticks, so sample animations at capture time.
- advance_visual_animations(MonotonicTime::now());
+ // Screenshot requests can arrive between display ticks, so sample animations and scrollbar visibility at capture
+ // time.
+ auto capture_time = MonotonicTime::now();
+ advance_visual_animations(capture_time);
+ update_scrollbar_visibility();
+ auto scrollbar_fade_frame = advance_scrollbar_fades(capture_time);
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_presenter.paint_screenshot(display_list_player, target_bitmap, frame_content(presented_scroll_state_snapshot, composited_context_resolver));
+ return scrollbar_fade_frame;
}
bool ContextState::can_paint_screenshot(Gfx::ShareableBitmap& target_bitmap) const
diff --git a/Services/Compositor/ContextState.h b/Services/Compositor/ContextState.h
index 17ad415ac5a..64b91d041a0 100644
--- a/Services/Compositor/ContextState.h
+++ b/Services/Compositor/ContextState.h
@@ -124,7 +124,7 @@ public:
Optional<Compositing::NestedContextWheelHit> nested_context_hit {};
};
- ContextState(Web::CompositorContextId, Optional<u64> page_id, CompositorStateWebContentClient&, Compositing::OwnedCanvasSurfaces, Function<void(Gfx::IntRect)> schedule_caret_repaint = {});
+ ContextState(Web::CompositorContextId, Optional<u64> page_id, CompositorStateWebContentClient&, Compositing::OwnedCanvasSurfaces, Function<void(Gfx::IntRect)> schedule_caret_repaint = {}, Function<void()> request_scrollbar_fade_frames = {});
~ContextState();
bool is_owned_by(CompositorStateWebContentClient const&) const;
@@ -194,7 +194,7 @@ public:
AsyncScrollResult smooth_scroll_to(Web::AsyncScrollNodeStableID, Gfx::FloatPoint offset, Gfx::FloatPoint main_thread_offset, Gfx::IntRect viewport_rect, Compositing::ScrollAnimationKind, Compositing::SmoothScrollInitiator, Optional<Compositing::AsyncScrollOperationID> operation_to_continue);
void cancel_smooth_scroll(Web::AsyncScrollNodeStableID);
Optional<Gfx::IntRect> advance_smooth_scroll_animations(MonotonicTime now);
- bool needs_animation_frames() { return has_active_smooth_scroll_animations() || has_active_scroll_fling() || m_elastic_overscroll_controller.has_rebound() || visual_animations_need_frame() || m_presenter.ui_overlay_fades_need_frame(); }
+ bool needs_animation_frames() { return has_active_smooth_scroll_animations() || has_active_scroll_fling() || m_elastic_overscroll_controller.has_rebound() || visual_animations_need_frame() || m_presenter.ui_overlay_fades_need_frame() || m_scrollbar_controller.fades_need_frame(MonotonicTime::now()); }
bool has_active_scroll_fling() const { return m_scroll_fling_controller.is_active(); }
// The wheel event of the next step of the fling, which is scrolled by like a wheel event from the UI.
@@ -209,6 +209,7 @@ public:
Optional<Web::MouseEvent> interrupt_scroll_fling(Web::MouseEvent const&, Optional<MonotonicTime> now_for_testing = {});
void set_scrolls_elastically(bool);
Optional<Gfx::IntRect> advance_elastic_overscroll_rebounds(MonotonicTime frame_time);
+ Optional<PendingFrame> advance_scrollbar_fades(MonotonicTime frame_time);
// The installed scrolling state, its routing admission, and how far the given scroller is stretched, as JSON.
String scroll_state_for_testing(Web::AsyncScrollNodeStableID stretched_scroller) const;
Web::InputTraceRecorder& input_trace() { return m_input_trace; }
@@ -247,7 +248,8 @@ public:
Optional<PreparedFrame> prepare_frame(DisplayListPlayerSkia&, PendingFrame, CompositedContextResolver const*);
bool present_synchronously(DisplayListPlayerSkia&, CompositedContextResolver const*);
bool can_paint_screenshot(Gfx::ShareableBitmap&) const;
- void paint_screenshot(DisplayListPlayerSkia&, Gfx::ShareableBitmap&, CompositedContextResolver const*);
+ // Returns the scrollbar fade damage that the screenshot did not present on screen.
+ Optional<PendingFrame> paint_screenshot(DisplayListPlayerSkia&, Gfx::ShareableBitmap&, CompositedContextResolver const*);
private:
struct ActiveSmoothScrollAnimation {
@@ -331,6 +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();
CompositorStateWebContentClient& m_web_content_client;
Web::CompositorContextId m_context_id;
@@ -357,6 +360,8 @@ private:
bool m_has_async_scrolling_state { false };
CaretBlinkTimer m_caret_blink_timer;
+ Function<void()> m_request_scrollbar_fade_frames;
+ RefPtr<Core::Timer> m_scrollbar_fade_out_timer;
Web::InputTraceRecorder m_input_trace;
Optional<u64> m_display_id;
double m_display_refresh_rate { 60.0 };
diff --git a/Services/Compositor/DisplayListPlayerSkia.cpp b/Services/Compositor/DisplayListPlayerSkia.cpp
index b5935f6ace4..faed192d2dc 100644
--- a/Services/Compositor/DisplayListPlayerSkia.cpp
+++ b/Services/Compositor/DisplayListPlayerSkia.cpp
@@ -35,6 +35,7 @@
#include <Compositor/DisplayListPlayerSkia.h>
#include <Compositor/DisplayListRasterCache.h>
+#include <Compositor/ScrollbarController.h>
#include <LibCompositing/DisplayList/CanvasSurfaceRegistry.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/ColorSpace.h>
@@ -88,9 +89,11 @@ void DisplayListPlayerSkia::execute(
ScrollStateSnapshot const& scroll_state_snapshot,
RefPtr<Gfx::PaintingSurface> surface,
OwnedCanvasSurfaces const* canvas_surfaces,
- CompositedContextResolver const* composited_context_resolver)
+ CompositedContextResolver const* composited_context_resolver,
+ ScrollbarController const* scrollbar_controller)
{
TemporaryChange composited_context_resolver_change { m_composited_context_resolver, composited_context_resolver };
+ TemporaryChange scrollbar_controller_change { m_scrollbar_controller, scrollbar_controller };
TemporaryChange raster_cache_change { m_raster_cache, &raster_cache };
if (m_layer_image_filter_cache->tree_structural_epoch != visual_context_tree.structural_epoch()) {
m_layer_image_filter_cache->entries_by_tree_structural_epoch_and_effect.clear();
@@ -206,8 +209,11 @@ static Gfx::IntRect thumb_rect_kept_inside_track(PaintScrollBar const& command)
return thumb_rect;
}
-static void paint_scrollbar_into_surface(Gfx::PaintingSurface& surface, PaintScrollBar const& command, Optional<SkRRect> const& corner_clip = {})
+static void paint_scrollbar_into_surface(Gfx::PaintingSurface& surface, PaintScrollBar const& command, float opacity, Optional<SkRRect> const& corner_clip = {})
{
+ if (opacity <= 0)
+ return;
+
auto gutter_rect = to_skia_rect(command.gutter_rect);
auto thumb_rect = to_skia_rect(thumb_rect_kept_inside_track(command));
@@ -219,6 +225,7 @@ static void paint_scrollbar_into_surface(Gfx::PaintingSurface& surface, PaintScr
auto gutter_fill_color = command.track_color;
SkPaint gutter_fill_paint;
gutter_fill_paint.setColor(to_skia_color(gutter_fill_color));
+ gutter_fill_paint.setAlphaf(gutter_fill_paint.getAlphaf() * opacity);
canvas.drawRect(gutter_rect, gutter_fill_paint);
if (thumb_rect.isEmpty())
@@ -242,6 +249,7 @@ 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.setAlphaf(thumb_fill_paint.getAlphaf() * opacity);
thumb_fill_paint.setAntiAlias(clipped_thumb.has_value());
draw_thumb(thumb_fill_paint);
@@ -251,17 +259,18 @@ static void paint_scrollbar_into_surface(Gfx::PaintingSurface& surface, PaintScr
stroke_paint.setStrokeWidth(1);
stroke_paint.setAntiAlias(true);
stroke_paint.setColor(to_skia_color(stroke_color));
+ stroke_paint.setAlphaf(stroke_paint.getAlphaf() * opacity);
draw_thumb(stroke_paint);
}
-void DisplayListPlayerSkia::paint_scrollbar(Gfx::PaintingSurface& surface, PaintScrollBar const& command, Gfx::IntRect const& corner_clip_rect, int corner_clip_radius)
+void DisplayListPlayerSkia::paint_scrollbar(Gfx::PaintingSurface& surface, PaintScrollBar const& command, float opacity, 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, corner_clip);
+ paint_scrollbar_into_surface(surface, command, opacity, corner_clip);
}
static SkMatrix vertical_glyph_run_rotation(Gfx::IntRect const& rect)
@@ -1505,7 +1514,8 @@ void DisplayListPlayerSkia::play_command(CompositorNestedContextWheelRegion cons
void DisplayListPlayerSkia::play_command(PaintScrollBar const& command)
{
- paint_scrollbar_into_surface(surface(), 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);
}
void DisplayListPlayerSkia::push_clip(ReplayClip const& clip)
diff --git a/Services/Compositor/DisplayListPlayerSkia.h b/Services/Compositor/DisplayListPlayerSkia.h
index b8b87d8efb5..cd5fa33a535 100644
--- a/Services/Compositor/DisplayListPlayerSkia.h
+++ b/Services/Compositor/DisplayListPlayerSkia.h
@@ -27,6 +27,8 @@ 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();
@@ -47,12 +49,13 @@ public:
Compositing::ScrollStateSnapshot const&,
RefPtr<Gfx::PaintingSurface>,
Compositing::OwnedCanvasSurfaces const*,
- Compositing::CompositedContextResolver const*);
+ Compositing::CompositedContextResolver const*,
+ ScrollbarController const* = nullptr);
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&, Gfx::IntRect const& corner_clip_rect = {}, int corner_clip_radius = 0);
+ void paint_scrollbar(Gfx::PaintingSurface&, Compositing::PaintScrollBar const&, float opacity, Gfx::IntRect const& corner_clip_rect = {}, int corner_clip_radius = 0);
private:
#define DECLARE_PLAY_COMMAND(command_type) \
@@ -85,6 +88,8 @@ private:
RefPtr<Gfx::SkiaBackendContext> m_skia_backend_context;
Compositing::CompositedContextResolver const* m_composited_context_resolver { nullptr };
+ // Says how opaque the scrollbars that the display list paints are.
+ ScrollbarController const* m_scrollbar_controller { nullptr };
DisplayListRasterCache* m_raster_cache { nullptr };
DisplayListRasterCache& raster_cache() const { return *m_raster_cache; }
diff --git a/Services/Compositor/ScrollbarController.cpp b/Services/Compositor/ScrollbarController.cpp
index 4eda4d41703..c187ed6a7d3 100644
--- a/Services/Compositor/ScrollbarController.cpp
+++ b/Services/Compositor/ScrollbarController.cpp
@@ -111,6 +111,7 @@ static Gfx::IntRect scrollbar_hit_rect(Compositing::AsyncScrollbar const& scroll
void ScrollbarController::clear()
{
m_scrollbars.clear();
+ m_visibilities.clear();
m_hovered_scrollbar_index.clear();
m_captured_scrollbar_index.clear();
m_thumb_grab_position = 0;
@@ -122,7 +123,15 @@ void ScrollbarController::set_scrollbars(Vector<Compositing::AsyncScrollbar> con
auto hovered_scrollbar_identity = scrollbar_identity_at(m_scrollbars, m_hovered_scrollbar_index);
auto captured_scrollbar_identity = scrollbar_identity_at(m_scrollbars, m_captured_scrollbar_index);
+ Vector<Visibility> visibilities;
+ visibilities.ensure_capacity(scrollbars.size());
+ for (auto const& scrollbar : scrollbars) {
+ auto previous_index = find_scrollbar_index(m_scrollbars, scrollbar_identity(scrollbar));
+ visibilities.unchecked_append(previous_index.has_value() ? m_visibilities[*previous_index] : Visibility {});
+ }
+
m_scrollbars = scrollbars;
+ m_visibilities = move(visibilities);
m_hovered_scrollbar_index = hovered_scrollbar_identity.has_value() ? find_scrollbar_index(m_scrollbars, *hovered_scrollbar_identity) : Optional<size_t> {};
m_captured_scrollbar_index = captured_scrollbar_identity.has_value() ? find_scrollbar_index(m_scrollbars, *captured_scrollbar_identity) : Optional<size_t> {};
if (!m_captured_scrollbar_index.has_value())
@@ -301,6 +310,85 @@ 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)
+{
+ 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 scrolled = visibility.scroll_offset.has_value() && *visibility.scroll_offset != scroll_offset;
+ visibility.scroll_offset = scroll_offset;
+
+ auto was_held_shown = visibility.is_held_shown;
+ visibility.is_held_shown = is_held_shown(i);
+ if (visibility.is_held_shown) {
+ visibility.fade.jump_to(1);
+ visibility.fade_out_time.clear();
+ continue;
+ }
+ if (scrolled)
+ visibility.fade.jump_to(1);
+ if (scrolled || was_held_shown)
+ visibility.fade_out_time = now + shown_duration_after_activity;
+ }
+}
+
+Optional<MonotonicTime> ScrollbarController::next_fade_out_time() const
+{
+ Optional<MonotonicTime> next_fade_out_time;
+ for (auto const& visibility : m_visibilities) {
+ if (visibility.fade_out_time.has_value() && (!next_fade_out_time.has_value() || *visibility.fade_out_time < *next_fade_out_time))
+ next_fade_out_time = visibility.fade_out_time;
+ }
+ return next_fade_out_time;
+}
+
+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))
+ return true;
+ }
+ return false;
+}
+
+Gfx::IntRect ScrollbarController::advance_fades(Compositing::AccumulatedVisualContextTree const& visual_context_tree, Compositing::ScrollStateSnapshot const& scroll_state_snapshot, MonotonicTime frame_time)
+{
+ Gfx::IntRect damage_rect;
+ for (size_t i = 0; i < m_scrollbars.size(); ++i) {
+ auto& visibility = m_visibilities[i];
+ if (visibility.fade_out_time.has_value() && *visibility.fade_out_time <= frame_time) {
+ visibility.fade_out_time.clear();
+ visibility.fade.fade_to(0, frame_time);
+ }
+ if (!visibility.fade.advance_to(frame_time))
+ 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));
+ }
+ return damage_rect;
+}
+
+float ScrollbarController::opacity_of_scrollbar_painted_by_display_list(Compositing::SpatialNodeIndex scroll_node_index, bool vertical) 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 1;
+}
+
bool ScrollbarController::paint(Gfx::PaintingSurface& surface, DisplayListPlayerSkia& display_list_player, Compositing::ScrollStateSnapshot const& scroll_state_snapshot) const
{
bool painted_a_scrollbar = false;
@@ -308,6 +396,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)
+ continue;
auto expanded = is_expanded(i);
Compositing::PaintScrollBar paint_scrollbar {
.scroll_node_index = scrollbar.scroll_node_index,
@@ -319,7 +410,7 @@ bool ScrollbarController::paint(Gfx::PaintingSurface& surface, DisplayListPlayer
.track_color = scrollbar.track_color,
.vertical = scrollbar.vertical,
};
- display_list_player.paint_scrollbar(surface, paint_scrollbar, scrollbar.corner_clip_rect, scrollbar.corner_clip_radius);
+ display_list_player.paint_scrollbar(surface, paint_scrollbar, scrollbar_opacity, scrollbar.corner_clip_rect, scrollbar.corner_clip_radius);
painted_a_scrollbar = true;
}
return painted_a_scrollbar;
@@ -330,4 +421,20 @@ bool ScrollbarController::is_expanded(size_t scrollbar_index) const
return m_hovered_scrollbar_index == scrollbar_index || m_captured_scrollbar_index == scrollbar_index;
}
+// The main thread hovers and drags the scrollbars the display list paints, and paints them enlarged meanwhile.
+bool ScrollbarController::is_held_shown(size_t scrollbar_index) const
+{
+ auto const& scrollbar = m_scrollbars[scrollbar_index];
+ if (scrollbar.is_painted_by_compositor)
+ return is_expanded(scrollbar_index);
+ return scrollbar.display_list_paints_enlarged_scrollbar || m_captured_scrollbar_index == scrollbar_index;
+}
+
+float ScrollbarController::opacity(size_t scrollbar_index) const
+{
+ if (s_scrollbars_always_shown)
+ return 1;
+ return m_visibilities[scrollbar_index].fade.opacity();
+}
+
}
diff --git a/Services/Compositor/ScrollbarController.h b/Services/Compositor/ScrollbarController.h
index 4aba34f0b8e..0e0b63c9186 100644
--- a/Services/Compositor/ScrollbarController.h
+++ b/Services/Compositor/ScrollbarController.h
@@ -7,8 +7,10 @@
#pragma once
#include <AK/Optional.h>
+#include <AK/Time.h>
#include <AK/Vector.h>
#include <Compositor/Forward.h>
+#include <LibCompositing/OverlayFade.h>
#include <LibCompositing/Scrolling/AsyncScrollingState.h>
#include <LibGfx/Forward.h>
#include <LibGfx/Point.h>
@@ -41,9 +43,26 @@ 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.
+ 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.
+ static void show_scrollbars_always_for_testing() { s_scrollbars_always_shown = true; }
+
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);
+ // 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.
+ 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;
+
bool is_empty() const { return m_scrollbars.is_empty(); }
Vector<Compositing::AsyncScrollbar> const& scrollbars() const { return m_scrollbars; }
bool has_captured_scrollbar() const { return m_captured_scrollbar_index.has_value(); }
@@ -60,15 +79,29 @@ public:
bool paint(Gfx::PaintingSurface&, DisplayListPlayerSkia&, Compositing::ScrollStateSnapshot const&) const;
private:
+ struct Visibility {
+ Compositing::OverlayFade fade;
+ // The scroll offset of the scroller as of the last update, which a scroll changes.
+ Optional<Gfx::FloatPoint> scroll_offset;
+ bool is_held_shown { false };
+ Optional<MonotonicTime> fade_out_time;
+ };
+
bool is_expanded(size_t scrollbar_index) const;
+ bool is_held_shown(size_t scrollbar_index) const;
+ float opacity(size_t scrollbar_index) 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);
Vector<Compositing::AsyncScrollbar> m_scrollbars;
+ // One for each scrollbar, in the same order.
+ Vector<Visibility> m_visibilities;
Optional<size_t> m_hovered_scrollbar_index;
Optional<size_t> m_captured_scrollbar_index;
float m_thumb_grab_position { 0 };
float m_last_primary_position_of_captured_drag { 0 };
+
+ static inline bool s_scrollbars_always_shown { false };
};
}
diff --git a/Services/Compositor/main.cpp b/Services/Compositor/main.cpp
index 4619a7a5fda..41954dd0d0d 100644
--- a/Services/Compositor/main.cpp
+++ b/Services/Compositor/main.cpp
@@ -7,6 +7,7 @@
#include <Compositor/ConnectionFromClient.h>
#include <Compositor/FontSkia.h>
#include <Compositor/Sandbox.h>
+#include <Compositor/ScrollbarController.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/CrashHandler.h>
#include <LibCore/EventLoop.h>
@@ -67,8 +68,10 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
if (auto result = Core::Platform::set_current_thread_qos(Core::Platform::ThreadQoS::UserInteractive); result.is_error())
warnln("Could not set main thread QoS: {}", result.error());
- if (enable_test_mode)
+ if (enable_test_mode) {
Compositor::force_font_hinting_for_testing();
+ Compositor::ScrollbarController::show_scrollbars_always_for_testing();
+ }
Core::ResourceImplementation::install(make<Core::ResourceImplementationFile>(TRY(String::from_utf8(resource_root))));
if (force_fontconfig)
diff --git a/Tests/Compositor/TestScrollbarController.cpp b/Tests/Compositor/TestScrollbarController.cpp
index f1ddb246e8e..284dd78ee47 100644
--- a/Tests/Compositor/TestScrollbarController.cpp
+++ b/Tests/Compositor/TestScrollbarController.cpp
@@ -107,3 +107,143 @@ TEST_CASE(starting_scrollbar_drag_uses_expanded_thumb_geometry)
auto horizontal_drag = begin_scrollbar_drag(Gfx::Orientation::Horizontal, { 25, 97 }, Gfx::IntRect { 18, 94, 24, 6 });
EXPECT_EQ(horizontal_drag.thumb_grab_position, 7);
}
+
+static Compositing::AsyncScrollbar scrollbar_painted_by_display_list(bool display_list_paints_enlarged_scrollbar = false)
+{
+ return {
+ .scroll_node_id = {
+ .document_id = Web::UniqueNodeID { 1 },
+ .scroll_node_index = Compositing::SpatialNodeIndex { 1 },
+ },
+ .scroller_stable_node_id = Web::AsyncScrollNodeStableID {
+ .node_id = Web::UniqueNodeID { 2 },
+ .kind = Web::AsyncScrollNodeKind::Element,
+ .pseudo_element_type = 0,
+ },
+ .scroll_node_index = Compositing::SpatialNodeIndex { 1 },
+ .context = {},
+ .paint_order_index = 0,
+ .gutter_rect = {},
+ .thumb_rect = { 98, 20, 2, 20 },
+ .track_rect = { 96, 0, 4, 100 },
+ .expanded_gutter_rect = { 92, 0, 8, 100 },
+ .expanded_thumb_rect = { 94, 20, 6, 20 },
+ .corner_clip_rect = {},
+ .corner_clip_radius = 0,
+ .scroll_size = 0.8,
+ .expanded_scroll_size = 0.8,
+ .min_scroll_offset = 0,
+ .max_scroll_offset = 100,
+ .thumb_color = Gfx::Color::Black,
+ .track_color = Gfx::Color::Transparent,
+ .vertical = true,
+ .is_painted_by_compositor = false,
+ .display_list_paints_enlarged_scrollbar = display_list_paints_enlarged_scrollbar,
+ };
+}
+
+static float opacity_of_scrollbar(Compositor::ScrollbarController const& controller)
+{
+ return controller.opacity_of_scrollbar_painted_by_display_list(Compositing::SpatialNodeIndex { 1 }, true);
+}
+
+static auto const half_fade_duration = AK::Duration::from_milliseconds(Compositing::OverlayFade::duration.to_milliseconds() / 2);
+
+static Compositing::AccumulatedVisualContextTree visual_context_tree_with_scroller()
+{
+ Compositing::VisualContextTreeTestBuilder visual_context_tree_builder;
+ visual_context_tree_builder.append_scroll(Compositing::VISUAL_VIEWPORT_NODE_INDEX);
+ return visual_context_tree_builder.finish();
+}
+
+TEST_CASE(scrollbar_is_hidden_until_its_scroller_scrolls)
+{
+ Compositor::ScrollbarController controller;
+ controller.set_scrollbars({ scrollbar_painted_by_display_list() });
+ Compositing::ScrollStateSnapshot scroll_state_snapshot;
+ auto start = MonotonicTime::now();
+
+ controller.update_visibility(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);
+ EXPECT_EQ(opacity_of_scrollbar(controller), 1);
+ EXPECT(controller.next_fade_out_time() == start + Compositor::ScrollbarController::shown_duration_after_activity);
+}
+
+TEST_CASE(scrollbar_fades_out_after_its_scroller_stops_scrolling)
+{
+ 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();
+ controller.update_visibility(scroll_state_snapshot, start);
+ scroll_state_snapshot.set_device_offset_for_index(Compositing::SpatialNodeIndex { 1 }, { 0, 10 });
+ controller.update_visibility(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)));
+ EXPECT(controller.fades_need_frame(shown_until));
+
+ (void)controller.advance_fades(visual_context_tree, scroll_state_snapshot, shown_until);
+ EXPECT_EQ(opacity_of_scrollbar(controller), 1);
+ EXPECT(!controller.next_fade_out_time().has_value());
+
+ auto damage_rect = controller.advance_fades(visual_context_tree, scroll_state_snapshot, shown_until + half_fade_duration);
+ EXPECT(opacity_of_scrollbar(controller) > 0);
+ EXPECT(opacity_of_scrollbar(controller) < 1);
+ EXPECT(damage_rect.contains(Gfx::IntRect { 92, 0, 8, 100 }));
+
+ (void)controller.advance_fades(visual_context_tree, scroll_state_snapshot, shown_until + Compositing::OverlayFade::duration);
+ EXPECT_EQ(opacity_of_scrollbar(controller), 0);
+ EXPECT(!controller.fades_need_frame(shown_until + Compositing::OverlayFade::duration));
+}
+
+TEST_CASE(scroll_while_fading_out_shows_scrollbar_again)
+{
+ 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();
+ controller.update_visibility(scroll_state_snapshot, start);
+ scroll_state_snapshot.set_device_offset_for_index(Compositing::SpatialNodeIndex { 1 }, { 0, 10 });
+ controller.update_visibility(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);
+ (void)controller.advance_fades(visual_context_tree, scroll_state_snapshot, shown_until + half_fade_duration);
+ EXPECT(opacity_of_scrollbar(controller) < 1);
+
+ 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);
+ 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)
+{
+ Compositor::ScrollbarController controller;
+ controller.set_scrollbars({ scrollbar_painted_by_display_list(true) });
+ Compositing::ScrollStateSnapshot scroll_state_snapshot;
+ auto start = MonotonicTime::now();
+
+ controller.update_visibility(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);
+ 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);
+ EXPECT_EQ(opacity_of_scrollbar(controller), 1);
+ EXPECT(controller.next_fade_out_time() == much_later + Compositor::ScrollbarController::shown_duration_after_activity);
+}