From d5412f4e780430c95e1555f1caf07d434b28b9b5 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Mon, 10 Apr 2023 11:52:44 -0400 Subject: LibWeb: Move VideoPaintable's cached mouse position to HTMLVideoElement The layout node, and therefore the painting box, is frequently destroyed and recreated. This causes us to forget the cached mouse position we use to highlight media controls. Move this cached position to the DOM node instead, which survives relayout. --- Userland/Libraries/LibWeb/HTML/HTMLVideoElement.h | 7 +++++++ Userland/Libraries/LibWeb/Painting/VideoPaintable.cpp | 14 +++++++++----- Userland/Libraries/LibWeb/Painting/VideoPaintable.h | 3 --- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Userland/Libraries/LibWeb/HTML/HTMLVideoElement.h b/Userland/Libraries/LibWeb/HTML/HTMLVideoElement.h index 2c1c82cb47..0a268189fd 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLVideoElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLVideoElement.h @@ -6,6 +6,7 @@ #pragma once +#include #include #include #include @@ -32,6 +33,9 @@ public: void set_current_frame(Badge, RefPtr frame); RefPtr const& current_frame() const { return m_current_frame; } + void set_layout_mouse_position(Badge, Optional mouse_position) { m_mouse_position = move(mouse_position); } + Optional const& layout_mouse_position(Badge) const { return m_mouse_position; } + private: HTMLVideoElement(DOM::Document&, DOM::QualifiedName); @@ -48,6 +52,9 @@ private: u32 m_video_width { 0 }; u32 m_video_height { 0 }; + + // Cached state for layout + Optional m_mouse_position; }; } diff --git a/Userland/Libraries/LibWeb/Painting/VideoPaintable.cpp b/Userland/Libraries/LibWeb/Painting/VideoPaintable.cpp index 3db544eee5..4b2c3e2058 100644 --- a/Userland/Libraries/LibWeb/Painting/VideoPaintable.cpp +++ b/Userland/Libraries/LibWeb/Painting/VideoPaintable.cpp @@ -64,11 +64,13 @@ void VideoPaintable::paint(PaintContext& context, PaintPhase phase) const auto video_rect = context.rounded_device_rect(absolute_rect()); ScopedCornerRadiusClip corner_clip { context, context.painter(), video_rect, normalized_border_radii_data(ShrinkRadiiForBorders::Yes) }; + auto const& video_element = layout_box().dom_node(); + auto const& layout_mouse_position = video_element.layout_mouse_position({}); + Optional mouse_position; - if (m_mouse_position.has_value()) - mouse_position = context.rounded_device_point(*m_mouse_position); + if (layout_mouse_position.has_value() && document().hovered_node() == &video_element) + mouse_position = context.rounded_device_point(*layout_mouse_position); - auto const& video_element = layout_box().dom_node(); auto paint_user_agent_controls = video_element.has_attribute(HTML::AttributeNames::controls); if (auto const& bitmap = layout_box().dom_node().current_frame()) { @@ -201,12 +203,14 @@ VideoPaintable::DispatchEventOfSameName VideoPaintable::handle_mouseup(Badge, CSSPixelPoint position, unsigned, unsigned) { + auto& video_element = layout_box().dom_node(); + if (absolute_rect().contains(position)) { - m_mouse_position = position; + video_element.set_layout_mouse_position({}, position); return DispatchEventOfSameName::Yes; } - m_mouse_position.clear(); + video_element.set_layout_mouse_position({}, {}); return DispatchEventOfSameName::No; } diff --git a/Userland/Libraries/LibWeb/Painting/VideoPaintable.h b/Userland/Libraries/LibWeb/Painting/VideoPaintable.h index 1d7204f0a6..ac4e1fa09e 100644 --- a/Userland/Libraries/LibWeb/Painting/VideoPaintable.h +++ b/Userland/Libraries/LibWeb/Painting/VideoPaintable.h @@ -6,7 +6,6 @@ #pragma once -#include #include #include @@ -32,8 +31,6 @@ private: void paint_loaded_video_controls(PaintContext&, HTML::HTMLVideoElement const&, DevicePixelRect video_rect, Optional const& mouse_position) const; void paint_placeholder_video_controls(PaintContext&, DevicePixelRect video_rect, Optional const& mouse_position) const; - - Optional m_mouse_position; }; } -- cgit v1.2.3