Truly independent web browser
LibWebView+UI/Qt: Limit how far a Picture-in-Picture window resizes
Luke Wilded737a257e060
diff --git a/Libraries/LibWebView/PictureInPictureWindow.cpp b/Libraries/LibWebView/PictureInPictureWindow.cppindex 7256c80c66c..d218e811e42 100644--- a/Libraries/LibWebView/PictureInPictureWindow.cpp+++ b/Libraries/LibWebView/PictureInPictureWindow.cpp@@ -16,31 +16,57 @@ Gfx::IntSize PictureInPictureWindow::aspect_ratio(Gfx::IntSize video_size)return video_size;}-static Gfx::IntSize size_with_width(Gfx::IntSize video_size, int width, Gfx::IntSize screen_size)+static Gfx::IntSize size_with_width(Gfx::IntSize aspect_ratio, int width){- auto aspect_ratio = PictureInPictureWindow::aspect_ratio(video_size);+ return { width, width * aspect_ratio.height() / aspect_ratio.width() };+}- // https://w3c.github.io/picture-in-picture/#pip- // It is also RECOMMENDED that the Picture-in-Picture window has a maximum and minimum size. For example, it could- // be restricted to be between a quarter and a half of one dimension of the screen.- auto height = width * aspect_ratio.height() / aspect_ratio.width();+static Gfx::IntSize size_with_height(Gfx::IntSize aspect_ratio, int height)+{+ return { height * aspect_ratio.width() / aspect_ratio.height(), height };+}- if (auto maximum_height = screen_size.height() / 2; height > maximum_height) {- height = maximum_height;- width = height * aspect_ratio.width() / aspect_ratio.height();- }+// https://w3c.github.io/picture-in-picture/#pip+// It is also RECOMMENDED that the Picture-in-Picture window has a maximum and minimum size. For example, it could be+// restricted to be between a quarter and a half of one dimension of the screen.+// NB: The window opens at a quarter of the screen's width, and can shrink below that for as long as its controls fit.+static constexpr int minimum_width = 240;+static constexpr int minimum_height = 100;- return { width, height };+Gfx::IntSize PictureInPictureWindow::minimum_size(Gfx::IntSize video_size)+{+ auto ratio = aspect_ratio(video_size);+ auto size = size_with_width(ratio, minimum_width);+ if (size.height() < minimum_height)+ size = size_with_height(ratio, minimum_height);+ return size;+}++Gfx::IntSize PictureInPictureWindow::maximum_size(Gfx::IntSize video_size, Gfx::IntSize screen_size)+{+ auto ratio = aspect_ratio(video_size);+ auto size = size_with_width(ratio, screen_size.width() / 2);+ if (size.height() > screen_size.height() / 2)+ size = size_with_height(ratio, screen_size.height() / 2);+ return size;}Gfx::IntSize PictureInPictureWindow::initial_size(Gfx::IntSize video_size, Gfx::IntSize screen_size){- return size_with_width(video_size, screen_size.width() / 4, screen_size);+ return size_for_video_size({ screen_size.width() / 4, 0 }, video_size, screen_size);}Gfx::IntSize PictureInPictureWindow::size_for_video_size(Gfx::IntSize window_size, Gfx::IntSize video_size, Gfx::IntSize screen_size){- return size_with_width(video_size, window_size.width(), screen_size);+ auto minimum = minimum_size(video_size);+ if (window_size.width() <= minimum.width())+ return minimum;++ auto maximum = maximum_size(video_size, screen_size);+ if (window_size.width() >= maximum.width())+ return maximum;++ return size_with_width(aspect_ratio(video_size), window_size.width());}void PictureInPictureWindow::report_page_close_of(ViewImplementation& view)diff --git a/Libraries/LibWebView/PictureInPictureWindow.h b/Libraries/LibWebView/PictureInPictureWindow.hindex dfa3f8dd47c..07e08786888 100644--- a/Libraries/LibWebView/PictureInPictureWindow.h+++ b/Libraries/LibWebView/PictureInPictureWindow.h@@ -21,8 +21,10 @@ public:AK_ALLOC_WITH_KMALLOC;static Gfx::IntSize aspect_ratio(Gfx::IntSize video_size);+ static Gfx::IntSize minimum_size(Gfx::IntSize video_size);+ static Gfx::IntSize maximum_size(Gfx::IntSize video_size, Gfx::IntSize screen_size);static Gfx::IntSize initial_size(Gfx::IntSize video_size, Gfx::IntSize screen_size);- // The window keeps its width as its video changes shape, unless that would make it too tall for the screen.+ // The window keeps its width as its video changes shape, as far as its minimum and maximum sizes allow.static Gfx::IntSize size_for_video_size(Gfx::IntSize window_size, Gfx::IntSize video_size, Gfx::IntSize screen_size);virtual ~PictureInPictureWindow() = default;diff --git a/Tests/LibWebView/CMakeLists.txt b/Tests/LibWebView/CMakeLists.txtindex 71af087364e..03aa3e7e2a4 100644--- a/Tests/LibWebView/CMakeLists.txt+++ b/Tests/LibWebView/CMakeLists.txt@@ -15,6 +15,7 @@ set(TEST_SOURCESTestHSTSStore.cppTestNavigationLoader.cppTestOmnibox.cpp+ TestPictureInPictureWindow.cppTestProfile.cppTestSessionHistory.cppTestSessionHistorySnapshotStorage.cppdiff --git a/Tests/LibWebView/TestPictureInPictureWindow.cpp b/Tests/LibWebView/TestPictureInPictureWindow.cppnew file mode 100644index 00000000000..6de44b7d80a--- /dev/null+++ b/Tests/LibWebView/TestPictureInPictureWindow.cpp@@ -0,0 +1,58 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <LibTest/TestCase.h>+#include <LibWebView/PictureInPictureWindow.h>++using WebView::PictureInPictureWindow;++static constexpr Gfx::IntSize screen_size { 1920, 1080 };+static constexpr Gfx::IntSize landscape_video { 1920, 1080 };+static constexpr Gfx::IntSize portrait_video { 1080, 1920 };+static constexpr Gfx::IntSize wide_video { 3200, 900 };++TEST_CASE(window_opens_at_a_quarter_of_the_screen_width)+{+ EXPECT_EQ(PictureInPictureWindow::initial_size(landscape_video, screen_size), Gfx::IntSize(480, 270));+}++TEST_CASE(window_opens_no_taller_than_half_of_the_screen)+{+ EXPECT_EQ(PictureInPictureWindow::initial_size(portrait_video, screen_size), Gfx::IntSize(303, 540));+}++TEST_CASE(minimum_size_keeps_the_aspect_ratio)+{+ EXPECT_EQ(PictureInPictureWindow::minimum_size(landscape_video), Gfx::IntSize(240, 135));+ EXPECT_EQ(PictureInPictureWindow::minimum_size(portrait_video), Gfx::IntSize(240, 426));+}++TEST_CASE(minimum_size_of_a_wide_video_keeps_a_minimum_height)+{+ EXPECT_EQ(PictureInPictureWindow::minimum_size(wide_video), Gfx::IntSize(355, 100));+}++TEST_CASE(maximum_size_is_half_of_the_screen)+{+ EXPECT_EQ(PictureInPictureWindow::maximum_size(landscape_video, screen_size), Gfx::IntSize(960, 540));+ EXPECT_EQ(PictureInPictureWindow::maximum_size(portrait_video, screen_size), Gfx::IntSize(303, 540));+}++TEST_CASE(window_keeps_its_width_when_its_video_changes_shape)+{+ EXPECT_EQ(PictureInPictureWindow::size_for_video_size({ 480, 270 }, { 640, 480 }, screen_size), Gfx::IntSize(480, 360));+}++TEST_CASE(window_stays_within_its_limits_when_its_video_changes_shape)+{+ EXPECT_EQ(PictureInPictureWindow::size_for_video_size({ 240, 180 }, wide_video, screen_size), Gfx::IntSize(355, 100));+ EXPECT_EQ(PictureInPictureWindow::size_for_video_size({ 900, 506 }, portrait_video, screen_size), Gfx::IntSize(303, 540));+}++TEST_CASE(window_without_a_video_size_is_sixteen_by_nine)+{+ EXPECT_EQ(PictureInPictureWindow::minimum_size({}), Gfx::IntSize(240, 135));+}diff --git a/UI/Qt/PictureInPictureWindow.cpp b/UI/Qt/PictureInPictureWindow.cppindex d9f3a75e5c1..0c5d24af88c 100644--- a/UI/Qt/PictureInPictureWindow.cpp+++ b/UI/Qt/PictureInPictureWindow.cpp@@ -126,14 +126,15 @@ public:// A frameless window cannot be resized from its edges on macOS unless AppKit is told it is resizable.make_appkit_window_resizable(*this);#endif- lock_aspect_ratio(video_size);+ set_size_limits(video_size);+ lock_aspect_ratio();-#if defined(AK_OS_LINUX)- // Qt also replaces the size hints when the window moves to a screen with another scale factor.+ // The maximum size depends on the screen, and on X11 Qt replaces the size hints when the window moves to a screen+ // with another scale factor.connect(windowHandle(), &QWindow::screenChanged, this, [this] {- set_x11_window_aspect_ratio(*this, m_aspect_ratio);+ set_size_limits(m_aspect_ratio);+ lock_aspect_ratio();});-#endif}virtual Gfx::IntSize size() const override { return { width(), height() }; }@@ -142,6 +143,9 @@ public:virtual void set_video_size(Gfx::IntSize video_size) override{+ // The limits change with the video's shape first, so that they allow the window's new size.+ set_size_limits(video_size);+auto available = screen()->availableGeometry();auto size = size_for_video_size({ width(), height() }, video_size, { available.width(), available.height() });if (size != Gfx::IntSize { width(), height() }) {@@ -155,7 +159,7 @@ public:setGeometry(new_geometry);}- lock_aspect_ratio(video_size);+ lock_aspect_ratio();}private:@@ -255,10 +259,21 @@ private:return edges;}- // On X11, Qt replaces the window's size hints whenever it changes the window's geometry, so this runs after that.- void lock_aspect_ratio(Gfx::IntSize video_size)+ void set_size_limits(Gfx::IntSize video_size){m_aspect_ratio = aspect_ratio(video_size);++ auto available = screen()->availableGeometry();+ auto minimum = minimum_size(m_aspect_ratio);+ auto maximum = maximum_size(m_aspect_ratio, { available.width(), available.height() });+ setMinimumSize(minimum.width(), minimum.height());+ setMaximumSize(maximum.width(), maximum.height());+ }++ // On X11, Qt replaces the window's size hints whenever it changes the window's geometry or size limits, so this runs+ // after those.+ void lock_aspect_ratio()+ {#if defined(AK_OS_MACOS)set_appkit_window_content_aspect_ratio(*this, m_aspect_ratio);#elif defined(AK_OS_LINUX)