Skip to content

Truly independent web browser

UI/Qt: Add a Picture-in-Picture window

Luke Wildef973bfcca9be
diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cpp
index 77d49048dce..cab9023b755 100644
--- a/UI/Qt/Application.cpp
+++ b/UI/Qt/Application.cpp
@@ -17,6 +17,7 @@
#include <UI/Qt/EventLoopImplementationQt.h>
#include <UI/Qt/ExternalURLHandler.h>
#include <UI/Qt/Menu.h>
+#include <UI/Qt/PictureInPictureWindow.h>
#include <UI/Qt/ProcessManagerWindow.h>
#include <UI/Qt/Settings.h>
#include <UI/Qt/StringUtils.h>
@@ -962,6 +963,13 @@ bool Application::confirm_stop_active_downloads(QWidget* parent)
return true;
}
+OwnPtr<WebView::PictureInPictureWindow> Application::create_picture_in_picture_window(WebView::WebContentPage& requesting_page, WebView::CanonicalTraversable& traversable, Gfx::IntSize video_size)
+{
+ if (browser_options().headless_mode.has_value())
+ return WebView::Application::create_picture_in_picture_window(requesting_page, traversable, video_size);
+ return Ladybird::create_picture_in_picture_window(traversable, requesting_page.view(), video_size);
+}
+
Optional<WebView::ViewImplementation&> Application::active_web_view() const
{
if (auto* active_tab = this->active_tab())
diff --git a/UI/Qt/Application.h b/UI/Qt/Application.h
index d8695e11182..1f6d9ce21d4 100644
--- a/UI/Qt/Application.h
+++ b/UI/Qt/Application.h
@@ -123,6 +123,9 @@ private:
virtual Optional<String> system_font_family() const override;
#endif
+ virtual bool supports_picture_in_picture() const override { return true; }
+ virtual OwnPtr<WebView::PictureInPictureWindow> create_picture_in_picture_window(WebView::WebContentPage& requesting_page, WebView::CanonicalTraversable&, Gfx::IntSize video_size) override;
+
virtual Optional<WebView::ViewImplementation&> active_web_view() const override;
virtual Vector<WebView::ViewImplementation&> active_window_web_views() const override;
virtual bool activate_tab_with_url(URL::URL const&) const override;
diff --git a/UI/Qt/CMakeLists.txt b/UI/Qt/CMakeLists.txt
index 1cfd47d67c5..3cc1a80565b 100644
--- a/UI/Qt/CMakeLists.txt
+++ b/UI/Qt/CMakeLists.txt
@@ -46,6 +46,7 @@ target_sources(ladybird PRIVATE
NativeTextEditingLineEdit.cpp
ProcessManagerWindow.cpp
PerformanceMonitorWidget.cpp
+ PictureInPictureWindow.cpp
Popover.cpp
SelectDropdown.cpp
Settings.cpp
diff --git a/UI/Qt/MacWindow.h b/UI/Qt/MacWindow.h
index 4fe63ea58ef..469b46998f7 100644
--- a/UI/Qt/MacWindow.h
+++ b/UI/Qt/MacWindow.h
@@ -25,6 +25,8 @@ namespace Ladybird {
// The CGDirectDisplayID of the screen the widget's window is on, or of the main screen while it has no window yet.
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 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 a0e92822168..9863d5f79ca 100644
--- a/UI/Qt/MacWindow.mm
+++ b/UI/Qt/MacWindow.mm
@@ -339,6 +339,22 @@ void hide_appkit_window_title(QWidget& widget)
window.titleVisibility = NSWindowTitleHidden;
}
+void make_appkit_window_resizable(QWidget& widget)
+{
+ auto* view = reinterpret_cast<NSView*>(widget.winId());
+ if (!view || !view.window)
+ return;
+ view.window.styleMask |= NSWindowStyleMaskResizable;
+}
+
+void keep_appkit_window_visible_while_inactive(QWidget& widget)
+{
+ auto* view = reinterpret_cast<NSView*>(widget.winId());
+ if (!view || !view.window)
+ return;
+ view.window.hidesOnDeactivate = NO;
+}
+
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
new file mode 100644
index 00000000000..f05d7bdcffa
--- /dev/null
+++ b/UI/Qt/PictureInPictureWindow.cpp
@@ -0,0 +1,224 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <UI/Qt/PictureInPictureWindow.h>
+#include <UI/Qt/WebContentView.h>
+#include <UI/Qt/WindowScreenObserver.h>
+
+#include <QApplication>
+#include <QCloseEvent>
+#include <QMouseEvent>
+#include <QNativeGestureEvent>
+#include <QResizeEvent>
+#include <QScreen>
+#include <QVBoxLayout>
+#include <QWheelEvent>
+#include <QWidget>
+#include <QWindow>
+
+#if defined(AK_OS_MACOS)
+# include <UI/Qt/MacWindow.h>
+#endif
+
+namespace Ladybird {
+
+static constexpr int screen_edge_margin = 16;
+static constexpr int resize_margin = 6;
+
+static Qt::WindowFlags window_flags()
+{
+#if defined(AK_OS_MACOS)
+ // A tool window is a panel on macOS, which never becomes the main window, so activating Ladybird brings a browser
+ // window forward rather than this one.
+ return Qt::Tool | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint;
+#else
+ return Qt::Window | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint;
+#endif
+}
+
+// A frameless window, placed in the bottom-right corner of the screen it is opened from, that floats above other windows.
+// It is moved by dragging anywhere on its page, and resized from its edges.
+class PictureInPictureWindow final
+ : public QWidget
+ , public WebView::PictureInPictureWindow {
+public:
+ AK_ALLOC_WITH_KMALLOC;
+
+ PictureInPictureWindow(WebView::CanonicalTraversable& traversable, WebView::ViewImplementation const& owner_view, Gfx::IntSize video_size)
+ : QWidget(nullptr, window_flags())
+ {
+ // The window's native window and view are created for the screen of the browser window it is opened from.
+ auto* screen = static_cast<WebContentView const&>(owner_view).screen();
+ setScreen(screen);
+
+ setAttribute(Qt::WA_ShowWithoutActivating);
+#if defined(AK_OS_MACOS)
+ // A tool window otherwise hides whenever Ladybird is inactive. Qt's WA_MacAlwaysShowToolWindow would also make
+ // it order every window to the front whenever Ladybird is activated, bringing browser windows forward too.
+ keep_appkit_window_visible_while_inactive(*this);
+#endif
+ setWindowTitle("Picture-in-Picture");
+
+ auto available = screen->availableGeometry();
+
+ m_screen_observer = new WindowScreenObserver(*this);
+ auto view_initial_state = WebContentViewInitialState {
+ .is_private = owner_view.is_private(),
+ .maximum_frames_per_second = m_screen_observer->refresh_rate(),
+ .display_id = m_screen_observer->display_id(),
+ .owner_view = &owner_view,
+ };
+ m_view = new WebContentView(this, traversable, view_initial_state);
+ m_screen_observer->on_device_pixel_ratio_change = [this] {
+ m_view->set_device_pixel_ratio(m_screen_observer->device_pixel_ratio());
+ };
+ m_screen_observer->on_display_metadata_change = [this] {
+ m_view->set_display_metadata(m_screen_observer->display_id(), m_screen_observer->refresh_rate());
+ };
+ report_page_close_of(*m_view);
+ m_view->installEventFilter(this);
+ m_view->on_finish_handling_mouse_event = [this](Web::MouseEvent const& event, Web::EventResult result) {
+ if (event.type == Web::MouseEvent::Type::MouseDown)
+ page_did_handle_press(result);
+ };
+
+ auto* layout = new QVBoxLayout(this);
+ layout->setContentsMargins(0, 0, 0, 0);
+ layout->addWidget(m_view);
+
+ auto size = initial_size(video_size, { available.width(), available.height() });
+ resize(size.width(), size.height());
+ move(available.right() - size.width() - screen_edge_margin, available.bottom() - size.height() - screen_edge_margin);
+ show();
+
+#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
+ }
+
+ virtual Gfx::IntSize size() const override { return { width(), height() }; }
+ virtual String handle() const override { return m_view->handle(); }
+ virtual void hide() override { QWidget::hide(); }
+
+private:
+ // A drag moves the window, unless the page claims the press that starts it, as the media controls do to scrub. Every
+ // press reaches the page, and a drag waits until the page has handled its press rather than guess while it is busy.
+ virtual bool eventFilter(QObject* object, QEvent* event) override
+ {
+ if (object != m_view)
+ return false;
+
+ switch (event->type()) {
+ case QEvent::MouseButtonPress: {
+ auto* mouse_event = static_cast<QMouseEvent*>(event);
+ if (mouse_event->button() != Qt::LeftButton)
+ return false;
+#if !defined(AK_OS_MACOS)
+ if (auto edges = resize_edges_for(mouse_event->position().toPoint()); edges != Qt::Edges {}) {
+ if (windowHandle()->startSystemResize(edges))
+ return true;
+ }
+#endif
+ m_press = Press { .position = mouse_event->globalPosition().toPoint() };
+ return false;
+ }
+ case QEvent::MouseMove: {
+ if (!m_press.has_value())
+ return false;
+ if (m_press->is_moving_window)
+ return true;
+ if (m_press->claim != PressClaim::Window)
+ return false;
+ auto* mouse_event = static_cast<QMouseEvent*>(event);
+ if ((mouse_event->globalPosition().toPoint() - m_press->position).manhattanLength() < QApplication::startDragDistance())
+ return false;
+ m_press->is_moving_window = true;
+ // The window takes the press over, so the page sees it cancelled.
+ m_view->cancel_mouse_press();
+#if defined(AK_OS_MACOS)
+ if (start_appkit_window_drag(*this))
+ return true;
+#endif
+ windowHandle()->startSystemMove();
+ return true;
+ }
+ case QEvent::MouseButtonRelease: {
+ // The page's press was cancelled once it moved the window, so the page does not see it end.
+ auto press_moved_window = m_press.has_value() && m_press->is_moving_window;
+ m_press.clear();
+ return press_moved_window;
+ }
+ case QEvent::NativeGesture:
+ // The window is sized to its video, so its page is never zoomed.
+ return static_cast<QNativeGestureEvent*>(event)->gestureType() == Qt::ZoomNativeGesture;
+ case QEvent::Wheel:
+ return static_cast<QWheelEvent*>(event)->modifiers().testFlag(Qt::ControlModifier);
+ default:
+ return false;
+ }
+ }
+
+ void page_did_handle_press(Web::EventResult result)
+ {
+ if (!m_press.has_value() || m_press->claim != PressClaim::Unknown)
+ return;
+ // A press that runs its default actions is handled too, so only one the page cancelled is claimed.
+ m_press->claim = result == Web::EventResult::Cancelled ? PressClaim::Page : PressClaim::Window;
+ }
+
+ Qt::Edges resize_edges_for(QPoint position) const
+ {
+ Qt::Edges edges;
+ if (position.x() < resize_margin)
+ edges |= Qt::LeftEdge;
+ if (position.x() >= width() - resize_margin)
+ edges |= Qt::RightEdge;
+ if (position.y() < resize_margin)
+ edges |= Qt::TopEdge;
+ if (position.y() >= height() - resize_margin)
+ edges |= Qt::BottomEdge;
+ return edges;
+ }
+
+ virtual void resizeEvent(QResizeEvent* event) override
+ {
+ QWidget::resizeEvent(event);
+ if (on_resize)
+ on_resize(size());
+ }
+
+ virtual void closeEvent(QCloseEvent* event) override
+ {
+ // Closing only hides the window, which stays until its page has closed, as the manager asks for once the page's
+ // video has exited.
+ event->accept();
+ if (on_close)
+ on_close();
+ }
+
+ WebContentView* m_view { nullptr };
+ WindowScreenObserver* m_screen_observer { nullptr };
+
+ enum class PressClaim : u8 {
+ Unknown,
+ Page,
+ Window,
+ };
+ struct Press {
+ QPoint position;
+ PressClaim claim { PressClaim::Unknown };
+ bool is_moving_window { false };
+ };
+ Optional<Press> m_press;
+};
+
+NonnullOwnPtr<WebView::PictureInPictureWindow> create_picture_in_picture_window(WebView::CanonicalTraversable& traversable, WebView::ViewImplementation const& owner_view, Gfx::IntSize video_size)
+{
+ return make<PictureInPictureWindow>(traversable, owner_view, video_size);
+}
+
+}
diff --git a/UI/Qt/PictureInPictureWindow.h b/UI/Qt/PictureInPictureWindow.h
new file mode 100644
index 00000000000..963111f0396
--- /dev/null
+++ b/UI/Qt/PictureInPictureWindow.h
@@ -0,0 +1,17 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/NonnullOwnPtr.h>
+#include <LibWebView/Forward.h>
+#include <LibWebView/PictureInPictureWindow.h>
+
+namespace Ladybird {
+
+NonnullOwnPtr<WebView::PictureInPictureWindow> create_picture_in_picture_window(WebView::CanonicalTraversable&, WebView::ViewImplementation const& owner_view, Gfx::IntSize video_size);
+
+}
diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cpp
index b16550c5870..94faf1f8262 100644
--- a/UI/Qt/Tab.cpp
+++ b/UI/Qt/Tab.cpp
@@ -542,6 +542,7 @@ Tab::Tab(BrowserWindow* window, Optional<WebView::CanonicalTraversable&> travers
.is_private = window->is_private(),
.maximum_frames_per_second = window->refresh_rate(),
.display_id = window->display_id(),
+ .owner_view = nullptr,
};
m_view = new WebContentView(this, traversable, AK::move(view_initial_state));
diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cpp
index 3f8ec11dd05..6734be8c5e3 100644
--- a/UI/Qt/WebContentView.cpp
+++ b/UI/Qt/WebContentView.cpp
@@ -164,6 +164,8 @@ WebContentView::WebContentView(QWidget* window, Optional<WebView::CanonicalTrave
this);
});
+ if (initial_state.owner_view)
+ set_owner_view(*initial_state.owner_view);
initialize_tab(Web::HTML::VisibilityState::Hidden, traversable);
m_accessibility_manager = make<WebView::AccessibilityTreeManager>();
@@ -803,6 +805,12 @@ void WebContentView::handle_pointer_leave()
enqueue_native_event(Web::MouseEvent::Type::MouseLeave, mouse_event);
}
+void WebContentView::cancel_mouse_press()
+{
+ static QMouseEvent mouse_event { QEvent::Type::MouseButtonRelease, {}, {}, Qt::MouseButton::NoButton, Qt::MouseButton::NoButton, Qt::KeyboardModifier::NoModifier };
+ enqueue_native_event(Web::MouseEvent::Type::MouseCancel, mouse_event);
+}
+
void WebContentView::mouseMoveEvent(QMouseEvent* event)
{
if (is_node_picker_active()) {
diff --git a/UI/Qt/WebContentView.h b/UI/Qt/WebContentView.h
index 0e81decf4a7..1cdae4432e6 100644
--- a/UI/Qt/WebContentView.h
+++ b/UI/Qt/WebContentView.h
@@ -87,6 +87,7 @@ struct WebContentViewInitialState {
WebView::IsPrivate is_private { WebView::IsPrivate::No };
double maximum_frames_per_second { 60.0 };
Optional<u64> display_id;
+ WebView::ViewImplementation const* owner_view { nullptr };
};
class WebContentView final
@@ -138,6 +139,7 @@ public:
void prepare_for_window_move();
void finish_window_move();
void close_select_dropdown_after_crash();
+ void cancel_mouse_press();
void review_hidden_crash_report(ByteString const& report_name);
enum class PaletteMode {