Skip to content

Truly independent web browser

LibWeb+LibWebView+UI/Qt: Keep a Picture-in-Picture window's aspect ratio

Luke Wilde3ba421c0ad28
diff --git a/.devcontainer/features/ladybird/install-fedora.sh b/.devcontainer/features/ladybird/install-fedora.sh
index 34e88606e35..57281cb2324 100644
--- a/.devcontainer/features/ladybird/install-fedora.sh
+++ b/.devcontainer/features/ladybird/install-fedora.sh
@@ -7,4 +7,4 @@ dnf install -y git gh
# Ladybird dev dependencies
dnf install -y autoconf-archive automake ccache cmake curl google-noto-sans-mono-fonts liberation-sans-fonts \
libdrm-devel libglvnd-devel libtool nasm ncurses-devel ninja-build patchelf perl-FindBin perl-IPC-Cmd perl-lib perl-Time-Piece qt6-qtbase-devel \
- qt6-qttools-devel qt6-qtwayland-devel tar unzip zip zlib-ng-compat-static
+ qt6-qttools-devel qt6-qtwayland-devel tar unzip xcb-util-wm-devel zip zlib-ng-compat-static
diff --git a/.devcontainer/features/ladybird/install-ubuntu.sh b/.devcontainer/features/ladybird/install-ubuntu.sh
index d38a22787a3..c5fd96527d7 100644
--- a/.devcontainer/features/ladybird/install-ubuntu.sh
+++ b/.devcontainer/features/ladybird/install-ubuntu.sh
@@ -31,7 +31,7 @@ install_cmake() {
### Install packages
apt update -y
-apt install -y autoconf autoconf-archive automake build-essential ccache curl fonts-liberation2 git glslang-tools libdrm-dev libgl1-mesa-dev libncurses-dev libpulse-dev libtool lsb-release nasm ninja-build pkg-config python3 qt6-base-private-dev qt6-positioning-dev qt6-tools-dev-tools qt6-wayland shellcheck tar unzip zip
+apt install -y autoconf autoconf-archive automake build-essential ccache curl fonts-liberation2 git glslang-tools libdrm-dev libgl1-mesa-dev libncurses-dev libpulse-dev libtool libxcb-icccm4-dev lsb-release nasm ninja-build pkg-config python3 qt6-base-private-dev qt6-positioning-dev qt6-tools-dev-tools qt6-wayland shellcheck tar unzip zip
install_cmake
diff --git a/Documentation/BuildInstructionsLadybird.md b/Documentation/BuildInstructionsLadybird.md
index 061d63bd80d..16751dbd9a2 100644
--- a/Documentation/BuildInstructionsLadybird.md
+++ b/Documentation/BuildInstructionsLadybird.md
@@ -20,7 +20,7 @@ CMake 3.30 or newer must be available in $PATH.
<!-- Note: If you change something here, please also change it in the `devcontainer/devcontainer.json` file. -->
```bash
-sudo apt install autoconf autoconf-archive automake build-essential ccache cmake curl fonts-liberation2 git glslang-tools libdrm-dev libgl1-mesa-dev libncurses-dev libpulse-dev libtool nasm ninja-build pkg-config python3-venv qt6-base-private-dev qt6-positioning-dev qt6-tools-dev-tools qt6-wayland tar unzip zip
+sudo apt install autoconf autoconf-archive automake build-essential ccache cmake curl fonts-liberation2 git glslang-tools libdrm-dev libgl1-mesa-dev libncurses-dev libpulse-dev libtool libxcb-icccm4-dev nasm ninja-build pkg-config python3-venv qt6-base-private-dev qt6-positioning-dev qt6-tools-dev-tools qt6-wayland tar unzip zip
```
#### CMake 3.30 or newer:
@@ -70,19 +70,19 @@ sudo apt update && sudo apt install g++-14 libstdc++-14-dev
### Arch Linux/Manjaro:
```
-sudo pacman -S --needed autoconf-archive base-devel ccache cmake curl git less libgl libpulse nasm ninja python qt6-base qt6-positioning qt6-tools ttf-liberation tar unzip zip
+sudo pacman -S --needed autoconf-archive base-devel ccache cmake curl git less libgl libpulse nasm ninja python qt6-base qt6-positioning qt6-tools ttf-liberation tar unzip xcb-util-wm zip
```
### Fedora or derivatives:
```
-sudo dnf install autoconf-archive automake ccache cmake curl git glslang libdrm-devel liberation-sans-fonts libglvnd-devel libtool nasm ncurses-devel ninja-build patchelf perl-FindBin perl-IPC-Cmd perl-lib perl-Time-Piece pulseaudio-libs-devel qt6-qtbase-private-devel qt6-qtpositioning-devel qt6-qttools-devel qt6-qtwayland-devel tar unzip zip zlib-ng-compat-static
+sudo dnf install autoconf-archive automake ccache cmake curl git glslang libdrm-devel liberation-sans-fonts libglvnd-devel libtool nasm ncurses-devel ninja-build patchelf perl-FindBin perl-IPC-Cmd perl-lib perl-Time-Piece pulseaudio-libs-devel qt6-qtbase-private-devel qt6-qtpositioning-devel qt6-qttools-devel qt6-qtwayland-devel tar unzip xcb-util-wm-devel zip zlib-ng-compat-static
```
### openSUSE:
```
-sudo zypper install autoconf-archive automake ccache cmake curl gcc14 gcc14-c++ git liberation-fonts libglvnd-devel libpulse-devel libtool nasm ncurses-devel ninja qt6-base-private-devel qt6-positioning-devel qt6-tools-devel qt6-wayland-devel tar unzip zip
+sudo zypper install autoconf-archive automake ccache cmake curl gcc14 gcc14-c++ git liberation-fonts libglvnd-devel libpulse-devel libtool nasm ncurses-devel ninja qt6-base-private-devel qt6-positioning-devel qt6-tools-devel qt6-wayland-devel tar unzip xcb-util-wm-devel zip
```
If one or more of the base repository packages are flagged as having an out-of-date version during the build process, you may need add the `devel:tools:building` repository. For example, on Leap 15.6, the `autoconf` package might be version 2.69, whereas the `gperf` package requires 2.70 to build.
@@ -122,7 +122,7 @@ This virtual environment can be created once and reused in future shell sessions
```
sudo xbps-install -Su # (optional) ensure packages are up to date to avoid "Transaction aborted due to unresolved dependencies."
-sudo xbps-install -S git bash gcc python3 curl cmake libtool zip unzip linux-headers make pkg-config autoconf automake autoconf-archive nasm ncurses-devel MesaLib-devel ninja pulseaudio-devel qt6-base-private-devel qt6-position-devel qt6-tools-devel qt6-wayland-devel
+sudo xbps-install -S git bash gcc python3 curl cmake libtool zip unzip linux-headers make pkg-config autoconf automake autoconf-archive nasm ncurses-devel MesaLib-devel ninja pulseaudio-devel qt6-base-private-devel qt6-position-devel qt6-tools-devel qt6-wayland-devel xcb-util-wm-devel
```
### NixOS or with Nix:
diff --git a/Libraries/LibWeb/HTML/HTMLVideoElement.cpp b/Libraries/LibWeb/HTML/HTMLVideoElement.cpp
index 51902c75c90..5012d29159f 100644
--- a/Libraries/LibWeb/HTML/HTMLVideoElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLVideoElement.cpp
@@ -160,6 +160,8 @@ void HTMLVideoElement::set_intrinsic_video_dimensions(Optional<Gfx::Size<u32>> d
queue_a_media_element_task([](HTMLMediaElement& self) {
self.dispatch_event(DOM::Event::create(HTML::relevant_global_object(self), HTML::EventNames::resize));
});
+
+ document().page().picture_in_picture_controller().video_size_did_change(*this, dimensions->to_type<int>());
}
update_representation();
diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.h
index 8133f74393d..e8c278fdb83 100644
--- a/Libraries/LibWeb/Page/Page.h
+++ b/Libraries/LibWeb/Page/Page.h
@@ -716,6 +716,7 @@ public:
virtual void page_did_request_fullscreen_window() { }
virtual void page_did_request_exit_fullscreen() { }
virtual void page_did_exit_picture_in_picture() { }
+ virtual void page_did_change_picture_in_picture_video_size(Gfx::IntSize) { }
virtual void page_did_change_picture_in_picture_player_controls_visibility(bool) { }
virtual void page_did_change_active_document_in_top_level_browsing_context(Web::DOM::Document&) { }
virtual void page_did_finish_loading(HTML::CrossProcessId, Optional<Utf16String> const&) { }
diff --git a/Libraries/LibWeb/PictureInPicture/PictureInPictureController.cpp b/Libraries/LibWeb/PictureInPicture/PictureInPictureController.cpp
index 47975c58c7a..2e1034e7a8e 100644
--- a/Libraries/LibWeb/PictureInPicture/PictureInPictureController.cpp
+++ b/Libraries/LibWeb/PictureInPicture/PictureInPictureController.cpp
@@ -362,6 +362,16 @@ void PictureInPictureController::window_did_resize(Gfx::IntSize window_size)
}));
}
+// The window keeps the aspect ratio of its video, including while it opens.
+void PictureInPictureController::video_size_did_change(HTML::HTMLVideoElement& video, Gfx::IntSize video_size)
+{
+ auto is_open_window_video = m_open_window && &m_open_window->video() == &video;
+ auto is_opening_window_video = m_window_awaiting_open.has_value() && m_window_awaiting_open->request.video.ptr() == &video;
+ if (!is_open_window_video && !is_opening_window_video)
+ return;
+ m_page->client().page_did_change_picture_in_picture_video_size(video_size);
+}
+
void PictureInPictureController::window_did_close(bool pause_video)
{
if (!m_open_window)
diff --git a/Libraries/LibWeb/PictureInPicture/PictureInPictureController.h b/Libraries/LibWeb/PictureInPicture/PictureInPictureController.h
index 958889c7b7d..634ad1f8607 100644
--- a/Libraries/LibWeb/PictureInPicture/PictureInPictureController.h
+++ b/Libraries/LibWeb/PictureInPicture/PictureInPictureController.h
@@ -34,6 +34,7 @@ public:
void did_open_window(Gfx::IntSize window_size);
void window_did_resize(Gfx::IntSize);
+ void video_size_did_change(HTML::HTMLVideoElement&, Gfx::IntSize);
void window_did_close(bool pause_video);
bool is_waiting_for_window_to_open() const { return m_window_awaiting_open.has_value(); }
diff --git a/Libraries/LibWebView/HeadlessWebView.cpp b/Libraries/LibWebView/HeadlessWebView.cpp
index 9a0fecec1b2..2f9a058c839 100644
--- a/Libraries/LibWebView/HeadlessWebView.cpp
+++ b/Libraries/LibWebView/HeadlessWebView.cpp
@@ -13,7 +13,7 @@ namespace WebView {
static Web::DevicePixelRect const screen_rect { 0, 0, 1920, 1080 };
static constexpr auto child_close_timeout_ms = 1000;
-// A headless window is never shown, so it keeps the size it opens with until it is closed.
+// A headless window is never shown, so it only changes size to keep the aspect ratio of its video.
class HeadlessPictureInPictureWindow final : public PictureInPictureWindow {
public:
AK_ALLOC_WITH_KMALLOC;
@@ -29,6 +29,18 @@ public:
virtual String handle() const override { return m_view->handle(); }
virtual void hide() override { }
+ virtual void set_video_size(Gfx::IntSize video_size) override
+ {
+ auto size = size_for_video_size(m_size, video_size, screen_rect.size().to_type<int>());
+ if (size == m_size)
+ return;
+
+ m_size = size;
+ m_view->reset_viewport_size(size.to_type<Web::DevicePixels>());
+ if (on_resize)
+ on_resize(size);
+ }
+
private:
Gfx::IntSize m_size;
NonnullOwnPtr<HeadlessWebView> m_view;
diff --git a/Libraries/LibWebView/PictureInPictureManager.cpp b/Libraries/LibWebView/PictureInPictureManager.cpp
index 35d2fb7e832..ca8b812ca30 100644
--- a/Libraries/LibWebView/PictureInPictureManager.cpp
+++ b/Libraries/LibWebView/PictureInPictureManager.cpp
@@ -62,6 +62,12 @@ void PictureInPictureManager::close_window(WebContentPage& page)
retire_window(*index);
}
+void PictureInPictureManager::video_size_did_change(WebContentPage& page, Gfx::IntSize video_size)
+{
+ if (auto index = index_of_window_for(page); index.has_value())
+ m_windows[*index].window->set_video_size(video_size);
+}
+
Optional<size_t> PictureInPictureManager::index_of_window_for(WebContentPage const& page) const
{
return m_windows.find_first_index_if([&](auto const& window) { return window.page.ptr() == &page; });
diff --git a/Libraries/LibWebView/PictureInPictureManager.h b/Libraries/LibWebView/PictureInPictureManager.h
index b54bec8a3e9..3a78373b24d 100644
--- a/Libraries/LibWebView/PictureInPictureManager.h
+++ b/Libraries/LibWebView/PictureInPictureManager.h
@@ -32,6 +32,7 @@ public:
// Returns the window's handle, or an empty string when no window could be opened.
String open_window(WebContentPage& requesting_page, CanonicalTraversable&, Gfx::IntSize video_size);
void close_window(WebContentPage&);
+ void video_size_did_change(WebContentPage&, Gfx::IntSize);
private:
struct Window {
diff --git a/Libraries/LibWebView/PictureInPictureWindow.cpp b/Libraries/LibWebView/PictureInPictureWindow.cpp
index 5313a6b26ae..7256c80c66c 100644
--- a/Libraries/LibWebView/PictureInPictureWindow.cpp
+++ b/Libraries/LibWebView/PictureInPictureWindow.cpp
@@ -9,25 +9,40 @@
namespace WebView {
-Gfx::IntSize PictureInPictureWindow::initial_size(Gfx::IntSize video_size, Gfx::IntSize screen_size)
+Gfx::IntSize PictureInPictureWindow::aspect_ratio(Gfx::IntSize video_size)
{
if (video_size.is_empty())
- video_size = { 16, 9 };
+ return { 16, 9 };
+ return video_size;
+}
+
+static Gfx::IntSize size_with_width(Gfx::IntSize video_size, int width, Gfx::IntSize screen_size)
+{
+ auto aspect_ratio = PictureInPictureWindow::aspect_ratio(video_size);
// 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 width = screen_size.width() / 4;
- auto height = width * video_size.height() / video_size.width();
+ auto height = width * aspect_ratio.height() / aspect_ratio.width();
if (auto maximum_height = screen_size.height() / 2; height > maximum_height) {
height = maximum_height;
- width = height * video_size.width() / video_size.height();
+ width = height * aspect_ratio.width() / aspect_ratio.height();
}
return { width, height };
}
+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);
+}
+
+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);
+}
+
void PictureInPictureWindow::report_page_close_of(ViewImplementation& view)
{
view.on_close = [this] {
diff --git a/Libraries/LibWebView/PictureInPictureWindow.h b/Libraries/LibWebView/PictureInPictureWindow.h
index d588142fe65..dfa3f8dd47c 100644
--- a/Libraries/LibWebView/PictureInPictureWindow.h
+++ b/Libraries/LibWebView/PictureInPictureWindow.h
@@ -14,18 +14,23 @@
namespace WebView {
-// The floating window that shows a page's Picture-in-Picture video. Sizes are in CSS pixels.
+// The floating window that shows a page's Picture-in-Picture video, and keeps that video's aspect ratio. Sizes are in
+// CSS pixels.
class WEBVIEW_API PictureInPictureWindow {
public:
AK_ALLOC_WITH_KMALLOC;
+ static Gfx::IntSize aspect_ratio(Gfx::IntSize video_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.
+ static Gfx::IntSize size_for_video_size(Gfx::IntSize window_size, Gfx::IntSize video_size, Gfx::IntSize screen_size);
virtual ~PictureInPictureWindow() = default;
virtual Gfx::IntSize size() const = 0;
virtual String handle() const = 0;
virtual void hide() = 0;
+ virtual void set_video_size(Gfx::IntSize) = 0;
// The user closes the window to stop watching its video, or to go back to the video's tab and watch it there.
enum class VideoKeepsPlaying : bool {
diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cpp
index 21fa3e24e7f..7fb1f126090 100644
--- a/Libraries/LibWebView/WebContentPage.cpp
+++ b/Libraries/LibWebView/WebContentPage.cpp
@@ -1424,6 +1424,11 @@ void WebContentPage::did_exit_picture_in_picture()
PictureInPictureManager::the().close_window(*this);
}
+void WebContentPage::did_change_picture_in_picture_video_size(Gfx::IntSize video_size)
+{
+ PictureInPictureManager::the().video_size_did_change(*this, video_size);
+}
+
void WebContentPage::did_change_picture_in_picture_player_controls_visibility(bool shown)
{
view().did_change_picture_in_picture_player_controls_visibility({}, shown);
diff --git a/Libraries/LibWebView/WebContentPage.h b/Libraries/LibWebView/WebContentPage.h
index b20236be18a..8c95772d0df 100644
--- a/Libraries/LibWebView/WebContentPage.h
+++ b/Libraries/LibWebView/WebContentPage.h
@@ -245,6 +245,7 @@ private:
virtual void did_request_fullscreen_window() override;
virtual void did_request_exit_fullscreen() override;
virtual void did_exit_picture_in_picture() override;
+ virtual void did_change_picture_in_picture_video_size(Gfx::IntSize video_size) override;
virtual void did_change_picture_in_picture_player_controls_visibility(bool shown) override;
virtual void did_request_file(ByteString path, i32 request_id) override;
virtual void did_request_color_picker(Color current_color) override;
diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cpp
index 77a7d322679..1767c3a83c9 100644
--- a/Services/WebContent/PageClient.cpp
+++ b/Services/WebContent/PageClient.cpp
@@ -913,6 +913,11 @@ void PageClient::page_did_exit_picture_in_picture()
client().async_did_exit_picture_in_picture(m_id);
}
+void PageClient::page_did_change_picture_in_picture_video_size(Gfx::IntSize video_size)
+{
+ client().async_did_change_picture_in_picture_video_size(m_id, video_size);
+}
+
void PageClient::page_did_change_picture_in_picture_player_controls_visibility(bool shown)
{
client().async_did_change_picture_in_picture_player_controls_visibility(m_id, shown);
diff --git a/Services/WebContent/PageClient.h b/Services/WebContent/PageClient.h
index e0e486fd9d1..177a0715170 100644
--- a/Services/WebContent/PageClient.h
+++ b/Services/WebContent/PageClient.h
@@ -237,6 +237,7 @@ private:
virtual void page_did_request_fullscreen_window() override;
virtual void page_did_request_exit_fullscreen() override;
virtual void page_did_exit_picture_in_picture() override;
+ virtual void page_did_change_picture_in_picture_video_size(Gfx::IntSize) override;
virtual void page_did_change_picture_in_picture_player_controls_visibility(bool shown) override;
virtual void page_did_request_tooltip_override(Web::CSSPixelPoint, ByteString const&) override;
virtual void page_did_stop_tooltip_override() override;
diff --git a/Services/WebContent/WebContentClient.ipc b/Services/WebContent/WebContentClient.ipc
index f2b5db8e0ad..e115731e909 100644
--- a/Services/WebContent/WebContentClient.ipc
+++ b/Services/WebContent/WebContentClient.ipc
@@ -223,6 +223,7 @@ endpoint WebContentClient routed by Web::PageId page_id
did_request_fullscreen_window(Web::PageId page_id) =|
did_request_exit_fullscreen(Web::PageId page_id) =|
did_exit_picture_in_picture(Web::PageId page_id) =|
+ did_change_picture_in_picture_video_size(Web::PageId page_id, Gfx::IntSize video_size) =|
did_change_picture_in_picture_player_controls_visibility(Web::PageId page_id, bool shown) =|
did_request_file(Web::PageId page_id, ByteString path, i32 request_id) =|
did_request_color_picker(Web::PageId page_id, Color current_color) =|
diff --git a/Tests/LibWeb/Text/expected/PictureInPicture/window-keeps-aspect-ratio-of-video.txt b/Tests/LibWeb/Text/expected/PictureInPicture/window-keeps-aspect-ratio-of-video.txt
new file mode 100644
index 00000000000..05e56a2108a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/PictureInPicture/window-keeps-aspect-ratio-of-video.txt
@@ -0,0 +1,2 @@
+window for a 854x480 video: 480x269
+window for a 160x120 video: 480x360
diff --git a/Tests/LibWeb/Text/input/PictureInPicture/window-keeps-aspect-ratio-of-video.html b/Tests/LibWeb/Text/input/PictureInPicture/window-keeps-aspect-ratio-of-video.html
new file mode 100644
index 00000000000..dad425bc089
--- /dev/null
+++ b/Tests/LibWeb/Text/input/PictureInPicture/window-keeps-aspect-ratio-of-video.html
@@ -0,0 +1,22 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<video src="../../../Assets/test-webm.webm"></video>
+<script>
+ asyncTest(async done => {
+ const video = document.querySelector("video");
+ if (video.readyState === HTMLMediaElement.HAVE_NOTHING)
+ await new Promise(resolve => video.addEventListener("loadedmetadata", resolve, { once: true }));
+
+ internals.grantTransientActivation();
+ const pipWindow = await video.requestPictureInPicture();
+ println(`window for a ${video.videoWidth}x${video.videoHeight} video: ${pipWindow.width}x${pipWindow.height}`);
+
+ const resized = new Promise(resolve => pipWindow.addEventListener("resize", resolve, { once: true }));
+ video.src = "../../../Assets/solid-blue-160x120.webm";
+ await resized;
+ println(`window for a ${video.videoWidth}x${video.videoHeight} video: ${pipWindow.width}x${pipWindow.height}`);
+
+ await document.exitPictureInPicture();
+ done();
+ });
+</script>
diff --git a/UI/Qt/CMakeLists.txt b/UI/Qt/CMakeLists.txt
index 3cc1a80565b..2e9ad064d23 100644
--- a/UI/Qt/CMakeLists.txt
+++ b/UI/Qt/CMakeLists.txt
@@ -18,6 +18,7 @@ endif()
if (LINUX AND NOT ANDROID)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GIO REQUIRED IMPORTED_TARGET gio-2.0 gio-unix-2.0)
+ pkg_check_modules(XCB_ICCCM REQUIRED IMPORTED_TARGET xcb-icccm)
endif()
qt_add_executable(ladybird main.cpp)
@@ -74,8 +75,8 @@ if (TARGET Qt6::Positioning)
endif()
if (LINUX AND NOT ANDROID)
- target_sources(ladybird PRIVATE ExternalURLActivationToken.cpp ExternalURLHandler.cpp)
- target_link_libraries(ladybird PRIVATE LibThreading PkgConfig::GIO Qt::GuiPrivate)
+ target_sources(ladybird PRIVATE ExternalURLActivationToken.cpp ExternalURLHandler.cpp X11Window.cpp X11Window.h)
+ target_link_libraries(ladybird PRIVATE LibThreading PkgConfig::GIO PkgConfig::XCB_ICCCM Qt::GuiPrivate)
endif()
if (APPLE)
diff --git a/UI/Qt/MacWindow.h b/UI/Qt/MacWindow.h
index 469b46998f7..fe90a1a58ef 100644
--- a/UI/Qt/MacWindow.h
+++ b/UI/Qt/MacWindow.h
@@ -27,6 +27,7 @@ Optional<u64> appkit_display_id_for_window(QWidget&);
void hide_appkit_window_title(QWidget&);
void make_appkit_window_resizable(QWidget&);
void keep_appkit_window_visible_while_inactive(QWidget&);
+void set_appkit_window_content_aspect_ratio(QWidget&, Gfx::IntSize);
void offset_appkit_window_controls(QWidget&, int x_offset, int y_offset);
void install_appkit_event_capture();
void make_appkit_window_first_responder(QWidget&);
diff --git a/UI/Qt/MacWindow.mm b/UI/Qt/MacWindow.mm
index 835a94b89cd..998a1f7c153 100644
--- a/UI/Qt/MacWindow.mm
+++ b/UI/Qt/MacWindow.mm
@@ -8,6 +8,7 @@
#include <LibGfx/Color.h>
#include <LibGfx/Point.h>
+#include <LibGfx/Size.h>
#include <QAbstractNativeEventFilter>
#include <QCoreApplication>
#include <QMouseEvent>
@@ -400,6 +401,14 @@ void keep_appkit_window_visible_while_inactive(QWidget& widget)
view.window.hidesOnDeactivate = NO;
}
+void set_appkit_window_content_aspect_ratio(QWidget& widget, Gfx::IntSize aspect_ratio)
+{
+ auto* view = reinterpret_cast<NSView*>(widget.winId());
+ if (!view || !view.window)
+ return;
+ view.window.contentAspectRatio = NSMakeSize(aspect_ratio.width(), aspect_ratio.height());
+}
+
void offset_appkit_window_controls(QWidget& widget, int x_offset, int y_offset)
{
auto* view = reinterpret_cast<NSView*>(widget.winId());
diff --git a/UI/Qt/PictureInPictureWindow.cpp b/UI/Qt/PictureInPictureWindow.cpp
index 28c1a88375d..d9f3a75e5c1 100644
--- a/UI/Qt/PictureInPictureWindow.cpp
+++ b/UI/Qt/PictureInPictureWindow.cpp
@@ -24,6 +24,14 @@
# include <UI/Qt/MacWindow.h>
#endif
+#if defined(AK_OS_WINDOWS)
+# include <AK/Windows.h>
+#endif
+
+#if defined(AK_OS_LINUX)
+# include <UI/Qt/X11Window.h>
+#endif
+
namespace Ladybird {
static constexpr int screen_edge_margin = 16;
@@ -117,6 +125,14 @@ public:
#if defined(AK_OS_MACOS)
// 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);
+
+#if defined(AK_OS_LINUX)
+ // Qt also 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);
+ });
#endif
}
@@ -124,6 +140,24 @@ public:
virtual String handle() const override { return m_view->handle(); }
virtual void hide() override { QWidget::hide(); }
+ virtual void set_video_size(Gfx::IntSize video_size) override
+ {
+ auto available = screen()->availableGeometry();
+ auto size = size_for_video_size({ width(), height() }, video_size, { available.width(), available.height() });
+ if (size != Gfx::IntSize { width(), height() }) {
+ // The window stays in the corner of the screen it is nearest.
+ auto old_geometry = geometry();
+ QRect new_geometry { old_geometry.topLeft(), QSize { size.width(), size.height() } };
+ if (old_geometry.center().x() > available.center().x())
+ new_geometry.moveRight(old_geometry.right());
+ if (old_geometry.center().y() > available.center().y())
+ new_geometry.moveBottom(old_geometry.bottom());
+ setGeometry(new_geometry);
+ }
+
+ lock_aspect_ratio(video_size);
+ }
+
private:
// A drag moves the window, unless the page claims the press that starts it, as the media controls do to scrub. Every
// press off the window's controls reaches the page, and a drag waits until the page has handled its press rather than
@@ -221,6 +255,52 @@ 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)
+ {
+ m_aspect_ratio = aspect_ratio(video_size);
+#if defined(AK_OS_MACOS)
+ set_appkit_window_content_aspect_ratio(*this, m_aspect_ratio);
+#elif defined(AK_OS_LINUX)
+ set_x11_window_aspect_ratio(*this, m_aspect_ratio);
+#endif
+ // Wayland has no window hint for an aspect ratio, so there the window takes its video's shape only when it opens
+ // or that shape changes.
+ }
+
+#if defined(AK_OS_WINDOWS)
+ // Windows lets a window adjust the rect that each step of a resize from its edges would give it.
+ virtual bool nativeEvent(QByteArray const& event_type, void* message, qintptr* result) override
+ {
+ auto* windows_message = static_cast<MSG*>(message);
+ if (windows_message->message != WM_SIZING)
+ return QWidget::nativeEvent(event_type, message, result);
+
+ auto& rect = *reinterpret_cast<RECT*>(windows_message->lParam);
+ auto width = rect.right - rect.left;
+ auto height = rect.bottom - rect.top;
+
+ // A top or bottom edge sets the height, and any other edge or corner sets the width. The other dimension then
+ // moves the edge that is being dragged, or else the right or bottom edge.
+ switch (windows_message->wParam) {
+ case WMSZ_TOP:
+ case WMSZ_BOTTOM:
+ rect.right = rect.left + height * m_aspect_ratio.width() / m_aspect_ratio.height();
+ break;
+ case WMSZ_TOPLEFT:
+ case WMSZ_TOPRIGHT:
+ rect.top = rect.bottom - width * m_aspect_ratio.height() / m_aspect_ratio.width();
+ break;
+ default:
+ rect.bottom = rect.top + width * m_aspect_ratio.height() / m_aspect_ratio.width();
+ break;
+ }
+
+ *result = TRUE;
+ return true;
+ }
+#endif
+
virtual void resizeEvent(QResizeEvent* event) override
{
QWidget::resizeEvent(event);
@@ -239,6 +319,7 @@ private:
WebContentView* m_view { nullptr };
WindowScreenObserver* m_screen_observer { nullptr };
+ Gfx::IntSize m_aspect_ratio;
enum class PressClaim : u8 {
Unknown,
diff --git a/UI/Qt/X11Window.cpp b/UI/Qt/X11Window.cpp
new file mode 100644
index 00000000000..97ba93a473b
--- /dev/null
+++ b/UI/Qt/X11Window.cpp
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibGfx/Size.h>
+#include <UI/Qt/X11Window.h>
+
+#include <QGuiApplication>
+#include <QWidget>
+
+#include <xcb/xcb_icccm.h>
+
+namespace Ladybird {
+
+void set_x11_window_aspect_ratio(QWidget& widget, Gfx::IntSize aspect_ratio)
+{
+ auto* x11_application = qGuiApp->nativeInterface<QNativeInterface::QX11Application>();
+ if (!x11_application)
+ return;
+
+ auto* connection = x11_application->connection();
+ auto window = static_cast<xcb_window_t>(widget.winId());
+
+ // Qt writes all of the window's size hints whenever it changes the window's geometry, so the aspect ratio joins the
+ // hints it last wrote, and has to be set again after any such change.
+ xcb_size_hints_t hints {};
+ xcb_icccm_get_wm_normal_hints_reply(connection, xcb_icccm_get_wm_normal_hints(connection, window), &hints, nullptr);
+ xcb_icccm_size_hints_set_aspect(&hints, aspect_ratio.width(), aspect_ratio.height(), aspect_ratio.width(), aspect_ratio.height());
+ xcb_icccm_set_wm_normal_hints(connection, window, &hints);
+ xcb_flush(connection);
+}
+
+}
diff --git a/UI/Qt/X11Window.h b/UI/Qt/X11Window.h
new file mode 100644
index 00000000000..862959ce8f1
--- /dev/null
+++ b/UI/Qt/X11Window.h
@@ -0,0 +1,18 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibGfx/Forward.h>
+
+class QWidget;
+
+namespace Ladybird {
+
+// Does nothing unless the widget's window is on an X11 display.
+void set_x11_window_aspect_ratio(QWidget&, Gfx::IntSize);
+
+}