Truly independent web browser
LibCompositing+Compositor: Draw Picture-in-Picture window controls
Luke Wildeda477de78772
diff --git a/Libraries/LibCompositing/Forward.h b/Libraries/LibCompositing/Forward.hindex 8935e1ced95..30eacea266a 100644--- a/Libraries/LibCompositing/Forward.h+++ b/Libraries/LibCompositing/Forward.h@@ -15,6 +15,8 @@ namespace Compositing {enum class PausedDebuggerOverlayAction : u8;struct PausedDebuggerOverlayState;+enum class PictureInPictureControl : u8;+struct PictureInPictureControlsState;struct UIOverlays;class AccumulatedVisualContextTree;diff --git a/Libraries/LibCompositing/OverlayFade.h b/Libraries/LibCompositing/OverlayFade.hnew file mode 100644index 00000000000..93df8d8f6b6--- /dev/null+++ b/Libraries/LibCompositing/OverlayFade.h@@ -0,0 +1,64 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/Optional.h>+#include <AK/StdLibExtras.h>+#include <AK/Time.h>++namespace Compositing {++// A fade that turns back from whatever opacity it has reached.+class OverlayFade {+public:+ static constexpr auto duration = AK::Duration::from_milliseconds(150);++ void fade_to(float target_opacity, MonotonicTime now)+ {+ if (target_opacity == m_target_opacity)+ return;+ m_initial_opacity = m_opacity;+ m_target_opacity = target_opacity;+ m_started_at = now;+ }++ // Returns whether the opacity changed.+ bool advance_to(MonotonicTime frame_time)+ {+ auto opacity = opacity_at(frame_time);+ if (opacity == m_opacity)+ return false;+ m_opacity = opacity;+ return true;+ }++ float opacity() const { return m_opacity; }+ bool is_fading() const { return m_opacity != m_target_opacity; }++private:+ float opacity_at(MonotonicTime frame_time) const+ {+ if (!m_started_at.has_value())+ return m_target_opacity;++ auto progress = (frame_time - *m_started_at).to_seconds_f64() / duration.to_seconds_f64();+ if (progress >= 1)+ return m_target_opacity;+ progress = max(progress, 0.0);++ // Smoothstep, so that the fade starts and ends gently.+ auto eased_progress = static_cast<float>(progress * progress * (3 - 2 * progress));+ return m_initial_opacity + (m_target_opacity - m_initial_opacity) * eased_progress;+ }++ float m_opacity { 0 };+ float m_initial_opacity { 0 };+ float m_target_opacity { 0 };+ Optional<MonotonicTime> m_started_at;+};++}diff --git a/Libraries/LibCompositing/OverlayGeometry.h b/Libraries/LibCompositing/OverlayGeometry.hindex c53779e17cd..93535cec19d 100644--- a/Libraries/LibCompositing/OverlayGeometry.h+++ b/Libraries/LibCompositing/OverlayGeometry.h@@ -19,20 +19,47 @@ inline int scaled_overlay_length(int length, double device_pixel_ratio)return max(1, static_cast<int>(round(length * device_pixel_ratio)));}+enum class OverlayButtonShape : u8 {+ Rectangle,+ Circle,+};+template<typename Action>struct OverlayButton {Action action;Gfx::IntRect bounds;+ OverlayButtonShape shape { OverlayButtonShape::Rectangle };++ // A round button leaves the corners of its bounds to whatever is below it.+ bool contains(Gfx::IntPoint position) const+ {+ if (shape == OverlayButtonShape::Rectangle)+ return bounds.contains(position);++ auto radius = bounds.width() / 2.0;+ auto distance_x = position.x() + 0.5 - (bounds.x() + radius);+ auto distance_y = position.y() + 0.5 - (bounds.y() + radius);+ return distance_x * distance_x + distance_y * distance_y <= radius * radius;+ }};template<typename Action, size_t button_count>Optional<Action> overlay_action_at(Array<OverlayButton<Action>, button_count> const& buttons, Gfx::IntPoint position){for (auto const& button : buttons) {- if (button.bounds.contains(position))+ if (button.contains(position))return button.action;}return {};}+template<typename Action, size_t button_count>+Gfx::IntRect overlay_buttons_bounds(Array<OverlayButton<Action>, button_count> const& buttons)+{+ Gfx::IntRect bounds;+ for (auto const& button : buttons)+ bounds.unite(button.bounds);+ return bounds;+}+}diff --git a/Libraries/LibCompositing/PictureInPictureControls.h b/Libraries/LibCompositing/PictureInPictureControls.hnew file mode 100644index 00000000000..eea540d1403--- /dev/null+++ b/Libraries/LibCompositing/PictureInPictureControls.h@@ -0,0 +1,59 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/Array.h>+#include <AK/Optional.h>+#include <LibCompositing/OverlayGeometry.h>+#include <LibGfx/Rect.h>+#include <LibGfx/Size.h>++namespace Compositing {++enum class PictureInPictureControl : u8 {+ BackToTab,+ Close,+};++inline Optional<PictureInPictureControl> picture_in_picture_control_from_underlying(u8 value)+{+ switch (value) {+ case to_underlying(PictureInPictureControl::BackToTab):+ return PictureInPictureControl::BackToTab;+ case to_underlying(PictureInPictureControl::Close):+ return PictureInPictureControl::Close;+ default:+ return {};+ }+}++struct PictureInPictureControlsGeometry {+ Gfx::IntRect back_to_tab_button;+ Gfx::IntRect close_button;++ Array<OverlayButton<PictureInPictureControl>, 2> buttons() const+ {+ using Button = OverlayButton<PictureInPictureControl>;+ return {+ Button { .action = PictureInPictureControl::BackToTab, .bounds = back_to_tab_button, .shape = OverlayButtonShape::Circle },+ Button { .action = PictureInPictureControl::Close, .bounds = close_button, .shape = OverlayButtonShape::Circle },+ };+ }+};++inline PictureInPictureControlsGeometry picture_in_picture_controls_geometry(Gfx::IntSize viewport_size, double device_pixel_ratio)+{+ auto button_size = scaled_overlay_length(28, device_pixel_ratio);+ auto inset = scaled_overlay_length(8, device_pixel_ratio);++ return {+ .back_to_tab_button = { inset, inset, button_size, button_size },+ .close_button = { viewport_size.width() - inset - button_size, inset, button_size, button_size },+ };+}++}diff --git a/Libraries/LibCompositing/UIOverlays.cpp b/Libraries/LibCompositing/UIOverlays.cppindex 6b4d9cdaf6a..43a3cc745d7 100644--- a/Libraries/LibCompositing/UIOverlays.cpp+++ b/Libraries/LibCompositing/UIOverlays.cpp@@ -39,10 +39,28 @@ ErrorOr<Compositing::PausedDebuggerOverlayState> decode(Decoder& decoder)};}+template<>+ErrorOr<void> encode(Encoder& encoder, Compositing::PictureInPictureControlsState const& state)+{+ TRY(encoder.encode(state.shown));+ TRY(encoder.encode(state.hovered_control));+ return {};+}++template<>+ErrorOr<Compositing::PictureInPictureControlsState> decode(Decoder& decoder)+{+ return Compositing::PictureInPictureControlsState {+ .shown = TRY(decoder.decode<bool>()),+ .hovered_control = TRY(decode_overlay_action<Compositing::PictureInPictureControl>(decoder, Compositing::picture_in_picture_control_from_underlying)),+ };+}+template<>ErrorOr<void> encode(Encoder& encoder, Compositing::UIOverlays const& overlays){TRY(encoder.encode(overlays.paused_debugger));+ TRY(encoder.encode(overlays.picture_in_picture_controls));return {};}@@ -51,6 +69,7 @@ ErrorOr<Compositing::UIOverlays> decode(Decoder& decoder){return Compositing::UIOverlays {.paused_debugger = TRY(decoder.decode<Optional<Compositing::PausedDebuggerOverlayState>>()),+ .picture_in_picture_controls = TRY(decoder.decode<Optional<Compositing::PictureInPictureControlsState>>()),};}diff --git a/Libraries/LibCompositing/UIOverlays.h b/Libraries/LibCompositing/UIOverlays.hindex 2bdd808ed44..91df00405a6 100644--- a/Libraries/LibCompositing/UIOverlays.h+++ b/Libraries/LibCompositing/UIOverlays.h@@ -10,6 +10,7 @@#include <AK/String.h>#include <LibCompositing/Export.h>#include <LibCompositing/PausedDebuggerOverlay.h>+#include <LibCompositing/PictureInPictureControls.h>#include <LibIPC/Forward.h>#include <LibWebCommon/Page/InputEvent.h>@@ -22,10 +23,19 @@ struct PausedDebuggerOverlayState {bool operator==(PausedDebuggerOverlayState const&) const = default;};-// What the browser draws over a context's page, on top of anything the page draws, with each overlay that is shown+// The controls stay present while they are hidden, so that they fade out.+struct PictureInPictureControlsState {+ bool shown { false };+ Optional<PictureInPictureControl> hovered_control;++ bool operator==(PictureInPictureControlsState const&) const = default;+};++// What the browser draws over a context's page, on top of anything the page draws, with each overlay the view has// present. The browser handles their input before the page sees any of it.struct UIOverlays {Optional<PausedDebuggerOverlayState> paused_debugger;+ Optional<PictureInPictureControlsState> picture_in_picture_controls;bool operator==(UIOverlays const&) const = default;};@@ -98,6 +108,11 @@ COMPOSITING_API ErrorOr<void> encode(Encoder&, Compositing::PausedDebuggerOverlatemplate<>COMPOSITING_API ErrorOr<Compositing::PausedDebuggerOverlayState> decode(Decoder&);+template<>+COMPOSITING_API ErrorOr<void> encode(Encoder&, Compositing::PictureInPictureControlsState const&);+template<>+COMPOSITING_API ErrorOr<Compositing::PictureInPictureControlsState> decode(Decoder&);+template<>COMPOSITING_API ErrorOr<void> encode(Encoder&, Compositing::UIOverlays const&);template<>diff --git a/Services/Compositor/CMakeLists.txt b/Services/Compositor/CMakeLists.txtindex 4229fe2d1bf..86f66fc28eb 100644--- a/Services/Compositor/CMakeLists.txt+++ b/Services/Compositor/CMakeLists.txt@@ -18,6 +18,7 @@ set(SOURCESKeyboardScrollController.cppOpenGLContext.cppPausedDebuggerOverlay.cpp+ PictureInPictureControls.cppRasterizeDisplayList.cppRenderClockConnection.cppScrollFlingController.cppdiff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cppindex 99af004366a..5e357dc426b 100644--- a/Services/Compositor/CompositorState.cpp+++ b/Services/Compositor/CompositorState.cpp@@ -701,9 +701,14 @@ void CompositorState::viewport_size_updated(Web::CompositorContextId context_id,if (!context)return;+ auto only_the_pixel_ratio_changed = viewport_size == context->presenter().viewport_size() && device_pixel_ratio != context->presenter().device_pixel_ratio();context->viewport_size_updated(viewport_size, device_pixel_ratio, window_resize_in_progress);resize_backing_stores_if_needed(context_id, *context);- if (context->presenter().ui_overlays().paused_debugger.has_value()) {++ // The paused debugger's page will not paint again. Any other page's next frame damages the whole viewport when its+ // size changes, but a new pixel ratio alone need not damage the overlays.+ auto const& overlays = context->presenter().ui_overlays();+ if (overlays.paused_debugger.has_value() || (only_the_pixel_ratio_changed && overlays.picture_in_picture_controls.has_value())) {if (auto viewport_rect = context->presenter().viewport_rect_for_ui_overlay(); viewport_rect.has_value())schedule_present_frame(context_id, *context, *viewport_rect);}@@ -982,6 +987,8 @@ void CompositorState::present_pending_frames_on_vsync(Optional<u64> display_id,context.presenter().queue_present_frame(PendingFrame::repainting_changes(*animation_frame));if (auto rebound_frame = context.advance_elastic_overscroll_rebounds(frame_time); rebound_frame.has_value())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);}// A render clock tick goes to the process's render clock thread, beside what its main thread is doing, withdiff --git a/Services/Compositor/ContextPresenter.cpp b/Services/Compositor/ContextPresenter.cppindex 3e4773d7dea..cda4dc49ab3 100644--- a/Services/Compositor/ContextPresenter.cpp+++ b/Services/Compositor/ContextPresenter.cpp@@ -8,6 +8,7 @@#include <Compositor/ContextScene.h>#include <Compositor/DisplayListPlayerSkia.h>#include <Compositor/PausedDebuggerOverlay.h>+#include <Compositor/PictureInPictureControls.h>#include <Compositor/ScrollbarController.h>#include <LibCompositing/DisplayList/CanvasSurfaceRegistry.h>#include <LibCompositing/DisplayList/DisplayListDamage.h>@@ -79,6 +80,22 @@ void ContextPresenter::finish_window_resize()void ContextPresenter::set_ui_overlays(Compositing::UIOverlays overlays){m_ui_overlays = move(overlays);++ auto const& picture_in_picture_controls = m_ui_overlays.picture_in_picture_controls;+ auto shows_picture_in_picture_controls = picture_in_picture_controls.has_value() && picture_in_picture_controls->shown;+ m_picture_in_picture_controls_fade.fade_to(shows_picture_in_picture_controls ? 1 : 0, MonotonicTime::now());+}++Optional<PendingFrame> ContextPresenter::advance_ui_overlay_fades(MonotonicTime frame_time)+{+ if (!m_picture_in_picture_controls_fade.advance_to(frame_time))+ return {};++ auto viewport_rect = viewport_rect_for_ui_overlay();+ if (!viewport_rect.has_value())+ return {};+ auto geometry = Compositing::picture_in_picture_controls_geometry(m_viewport_size, m_device_pixel_ratio);+ return PendingFrame { *viewport_rect, Compositing::overlay_buttons_bounds(geometry.buttons()) };}Optional<Gfx::IntRect> ContextPresenter::viewport_rect_for_ui_overlay() const@@ -489,8 +506,13 @@ void ContextPresenter::paint_current_display_list(DisplayListPlayerSkia& display&m_canvas_surface_registry,frame_content.composited_context_resolver);frame_content.scrollbar_controller.paint(target_surface, display_list_player, frame_content.presented_scroll_state_snapshot);- if (paint_ui_overlay == PaintUIOverlay::Yes && m_ui_overlays.paused_debugger.has_value())+ if (paint_ui_overlay == PaintUIOverlay::No)+ return;++ if (m_ui_overlays.paused_debugger.has_value())paint_paused_debugger_overlay(target_surface, m_viewport_size, m_device_pixel_ratio, *m_ui_overlays.paused_debugger);+ if (auto opacity = m_picture_in_picture_controls_fade.opacity(); m_ui_overlays.picture_in_picture_controls.has_value() && opacity > 0)+ paint_picture_in_picture_controls(target_surface, m_viewport_size, m_device_pixel_ratio, *m_ui_overlays.picture_in_picture_controls, opacity);};if (damage_rect.has_value() && !damage_rect->is_empty() && presents_to_client() && damage_rect->size() != surface.size() && surface.skia_backend_context()) {diff --git a/Services/Compositor/ContextPresenter.h b/Services/Compositor/ContextPresenter.hindex c995624c3f1..3ed3afd3b60 100644--- a/Services/Compositor/ContextPresenter.h+++ b/Services/Compositor/ContextPresenter.h@@ -19,6 +19,7 @@#include <LibCompositing/DisplayList/CompositedContext.h>#include <LibCompositing/DisplayList/DisplayList.h>#include <LibCompositing/Forward.h>+#include <LibCompositing/OverlayFade.h>#include <LibCompositing/Scrolling/ScrollState.h>#include <LibCompositing/Types.h>#include <LibCompositing/UIOverlays.h>@@ -85,6 +86,8 @@ public:Compositing::UIOverlays const& ui_overlays() const { return m_ui_overlays; }void set_ui_overlays(Compositing::UIOverlays);Optional<Gfx::IntRect> viewport_rect_for_ui_overlay() const;+ bool ui_overlay_fades_need_frame() const { return m_picture_in_picture_controls_fade.is_fading(); }+ Optional<PendingFrame> advance_ui_overlay_fades(MonotonicTime frame_time);Optional<BackingStoreManager::Publication> resize_backing_stores_if_needed(RefPtr<Gfx::SkiaBackendContext> const&, BackingStoreManager::GpuSharing);void invalidate_backing_stores();@@ -148,6 +151,7 @@ private:double m_device_pixel_ratio { 1.0 };Compositing::WindowResizingInProgress m_window_resize_in_progress { Compositing::WindowResizingInProgress::No };Compositing::UIOverlays m_ui_overlays;+ Compositing::OverlayFade m_picture_in_picture_controls_fade;BackingStoreManager m_backing_store_manager;RefPtr<Gfx::PaintingSurface> m_latest_rendered_surface;diff --git a/Services/Compositor/ContextState.h b/Services/Compositor/ContextState.hindex 5034cfe8f96..8933fdd8db5 100644--- a/Services/Compositor/ContextState.h+++ b/Services/Compositor/ContextState.h@@ -139,7 +139,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(); }+ 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 has_active_scroll_fling() const { return m_scroll_fling_controller.is_active(); }struct ScrollFlingStep {diff --git a/Services/Compositor/PictureInPictureControls.cpp b/Services/Compositor/PictureInPictureControls.cppnew file mode 100644index 00000000000..09b32d6a63a--- /dev/null+++ b/Services/Compositor/PictureInPictureControls.cpp@@ -0,0 +1,96 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <AK/Span.h>+#include <Compositor/PictureInPictureControls.h>+#include <LibCompositing/UIOverlays.h>+#include <LibGfx/PaintingSurface.h>+#include <LibGfx/SkiaUtils.h>+#include <core/SkCanvas.h>+#include <core/SkPaint.h>+#include <core/SkPathBuilder.h>++namespace Compositor {++// The icons are drawn on a grid of 24 units.+static constexpr SkPoint back_to_tab_frame_outside[] = { { 3, 3 }, { 21, 3 }, { 21, 21 }, { 3, 21 } };+static constexpr SkPoint back_to_tab_frame_inside[] = { { 5, 5 }, { 5, 19 }, { 19, 19 }, { 19, 5 } };+static constexpr SkPoint back_to_tab_arrow[] = {+ { 16.6f, 6 },+ { 9, 13.6f },+ { 9, 9 },+ { 7, 9 },+ { 7, 17 },+ { 15, 17 },+ { 15, 15 },+ { 10.4f, 15 },+ { 18, 7.4f },+};+static constexpr SkPoint close_cross[] = {+ { 6.4f, 5 },+ { 12, 10.6f },+ { 17.6f, 5 },+ { 19, 6.4f },+ { 13.4f, 12 },+ { 19, 17.6f },+ { 17.6f, 19 },+ { 12, 13.4f },+ { 6.4f, 19 },+ { 5, 17.6f },+ { 10.6f, 12 },+ { 5, 6.4f },+};++static void add_polygon(SkPathBuilder& path, ReadonlySpan<SkPoint> points)+{+ path.moveTo(points.first());+ for (auto const& point : points.slice(1))+ path.lineTo(point);+ path.close();+}++void paint_picture_in_picture_controls(Gfx::PaintingSurface& surface, Gfx::IntSize viewport_size, double device_pixel_ratio, Compositing::PictureInPictureControlsState const& state, float opacity)+{+ static constexpr auto button_color = Color(0, 0, 0, 153);+ static constexpr auto icon_color = Color(0xff, 0xff, 0xff);+ static constexpr auto hovered_icon_color = Color(0xe0, 0xd4, 0xff);++ auto& canvas = surface.canvas();+ auto geometry = Compositing::picture_in_picture_controls_geometry(viewport_size, device_pixel_ratio);+ auto bounds = Gfx::to_skia_rect(Compositing::overlay_buttons_bounds(geometry.buttons()));+ canvas.saveLayerAlphaf(&bounds, opacity);++ SkPaint paint;+ paint.setAntiAlias(true);++ auto paint_button = [&](Gfx::IntRect const& button, Compositing::PictureInPictureControl control, SkPathBuilder& icon) {+ auto center = button.to_type<float>().center();+ paint.setColor(Gfx::to_skia_color(button_color));+ canvas.drawCircle(center.x(), center.y(), button.width() / 2.0f, paint);++ auto icon_size = static_cast<float>(20 * device_pixel_ratio);+ canvas.save();+ canvas.translate(center.x() - icon_size / 2, center.y() - icon_size / 2);+ canvas.scale(icon_size / 24, icon_size / 24);+ paint.setColor(Gfx::to_skia_color(state.hovered_control == control ? hovered_icon_color : icon_color));+ canvas.drawPath(icon.detach(), paint);+ canvas.restore();+ };++ SkPathBuilder back_to_tab_icon { SkPathFillType::kEvenOdd };+ add_polygon(back_to_tab_icon, back_to_tab_frame_outside);+ add_polygon(back_to_tab_icon, back_to_tab_frame_inside);+ add_polygon(back_to_tab_icon, back_to_tab_arrow);+ paint_button(geometry.back_to_tab_button, Compositing::PictureInPictureControl::BackToTab, back_to_tab_icon);++ SkPathBuilder close_icon;+ add_polygon(close_icon, close_cross);+ paint_button(geometry.close_button, Compositing::PictureInPictureControl::Close, close_icon);++ canvas.restore();+}++}diff --git a/Services/Compositor/PictureInPictureControls.h b/Services/Compositor/PictureInPictureControls.hnew file mode 100644index 00000000000..59e02d2e941--- /dev/null+++ b/Services/Compositor/PictureInPictureControls.h@@ -0,0 +1,17 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <LibCompositing/Forward.h>+#include <LibGfx/Forward.h>+#include <LibGfx/Size.h>++namespace Compositor {++void paint_picture_in_picture_controls(Gfx::PaintingSurface&, Gfx::IntSize viewport_size, double device_pixel_ratio, Compositing::PictureInPictureControlsState const&, float opacity);++}diff --git a/Tests/LibCompositing/CMakeLists.txt b/Tests/LibCompositing/CMakeLists.txtindex b71cc01c1f4..e8ec2e18f80 100644--- a/Tests/LibCompositing/CMakeLists.txt+++ b/Tests/LibCompositing/CMakeLists.txt@@ -1,7 +1,9 @@set(TEST_SOURCESTestElasticOverscroll.cpp+ TestOverlayFade.cppTestOverlayGeometry.cppTestPausedDebuggerOverlay.cpp+ TestPictureInPictureControls.cppTestScrollState.cppTestSmoothScrollAnimation.cppTestTextureUpload.cppdiff --git a/Tests/LibCompositing/TestOverlayFade.cpp b/Tests/LibCompositing/TestOverlayFade.cppnew file mode 100644index 00000000000..f7aae759307--- /dev/null+++ b/Tests/LibCompositing/TestOverlayFade.cpp@@ -0,0 +1,88 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibCompositing/OverlayFade.h>+#include <LibTest/TestCase.h>++using namespace Compositing;++static MonotonicTime after(MonotonicTime start, i64 milliseconds)+{+ return start + AK::Duration::from_milliseconds(milliseconds);+}++TEST_CASE(a_fade_starts_transparent_and_at_rest)+{+ OverlayFade fade;+ EXPECT_EQ(fade.opacity(), 0.0f);+ EXPECT(!fade.is_fading());+}++TEST_CASE(a_fade_eases_in_and_out_to_its_target)+{+ auto start = MonotonicTime::now();+ OverlayFade fade;+ fade.fade_to(1, start);+ EXPECT(fade.is_fading());++ EXPECT(!fade.advance_to(start));+ EXPECT_EQ(fade.opacity(), 0.0f);++ EXPECT(fade.advance_to(after(start, 30)));+ EXPECT_APPROXIMATE_WITH_ERROR(fade.opacity(), 0.104f, 0.0001f);++ fade.advance_to(after(start, 75));+ EXPECT_APPROXIMATE(fade.opacity(), 0.5f);++ fade.advance_to(after(start, 150));+ EXPECT_EQ(fade.opacity(), 1.0f);+ EXPECT(!fade.is_fading());++ EXPECT(!fade.advance_to(after(start, 300)));+ EXPECT_EQ(fade.opacity(), 1.0f);+}++TEST_CASE(a_fade_does_not_go_back_in_time)+{+ auto start = MonotonicTime::now();+ OverlayFade fade;+ fade.fade_to(1, start);++ EXPECT(!fade.advance_to(after(start, -10)));+ EXPECT_EQ(fade.opacity(), 0.0f);+}++TEST_CASE(a_fade_turns_back_from_where_it_has_reached)+{+ auto start = MonotonicTime::now();+ OverlayFade fade;+ fade.fade_to(1, start);+ fade.advance_to(after(start, 75));++ auto turned_at = after(start, 75);+ fade.fade_to(0, turned_at);+ EXPECT(!fade.advance_to(turned_at));+ EXPECT_APPROXIMATE(fade.opacity(), 0.5f);++ fade.advance_to(after(turned_at, 75));+ EXPECT_APPROXIMATE(fade.opacity(), 0.25f);++ fade.advance_to(after(turned_at, 150));+ EXPECT_EQ(fade.opacity(), 0.0f);+ EXPECT(!fade.is_fading());+}++TEST_CASE(fading_to_the_same_target_keeps_the_fade_going)+{+ auto start = MonotonicTime::now();+ OverlayFade fade;+ fade.fade_to(1, start);+ fade.advance_to(after(start, 75));++ fade.fade_to(1, after(start, 75));+ fade.advance_to(after(start, 150));+ EXPECT_EQ(fade.opacity(), 1.0f);+}diff --git a/Tests/LibCompositing/TestOverlayGeometry.cpp b/Tests/LibCompositing/TestOverlayGeometry.cppindex 8e3d60b0924..7ebf241b9e4 100644--- a/Tests/LibCompositing/TestOverlayGeometry.cpp+++ b/Tests/LibCompositing/TestOverlayGeometry.cpp@@ -19,3 +19,36 @@ TEST_CASE(overlay_lengths_are_at_least_one_device_pixel){EXPECT_EQ(scaled_overlay_length(1, 0.25), 1);}++enum class TestAction : u8 {+ First,+ Second,+};++TEST_CASE(a_round_button_leaves_the_corners_of_its_bounds)+{+ OverlayButton<TestAction> round_button { .action = TestAction::First, .bounds = { 8, 8, 28, 28 }, .shape = OverlayButtonShape::Circle };+ EXPECT(round_button.contains({ 22, 22 }));+ EXPECT(round_button.contains({ 8, 22 }));+ EXPECT(round_button.contains({ 22, 35 }));+ EXPECT(!round_button.contains({ 31, 32 }));+ EXPECT(!round_button.contains({ 32, 31 }));+ EXPECT(!round_button.contains({ 8, 8 }));+ EXPECT(!round_button.contains({ 34, 10 }));++ OverlayButton<TestAction> rectangular_button { .action = TestAction::First, .bounds = { 8, 8, 28, 28 }, .shape = OverlayButtonShape::Rectangle };+ EXPECT(rectangular_button.contains({ 8, 8 }));+ EXPECT(!rectangular_button.contains({ 36, 36 }));+}++TEST_CASE(buttons_are_found_by_position_and_bounded_together)+{+ Array<OverlayButton<TestAction>, 2> buttons {+ OverlayButton<TestAction> { .action = TestAction::First, .bounds = { 0, 0, 10, 10 }, .shape = OverlayButtonShape::Rectangle },+ OverlayButton<TestAction> { .action = TestAction::Second, .bounds = { 90, 20, 10, 10 }, .shape = OverlayButtonShape::Rectangle },+ };+ EXPECT(overlay_action_at(buttons, { 5, 5 }) == TestAction::First);+ EXPECT(overlay_action_at(buttons, { 95, 25 }) == TestAction::Second);+ EXPECT(!overlay_action_at(buttons, { 50, 15 }).has_value());+ EXPECT_EQ(overlay_buttons_bounds(buttons), (Gfx::IntRect { 0, 0, 100, 30 }));+}diff --git a/Tests/LibCompositing/TestPictureInPictureControls.cpp b/Tests/LibCompositing/TestPictureInPictureControls.cppnew file mode 100644index 00000000000..63d087a4b68--- /dev/null+++ b/Tests/LibCompositing/TestPictureInPictureControls.cpp@@ -0,0 +1,45 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibCompositing/PictureInPictureControls.h>+#include <LibTest/TestCase.h>++using namespace Compositing;++TEST_CASE(buttons_sit_in_the_top_corners)+{+ auto geometry = picture_in_picture_controls_geometry({ 800, 600 }, 1.0);++ EXPECT_EQ(geometry.back_to_tab_button, (Gfx::IntRect { 8, 8, 28, 28 }));+ EXPECT_EQ(geometry.close_button, (Gfx::IntRect { 764, 8, 28, 28 }));+}++TEST_CASE(geometry_scales_with_device_pixel_ratio)+{+ auto geometry = picture_in_picture_controls_geometry({ 1600, 1200 }, 2.0);++ EXPECT_EQ(geometry.back_to_tab_button, (Gfx::IntRect { 16, 16, 56, 56 }));+ EXPECT_EQ(geometry.close_button, (Gfx::IntRect { 1528, 16, 56, 56 }));+}++TEST_CASE(controls_are_round)+{+ auto geometry = picture_in_picture_controls_geometry({ 800, 600 }, 1.0);+ auto buttons = geometry.buttons();++ EXPECT(overlay_action_at(buttons, geometry.back_to_tab_button.center()) == PictureInPictureControl::BackToTab);+ EXPECT(overlay_action_at(buttons, geometry.close_button.center()) == PictureInPictureControl::Close);+ EXPECT(!overlay_action_at(buttons, geometry.back_to_tab_button.top_left()).has_value());+ EXPECT(!overlay_action_at(buttons, { 790, 10 }).has_value());+ EXPECT(!overlay_action_at(buttons, { 400, 22 }).has_value());+}++TEST_CASE(control_values_are_validated)+{+ EXPECT(picture_in_picture_control_from_underlying(to_underlying(PictureInPictureControl::BackToTab)) == PictureInPictureControl::BackToTab);+ EXPECT(picture_in_picture_control_from_underlying(to_underlying(PictureInPictureControl::Close)) == PictureInPictureControl::Close);+ EXPECT(!picture_in_picture_control_from_underlying(255).has_value());+}diff --git a/Tests/LibCompositing/TestUIOverlays.cpp b/Tests/LibCompositing/TestUIOverlays.cppindex fb696566327..3df5225806f 100644--- a/Tests/LibCompositing/TestUIOverlays.cpp+++ b/Tests/LibCompositing/TestUIOverlays.cpp@@ -37,6 +37,10 @@ TEST_CASE(overlays_survive_ipc).font_family = "Inter"_string,.hovered_action = PausedDebuggerOverlayAction::Continue,};+ overlays.picture_in_picture_controls = PictureInPictureControlsState {+ .shown = true,+ .hovered_control = PictureInPictureControl::BackToTab,+ };auto decoded = MUST(decode_overlays([&](IPC::Encoder& encoder) { return encoder.encode(overlays); }));EXPECT(decoded == overlays);@@ -48,6 +52,7 @@ TEST_CASE(an_overlay_that_is_not_shown_stays_absent)auto decoded = MUST(decode_overlays([&](IPC::Encoder& encoder) { return encoder.encode(overlays); }));EXPECT(!decoded.paused_debugger.has_value());+ EXPECT(!decoded.picture_in_picture_controls.has_value());}TEST_CASE(an_unknown_paused_debugger_action_is_rejected)@@ -56,6 +61,19 @@ TEST_CASE(an_unknown_paused_debugger_action_is_rejected)TRY(encoder.encode(true));TRY(encoder.encode(Optional<String> {}));TRY(encoder.encode(Optional<u8> { 255 }));+ TRY(encoder.encode(false));+ return {};+ });+ EXPECT(decoded.is_error());+}++TEST_CASE(an_unknown_picture_in_picture_control_is_rejected)+{+ auto decoded = decode_overlays([](IPC::Encoder& encoder) -> ErrorOr<void> {+ TRY(encoder.encode(false));+ TRY(encoder.encode(true));+ TRY(encoder.encode(true));+ TRY(encoder.encode(Optional<u8> { 255 }));return {};});EXPECT(decoded.is_error());