Truly independent web browser
LibWeb+LibWebView+UI/Qt: Add controls to the Picture-in-Picture window
Luke Wildef81e803fc0fc
diff --git a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp b/Libraries/LibWeb/HTML/HTMLMediaElement.cppindex 6f3ebc90590..62da312a76e 100644--- a/Libraries/LibWeb/HTML/HTMLMediaElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLMediaElement.cpp@@ -3482,7 +3482,15 @@ void HTMLMediaElement::reject_pending_play_promises(ReadonlySpan<GC::Ref<WebIDL:void HTMLMediaElement::create_controls(){if (!m_controls.has_value())- m_controls.emplace(*this, *this);+ m_controls.emplace(*this, media_element_for_controls());+}++void HTMLMediaElement::media_element_for_controls_changed()+{+ if (!m_controls.has_value())+ return;+ m_controls.clear();+ create_controls();}void HTMLMediaElement::destroy_controls()@@ -3490,4 +3498,10 @@ void HTMLMediaElement::destroy_controls()m_controls.clear();}+void HTMLMediaElement::set_picture_in_picture_window_controls_in_use(bool in_use)+{+ if (m_controls.has_value())+ m_controls->set_picture_in_picture_window_controls_in_use(in_use);+}+}diff --git a/Libraries/LibWeb/HTML/HTMLMediaElement.h b/Libraries/LibWeb/HTML/HTMLMediaElement.hindex b357d0baf0e..7546e21654e 100644--- a/Libraries/LibWeb/HTML/HTMLMediaElement.h+++ b/Libraries/LibWeb/HTML/HTMLMediaElement.h@@ -184,6 +184,8 @@ public:void create_controls();void destroy_controls();+ void media_element_for_controls_changed();+ void set_picture_in_picture_window_controls_in_use(bool);CORSSettingAttribute crossorigin() const { return m_crossorigin; }@@ -289,6 +291,7 @@ private:bool video_sink_should_tick() const;virtual bool is_shown_elsewhere() const { return false; }+ virtual HTMLMediaElement& media_element_for_controls() { return *this; }// Mirrors what PlaybackManager and the compositor were last told; a freshly reserved sink is assumed to tick.mutable bool m_video_sink_is_ticking { true };diff --git a/Libraries/LibWeb/HTML/HTMLVideoElement.cpp b/Libraries/LibWeb/HTML/HTMLVideoElement.cppindex 5d28cc77f43..51902c75c90 100644--- a/Libraries/LibWeb/HTML/HTMLVideoElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLVideoElement.cpp@@ -83,6 +83,7 @@ void HTMLVideoElement::set_visual_source(GC::Ptr<HTMLVideoElement> source)source->video_sink_ticking_inputs_changed();}Painting::push_video_paint_facts(*this);+ media_element_for_controls_changed();}void HTMLVideoElement::for_each_visual_clone(Function<void(HTMLVideoElement&)> const& callback) const@@ -107,6 +108,14 @@ bool HTMLVideoElement::is_shown_elsewhere() constreturn any_of(m_visual_clones, [](auto const& clone) { return clone.ptr() != nullptr; });}+// The controls of an element that shows another element's frames play that element's media.+HTMLMediaElement& HTMLVideoElement::media_element_for_controls()+{+ if (m_visual_source)+ return *m_visual_source;+ return *this;+}+void HTMLVideoElement::adopted_from(DOM::Document& old_document){Base::adopted_from(old_document);diff --git a/Libraries/LibWeb/HTML/HTMLVideoElement.h b/Libraries/LibWeb/HTML/HTMLVideoElement.hindex 15c5d15604a..3a4f1395996 100644--- a/Libraries/LibWeb/HTML/HTMLVideoElement.h+++ b/Libraries/LibWeb/HTML/HTMLVideoElement.h@@ -88,6 +88,7 @@ private:virtual bool is_html_video_element() const override { return true; }virtual bool is_shown_elsewhere() const override;+ virtual HTMLMediaElement& media_element_for_controls() override;virtual CSS::ElementBoxKind box_kind() const override;diff --git a/Libraries/LibWeb/HTML/MediaControls.cpp b/Libraries/LibWeb/HTML/MediaControls.cppindex 1b0b6268d22..e2c2fc9b99e 100644--- a/Libraries/LibWeb/HTML/MediaControls.cpp+++ b/Libraries/LibWeb/HTML/MediaControls.cpp@@ -25,6 +25,7 @@#include <LibWeb/HTML/Scripting/Environments.h>#include <LibWeb/HTML/TimeRanges.h>#include <LibWeb/HTML/Window.h>+#include <LibWeb/Page/Page.h>#include <LibWeb/UIEvents/EventNames.h>#include <LibWeb/UIEvents/KeyboardEvent.h>#include <LibWeb/UIEvents/MouseEvent.h>@@ -62,7 +63,15 @@ void MediaControls::create_shadow_tree()shadow_root->set_user_agent_internal(true);host.set_shadow_root(shadow_root);- m_dom = MediaControlsDOM(document, *shadow_root, is_video ? MediaControlsDOM::Options::Video : MediaControlsDOM::Options::None);+ auto options = MediaControlsDOM::Options::None;+ if (is_video) {+ options |= MediaControlsDOM::Options::Video;+ // Only an element that shows its own media can show it fullscreen. One that shows another element's media is+ // the player of a Picture-in-Picture window.+ if (&host == m_media_element.ptr().ptr())+ options |= MediaControlsDOM::Options::Fullscreen;+ }+ m_dom = MediaControlsDOM(document, *shadow_root, options);if (is_video)MUST(m_dom->container->class_list()->add("video"_utf16));@@ -396,6 +405,7 @@ void MediaControls::set_up_event_listeners()hide_controls();return true;});+add_event_listener(realm, *m_dom->control_bar, UIEvents::EventNames::mouseenter, [this] {m_hovering_controls = true;show_controls();@@ -769,6 +779,9 @@ void MediaControls::show_controls()MUST(m_dom->control_bar->class_list()->add(visible_class()));+ if (!exchange(m_control_bar_is_shown, true))+ control_bar_visibility_did_change();+if (!m_hover_timer) {constexpr int hover_timeout_ms = 1000;m_hover_timer = Core::Timer::create_single_shot(hover_timeout_ms, [&] {@@ -785,13 +798,36 @@ void MediaControls::hide_controls()VERIFY(m_media_element);VERIFY(m_dom->control_bar);- if (m_scrubbing_timeline != Scrubbing::No || m_scrubbing_volume || m_hovering_controls)+ if (m_scrubbing_timeline != Scrubbing::No || m_scrubbing_volume || m_hovering_controls || m_picture_in_picture_window_controls_in_use)return;if (m_dom->placeholder_circle && should_show_placeholder())return;MUST(m_dom->control_bar->class_list()->remove(visible_class()));m_hover_timer.clear();+ m_control_bar_is_shown = false;+ control_bar_visibility_did_change();+}++bool MediaControls::is_picture_in_picture_player() const+{+ return m_host.ptr() != m_media_element.ptr();+}++// The browser's controls over a Picture-in-Picture window show while its player's do, and the player's stay shown while+// the browser's are hovered, so that the two fade together.+void MediaControls::control_bar_visibility_did_change()+{+ if (is_picture_in_picture_player())+ m_host->document().page().client().page_did_change_picture_in_picture_player_controls_visibility(m_control_bar_is_shown);+}++void MediaControls::set_picture_in_picture_window_controls_in_use(bool in_use)+{+ if (!is_picture_in_picture_player())+ return;+ m_picture_in_picture_window_controls_in_use = in_use;+ show_controls();}}diff --git a/Libraries/LibWeb/HTML/MediaControls.h b/Libraries/LibWeb/HTML/MediaControls.hindex a45620fb38a..c57382250cb 100644--- a/Libraries/LibWeb/HTML/MediaControls.h+++ b/Libraries/LibWeb/HTML/MediaControls.h@@ -26,6 +26,8 @@ public:void visit_edges(GC::Cell::Visitor&);+ void set_picture_in_picture_window_controls_in_use(bool);+private:void create_shadow_tree();@@ -80,6 +82,8 @@ private:void show_controls();void hide_controls();+ bool is_picture_in_picture_player() const;+ void control_bar_visibility_did_change();// The controls live in the shadow tree of their host, and play the media of their media element. The two are one// element unless the host shows another element's media.@@ -106,6 +110,8 @@ private:RefPtr<Core::Timer> m_scrub_seek_preemption_timer;bool m_scrubbing_volume { false };bool m_hovering_controls { false };+ bool m_picture_in_picture_window_controls_in_use { false };+ bool m_control_bar_is_shown { false };RefPtr<Core::Timer> m_hover_timer;diff --git a/Libraries/LibWeb/HTML/MediaControls.html b/Libraries/LibWeb/HTML/MediaControls.htmlindex 5d373606ad3..6fbc6e9e13e 100644--- a/Libraries/LibWeb/HTML/MediaControls.html+++ b/Libraries/LibWeb/HTML/MediaControls.html@@ -37,7 +37,7 @@<div data-name="volume-fill" class="volume-fill"></div></div></div>- <button data-name="fullscreen-button" data-option="video" class="control-button fullscreen-button">+ <button data-name="fullscreen-button" data-option="fullscreen" class="control-button fullscreen-button"><svg data-name="fullscreen-icon" class="icon fullscreen-icon" viewBox="0 0 24 24"><path class="fullscreen-maximize-path" d="M3 3h6v2H5v4H3Zm12 0h6v6h-2V5h-4Zm6 12v6h-6v-2h4v-4ZM3 15v6h6v-2H5v-4Z"/><path class="fullscreen-minimize-path" d="M9 3v6H3V7h4V3Zm6 0v6h6V7h-4V3ZM3 15h6v6H7v-4H3Zm12 0v6h2v-4h4v-2Z"/>diff --git a/Libraries/LibWeb/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cppindex 832bdf2fcc4..fd6181ee9d2 100644--- a/Libraries/LibWeb/Page/Page.cpp+++ b/Libraries/LibWeb/Page/Page.cpp@@ -1719,6 +1719,22 @@ void Page::toggle_media_controls_state()media_element->set_attribute_value(HTML::AttributeNames::controls, Utf16String {});}+// The browser's controls over a Picture-in-Picture window keep the controls of the player that its page shows up while+// they are hovered or pressed, as the player's own controls do.+void Page::set_picture_in_picture_window_controls_in_use(bool in_use)+{+ if (!has_local_traversable())+ return;+ auto document = local_traversable()->active_document();+ if (!document)+ return;++ document->for_each_in_inclusive_subtree_of_type<HTML::HTMLMediaElement>([&](auto& media_element) {+ media_element.set_picture_in_picture_window_controls_in_use(in_use);+ return TraversalDecision::Continue;+ });+}+void Page::set_page_mute_state(HTML::MuteState mute_state){if (m_mute_state == mute_state)diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.hindex 224977d29d6..8133f74393d 100644--- a/Libraries/LibWeb/Page/Page.h+++ b/Libraries/LibWeb/Page/Page.h@@ -398,6 +398,7 @@ public:void toggle_media_loop_state();void toggle_media_fullscreen_state();void toggle_media_controls_state();+ void set_picture_in_picture_window_controls_in_use(bool);HTML::MuteState page_mute_state() const { return m_mute_state; }void set_page_mute_state(HTML::MuteState);@@ -715,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_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&) { }virtual Optional<u64> page_did_start_download(HTML::CrossProcessId navigable_id, Optional<Utf16String> const& navigation_id, URL::URL const&, ByteString const& suggested_filename, Optional<u64> total_size, int request_server_client_id, u64 request_server_request_id, ByteBuffer initial_data)diff --git a/Libraries/LibWeb/PictureInPicture/PictureInPictureController.cpp b/Libraries/LibWeb/PictureInPicture/PictureInPictureController.cppindex f023f64efcb..47975c58c7a 100644--- a/Libraries/LibWeb/PictureInPicture/PictureInPictureController.cpp+++ b/Libraries/LibWeb/PictureInPicture/PictureInPictureController.cpp@@ -146,7 +146,8 @@ void PictureInPictureController::process_pending_operations()}}-// The window's page is a player whose video shows the frames of the video in Picture-in-Picture.+// The window's page is a player whose video shows the frames of the video in Picture-in-Picture, and plays it+// through its controls.static void create_player(DOM::Document& document, HTML::HTMLVideoElement& source){PictureInPicturePlayerDOM player { document, *document.body() };@@ -361,10 +362,14 @@ void PictureInPictureController::window_did_resize(Gfx::IntSize window_size)}));}-void PictureInPictureController::window_did_close()+void PictureInPictureController::window_did_close(bool pause_video){- if (m_open_window)- enqueue(UserInterfaceClosedWindow { *m_open_window });+ if (!m_open_window)+ return;+ // The user closed the window to stop watching its video, rather than to go back to its tab and watch it there.+ if (pause_video)+ m_open_window->video().pause();+ enqueue(UserInterfaceClosedWindow { *m_open_window });}// https://w3c.github.io/picture-in-picture/#unloading-stepsdiff --git a/Libraries/LibWeb/PictureInPicture/PictureInPictureController.h b/Libraries/LibWeb/PictureInPicture/PictureInPictureController.hindex 313df18f314..958889c7b7d 100644--- a/Libraries/LibWeb/PictureInPicture/PictureInPictureController.h+++ b/Libraries/LibWeb/PictureInPicture/PictureInPictureController.h@@ -34,7 +34,7 @@ public:void did_open_window(Gfx::IntSize window_size);void window_did_resize(Gfx::IntSize);- void window_did_close();+ void window_did_close(bool pause_video);bool is_waiting_for_window_to_open() const { return m_window_awaiting_open.has_value(); }bool has_open_window() const { return m_open_window != nullptr; }diff --git a/Libraries/LibWeb/PictureInPicture/PictureInPicturePlayer.html b/Libraries/LibWeb/PictureInPicture/PictureInPicturePlayer.htmlindex b2985bb671a..1cd1c02d5f3 100644--- a/Libraries/LibWeb/PictureInPicture/PictureInPicturePlayer.html+++ b/Libraries/LibWeb/PictureInPicture/PictureInPicturePlayer.html@@ -1,2 +1,2 @@<link rel="stylesheet" href="PictureInPicturePlayer.css">-<video data-name="video" class="video"></video>+<video data-name="video" class="video" controls></video>diff --git a/Libraries/LibWebView/CompositorClient.cpp b/Libraries/LibWebView/CompositorClient.cppindex f9cd3cb4afa..2eb170af0d5 100644--- a/Libraries/LibWebView/CompositorClient.cpp+++ b/Libraries/LibWebView/CompositorClient.cpp@@ -45,14 +45,14 @@ void CompositorClient::did_allocate_backing_stores(Web::CompositorContextId contasync_presented_bitmap_ready_to_paint(context_id, bitmap_ids[0]);}-void CompositorClient::did_present_frame(Web::CompositorContextId context_id, Gfx::IntRect content_rect, Gfx::IntRect damage_rect, i32 bitmap_id)+void CompositorClient::did_present_frame(Web::CompositorContextId context_id, Gfx::IntRect content_rect, Gfx::IntRect damage_rect, i32 bitmap_id, double device_pixel_ratio){auto web_content_client = WebContentClient::client_for_compositor_context_id(context_id);if (web_content_client.has_value()) {auto page_id = web_content_client->page_id_for_compositor_context_id(context_id);VERIFY(page_id.has_value());if (auto* page = web_content_client->page(*page_id)) {- page->did_present_bitmap(content_rect, damage_rect, bitmap_id);+ page->did_present_bitmap(content_rect, damage_rect, bitmap_id, device_pixel_ratio);return;}}diff --git a/Libraries/LibWebView/CompositorClient.h b/Libraries/LibWebView/CompositorClient.hindex cf015e970e6..9d41da15e8a 100644--- a/Libraries/LibWebView/CompositorClient.h+++ b/Libraries/LibWebView/CompositorClient.h@@ -33,7 +33,7 @@ private:virtual void did_allocate_backing_stores(Web::CompositorContextId, Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores) override;virtual void did_add_backing_stores(Web::CompositorContextId, Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores) override;virtual void did_retire_backing_stores(Web::CompositorContextId, Vector<i32> bitmap_ids) override;- virtual void did_present_frame(Web::CompositorContextId, Gfx::IntRect content_rect, Gfx::IntRect damage_rect, i32 bitmap_id) override;+ virtual void did_present_frame(Web::CompositorContextId, Gfx::IntRect content_rect, Gfx::IntRect damage_rect, i32 bitmap_id, double device_pixel_ratio) override;virtual void did_consume_input_event(Web::CompositorContextId, u64 event_id) override;virtual void did_not_dispatch_input_event(Web::CompositorContextId, u64 event_id) override;};diff --git a/Libraries/LibWebView/PictureInPictureManager.cpp b/Libraries/LibWebView/PictureInPictureManager.cppindex 68ce216b95b..35d2fb7e832 100644--- a/Libraries/LibWebView/PictureInPictureManager.cpp+++ b/Libraries/LibWebView/PictureInPictureManager.cpp@@ -31,7 +31,7 @@ String PictureInPictureManager::open_window(WebContentPage& requesting_page, Canauto page = m_windows.last().page;retire_window(m_windows.size() - 1);if (page->is_open())- page->async_picture_in_picture_window_did_close();+ page->async_picture_in_picture_window_did_close(false);}}@@ -43,8 +43,8 @@ String PictureInPictureManager::open_window(WebContentPage& requesting_page, Canif (auto index = index_of(*window); index.has_value() && m_windows[*index].page->is_open())m_windows[*index].page->async_picture_in_picture_window_did_resize(size);};- window->on_close = [this, window = window.ptr()] {- window_did_close(*window);+ window->on_close = [this, window = window.ptr()](auto video_keeps_playing) {+ window_did_close(*window, video_keeps_playing);};window->on_page_close = [this, window = window.ptr()] {window_page_did_close(*window);@@ -81,7 +81,9 @@ void PictureInPictureManager::retire_window(size_t index)m_retired_windows.append(move(window));}-void PictureInPictureManager::window_did_close(PictureInPictureWindow& window)+// The window is gone as soon as the user closes it, and its page only learns of that after, so a page that is busy or+// misbehaves cannot keep it open.+void PictureInPictureManager::window_did_close(PictureInPictureWindow& window, PictureInPictureWindow::VideoKeepsPlaying video_keeps_playing){auto index = index_of(window);if (!index.has_value())@@ -90,7 +92,7 @@ void PictureInPictureManager::window_did_close(PictureInPictureWindow& window)retire_window(*index);if (page->is_open())- page->async_picture_in_picture_window_did_close();+ page->async_picture_in_picture_window_did_close(video_keeps_playing == PictureInPictureWindow::VideoKeepsPlaying::No);}void PictureInPictureManager::window_page_did_close(PictureInPictureWindow& window)diff --git a/Libraries/LibWebView/PictureInPictureManager.h b/Libraries/LibWebView/PictureInPictureManager.hindex a5de3001cd4..b54bec8a3e9 100644--- a/Libraries/LibWebView/PictureInPictureManager.h+++ b/Libraries/LibWebView/PictureInPictureManager.h@@ -16,6 +16,7 @@#include <LibGfx/Size.h>#include <LibWebCommon/Page/PageId.h>#include <LibWebView/Forward.h>+#include <LibWebView/PictureInPictureWindow.h>namespace WebView {@@ -41,7 +42,7 @@ private:Optional<size_t> index_of_window_for(WebContentPage const&) const;Optional<size_t> index_of(PictureInPictureWindow const&) const;void retire_window(size_t index);- void window_did_close(PictureInPictureWindow&);+ void window_did_close(PictureInPictureWindow&, PictureInPictureWindow::VideoKeepsPlaying);void window_page_did_close(PictureInPictureWindow&);Vector<Window> m_windows;diff --git a/Libraries/LibWebView/PictureInPictureWindow.h b/Libraries/LibWebView/PictureInPictureWindow.hindex 27f0a5674cb..d588142fe65 100644--- a/Libraries/LibWebView/PictureInPictureWindow.h+++ b/Libraries/LibWebView/PictureInPictureWindow.h@@ -27,8 +27,14 @@ public:virtual String handle() const = 0;virtual void hide() = 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 {+ No,+ Yes,+ };+Function<void(Gfx::IntSize)> on_resize;- Function<void()> on_close;+ Function<void(VideoKeepsPlaying)> on_close;Function<void()> on_page_close;protected:diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cppindex c6f019ee617..609a62f13d2 100644--- a/Libraries/LibWebView/ViewImplementation.cpp+++ b/Libraries/LibWebView/ViewImplementation.cpp@@ -226,7 +226,7 @@ void ViewImplementation::set_favicon(Badge<WebContentPage>, Optional<Gfx::Bitmap// The tab is displayed in a process of its own, whose page starts from a document standing in for the entry of the// document state given. The outgoing process keeps displaying the tab's document until the UI process has unloaded// it there, before a document activates in the new page.-void ViewImplementation::server_did_paint(Badge<WebContentPage>, i32 bitmap_id, Gfx::IntSize size, Gfx::IntRect damage_rect)+void ViewImplementation::server_did_paint(Badge<WebContentPage>, i32 bitmap_id, Gfx::IntSize size, Gfx::IntRect damage_rect, double device_pixel_ratio){bool did_swap_bitmap = false;auto previous_front_bitmap_id = m_front_bitmap.id;@@ -234,6 +234,7 @@ void ViewImplementation::server_did_paint(Badge<WebContentPage>, i32 bitmap_id,if (bitmap_index.has_value()) {m_has_usable_bitmap = true;m_other_bitmaps[*bitmap_index].last_painted_size = size.to_type<Web::DevicePixels>();+ m_other_bitmaps[*bitmap_index].last_painted_device_pixel_ratio = device_pixel_ratio;swap(m_other_bitmaps[*bitmap_index], m_front_bitmap);m_backup_shared_image_buffer = nullptr;did_swap_bitmap = true;@@ -683,8 +684,8 @@ void ViewImplementation::enqueue_input_event(Web::InputEvent event)auto* mouse_event = event.get_pointer<Web::MouseEvent>();if (m_debugger_paused) {if (mouse_event) {- auto activated_action = handle_ui_overlay_mouse_event(m_debugger_overlay_pointer_state, *mouse_event, [&](Gfx::IntSize viewport_size) {- return Compositing::paused_debugger_overlay_geometry(viewport_size, device_pixel_ratio()).buttons();+ auto activated_action = handle_ui_overlay_mouse_event(m_debugger_overlay_pointer_state, *mouse_event, [](Gfx::IntSize viewport_size, double device_pixel_ratio) {+ return Compositing::paused_debugger_overlay_geometry(viewport_size, device_pixel_ratio).buttons();});if (activated_action.has_value()) {resume_debugger(*activated_action == Compositing::PausedDebuggerOverlayAction::StepOver@@ -695,6 +696,9 @@ void ViewImplementation::enqueue_input_event(Web::InputEvent event)return;}+ if (m_has_picture_in_picture_controls && mouse_event && handle_picture_in_picture_controls_mouse_event(*mouse_event))+ return;+// User input enables a single request for an external URL.if ((key_event && key_event->type == Web::KeyEvent::Type::KeyDown && !key_event->repeat&& first_is_one_of(key_event->key, Web::UIEvents::KeyCode::Key_Return, Web::UIEvents::KeyCode::Key_Space))@@ -1756,8 +1760,9 @@ void ViewImplementation::did_leave_tooltip_area(Badge<WebContentPage>)template<typename Action, typename ButtonsForViewport>Optional<Action> ViewImplementation::handle_ui_overlay_mouse_event(Compositing::OverlayPointerState<Action>& pointer_state, Web::MouseEvent const& event, ButtonsForViewport const& buttons_for_viewport){- auto buttons = buttons_for_viewport(viewport_size().to_type<int>());- auto outcome = pointer_state.handle_mouse_event(event, Compositing::overlay_action_at(buttons, event.position.to_type<int>()));+ auto overlay_position = ui_overlay_position(event.position);+ auto buttons = buttons_for_viewport(overlay_position.viewport_size, overlay_position.device_pixel_ratio);+ auto outcome = pointer_state.handle_mouse_event(event, Compositing::overlay_action_at(buttons, overlay_position.position));if (outcome.hover_changed) {update_ui_overlays();apply_cursor();@@ -1783,11 +1788,24 @@ static StringView paused_debugger_overlay_action_tooltip(Compositing::PausedDebuVERIFY_NOT_REACHED();}+static StringView picture_in_picture_control_tooltip(Compositing::PictureInPictureControl control)+{+ switch (control) {+ case Compositing::PictureInPictureControl::BackToTab:+ return "Back to tab"sv;+ case Compositing::PictureInPictureControl::Close:+ return "Close"sv;+ }+ VERIFY_NOT_REACHED();+}+void ViewImplementation::apply_tooltip(){auto tooltip = m_page_tooltip;if (auto action = m_debugger_overlay_pointer_state.hovered_action(); action.has_value())tooltip = paused_debugger_overlay_action_tooltip(*action);+ if (auto control = m_picture_in_picture_controls_pointer_state.hovered_action(); control.has_value())+ tooltip = picture_in_picture_control_tooltip(*control);if (tooltip.has_value()) {if (on_enter_tooltip_area)@@ -1811,6 +1829,120 @@ void ViewImplementation::set_debugger_paused(bool paused)update_ui_overlays();}+// The compositor draws the controls and the view handles them, rather than the page, so that they work whatever the+// page does.+// FIXME: Expose the controls to accessibility clients, which only see the page's tree.+void ViewImplementation::enable_picture_in_picture_controls()+{+ m_has_picture_in_picture_controls = true;++ // The same delay as the page's media controls.+ static constexpr int hide_delay_ms = 1000;+ m_picture_in_picture_controls_hide_timer = Core::Timer::create_single_shot(hide_delay_ms, [this] {+ if (!m_picture_in_picture_controls_pointer_state.hovered_action().has_value())+ set_picture_in_picture_controls_shown(false);+ });++ update_ui_overlays();+}++// Returns whether the event was the controls', which the page then never sees.+bool ViewImplementation::handle_picture_in_picture_controls_mouse_event(Web::MouseEvent const& event)+{+ // A cancelled press counts as the pointer leaving the controls, as it does for the page.+ Optional<Web::MouseEvent> leave_for_cancel;+ if (event.type == Web::MouseEvent::Type::MouseCancel) {+ leave_for_cancel = event.clone_without_browser_data();+ leave_for_cancel->type = Web::MouseEvent::Type::MouseLeave;+ }+ auto const& controls_event = leave_for_cancel.has_value() ? *leave_for_cancel : event;++ if (controls_event.type == Web::MouseEvent::Type::MouseMove) {+ set_picture_in_picture_controls_shown(true);+ m_picture_in_picture_controls_hide_timer->restart();+ } else if (controls_event.type == Web::MouseEvent::Type::MouseLeave) {+ m_picture_in_picture_controls_hide_timer->stop();+ set_picture_in_picture_controls_shown(false);+ }++ if (event.type == Web::MouseEvent::Type::MouseDown && !picture_in_picture_controls_are_shown())+ return false;++ // A control is in use while it is hovered, and for the rest of a press on it, as the page sees none of that press.+ auto& pointer_state = m_picture_in_picture_controls_pointer_state;+ auto controls_in_use = [&] { return pointer_state.hovered_action().has_value() || pointer_state.is_pressing(); };+ auto was_pressing = pointer_state.is_pressing();+ auto was_in_use = controls_in_use();+ auto activated_control = handle_ui_overlay_mouse_event(pointer_state, controls_event, [](Gfx::IntSize viewport_size, double device_pixel_ratio) {+ return Compositing::picture_in_picture_controls_geometry(viewport_size, device_pixel_ratio).buttons();+ });+ if (auto is_in_use = controls_in_use(); is_in_use != was_in_use)+ page().async_set_picture_in_picture_window_controls_in_use(is_in_use);+ if (activated_control.has_value() && on_picture_in_picture_control_activated)+ on_picture_in_picture_control_activated(*activated_control);++ // The page still learns that the pointer left, even during a press on a control.+ if (event.type == Web::MouseEvent::Type::MouseLeave)+ return false;+ return was_pressing || pointer_state.is_pressing();+}++void ViewImplementation::set_picture_in_picture_controls_shown(bool shown)+{+ if (m_picture_in_picture_controls_shown == shown)+ return;++ m_picture_in_picture_controls_shown = shown;+ update_ui_overlays();+}++// The controls show while the player's own controls do, as the player's stay shown while the controls are in use, so+// that the two fade together. The page can only keep the controls up, which then still show and work without it.+void ViewImplementation::did_change_picture_in_picture_player_controls_visibility(Badge<WebContentPage>, bool shown)+{+ if (m_picture_in_picture_player_controls_shown == shown)+ return;++ m_picture_in_picture_player_controls_shown = shown;+ if (m_has_picture_in_picture_controls)+ update_ui_overlays();+}++Optional<ViewImplementation::DisplayedFrame> ViewImplementation::displayed_frame() const+{+ if (m_has_usable_bitmap)+ return DisplayedFrame { m_front_bitmap.last_painted_size.to_type<int>(), m_front_bitmap.last_painted_device_pixel_ratio };+ if (m_backup_shared_image_buffer)+ return DisplayedFrame { m_backup_bitmap_size.to_type<int>(), m_backup_bitmap_device_pixel_ratio };+ return {};+}++// The compositor draws the browser's overlays over the frame it presents, at the pixel ratio it painted the frame at,+// and the frame may be scaled to fit the view.+ViewImplementation::UIOverlayPosition ViewImplementation::ui_overlay_position(Web::DevicePixelPoint position) const+{+ auto view_size = viewport_size().to_type<int>();+ auto view_position = position.to_type<int>();+ auto frame = displayed_frame();+ if (!frame.has_value() || frame->size.is_empty())+ return { view_position, view_size, m_device_pixel_ratio };+ if (!m_scales_frames_to_fit || frame->size == view_size)+ return { view_position, frame->size, frame->device_pixel_ratio };++ auto fitted_frame = rect_fitting_frame(frame->size, view_size);+ if (fitted_frame.is_empty())+ return { view_position, view_size, m_device_pixel_ratio };++ return {+ .position = {+ (view_position.x() - fitted_frame.x()) * frame->size.width() / fitted_frame.width(),+ (view_position.y() - fitted_frame.y()) * frame->size.height() / fitted_frame.height(),+ },+ .viewport_size = frame->size,+ .device_pixel_ratio = frame->device_pixel_ratio,+ };+}+void ViewImplementation::update_ui_overlays(){Compositing::UIOverlays overlays;@@ -1820,6 +1952,12 @@ void ViewImplementation::update_ui_overlays().hovered_action = m_debugger_overlay_pointer_state.hovered_action(),};}+ if (m_has_picture_in_picture_controls) {+ overlays.picture_in_picture_controls = Compositing::PictureInPictureControlsState {+ .shown = picture_in_picture_controls_are_shown(),+ .hovered_control = m_picture_in_picture_controls_pointer_state.hovered_action(),+ };+ }Application::the().update_compositor_ui_overlays(page().compositor_context_id(), overlays);}@@ -2371,6 +2509,7 @@ void ViewImplementation::install_backing_stores(Vector<i32> bitmap_ids, Vector<G// NOTE: We keep the outgoing front bitmap as a backup so we have something to paint until we get a new one.m_backup_shared_image_buffer = move(m_front_bitmap.shared_image_buffer);m_backup_bitmap_size = m_front_bitmap.last_painted_size;+ m_backup_bitmap_device_pixel_ratio = m_front_bitmap.last_painted_device_pixel_ratio;}m_has_usable_bitmap = false;m_front_bitmap.id = bitmap_ids[0];@@ -2779,6 +2918,7 @@ void ViewImplementation::display_page_changed(RefPtr<WebContentPage> previous_paif (m_has_usable_bitmap) {m_backup_shared_image_buffer = move(m_front_bitmap.shared_image_buffer);m_backup_bitmap_size = m_front_bitmap.last_painted_size;+ m_backup_bitmap_device_pixel_ratio = m_front_bitmap.last_painted_device_pixel_ratio;}if (previous_page->is_open()) {Application::the().update_compositor_context_visibility(previous_page->compositor_context_id(), Web::HTML::VisibilityState::Hidden);diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.hindex 8cd5f938749..6d5e41e87d1 100644--- a/Libraries/LibWebView/ViewImplementation.h+++ b/Libraries/LibWebView/ViewImplementation.h@@ -133,7 +133,7 @@ public:String const& handle() const { return m_window_handle; }- void server_did_paint(Badge<WebContentPage>, i32 bitmap_id, Gfx::IntSize size, Gfx::IntRect damage_rect);+ void server_did_paint(Badge<WebContentPage>, i32 bitmap_id, Gfx::IntSize size, Gfx::IntRect damage_rect, double device_pixel_ratio);void set_window_position(Gfx::IntPoint);void set_window_size(Gfx::IntSize);@@ -196,6 +196,7 @@ public:Optional<u64> display_id() const { return m_display_id; }double maximum_frames_per_second() const { return m_maximum_frames_per_second; }void set_scales_frames_to_fit(bool scales_frames_to_fit) { m_scales_frames_to_fit = scales_frames_to_fit; }+ void enable_picture_in_picture_controls();void enqueue_input_event(Web::InputEvent);// The page reporting on an event must be the one handling it.void did_finish_handling_input_event(Badge<WebContentPage>, WebContentPage const& reporting_page, u64 event_id, Web::EventResult event_result);@@ -218,6 +219,15 @@ public:void did_request_cursor_change(Badge<WebContentPage>, Gfx::Cursor);void did_enter_tooltip_area(Badge<WebContentPage>, ByteString title);void did_leave_tooltip_area(Badge<WebContentPage>);+ void did_change_picture_in_picture_player_controls_visibility(Badge<WebContentPage>, bool shown);+ bool picture_in_picture_controls_are_shown_for_testing() const { return picture_in_picture_controls_are_shown(); }+ bool picture_in_picture_player_controls_are_shown_for_testing() const { return m_picture_in_picture_player_controls_shown; }+ Gfx::Cursor const& page_cursor_for_testing() const { return m_page_cursor; }+ Optional<ByteString> const& page_tooltip_for_testing() const { return m_page_tooltip; }+ Optional<double> displayed_frame_device_pixel_ratio_for_testing() const+ {+ return displayed_frame().map([](auto const& frame) { return frame.device_pixel_ratio; });+ }void set_preferred_color_scheme(Web::CSS::PreferredColorScheme);void set_preferred_contrast(Web::CSS::PreferredContrast);@@ -523,6 +533,7 @@ public:Function<void(Gfx::IntPoint content_position, i32 minimum_width, Vector<Web::HTML::SelectItem> items)> on_request_select_dropdown;Function<void(Web::KeyEvent const&)> on_finish_handling_key_event;Function<void(Web::MouseEvent const&, Web::EventResult)> on_finish_handling_mouse_event;+ Function<void(Compositing::PictureInPictureControl)> on_picture_in_picture_control_activated;Function<void(Web::DragEvent const&)> on_finish_handling_drag_event;Function<void(String const&)> on_test_finish;Function<void(double milliseconds)> on_set_test_timeout;@@ -634,9 +645,23 @@ protected:void handle_resize();void fail_pending_debugger_requests();void set_debugger_paused(bool);+ bool handle_picture_in_picture_controls_mouse_event(Web::MouseEvent const&);+ void set_picture_in_picture_controls_shown(bool);+ bool picture_in_picture_controls_are_shown() const { return m_picture_in_picture_controls_shown || m_picture_in_picture_player_controls_shown; }+ struct DisplayedFrame {+ Gfx::IntSize size;+ double device_pixel_ratio { 1.0 };+ };+ Optional<DisplayedFrame> displayed_frame() const;+ struct UIOverlayPosition {+ Gfx::IntPoint position;+ Gfx::IntSize viewport_size;+ double device_pixel_ratio { 1.0 };+ };+ UIOverlayPosition ui_overlay_position(Web::DevicePixelPoint) const;template<typename Action, typename ButtonsForViewport>Optional<Action> handle_ui_overlay_mouse_event(Compositing::OverlayPointerState<Action>&, Web::MouseEvent const&, ButtonsForViewport const&);- bool is_ui_overlay_hovered() const { return m_debugger_overlay_pointer_state.hovered_action().has_value(); }+ bool is_ui_overlay_hovered() const { return m_debugger_overlay_pointer_state.hovered_action().has_value() || m_picture_in_picture_controls_pointer_state.hovered_action().has_value(); }void apply_cursor();void apply_tooltip();void update_ui_overlays();@@ -700,6 +725,7 @@ protected:struct SharedBitmap {i32 id { -1 };Web::DevicePixelSize last_painted_size;+ double last_painted_device_pixel_ratio { 1.0 };OwnPtr<Gfx::SharedImageBuffer> shared_image_buffer;};@@ -829,6 +855,11 @@ protected:bool m_debugger_is_attached { false };bool m_debugger_paused { false };Compositing::OverlayPointerState<Compositing::PausedDebuggerOverlayAction> m_debugger_overlay_pointer_state;+ bool m_has_picture_in_picture_controls { false };+ bool m_picture_in_picture_controls_shown { false };+ bool m_picture_in_picture_player_controls_shown { false };+ Compositing::OverlayPointerState<Compositing::PictureInPictureControl> m_picture_in_picture_controls_pointer_state;+ RefPtr<Core::Timer> m_picture_in_picture_controls_hide_timer;Gfx::Cursor m_page_cursor { Gfx::StandardCursor::Arrow };Optional<ByteString> m_page_tooltip;@@ -845,6 +876,7 @@ protected:OwnPtr<Gfx::SharedImageBuffer> m_backup_shared_image_buffer;Web::DevicePixelSize m_backup_bitmap_size;+ double m_backup_bitmap_device_pixel_ratio { 1.0 };Gfx::Color m_page_background_color { 255, 255, 255 };Gfx::Color m_system_canvas_background_color { 255, 255, 255 };Web::CSS::PreferredColorScheme m_preferred_color_scheme { Web::CSS::PreferredColorScheme::Auto };diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cppindex 243a10e76ff..21fa3e24e7f 100644--- a/Libraries/LibWebView/WebContentPage.cpp+++ b/Libraries/LibWebView/WebContentPage.cpp@@ -538,12 +538,12 @@ void WebContentPage::did_not_dispatch_input_event_through_compositor(u64 event_i}}-void WebContentPage::did_present_bitmap(Gfx::IntRect content_rect, Gfx::IntRect damage_rect, i32 bitmap_id)+void WebContentPage::did_present_bitmap(Gfx::IntRect content_rect, Gfx::IntRect damage_rect, i32 bitmap_id, double device_pixel_ratio){dbgln_if(COMPOSITOR_DEBUG, "[Compositor] UI compositor IPC did_paint for page {} bitmap {} rect={}x{} at {},{}",m_id, bitmap_id, content_rect.width(), content_rect.height(), content_rect.x(), content_rect.y());if (displays_tab()) {- view().server_did_paint({}, bitmap_id, content_rect.size(), damage_rect);+ view().server_did_paint({}, bitmap_id, content_rect.size(), damage_rect, device_pixel_ratio);return;}dbgln_if(COMPOSITOR_DEBUG, "[Compositor] UI dropping did_paint for page {} bitmap {}: no view", m_id, bitmap_id);@@ -1424,6 +1424,11 @@ void WebContentPage::did_exit_picture_in_picture()PictureInPictureManager::the().close_window(*this);}+void WebContentPage::did_change_picture_in_picture_player_controls_visibility(bool shown)+{+ view().did_change_picture_in_picture_player_controls_visibility({}, shown);+}+// A navigation's population steps run in the process with its source document, which need not have hosted local file// content. For a navigation to a file: URL the UI process admitted, that process reads the file the URL names, as it does// for a history step to an entry of one.diff --git a/Libraries/LibWebView/WebContentPage.h b/Libraries/LibWebView/WebContentPage.hindex b1ca3396abb..b20236be18a 100644--- a/Libraries/LibWebView/WebContentPage.h+++ b/Libraries/LibWebView/WebContentPage.h@@ -133,7 +133,7 @@ public:bool handle_and_dispatch_mouse_event_in_compositor(Web::MouseEvent const&);void did_consume_input_event_in_compositor(u64 event_id);void did_not_dispatch_input_event_through_compositor(u64 event_id);- void did_present_bitmap(Gfx::IntRect content_rect, Gfx::IntRect damage_rect, i32 bitmap_id);+ void did_present_bitmap(Gfx::IntRect content_rect, Gfx::IntRect damage_rect, i32 bitmap_id, double device_pixel_ratio);void did_present_backing_stores(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores);void did_add_backing_stores(Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores);void did_retire_backing_stores(ReadonlySpan<i32> bitmap_ids);@@ -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_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;virtual void did_request_geolocation_position(u64 request_id) override;diff --git a/Services/Compositor/CompositorControlClient.ipc b/Services/Compositor/CompositorControlClient.ipcindex 0b520a572c0..c81d41bec6b 100644--- a/Services/Compositor/CompositorControlClient.ipc+++ b/Services/Compositor/CompositorControlClient.ipc@@ -7,7 +7,7 @@ endpoint CompositorControlClientdid_allocate_backing_stores(Web::CompositorContextId context_id, Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores) =|did_add_backing_stores(Web::CompositorContextId context_id, Vector<i32> bitmap_ids, Vector<Gfx::SharedImage> backing_stores) =|did_retire_backing_stores(Web::CompositorContextId context_id, Vector<i32> bitmap_ids) =|- did_present_frame(Web::CompositorContextId context_id, Gfx::IntRect content_rect, Gfx::IntRect damage_rect, i32 bitmap_id) =|+ did_present_frame(Web::CompositorContextId context_id, Gfx::IntRect content_rect, Gfx::IntRect damage_rect, i32 bitmap_id, double device_pixel_ratio) =|did_consume_input_event(Web::CompositorContextId context_id, u64 event_id) =|did_not_dispatch_input_event(Web::CompositorContextId context_id, u64 event_id) =|}diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cppindex 5e357dc426b..a7097ff6c28 100644--- a/Services/Compositor/CompositorState.cpp+++ b/Services/Compositor/CompositorState.cpp@@ -843,7 +843,7 @@ void CompositorState::present_frame(Web::CompositorContextId context_id, Contextif (!prepared_frame.has_value())return;- m_pending_async_presents.append(context_id, pending_frame.viewport_rect, prepared_frame->damage_rect, prepared_frame->bitmap_id);+ m_pending_async_presents.append(context_id, pending_frame.viewport_rect, prepared_frame->damage_rect, prepared_frame->bitmap_id, prepared_frame->device_pixel_ratio);auto* pending_present = &m_pending_async_presents.last();auto& event_loop = Core::EventLoop::current();@@ -1066,6 +1066,7 @@ void CompositorState::did_finish_async_present(PendingAsyncPresent& pending_presauto viewport_rect = pending_present.viewport_rect;auto damage_rect = pending_present.damage_rect;auto bitmap_id = pending_present.bitmap_id;+ auto device_pixel_ratio = pending_present.device_pixel_ratio;auto was_cancelled = pending_present.was_cancelled;(void)m_pending_async_presents.remove(pending_present_iterator);if (m_pending_async_presents.is_empty() && m_gpu_completion_timer)@@ -1080,7 +1081,7 @@ void CompositorState::did_finish_async_present(PendingAsyncPresent& pending_prescontext->presenter().did_finish_gpu_present(bitmap_id);if (context->presents_to_client()) {VERIFY(m_client);- m_client->did_present_frame(context_id, viewport_rect, damage_rect, bitmap_id);+ m_client->did_present_frame(context_id, viewport_rect, damage_rect, bitmap_id, device_pixel_ratio);}resize_backing_stores_if_needed(context_id, *context);if (auto parent_context_id = context->parent_context_id(); parent_context_id.has_value()) {diff --git a/Services/Compositor/CompositorState.h b/Services/Compositor/CompositorState.hindex fccc01ae026..941eab2c630 100644--- a/Services/Compositor/CompositorState.h+++ b/Services/Compositor/CompositorState.h@@ -43,7 +43,7 @@ public:// The stores join the ones allocated last, and leave them again once retired.virtual void did_add_backing_stores(Web::CompositorContextId, Vector<i32> bitmap_ids, Vector<Gfx::SharedImage>&& backing_stores) = 0;virtual void did_retire_backing_stores(Web::CompositorContextId, Vector<i32> bitmap_ids) = 0;- virtual void did_present_frame(Web::CompositorContextId, Gfx::IntRect content_rect, Gfx::IntRect damage_rect, i32 bitmap_id) = 0;+ virtual void did_present_frame(Web::CompositorContextId, Gfx::IntRect content_rect, Gfx::IntRect damage_rect, i32 bitmap_id, double device_pixel_ratio) = 0;// The compositor performed the event's whole default action, so the UI hears nothing more about it.virtual void did_consume_input_event(Web::CompositorContextId, u64 event_id) = 0;// No context could take the event; the UI sends it to WebContent itself.@@ -148,11 +148,12 @@ private:CompositorState(RefPtr<Gfx::SkiaBackendContext>);struct PendingAsyncPresent {- PendingAsyncPresent(Web::CompositorContextId context_id, Gfx::IntRect viewport_rect, Gfx::IntRect damage_rect, i32 bitmap_id)+ PendingAsyncPresent(Web::CompositorContextId context_id, Gfx::IntRect viewport_rect, Gfx::IntRect damage_rect, i32 bitmap_id, double device_pixel_ratio): context_id(context_id), viewport_rect(viewport_rect), damage_rect(damage_rect), bitmap_id(bitmap_id)+ , device_pixel_ratio(device_pixel_ratio){}@@ -160,6 +161,7 @@ private:Gfx::IntRect viewport_rect;Gfx::IntRect damage_rect;i32 bitmap_id { 0 };+ double device_pixel_ratio { 1.0 };bool was_cancelled { false };};diff --git a/Services/Compositor/ConnectionFromClient.cpp b/Services/Compositor/ConnectionFromClient.cppindex 2afc581f47b..61c8fd58c81 100644--- a/Services/Compositor/ConnectionFromClient.cpp+++ b/Services/Compositor/ConnectionFromClient.cpp@@ -46,9 +46,9 @@ void ConnectionFromClient::did_retire_backing_stores(Web::CompositorContextId coasync_did_retire_backing_stores(context_id, move(bitmap_ids));}-void ConnectionFromClient::did_present_frame(Web::CompositorContextId context_id, Gfx::IntRect content_rect, Gfx::IntRect damage_rect, i32 bitmap_id)+void ConnectionFromClient::did_present_frame(Web::CompositorContextId context_id, Gfx::IntRect content_rect, Gfx::IntRect damage_rect, i32 bitmap_id, double device_pixel_ratio){- async_did_present_frame(context_id, content_rect, damage_rect, bitmap_id);+ async_did_present_frame(context_id, content_rect, damage_rect, bitmap_id, device_pixel_ratio);}void ConnectionFromClient::did_consume_input_event(Web::CompositorContextId context_id, u64 event_id)diff --git a/Services/Compositor/ConnectionFromClient.h b/Services/Compositor/ConnectionFromClient.hindex ebed7e32e11..c14b647c62b 100644--- a/Services/Compositor/ConnectionFromClient.h+++ b/Services/Compositor/ConnectionFromClient.h@@ -35,7 +35,7 @@ private:virtual void did_allocate_backing_stores(Web::CompositorContextId, Vector<i32> bitmap_ids, Vector<Gfx::SharedImage>&& backing_stores) override;virtual void did_add_backing_stores(Web::CompositorContextId, Vector<i32> bitmap_ids, Vector<Gfx::SharedImage>&& backing_stores) override;virtual void did_retire_backing_stores(Web::CompositorContextId, Vector<i32> bitmap_ids) override;- virtual void did_present_frame(Web::CompositorContextId, Gfx::IntRect content_rect, Gfx::IntRect damage_rect, i32 bitmap_id) override;+ virtual void did_present_frame(Web::CompositorContextId, Gfx::IntRect content_rect, Gfx::IntRect damage_rect, i32 bitmap_id, double device_pixel_ratio) override;virtual void did_consume_input_event(Web::CompositorContextId, u64 event_id) override;virtual void did_not_dispatch_input_event(Web::CompositorContextId, u64 event_id) override;diff --git a/Services/Compositor/ContextPresenter.cpp b/Services/Compositor/ContextPresenter.cppindex cda4dc49ab3..9eaf02ec29c 100644--- a/Services/Compositor/ContextPresenter.cpp+++ b/Services/Compositor/ContextPresenter.cpp@@ -316,6 +316,7 @@ Optional<PreparedFrame> ContextPresenter::prepare_frame(DisplayListPlayerSkia& d.rendered_surface = &back_store,.bitmap_id = render_target->bitmap_id,.damage_rect = damage_rect,+ .device_pixel_ratio = m_device_pixel_ratio,};}diff --git a/Services/Compositor/ContextPresenter.h b/Services/Compositor/ContextPresenter.hindex 3ed3afd3b60..7da1c6f1110 100644--- a/Services/Compositor/ContextPresenter.h+++ b/Services/Compositor/ContextPresenter.h@@ -53,6 +53,7 @@ struct PreparedFrame {Gfx::PaintingSurface* rendered_surface { nullptr };i32 bitmap_id { 0 };Gfx::IntRect damage_rect;+ double device_pixel_ratio { 1.0 };};// Rasterizes the scene of a context into its backing stores and keeps track of the frames it presents.diff --git a/Services/WebContent/ConnectionFromClient.cpp b/Services/WebContent/ConnectionFromClient.cppindex d25b8346c3f..dd7b4712dc4 100644--- a/Services/WebContent/ConnectionFromClient.cpp+++ b/Services/WebContent/ConnectionFromClient.cpp@@ -3655,10 +3655,16 @@ void ConnectionFromClient::picture_in_picture_window_did_resize(Web::PageId pagepage->page().picture_in_picture_controller().window_did_resize(window_size);}-void ConnectionFromClient::picture_in_picture_window_did_close(Web::PageId page_id)+void ConnectionFromClient::picture_in_picture_window_did_close(Web::PageId page_id, bool pause_video){if (auto page = this->page(page_id); page.has_value())- page->page().picture_in_picture_controller().window_did_close();+ page->page().picture_in_picture_controller().window_did_close(pause_video);+}++void ConnectionFromClient::set_picture_in_picture_window_controls_in_use(Web::PageId page_id, bool in_use)+{+ if (auto page = this->page(page_id); page.has_value())+ page->page().set_picture_in_picture_window_controls_in_use(in_use);}}diff --git a/Services/WebContent/ConnectionFromClient.h b/Services/WebContent/ConnectionFromClient.hindex 395cd202cfe..c4ecc30c143 100644--- a/Services/WebContent/ConnectionFromClient.h+++ b/Services/WebContent/ConnectionFromClient.h@@ -345,7 +345,8 @@ private:virtual void set_has_picture_in_picture_support(Web::PageId page_id, bool has_support) override;virtual void did_open_picture_in_picture_window(Web::PageId page_id, Gfx::IntSize window_size) override;virtual void picture_in_picture_window_did_resize(Web::PageId page_id, Gfx::IntSize window_size) override;- virtual void picture_in_picture_window_did_close(Web::PageId page_id) override;+ virtual void picture_in_picture_window_did_close(Web::PageId page_id, bool pause_video) override;+ virtual void set_picture_in_picture_window_controls_in_use(Web::PageId page_id, bool in_use) override;RefPtr<TestConnection> m_test_connection;RefPtr<Web::Compositor::CompositorConnection> m_compositor_connection;diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cppindex e9ee25673b2..77a7d322679 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_player_controls_visibility(bool shown)+{+ client().async_did_change_picture_in_picture_player_controls_visibility(m_id, shown);+}+void PageClient::page_did_request_tooltip_override(Web::CSSPixelPoint position, ByteString const& title){auto device_position = page().css_to_device_point(position);diff --git a/Services/WebContent/PageClient.h b/Services/WebContent/PageClient.hindex 8faf48ab2e8..e0e486fd9d1 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_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;virtual void page_did_enter_tooltip_area(ByteString const&) override;diff --git a/Services/WebContent/WebContentClient.ipc b/Services/WebContent/WebContentClient.ipcindex e0fcb87ee45..f2b5db8e0ad 100644--- a/Services/WebContent/WebContentClient.ipc+++ b/Services/WebContent/WebContentClient.ipc@@ -223,6 +223,7 @@ endpoint WebContentClient routed by Web::PageId page_iddid_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_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) =|did_request_geolocation_position(Web::PageId page_id, u64 request_id) =|diff --git a/Services/WebContent/WebContentServer.ipc b/Services/WebContent/WebContentServer.ipcindex e97bc83c52c..542a03878dd 100644--- a/Services/WebContent/WebContentServer.ipc+++ b/Services/WebContent/WebContentServer.ipc@@ -323,5 +323,6 @@ endpoint WebContentServer routed by Web::PageId page_idset_has_picture_in_picture_support(Web::PageId page_id, bool has_support) =|did_open_picture_in_picture_window(Web::PageId page_id, Gfx::IntSize window_size) =|picture_in_picture_window_did_resize(Web::PageId page_id, Gfx::IntSize window_size) =|- picture_in_picture_window_did_close(Web::PageId page_id) =|+ picture_in_picture_window_did_close(Web::PageId page_id, bool pause_video) =|+ set_picture_in_picture_window_controls_in_use(Web::PageId page_id, bool in_use) =|}diff --git a/Tests/LibWebView/CMakeLists.txt b/Tests/LibWebView/CMakeLists.txtindex a2a89a06d78..71af087364e 100644--- a/Tests/LibWebView/CMakeLists.txt+++ b/Tests/LibWebView/CMakeLists.txt@@ -175,6 +175,13 @@ if (NOT WIN32)target_compile_definitions(TestUserActivation PRIVATE LADYBIRD_BINARY_PATH="$<TARGET_FILE_DIR:ladybird>")endif()+ add_executable(TestPictureInPictureWindowControls TestPictureInPictureWindowControls.cpp)+ add_dependencies(TestPictureInPictureWindowControls ladybird_build_resource_files ${ladybird_helper_processes})+ target_link_libraries(TestPictureInPictureWindowControls PRIVATE AK LibCore LibDiff LibFileSystem LibGfx LibImageDecoders LibImageDecoderClient LibIPC LibJS LibMain LibRequests LibURL LibWeb LibWebView)+ if (APPLE)+ target_compile_definitions(TestPictureInPictureWindowControls PRIVATE LADYBIRD_BINARY_PATH="$<TARGET_FILE_DIR:ladybird>")+ endif()+add_executable(TestShadowTreeTooltipsAndLinks TestShadowTreeTooltipsAndLinks.cpp)add_dependencies(TestShadowTreeTooltipsAndLinks ladybird_build_resource_files ${ladybird_helper_processes})target_link_libraries(TestShadowTreeTooltipsAndLinks PRIVATE AK LibCore LibDiff LibFileSystem LibGfx LibImageDecoders LibImageDecoderClient LibIPC LibJS LibMain LibRequests LibURL LibWeb LibWebView)@@ -363,6 +370,9 @@ if (BUILD_TESTING AND NOT WIN32)add_test(NAME TestUserActivation COMMAND $<TARGET_FILE:TestUserActivation>)set_tests_properties(TestUserActivation PROPERTIES TIMEOUT 60 ENVIRONMENT LADYBIRD_SOURCE_DIR=${LADYBIRD_SOURCE_DIR})+ add_test(NAME TestPictureInPictureWindowControls COMMAND $<TARGET_FILE:TestPictureInPictureWindowControls>)+ set_tests_properties(TestPictureInPictureWindowControls PROPERTIES TIMEOUT 60 ENVIRONMENT LADYBIRD_SOURCE_DIR=${LADYBIRD_SOURCE_DIR})+add_test(NAME TestShadowTreeTooltipsAndLinks COMMAND $<TARGET_FILE:TestShadowTreeTooltipsAndLinks>)set_tests_properties(TestShadowTreeTooltipsAndLinks PROPERTIES TIMEOUT 60 ENVIRONMENT LADYBIRD_SOURCE_DIR=${LADYBIRD_SOURCE_DIR})diff --git a/Tests/LibWebView/TestPictureInPictureWindowControls.cpp b/Tests/LibWebView/TestPictureInPictureWindowControls.cppnew file mode 100644index 00000000000..7bb8377daa7--- /dev/null+++ b/Tests/LibWebView/TestPictureInPictureWindowControls.cpp@@ -0,0 +1,348 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <AK/LexicalPath.h>+#include <AK/Random.h>+#include <AK/ScopeGuard.h>+#include <AK/String.h>+#include <AK/Utf16String.h>+#include <AK/Vector.h>+#include <LibCore/Directory.h>+#include <LibCore/Environment.h>+#include <LibCore/EventLoop.h>+#include <LibCore/StandardPaths.h>+#include <LibCore/System.h>+#include <LibCore/Timer.h>+#include <LibFileSystem/FileSystem.h>+#include <LibGfx/Cursor.h>+#include <LibGfx/SystemTheme.h>+#include <LibMain/Main.h>+#include <LibWebCommon/Page/InputEvent.h>+#include <LibWebView/Application.h>+#include <LibWebView/HeadlessWebView.h>+#include <LibWebView/PictureInPictureWindow.h>+#include <LibWebView/ProcessManager.h>+#include <LibWebView/Utilities.h>+#include <signal.h>++namespace {++class TestApplication : public WebView::Application {+ WEB_VIEW_APPLICATION(TestApplication)++public:+ explicit TestApplication(Optional<ByteString> ladybird_binary_path)+ : WebView::Application(move(ladybird_binary_path))+ {+ }++ virtual void create_platform_options(WebView::BrowserOptions& browser_options, WebView::RequestServerOptions&, WebView::WebContentOptions& web_content_options) override+ {+ browser_options.headless_mode = WebView::HeadlessMode::Test;+ browser_options.disable_sql_database = WebView::DisableSQLDatabase::Yes;+ web_content_options.is_test_mode = WebView::IsTestMode::Yes;+ }++ virtual bool should_coordinate_browser_process() const override { return false; }++ virtual OwnPtr<WebView::PictureInPictureWindow> create_picture_in_picture_window(WebView::WebContentPage& requesting_page, WebView::CanonicalTraversable& traversable, Gfx::IntSize video_size) override+ {+ auto window = WebView::Application::create_picture_in_picture_window(requesting_page, traversable, video_size);+ if (window) {+ picture_in_picture_window_handle = window->handle();+ picture_in_picture_window_size = window->size();+ }+ return window;+ }++ String picture_in_picture_window_handle;+ Gfx::IntSize picture_in_picture_window_size;+};++// Tests/LibWeb/Assets/solid-blue-160x120.webm+constexpr auto video_url = "data:video/webm;base64,"+ "GkXfo59ChoEBQveBAULygQRC84EIQoKEd2VibUKHgQJChYECGFOAZwEAAAAAAAJHEU2bdLpNu4tTq4QVSalmU6yBoU27i1OrhBZUrmtTrIHYTb"+ "uMU6uEElTDZ1OsggElTbuMU6uEHFO7a1OsggIx7AEAAAAAAABZAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAVSalmsirXsYMPQkBNgI1MYXZmNjIuMTIuMTAwV0GNTGF2ZjYyLj"+ "EyLjEwMESJiEBUAAAAAAAAFlSua8iuAQAAAAAAAD/XgQFzxYi7y8SD17A9dpyBACK1nIN1bmSIgQCGhVZfVlA5g4EBI+ODhAJiWgDgkLCBoLqB"+ "eJqBAlWwhFW5gQESVMNnQIBzc6BjwIBnyJpFo4dFTkNPREVSRIeNTGF2ZjYyLjEyLjEwMHNz2mPAi2PFiLvLxIPXsD12Z8ilRaOHRU5DT0RFUk"+ "SHmExhdmM2Mi4yOC4xMDAgbGlidnB4LXZwOWfIoUWjiERVUkFUSU9ORIeTMDA6MDA6MDAuMDgwMDAwMDAwAB9DtnVAgOeBAKPkgQAAgIJJg0IA"+ "CfAHdgY4JBwYQgAA8EfY/WIM2+nMNg69o5wAAGsolj+HwagtlxsozzSBUQjvp5qKfWffW0n9FjLeyZR10gJc8B6pWRwBd9w2DbjItsNWdXtxBq"+ "bMrifsah2MQKOVgQAoAIYAQJKcKElAAANwAABZ79oAHFO7a5G7j7OBALeK94EB8YIBq/CBAw=="sv;++void wait_for(int milliseconds)+{+ bool elapsed = false;+ auto timer = Core::Timer::create_single_shot(milliseconds, [&] { elapsed = true; });+ timer->start();+ Core::EventLoop::current().spin_until([&] { return elapsed; });+}++void signal_renderers(int signal)+{+ WebView::Application::process_manager().for_each_process([&](WebView::Process& process) {+ if (process.type() == WebView::ProcessType::WebContent)+ MUST(Core::System::kill(process.pid(), signal));+ });+}++void send_mouse_event(WebView::ViewImplementation& view, Web::MouseEvent::Type type, Web::DevicePixelPoint position, Web::UIEvents::MouseButton button, Web::UIEvents::MouseButton buttons)+{+ view.enqueue_input_event(Web::MouseEvent {+ .type = type,+ .position = position,+ .screen_position = position,+ .button = button,+ .buttons = buttons,+ .click_count = type == Web::MouseEvent::Type::MouseDown ? 1 : 0,+ .browser_data = nullptr,+ });+}++void move_mouse_to(WebView::ViewImplementation& view, Web::DevicePixelPoint position, Web::UIEvents::MouseButton buttons = Web::UIEvents::MouseButton::None)+{+ send_mouse_event(view, Web::MouseEvent::Type::MouseMove, position, Web::UIEvents::MouseButton::None, buttons);+}++void press_mouse_at(WebView::ViewImplementation& view, Web::DevicePixelPoint position)+{+ send_mouse_event(view, Web::MouseEvent::Type::MouseDown, position, Web::UIEvents::MouseButton::Primary, Web::UIEvents::MouseButton::Primary);+}++void release_mouse_at(WebView::ViewImplementation& view, Web::DevicePixelPoint position)+{+ send_mouse_event(view, Web::MouseEvent::Type::MouseUp, position, Web::UIEvents::MouseButton::Primary, Web::UIEvents::MouseButton::None);+}++void click_at(WebView::ViewImplementation& view, Web::DevicePixelPoint position)+{+ press_mouse_at(view, position);+ release_mouse_at(view, position);+}++bool is_cursor(Gfx::Cursor const& cursor, Gfx::StandardCursor standard_cursor)+{+ return cursor.has<Gfx::StandardCursor>() && cursor.get<Gfx::StandardCursor>() == standard_cursor;+}++}++// The browser's controls over a Picture-in-Picture window's page take the presses on them, which the page never sees,+// and show their own cursor and tooltip in place of the page's while hovered. They work while the page's renderer is+// stopped.++ErrorOr<int> ladybird_main(Main::Arguments arguments)+{+ auto test_config_directory = ByteString::formatted("{}/Ladybird-TestPictureInPictureWindowControls-{}", Core::StandardPaths::tempfile_directory(), generate_random_uuid());+ TRY(Core::Directory::create(test_config_directory, Core::Directory::CreateDirectories::Yes));+ auto cleanup_test_config_directory = ScopeGuard([&] {+ MUST(FileSystem::remove(test_config_directory, FileSystem::RecursionMode::Allowed));+ });+ MUST(Core::Environment::set("XDG_CONFIG_HOME"sv, test_config_directory, Core::Environment::Overwrite::Yes));++#if defined(LADYBIRD_BINARY_PATH)+ auto app = TRY(TestApplication::create(arguments, LADYBIRD_BINARY_PATH));+#else+ auto app = TRY(TestApplication::create(arguments, OptionalNone {}));+#endif++ auto theme_path = LexicalPath::join(WebView::s_ladybird_resource_root, "themes"sv, "Default.ini"sv);+ auto theme = TRY(Gfx::load_system_theme(theme_path.string()));++ auto view = WebView::HeadlessWebView::create(move(theme), { 800, 600 });++ size_t loads_finished = 0;+ view->on_load_finish = [&](auto const&) { ++loads_finished; };+ Core::EventLoop::current().spin_until([&]() { return loads_finished >= 1; });++ Utf16String last_title;+ view->on_title_change = [&](auto const& title) { last_title = title; };+ Gfx::Cursor last_cursor { Gfx::StandardCursor::Arrow };+ view->on_cursor_change = [&](auto const& cursor) { last_cursor = cursor; };+ Optional<ByteString> tooltip;+ view->on_enter_tooltip_area = [&](auto const& text) { tooltip = text; };+ view->on_leave_tooltip_area = [&]() { tooltip.clear(); };+ Vector<Compositing::PictureInPictureControl> activated_controls;+ view->on_picture_in_picture_control_activated = [&](auto control) { activated_controls.append(control); };++ view->enable_picture_in_picture_controls();++ // The page logs where it is pressed, released and dragged, and its top-right corner has a cursor and tooltip of its+ // own.+ view->load_html(R"~~~(<!DOCTYPE html>+<title>page saw</title>+<body style="margin:0">+<div title="Page tooltip" style="position:fixed;top:0;right:0;width:100px;height:100px;cursor:crosshair"></div>+<script>+function log(name, event) { document.title += " " + name + " " + event.clientX; }+addEventListener("pointerdown", event => log("down", event));+addEventListener("pointerup", event => log("up", event));+addEventListener("pointermove", event => { if (event.buttons) log("drag", event); });+</script>+</body>)~~~"sv);+ Core::EventLoop::current().spin_until([&]() { return loads_finished >= 2; });+ VERIFY(last_title == "page saw"sv);++ // The Close button is centred at (778, 22) and Back to tab at (22, 22). (790, 10) is in the corner of Close's bounds,+ // over the page's corner but outside the round button.+ Web::DevicePixelPoint close_button { 778, 22 };+ Web::DevicePixelPoint back_to_tab_button { 22, 22 };+ Web::DevicePixelPoint corner_beside_close_button { 790, 10 };+ Web::DevicePixelPoint middle_of_page { 400, 300 };++ move_mouse_to(*view, close_button);+ VERIFY(is_cursor(last_cursor, Gfx::StandardCursor::Hand));+ VERIFY(tooltip == "Close"sv);+ // The page reports its cursor and tooltip apart from finishing the move, so the wait is for those.+ Core::EventLoop::current().spin_until([&]() {+ return is_cursor(view->page_cursor_for_testing(), Gfx::StandardCursor::Crosshair) && view->page_tooltip_for_testing() == "Page tooltip"sv;+ });+ VERIFY(is_cursor(last_cursor, Gfx::StandardCursor::Hand));+ VERIFY(tooltip == "Close"sv);+ outln("a hovered control shows its own cursor and tooltip over the page's");++ move_mouse_to(*view, corner_beside_close_button);+ VERIFY(is_cursor(last_cursor, Gfx::StandardCursor::Crosshair));+ VERIFY(tooltip == "Page tooltip"sv);+ outln("the page's cursor and tooltip return outside the round control");++ move_mouse_to(*view, close_button);+ click_at(*view, close_button);+ VERIFY(activated_controls.size() == 1 && activated_controls[0] == Compositing::PictureInPictureControl::Close);+ outln("a click on a control activates it");++ move_mouse_to(*view, back_to_tab_button);+ press_mouse_at(*view, back_to_tab_button);+ move_mouse_to(*view, middle_of_page, Web::UIEvents::MouseButton::Primary);+ release_mouse_at(*view, middle_of_page);+ VERIFY(activated_controls.size() == 1);+ outln("a press on a control released elsewhere does nothing");++ click_at(*view, corner_beside_close_button);++ // Once the pointer leaves, the controls hide and a press where one was is the page's.+ send_mouse_event(*view, Web::MouseEvent::Type::MouseLeave, {}, Web::UIEvents::MouseButton::None, Web::UIEvents::MouseButton::None);+ click_at(*view, close_button);+ VERIFY(activated_controls.size() == 1);++ click_at(*view, middle_of_page);+ Core::EventLoop::current().spin_until([&]() { return last_title.ends_with(" up 400"sv); });+ VERIFY(last_title == "page saw down 790 up 790 down 778 up 778 down 400 up 400"sv);+ outln("the page sees only the presses off the shown controls");++ signal_renderers(SIGSTOP);+ move_mouse_to(*view, close_button);+ click_at(*view, close_button);+ signal_renderers(SIGCONT);+ VERIFY(activated_controls.size() == 2 && activated_controls[1] == Compositing::PictureInPictureControl::Close);+ outln("a control works while the page's renderer is stopped");++ // The window cancels a press on the page that it takes over to move itself.+ move_mouse_to(*view, middle_of_page);+ VERIFY(view->picture_in_picture_controls_are_shown_for_testing());+ press_mouse_at(*view, middle_of_page);+ send_mouse_event(*view, Web::MouseEvent::Type::MouseCancel, {}, Web::UIEvents::MouseButton::None, Web::UIEvents::MouseButton::None);+ VERIFY(!view->picture_in_picture_controls_are_shown_for_testing());+ outln("a cancelled press hides the controls at once");++ // A real window's page shows a player, whose controls fade together with the window's. The video is moved off its+ // first frame, as its controls otherwise stay shown over it.+ view->load_html(MUST(String::formatted(+ "<!DOCTYPE html>"+ "<button onclick=\"video.requestPictureInPicture().then(() => {{ document.title = 'entered'; }})\" style=\"position:fixed;left:0;top:0;width:800px;height:600px\"></button>"+ "<video id=video src=\"{}\"></video>"+ "<script>"+ "video.addEventListener('seeked', () => {{ document.title = 'ready'; }}, {{ once: true }});"+ "video.currentTime = 0.04;"+ "</script>",+ video_url)));+ Core::EventLoop::current().spin_until([&]() { return last_title == "ready"sv; });+ click_at(*view, middle_of_page);+ Core::EventLoop::current().spin_until([&]() { return last_title == "entered"sv; });++ auto& window_view = WebView::ViewImplementation::find_view_by_handle(app->picture_in_picture_window_handle).value();+ window_view.enable_picture_in_picture_controls();+ auto window_size = app->picture_in_picture_window_size;+ Web::DevicePixelPoint player_control_bar { window_size.width() / 2, window_size.height() - 10 };+ Web::DevicePixelPoint window_close_button { window_size.width() - 22, 22 };+ Web::DevicePixelPoint middle_of_window { window_size.width() / 2, window_size.height() / 2 };++ // The player's control bar shows on the first move and is hovered from the next.+ move_mouse_to(window_view, player_control_bar);+ Core::EventLoop::current().spin_until([&]() { return window_view.picture_in_picture_player_controls_are_shown_for_testing(); });+ move_mouse_to(window_view, player_control_bar);+ wait_for(1500);+ VERIFY(window_view.picture_in_picture_player_controls_are_shown_for_testing());+ VERIFY(window_view.picture_in_picture_controls_are_shown_for_testing());+ outln("the window's controls stay shown while the player's control bar is hovered");++ move_mouse_to(window_view, window_close_button);+ wait_for(1500);+ VERIFY(window_view.picture_in_picture_player_controls_are_shown_for_testing());+ outln("the player's controls stay shown while a window control is hovered");++ move_mouse_to(window_view, middle_of_window);+ Core::EventLoop::current().spin_until([&]() { return !window_view.picture_in_picture_controls_are_shown_for_testing(); });+ VERIFY(!window_view.picture_in_picture_player_controls_are_shown_for_testing());+ outln("both fade once the pointer rests away from them");++ // A press on the page can move onto a control before the window takes it over.+ Gfx::Cursor window_cursor { Gfx::StandardCursor::Arrow };+ window_view.on_cursor_change = [&](auto const& cursor) { window_cursor = cursor; };+ Optional<ByteString> window_tooltip;+ window_view.on_enter_tooltip_area = [&](auto const& text) { window_tooltip = text; };+ window_view.on_leave_tooltip_area = [&]() { window_tooltip.clear(); };+ move_mouse_to(window_view, middle_of_window);+ press_mouse_at(window_view, middle_of_window);+ move_mouse_to(window_view, window_close_button, Web::UIEvents::MouseButton::Primary);+ VERIFY(is_cursor(window_cursor, Gfx::StandardCursor::Hand));+ VERIFY(window_tooltip == "Close"sv);+ send_mouse_event(window_view, Web::MouseEvent::Type::MouseCancel, {}, Web::UIEvents::MouseButton::None, Web::UIEvents::MouseButton::None);+ VERIFY(!is_cursor(window_cursor, Gfx::StandardCursor::Hand));+ VERIFY(window_tooltip != "Close"sv);+ Core::EventLoop::current().spin_until([&]() {+ return !window_view.picture_in_picture_controls_are_shown_for_testing() && !window_view.picture_in_picture_player_controls_are_shown_for_testing();+ });+ outln("a cancelled press leaves no control hovered, so both fade");++ // The page sees none of a press on a control, even once the press moves off it.+ move_mouse_to(window_view, window_close_button);+ press_mouse_at(window_view, window_close_button);+ move_mouse_to(window_view, middle_of_window, Web::UIEvents::MouseButton::Primary);+ wait_for(1500);+ VERIFY(window_view.picture_in_picture_player_controls_are_shown_for_testing());+ release_mouse_at(window_view, middle_of_window);+ Core::EventLoop::current().spin_until([&]() {+ return !window_view.picture_in_picture_controls_are_shown_for_testing() && !window_view.picture_in_picture_player_controls_are_shown_for_testing();+ });+ outln("the player's controls stay shown for the rest of a press on a window control");++ // The controls are where the displayed frame shows them, at the pixel ratio it was painted at, until a frame at a new+ // ratio replaces it. (W - 18, 18) is inside Close at 1x but outside it at 2x, and (W - 44, 44) is the reverse.+ Vector<Compositing::PictureInPictureControl> window_activated_controls;+ window_view.on_picture_in_picture_control_activated = [&](auto control) { window_activated_controls.append(control); };+ Web::DevicePixelPoint close_button_at_1x { window_size.width() - 18, 18 };+ Web::DevicePixelPoint close_button_at_2x { window_size.width() - 44, 44 };++ signal_renderers(SIGSTOP);+ window_view.set_device_pixel_ratio_for_testing(window_view.page(), 2.0);+ move_mouse_to(window_view, close_button_at_1x);+ click_at(window_view, close_button_at_1x);+ VERIFY(window_activated_controls.size() == 1);+ signal_renderers(SIGCONT);+ outln("a control is where the displayed frame shows it while a frame at a new pixel ratio is pending");++ Core::EventLoop::current().spin_until([&]() { return window_view.displayed_frame_device_pixel_ratio_for_testing() == 2.0; });+ move_mouse_to(window_view, close_button_at_2x);+ click_at(window_view, close_button_at_2x);+ VERIFY(window_activated_controls.size() == 2);+ move_mouse_to(window_view, close_button_at_1x);+ click_at(window_view, close_button_at_1x);+ VERIFY(window_activated_controls.size() == 2);+ outln("a control follows the frame at the new pixel ratio once it is displayed");++ return 0;+}diff --git a/UI/Qt/MacWindow.mm b/UI/Qt/MacWindow.mmindex 9863d5f79ca..835a94b89cd 100644--- a/UI/Qt/MacWindow.mm+++ b/UI/Qt/MacWindow.mm@@ -10,6 +10,8 @@#include <LibGfx/Point.h>#include <QAbstractNativeEventFilter>#include <QCoreApplication>+#include <QMouseEvent>+#include <QPointer>#include <QWidget>#include <QWindow>#include <UI/Qt/WebContentView.h>@@ -192,7 +194,7 @@ static NSPoint widget_position_to_ns_point(QWidget& widget, Gfx::IntPoint positireturn point;}-static Optional<Gfx::IntPoint> widget_position_for_appkit_event(QWidget& widget, NSEvent* event)+static Optional<QPointF> widget_point_for_appkit_event(QWidget& widget, NSEvent* event){auto* view = reinterpret_cast<NSView*>(widget.winId());if (!view || !event.window)@@ -201,11 +203,19 @@ static Optional<Gfx::IntPoint> widget_position_for_appkit_event(QWidget& widget,auto point = [view convertPoint:event.locationInWindow fromView:nil];if (![view isFlipped])point.y = NSHeight(view.bounds) - point.y;+ return QPointF { point.x, point.y };+}++static Optional<Gfx::IntPoint> widget_position_for_appkit_event(QWidget& widget, NSEvent* event)+{+ auto point = widget_point_for_appkit_event(widget, event);+ if (!point.has_value())+ return {};auto device_pixel_ratio = widget.devicePixelRatioF();return Gfx::IntPoint {- static_cast<int>(point.x * device_pixel_ratio),- static_cast<int>(point.y * device_pixel_ratio),+ static_cast<int>(point->x() * device_pixel_ratio),+ static_cast<int>(point->y() * device_pixel_ratio),};}@@ -267,6 +277,39 @@ static bool perform_dictionary_lookup_for_event(NSEvent* event)return view->look_up_selected_text_at(*position);}+static QPointer<WebContentView> s_view_following_mouse_while_inactive;++// Qt ignores mouse movement while Ladybird is inactive, so it is passed on here to a view that follows it anyway.+static void follow_mouse_while_inactive(NSEvent* event)+{+ if (!event || NSApp.active)+ return;++ if (event.type == NSEventTypeMouseExited) {+ auto view = s_view_following_mouse_while_inactive;+ if (!view || event.window != reinterpret_cast<NSView*>(view->winId()).window)+ return;+ s_view_following_mouse_while_inactive = nullptr;+ QEvent leave_event { QEvent::Leave };+ QCoreApplication::sendEvent(view, &leave_event);+ return;+ }++ if (event.type != NSEventTypeMouseMoved)+ return;++ auto* view = web_content_view_for_appkit_event(event);+ if (!view || !view->follows_mouse_while_inactive())+ return;+ auto point = widget_point_for_appkit_event(*view, event);+ if (!point.has_value())+ return;++ s_view_following_mouse_while_inactive = view;+ QMouseEvent move_event { QEvent::MouseMove, *point, view->mapToGlobal(*point), Qt::NoButton, Qt::NoButton, Qt::NoModifier };+ QCoreApplication::sendEvent(view, &move_event);+}+class LadybirdAppKitEventCaptureFilter final : public QAbstractNativeEventFilter {public:AK_ALLOC_WITH_KMALLOC;@@ -277,6 +320,8 @@ public:return false;auto* event = static_cast<NSEvent*>(message);+ follow_mouse_while_inactive(event);+if (perform_dictionary_lookup_for_event(event))return true;diff --git a/UI/Qt/PictureInPictureWindow.cpp b/UI/Qt/PictureInPictureWindow.cppindex 9d99eb759e6..28c1a88375d 100644--- a/UI/Qt/PictureInPictureWindow.cpp+++ b/UI/Qt/PictureInPictureWindow.cpp@@ -4,6 +4,7 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <LibCompositing/PictureInPictureControls.h>#include <UI/Qt/PictureInPictureWindow.h>#include <UI/Qt/WebContentView.h>#include <UI/Qt/WindowScreenObserver.h>@@ -80,6 +81,8 @@ public:};report_page_close_of(*m_view);m_view->installEventFilter(this);+ // The window's controls, and its page's, show on hover, which works while Ladybird is in the background too.+ m_view->set_follows_mouse_while_inactive(true);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);@@ -89,6 +92,19 @@ public:// scaled to keep the video filling the window.m_view->set_scales_frames_to_fit(true);+ m_view->enable_picture_in_picture_controls();+ m_view->on_picture_in_picture_control_activated = [this, owner_view_id = owner_view.view_id()](auto control) {+ switch (control) {+ case Compositing::PictureInPictureControl::BackToTab:+ go_back_to_tab(owner_view_id);+ break;+ case Compositing::PictureInPictureControl::Close:+ if (on_close)+ on_close(VideoKeepsPlaying::No);+ break;+ }+ };+auto* layout = new QVBoxLayout(this);layout->setContentsMargins(0, 0, 0, 0);layout->addWidget(m_view);@@ -110,7 +126,8 @@ public: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.+ // press off the window's controls 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)@@ -166,6 +183,22 @@ private:}}+ // The window belongs to the tab it was opened from, which comes forward along with its own window, even from being+ // minimized, to play the video there.+ void go_back_to_tab(u64 owner_view_id)+ {+ if (auto owner = WebView::ViewImplementation::find_view_by_id(owner_view_id); owner.has_value()) {+ auto* browser_window = static_cast<WebContentView&>(*owner).window();+ browser_window->setWindowState(browser_window->windowState() & ~Qt::WindowMinimized);+ browser_window->raise();+ browser_window->activateWindow();+ if (owner->on_activate_tab)+ owner->on_activate_tab();+ }+ if (on_close)+ on_close(VideoKeepsPlaying::Yes);+ }+void page_did_handle_press(Web::EventResult result){if (!m_press.has_value() || m_press->claim != PressClaim::Unknown)@@ -201,7 +234,7 @@ private:// video has exited.event->accept();if (on_close)- on_close();+ on_close(VideoKeepsPlaying::No);}WebContentView* m_view { nullptr };diff --git a/UI/Qt/WebContentView.h b/UI/Qt/WebContentView.hindex 1cdae4432e6..ef3d9d048af 100644--- a/UI/Qt/WebContentView.h+++ b/UI/Qt/WebContentView.h@@ -157,6 +157,10 @@ public:QPoint map_point_to_global_position(Gfx::IntPoint) const;+ // On macOS, Qt ignores mouse movement while Ladybird is inactive, so a view that should still follow it opts in.+ bool follows_mouse_while_inactive() const { return m_follows_mouse_while_inactive; }+ void set_follows_mouse_while_inactive(bool follows) { m_follows_mouse_while_inactive = follows; }+signals:void ready_to_paint();void urls_dropped(QList<QUrl> const&);@@ -241,6 +245,8 @@ private:bool m_report_accessibility_focus_with_next_tree { false };#endif+ bool m_follows_mouse_while_inactive { false };+bool m_tooltip_override { false };Optional<ByteString> m_tooltip_text;QTimer m_tooltip_hover_timer;