Truly independent web browser
LibWeb: Remove the document's hit-test display list
Aliaksandr Kalenik1b746fdd93b6
diff --git a/Libraries/LibWeb/CMakeLists.txt b/Libraries/LibWeb/CMakeLists.txtindex ad58aad81d9..f4efd1f76ab 100644--- a/Libraries/LibWeb/CMakeLists.txt+++ b/Libraries/LibWeb/CMakeLists.txt@@ -709,7 +709,7 @@ set(SOURCESPainting/PaintFacts.cppPainting/ChromeWidget.cppPainting/DocumentPaintState.cpp- Painting/HitTestDisplayList.cpp+ Painting/HitTestQuery.cppPainting/HitTestResult.cppPainting/CompositorAnimationEffectState.cppPainting/PaintingRustBridge.cppdiff --git a/Libraries/LibWeb/CSS/UpdateStyle.cpp b/Libraries/LibWeb/CSS/UpdateStyle.cppindex 0795878078d..47a3fe66c03 100644--- a/Libraries/LibWeb/CSS/UpdateStyle.cpp+++ b/Libraries/LibWeb/CSS/UpdateStyle.cpp@@ -147,7 +147,7 @@ static void apply_document_style_invalidation_after_style_change(DOM::Document&if (invalidation.invalidates_hit_test_display_list())document.set_needs_to_record_display_list();else- document.set_needs_to_record_display_list_keeping_hit_test_display_list();+ document.set_needs_to_record_display_list_keeping_hit_test_items_on_screen();}// One element's computed style answers for another only while the inputs it was keyed on still mean what they meant. Adiff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cppindex ea7f752b22f..75ff3e8a3a5 100644--- a/Libraries/LibWeb/DOM/Document.cpp+++ b/Libraries/LibWeb/DOM/Document.cpp@@ -245,7 +245,7 @@#include <LibWeb/Painting/ChromeWidget.h>#include <LibWeb/Painting/CompositorAnimationEffectState.h>#include <LibWeb/Painting/DocumentPaintState.h>-#include <LibWeb/Painting/HitTestDisplayList.h>+#include <LibWeb/Painting/HitTestQuery.h>#include <LibWeb/Painting/PaintFacts.h>#include <LibWeb/Painting/PaintableTypes.h>#include <LibWeb/Painting/PaintingRustBridge.h>@@ -2042,7 +2042,7 @@ void Document::tear_down_layout_tree()auto* layout_root = layout_root_if_live(read);if (layout_root)layout_root->prepare_subtree_for_detach_from_layout_tree();- m_hit_test_display_list = nullptr;+ m_hit_test_items_on_screen = {};m_chrome_widget_registry->clear();if (layout_root)layout_node_arena().free_subtree(read, Layout::Node::slot_id(layout_root));@@ -11861,7 +11861,7 @@ void Document::set_needs_repaint(InvalidateDisplayList should_invalidate_displaycase InvalidateDisplayList::No:break;case InvalidateDisplayList::PaintCommands:- set_needs_to_record_display_list_keeping_hit_test_display_list();+ set_needs_to_record_display_list_keeping_hit_test_items_on_screen();break;case InvalidateDisplayList::PaintCommandsAndHitTestList:set_needs_to_record_display_list();@@ -12043,11 +12043,11 @@ Vector<Compositing::RustFFI::NodeSlotId> Document::collect_scroll_snap_containervoid Document::set_needs_to_record_display_list(){- m_hit_test_display_list = nullptr;- set_needs_to_record_display_list_keeping_hit_test_display_list();+ m_hit_test_items_on_screen = {};+ set_needs_to_record_display_list_keeping_hit_test_items_on_screen();}-void Document::set_needs_to_record_display_list_keeping_hit_test_display_list()+void Document::set_needs_to_record_display_list_keeping_hit_test_items_on_screen(){if (auto navigable = this->navigable())navigable->set_needs_to_record_display_list();@@ -12122,33 +12122,42 @@ Optional<Painting::DisplayListRecording> Document::start_recording(Layout::Begunreturn Painting::start_rust_display_list_recording(read, *this, visual_context_tree_structural_epoch, surface_clear_color, cache_mode, config, overlay_inputs, move(committed));}-void Document::finish_display_list_recording(Layout::BegunRead const& read, Painting::DisplayListRecording const& recording, Painting::HitTestListStands hit_test_list_stands)+void Document::finish_display_list_recording(Layout::BegunRead const& read, Painting::DisplayListRecording const& recording, Painting::HitTestItemsStand hit_test_items_stand){Painting::publish_rust_display_list_recording(read, *this);- adopt_published_recording(hit_test_list_read(read, hit_test_list_stands), recording);+ adopt_published_recording(hit_test_items_stand, recording);}-Painting::HitTestListAdoption Document::adopt_published_recording(Optional<Layout::BegunRead const&> hit_test_list_read, Painting::DisplayListRecording const& recording)+bool Document::hit_test_items_on_screen_are_as_adopted() const{- auto adoption = Painting::HitTestListAdoption::Kept;- if (!hit_test_list_read.has_value()) {- m_hit_test_display_list = nullptr;- adoption = Painting::HitTestListAdoption::Dropped;- } else if (!m_hit_test_display_list || !m_hit_test_display_list->is_current()) {- m_hit_test_display_list = Painting::HitTestDisplayList::create_from_rust_recording(*hit_test_list_read, recording.visual_context_tree_structural_epoch, layout_node_arena(), *m_chrome_widget_registry);- adoption = Painting::HitTestListAdoption::TookNew;+ if (!m_hit_test_items_on_screen.has_value() || m_hit_test_items_on_screen->generation == 0)+ return false;+ return Layout::RustFFI::layout_hit_test_items_on_screen_generation(m_layout_node_arena->host()) == m_hit_test_items_on_screen->generation;+}++Painting::HitTestItemsAdoption Document::adopt_published_recording(Painting::HitTestItemsStand hit_test_items_stand, Painting::DisplayListRecording const& recording)+{+ if (hit_test_items_stand == Painting::HitTestItemsStand::No) {+ m_hit_test_items_on_screen = {};+ return Painting::HitTestItemsAdoption::Dropped;}- return adoption;+ if (hit_test_items_on_screen_are_as_adopted())+ return Painting::HitTestItemsAdoption::Kept;+ m_hit_test_items_on_screen = HitTestItemsOnScreen {+ .generation = Layout::RustFFI::layout_hit_test_items_on_screen_generation(layout_node_arena().host()),+ .visual_context_tree_structural_epoch = recording.visual_context_tree_structural_epoch,+ };+ return Painting::HitTestItemsAdoption::TookNew;}-bool Document::hit_test_display_list_is_current(Layout::BegunRead const& read)+bool Document::hit_test_items_on_screen_are_current(Layout::BegunRead const& read){Painting::publish_image_map_area_facts_if_needed(read, *this);if (!has_committed_viewport_box())return false;- // Reading the epoch prepares the paint properties, which can drop the hit-test list.+ // Reading the epoch prepares the paint properties, which can change what the rows say can be hit.auto structural_epoch = paint_state().visual_context_tree_structural_epoch(read);- return m_hit_test_display_list && m_hit_test_display_list->is_current() && m_hit_test_display_list->visual_context_tree_structural_epoch() == structural_epoch;+ return hit_test_items_on_screen_are_as_adopted() && m_hit_test_items_on_screen->visual_context_tree_structural_epoch == structural_epoch;}void Document::set_caret_hit_test_debug_rect(Optional<CSSPixelRect> rect)@@ -12170,11 +12179,10 @@ Optional<Painting::HitTestQuery> Document::prepare_hit_test_query(Layout::BegunR// A hit test reads the document's rows as they are now, so it waits for no recording and makes none.Layout::RustFFI::render_state_prepare_paint_properties_before_hit_test(layout_node_arena().host(), &read);- if (!m_hit_test_display_list)- m_hit_test_display_list = Painting::HitTestDisplayList::create_before_any_recording(layout_node_arena(), *m_chrome_widget_registry);return Painting::HitTestQuery {- *m_hit_test_display_list,+ layout_node_arena(),+ *m_chrome_widget_registry,page().client().device_pixels_per_css_pixel(),page().chrome_metrics(),visual_animation_sample_time_nanoseconds,@@ -12198,7 +12206,7 @@ Optional<Painting::HitTestResult> Document::hit_test(Layout::BegunRead const& re// 2. If there is a box in the viewport that would be a target for hit testing at coordinates x,y, when applying// the transforms that apply to the descendants of the viewport, return the associated element and terminate// these steps.- auto result = query->list().hit_test(read, position, *query);+ auto result = query->hit_test(read, position);if (result.has_value() && (result->chrome_widget || result->is_scrollbar || result->dom_node()))return result;@@ -12214,7 +12222,7 @@ Optional<Painting::CaretPosition> Document::caret_position_from_point(Layout::Beauto query = prepare_hit_test_query(read);if (!query.has_value())return {};- return query->list().caret_position_from_point(read, position, *query, constraint_scope);+ return query->caret_position_from_point(read, position, constraint_scope);}TraversalDecision Document::hit_test_all(Layout::BegunRead const& read, CSSPixelPoint position, Function<TraversalDecision(Painting::HitTestResult)> const& callback)@@ -12222,7 +12230,7 @@ TraversalDecision Document::hit_test_all(Layout::BegunRead const& read, CSSPixelauto query = prepare_hit_test_query(read);if (!query.has_value())return TraversalDecision::Continue;- return query->list().hit_test_all(read, position, *query, callback);+ return query->hit_test_all(read, position, callback);}Unicode::Segmenter& Document::grapheme_segmenter() constdiff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.hindex 04eeb4ad7e9..2d8a54c093a 100644--- a/Libraries/LibWeb/DOM/Document.h+++ b/Libraries/LibWeb/DOM/Document.h@@ -1659,12 +1659,11 @@ public:// Commits the navigable's next frame, a recording of the document, to the render owner, which presents it with// `presentation` beside the event loop.Painting::DisplayListRecording commit_display_list_recording(Layout::BegunRead const& read, HTML::PaintConfig, Compositor::FlightPresentation&& presentation);- void finish_display_list_recording(Layout::BegunRead const& read, Painting::DisplayListRecording const&, Painting::HitTestListStands = Painting::HitTestListStands::Yes);- // Takes in the hit-test list `recording` made, read in `hit_test_list_read`, where that still stands for the- // document's boxes. Where none does, nothing is read.- Painting::HitTestListAdoption adopt_published_recording(Optional<Layout::BegunRead const&> hit_test_list_read, Painting::DisplayListRecording const&);- // Indicates whether a query of the hit test list would require a synchronous update to it.- [[nodiscard]] bool hit_test_display_list_is_current(Layout::BegunRead const& read);+ void finish_display_list_recording(Layout::BegunRead const& read, Painting::DisplayListRecording const&, Painting::HitTestItemsStand = Painting::HitTestItemsStand::Yes);+ // Takes note of the hit-test items of the frame `recording` made, where they still stand for the document's boxes.+ Painting::HitTestItemsAdoption adopt_published_recording(Painting::HitTestItemsStand, Painting::DisplayListRecording const&);+ // Whether the frame the document adopted last still places what can be hit as the document's rows do.+ [[nodiscard]] bool hit_test_items_on_screen_are_current(Layout::BegunRead const& read);// Where a visual animation sample time is given, the hit test reads the compositor's animations at that time, as a// frame of the compositor's shows them.Optional<Painting::HitTestQuery> prepare_hit_test_query(Layout::BegunRead const& read, Optional<i64> visual_animation_sample_time_nanoseconds = {});@@ -1680,7 +1679,7 @@ public:RenderingStats& ensure_rendering_stats() const;void set_needs_to_record_display_list();- void set_needs_to_record_display_list_keeping_hit_test_display_list();+ void set_needs_to_record_display_list_keeping_hit_test_items_on_screen();Unicode::Segmenter& grapheme_segmenter() const;Unicode::Segmenter& line_segmenter() const;@@ -2480,9 +2479,15 @@ private:HashTable<GC::Ref<Element>> m_list_owners_pending_item_renumber;HashTable<GC::Ref<Element>> m_list_owners_with_stale_item_counters;bool m_stale_list_item_counter_rendered { false };- RefPtr<Painting::HitTestDisplayList> m_hit_test_display_list;- // The previous recording's list, retained so cached per-paintable item ranges can be spliced into- // the next recording. Rotated only by cache-read-write recordings; survives display list invalidation.+ // The hit-test items of the frame the document adopted last: their generation as the frame landed, and the+ // structural epoch of the visual context tree the frame was recorded against. None once something that can be hit+ // changed, or where the boxes of the frame may be gone.+ struct HitTestItemsOnScreen {+ u64 generation { 0 };+ u64 visual_context_tree_structural_epoch { 0 };+ };+ Optional<HitTestItemsOnScreen> m_hit_test_items_on_screen;+ [[nodiscard]] bool hit_test_items_on_screen_are_as_adopted() const;Optional<CSSPixelRect> m_caret_hit_test_debug_rect;mutable StyleUpdateStatistics m_style_update_statistics;diff --git a/Libraries/LibWeb/HTML/FormAssociatedElement.cpp b/Libraries/LibWeb/HTML/FormAssociatedElement.cppindex a272a69ee25..7f8cd8438f6 100644--- a/Libraries/LibWeb/HTML/FormAssociatedElement.cpp+++ b/Libraries/LibWeb/HTML/FormAssociatedElement.cpp@@ -44,7 +44,6 @@#include <LibWeb/Layout/TextNode.h>#include <LibWeb/Page/EventHandler.h>#include <LibWeb/Painting/BoxViews.h>-#include <LibWeb/Painting/HitTestDisplayList.h>#include <LibWeb/Selection/CaretNavigation.h>#include <LibWeb/UIEvents/InputTypes.h>#include <LibWebCommon/Infra/Strings.h>diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cppindex 66063af6404..b10e205e84d 100644--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp@@ -7318,22 +7318,22 @@ void LocalNavigable::finish_recording_in_flight(RecordingInFlight& in_flight, La// What the recording made goes to a document this navigable still presents, which still has the boxes it recorded.if (has_been_destroyed() || !has_compositor_context() || active_document().ptr() != document.ptr() || !document->has_committed_viewport_box())return;- // A frame presented without a recording keeps the hit-test list.- auto hit_test_list_adoption = Painting::HitTestListAdoption::Kept;+ // A frame presented without a recording keeps the hit-test items on screen.+ auto hit_test_items_adoption = Painting::HitTestItemsAdoption::Kept;if (landing != Layout::RustFFI::FfiRecordingLanding::PresentedUnrecorded) {// What the recording made stands as the compositor's frame. One that landed after the host wrote the document's- // rows leaves no hit-test list, as the boxes it names may be gone.- auto const hit_test_list_stands = landing == Layout::RustFFI::FfiRecordingLanding::LandedBehindRows ? Painting::HitTestListStands::No : Painting::HitTestListStands::Yes;+ // rows leaves no hit-test items standing, as the boxes it shows may be gone.+ auto const hit_test_items_stand = landing == Layout::RustFFI::FfiRecordingLanding::LandedBehindRows ? Painting::HitTestItemsStand::No : Painting::HitTestItemsStand::Yes;Painting::take_recording_trace_if_pending(read, *document);- hit_test_list_adoption = document->adopt_published_recording(Painting::hit_test_list_read(read, hit_test_list_stands), *in_flight.recording);+ hit_test_items_adoption = document->adopt_published_recording(hit_test_items_stand, *in_flight.recording);}// A frame that kept its hit-test items, and sent no visual context tree, placed them as the frame before it did,// unless it sent other scroll offsets. The hover waits for a scroll of the user's to stop instead (see// schedule_hover_update_after_async_scroll).auto const frame_placement = [&] {- if (hit_test_list_adoption == Painting::HitTestListAdoption::TookNew)+ if (hit_test_items_adoption == Painting::HitTestItemsAdoption::TookNew)return Web::EventHandler::FramePlacement::Changed;- if (hit_test_list_adoption != Painting::HitTestListAdoption::Kept)+ if (hit_test_items_adoption != Painting::HitTestItemsAdoption::Kept)return Web::EventHandler::FramePlacement::Unchanged;if (in_flight.sends_visual_context_tree)return Web::EventHandler::FramePlacement::Changed;diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cppindex fb648df6ce0..7079131efcb 100644--- a/Libraries/LibWeb/Page/EventHandler.cpp+++ b/Libraries/LibWeb/Page/EventHandler.cpp@@ -74,7 +74,6 @@#include <LibWeb/Painting/BoxViews.h>#include <LibWeb/Painting/ChromeWidget.h>#include <LibWeb/Painting/DocumentPaintState.h>-#include <LibWeb/Painting/HitTestDisplayList.h>#include <LibWeb/Painting/ScrollSnap.h>#include <LibWeb/Selection/Selection.h>#include <LibWeb/UIEvents/EventNames.h>@@ -1528,7 +1527,7 @@ bool EventHandler::update_hover_where_the_pointer_rests(HTML::HoverUpdatePoint hif (document->layout_is_known_stale())return true;}- if (hover_update_point == HTML::HoverUpdatePoint::BetweenTasks && !document->hit_test_display_list_is_current(read)) {+ if (hover_update_point == HTML::HoverUpdatePoint::BetweenTasks && !document->hit_test_items_on_screen_are_current(read)) {return true;}m_hover_may_be_stale = false;diff --git a/Libraries/LibWeb/Painting/DisplayListRecording.h b/Libraries/LibWeb/Painting/DisplayListRecording.hindex 10ba49a0292..ffa78e8e8f9 100644--- a/Libraries/LibWeb/Painting/DisplayListRecording.h+++ b/Libraries/LibWeb/Painting/DisplayListRecording.h@@ -12,28 +12,20 @@namespace Web::Painting {-// Whether the hit-test list a recording made still names the document's boxes: not where the recording landed after-// the host wrote the document's rows.-enum class HitTestListStands {+// Whether the hit-test items of the frame a recording made still stand for the document's boxes: not where the+// recording landed after the host wrote the document's rows.+enum class HitTestItemsStand {No,Yes,};-// What a document did with the hit-test list a recording made as it took the recording in.-enum class HitTestListAdoption : u8 {+// What a document did with the hit-test items of the frame a recording made as it took the recording in.+enum class HitTestItemsAdoption : u8 {Dropped,Kept,TookNew,};-// The read a recording's hit-test list is made in, `read`, where the list stands, and none otherwise.-inline Optional<Layout::BegunRead const&> hit_test_list_read(Layout::BegunRead const& read, HitTestListStands stands)-{- if (stands == HitTestListStands::No)- return {};- return read;-}-// A recording of a document's display list, started from the document as it was then: done in step with the host, or// in flight beside the event loop until the event loop takes it in. Rust keeps what it records.struct DisplayListRecording {diff --git a/Libraries/LibWeb/Painting/Forward.h b/Libraries/LibWeb/Painting/Forward.hindex e10a4c051f5..39f36dd7369 100644--- a/Libraries/LibWeb/Painting/Forward.h+++ b/Libraries/LibWeb/Painting/Forward.h@@ -12,9 +12,8 @@namespace Web::Painting {class DocumentPaintState;-class HitTestDisplayList;class HitTestQuery;-enum class HitTestListAdoption : u8;+enum class HitTestItemsAdoption : u8;struct CaretPosition;struct DisplayListRecording;struct HitTestResult;diff --git a/Libraries/LibWeb/Painting/HitTestDisplayList.h b/Libraries/LibWeb/Painting/HitTestDisplayList.hdeleted file mode 100644index be210ce4840..00000000000--- a/Libraries/LibWeb/Painting/HitTestDisplayList.h+++ /dev/null@@ -1,81 +0,0 @@-/*- * Copyright (c) 2026-present, the Ladybird developers.- *- * SPDX-License-Identifier: BSD-2-Clause- */--#pragma once--#include <AK/RefCounted.h>-#include <AK/Vector.h>-#include <LibCompositing/Forward.h>-#include <LibGC/Cell.h>-#include <LibGC/Ptr.h>-#include <LibWeb/Forward.h>-#include <LibWeb/Layout/LayoutRustFFI.h>-#include <LibWeb/Layout/NodeArena.h>-#include <LibWeb/Painting/ChromeMetrics.h>-#include <LibWeb/Painting/ChromeWidget.h>-#include <LibWeb/Painting/HitTestResult.h>-#include <LibWeb/Painting/Scrolling.h>--namespace Web::Painting {--class HitTestDisplayList;--// A query of a document's hit test list, with what it reads besides the list and the document's scroll state, such as-// the time it samples the compositor's animations at. Only the document makes one, once it has prepared its paint-// properties and found the list current against the visual context tree, so every item a query looks at reads the same-// preparation, and nothing in a query prepares the document again.-class HitTestQuery {-public:- HitTestDisplayList const& list() const { return m_list; }- double device_pixels_per_css_pixel() const { return m_device_pixels_per_css_pixel; }- ChromeMetrics const& chrome_metrics() const { return m_chrome_metrics; }- // The time the query reads the compositor's animations at, as a frame of the compositor's shows them, if any.- Optional<i64> visual_animation_sample_time_nanoseconds() const { return m_visual_animation_sample_time_nanoseconds; }--private:- friend class DOM::Document;-- HitTestQuery(HitTestDisplayList const& list, double device_pixels_per_css_pixel, ChromeMetrics chrome_metrics, Optional<i64> visual_animation_sample_time_nanoseconds)- : m_list(list)- , m_device_pixels_per_css_pixel(device_pixels_per_css_pixel)- , m_chrome_metrics(chrome_metrics)- , m_visual_animation_sample_time_nanoseconds(visual_animation_sample_time_nanoseconds)- {- }-- HitTestDisplayList const& m_list;- double m_device_pixels_per_css_pixel { 1 };- ChromeMetrics m_chrome_metrics;- Optional<i64> m_visual_animation_sample_time_nanoseconds;-};--class WEB_API HitTestDisplayList : public RefCounted<HitTestDisplayList> {-public:- static NonnullRefPtr<HitTestDisplayList> create_from_rust_recording(Layout::BegunRead const& read, u64 visual_context_tree_structural_epoch, Layout::NodeArena&, ChromeWidgetRegistry&);- // What answers hit tests of a document no recording of which has landed: it is current for no frame.- static NonnullRefPtr<HitTestDisplayList> create_before_any_recording(Layout::NodeArena&, ChromeWidgetRegistry&);-- u64 visual_context_tree_structural_epoch() const { return m_visual_context_tree_structural_epoch; }- [[nodiscard]] bool is_current() const;- [[nodiscard]] Optional<HitTestResult> hit_test(Layout::BegunRead const& read, CSSPixelPoint, HitTestQuery const&) const;- // The caret position the box hit at the point gives it, or when constraint_scope is given, the one that node's box- // gives it wherever the point is.- [[nodiscard]] Optional<CaretPosition> caret_position_from_point(Layout::BegunRead const& read, CSSPixelPoint, HitTestQuery const&, GC::Ptr<DOM::Node const> constraint_scope = nullptr) const;- TraversalDecision hit_test_all(Layout::BegunRead const& read, CSSPixelPoint, HitTestQuery const&, Function<TraversalDecision(HitTestResult)> const&) const;--private:- HitTestDisplayList(u64 visual_context_tree_structural_epoch, Layout::NodeArena&, ChromeWidgetRegistry&, u64 rust_generation);-- [[nodiscard]] HitTestResult hit_test_result_for(Layout::BegunRead const& read, Layout::RustFFI::FfiPointHit const&) const;- [[nodiscard]] Optional<CaretPosition> caret_position_from(Layout::RustFFI::FfiCaretAt const&) const;-- u64 m_visual_context_tree_structural_epoch { 0 };- NonnullRefPtr<Layout::NodeArena> m_arena;- NonnullRefPtr<ChromeWidgetRegistry> m_chrome_widget_registry;- u64 m_rust_generation { 0 };-};--}diff --git a/Libraries/LibWeb/Painting/HitTestDisplayList.cpp b/Libraries/LibWeb/Painting/HitTestQuery.cppsimilarity index 74%rename from Libraries/LibWeb/Painting/HitTestDisplayList.cpprename to Libraries/LibWeb/Painting/HitTestQuery.cppindex fbef40f8a70..28ec9169056 100644--- a/Libraries/LibWeb/Painting/HitTestDisplayList.cpp+++ b/Libraries/LibWeb/Painting/HitTestQuery.cpp@@ -11,43 +11,19 @@#include <LibWeb/Painting/BoxViews.h>#include <LibWeb/Painting/ChromeMetrics.h>#include <LibWeb/Painting/ChromeWidget.h>-#include <LibWeb/Painting/HitTestDisplayList.h>+#include <LibWeb/Painting/HitTestQuery.h>#include <LibWeb/Painting/PaintingRustBridge.h>#include <LibWeb/Painting/ResizeHandle.h>namespace Web::Painting {-NonnullRefPtr<HitTestDisplayList> HitTestDisplayList::create_from_rust_recording(Layout::BegunRead const& read, u64 visual_context_tree_structural_epoch, Layout::NodeArena& arena, ChromeWidgetRegistry& chrome_widget_registry)-{- (void)read;- return adopt_ref(*new HitTestDisplayList(visual_context_tree_structural_epoch, arena, chrome_widget_registry, Layout::RustFFI::layout_hit_test_list_generation(arena.host())));-}--NonnullRefPtr<HitTestDisplayList> HitTestDisplayList::create_before_any_recording(Layout::NodeArena& arena, ChromeWidgetRegistry& chrome_widget_registry)-{- return adopt_ref(*new HitTestDisplayList(0, arena, chrome_widget_registry, 0));-}--HitTestDisplayList::HitTestDisplayList(u64 visual_context_tree_structural_epoch, Layout::NodeArena& arena, ChromeWidgetRegistry& chrome_widget_registry, u64 rust_generation)- : m_visual_context_tree_structural_epoch(visual_context_tree_structural_epoch)- , m_arena(arena)- , m_chrome_widget_registry(chrome_widget_registry)- , m_rust_generation(rust_generation)-{-}--bool HitTestDisplayList::is_current() const-{- return m_rust_generation != 0 && Layout::RustFFI::layout_hit_test_list_generation(m_arena->host()) == m_rust_generation;-}--static Layout::RustFFI::FfiHitTestQuery ffi_query(HitTestQuery const& query)+Layout::RustFFI::FfiHitTestQuery HitTestQuery::ffi_query() const{return {- .device_pixels_per_css_pixel = query.device_pixels_per_css_pixel(),- .chrome_metrics = query.chrome_metrics(),- .has_visual_animation_sample_time = query.visual_animation_sample_time_nanoseconds().has_value(),- .visual_animation_sample_time_ns = query.visual_animation_sample_time_nanoseconds().value_or(0),+ .device_pixels_per_css_pixel = m_device_pixels_per_css_pixel,+ .chrome_metrics = m_chrome_metrics,+ .has_visual_animation_sample_time = m_visual_animation_sample_time_nanoseconds.has_value(),+ .visual_animation_sample_time_ns = m_visual_animation_sample_time_nanoseconds.value_or(0),};}@@ -65,7 +41,7 @@ static DOM::NodeIdentity image_map_area_for_point(Layout::Node const& layout_nodreturn DOM::NodeIdentity::of_style_node(CSS::StyleNodeID { area });}-HitTestResult HitTestDisplayList::hit_test_result_for(Layout::BegunRead const& read, Layout::RustFFI::FfiPointHit const& hit) const+HitTestResult HitTestQuery::hit_test_result_for(Layout::BegunRead const& read, Layout::RustFFI::FfiPointHit const& hit) const{CSSPixelPoint local_point = hit.local;auto const* paintable_layout_node = layout_node_for_committed_slot(read, *m_arena, hit.paintable);@@ -121,7 +97,7 @@ HitTestResult HitTestDisplayList::hit_test_result_for(Layout::BegunRead const& rreturn result;}-Optional<CaretPosition> HitTestDisplayList::caret_position_from(Layout::RustFFI::FfiCaretAt const& caret_at) const+Optional<CaretPosition> HitTestQuery::caret_position_from(Layout::RustFFI::FfiCaretAt const& caret_at) const{auto const& resolved = caret_at.caret;if (!resolved.has_position)@@ -176,27 +152,27 @@ Optional<CaretPosition> HitTestDisplayList::caret_position_from(Layout::RustFFI:};}-Optional<CaretPosition> HitTestDisplayList::caret_position_from_point(Layout::BegunRead const& read, CSSPixelPoint point, HitTestQuery const& query, GC::Ptr<DOM::Node const> constraint_scope) const+Optional<CaretPosition> HitTestQuery::caret_position_from_point(Layout::BegunRead const& read, CSSPixelPoint point, GC::Ptr<DOM::Node const> constraint_scope) const{auto const* scope_layout_node = constraint_scope ? constraint_scope->unsafe_layout_node(read) : nullptr;auto scope = scope_layout_node ? Layout::Node::slot_id(scope_layout_node) : Compositing::RustFFI::NodeSlotId { Compositing::RustFFI::INVALID_NODE_SLOT_INDEX };Layout::RustFFI::FfiCaretAt caret_at;- Layout::RustFFI::layout_hit_test_caret_position_from_point(m_arena->host(), &read, ffi_query(query), point, scope, &caret_at);+ Layout::RustFFI::layout_hit_test_caret_position_from_point(m_arena->host(), &read, ffi_query(), point, scope, &caret_at);return caret_position_from(caret_at);}-Optional<HitTestResult> HitTestDisplayList::hit_test(Layout::BegunRead const& read, CSSPixelPoint point, HitTestQuery const& query) const+Optional<HitTestResult> HitTestQuery::hit_test(Layout::BegunRead const& read, CSSPixelPoint point) const{Layout::RustFFI::FfiPointHit hit;- if (!Layout::RustFFI::layout_hit_test_topmost_hit(m_arena->host(), &read, ffi_query(query), point, &hit))+ if (!Layout::RustFFI::layout_hit_test_topmost_hit(m_arena->host(), &read, ffi_query(), point, &hit))return {};return hit_test_result_for(read, hit);}-TraversalDecision HitTestDisplayList::hit_test_all(Layout::BegunRead const& read, CSSPixelPoint point, HitTestQuery const& query, Function<TraversalDecision(HitTestResult)> const& callback) const+TraversalDecision HitTestQuery::hit_test_all(Layout::BegunRead const& read, CSSPixelPoint point, Function<TraversalDecision(HitTestResult)> const& callback) const{Vector<Layout::RustFFI::FfiPointHit> hits;- Layout::RustFFI::layout_hit_test_all_hits(m_arena->host(), &read, ffi_query(query), point, &hits, [](void* sink, Layout::RustFFI::FfiPointHit const* hit) {+ Layout::RustFFI::layout_hit_test_all_hits(m_arena->host(), &read, ffi_query(), point, &hits, [](void* sink, Layout::RustFFI::FfiPointHit const* hit) {static_cast<Vector<Layout::RustFFI::FfiPointHit>*>(sink)->append(*hit);});for (auto const& hit : hits) {diff --git a/Libraries/LibWeb/Painting/HitTestQuery.h b/Libraries/LibWeb/Painting/HitTestQuery.hnew file mode 100644index 00000000000..db8b1e6ca58--- /dev/null+++ b/Libraries/LibWeb/Painting/HitTestQuery.h@@ -0,0 +1,60 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/RefCounted.h>+#include <AK/Vector.h>+#include <LibCompositing/Forward.h>+#include <LibGC/Cell.h>+#include <LibGC/Ptr.h>+#include <LibWeb/Forward.h>+#include <LibWeb/Layout/LayoutRustFFI.h>+#include <LibWeb/Layout/NodeArena.h>+#include <LibWeb/Painting/ChromeMetrics.h>+#include <LibWeb/Painting/ChromeWidget.h>+#include <LibWeb/Painting/HitTestResult.h>+#include <LibWeb/Painting/Scrolling.h>++namespace Web::Painting {++// A hit test of a document's rows, with what it reads besides the rows and the document's scroll state, such as the+// time it samples the compositor's animations at. Only the document makes one, once it has prepared its paint+// properties, so every box a query looks at reads the same preparation, and nothing in a query prepares the document+// again.+class HitTestQuery {+public:+ [[nodiscard]] Optional<HitTestResult> hit_test(Layout::BegunRead const& read, CSSPixelPoint) const;+ // The caret position the box hit at the point gives it, or when constraint_scope is given, the one that node's box+ // gives it wherever the point is.+ [[nodiscard]] Optional<CaretPosition> caret_position_from_point(Layout::BegunRead const& read, CSSPixelPoint, GC::Ptr<DOM::Node const> constraint_scope = nullptr) const;+ TraversalDecision hit_test_all(Layout::BegunRead const& read, CSSPixelPoint, Function<TraversalDecision(HitTestResult)> const&) const;++private:+ friend class DOM::Document;++ HitTestQuery(Layout::NodeArena& arena, ChromeWidgetRegistry& chrome_widget_registry, double device_pixels_per_css_pixel, ChromeMetrics chrome_metrics, Optional<i64> visual_animation_sample_time_nanoseconds)+ : m_arena(arena)+ , m_chrome_widget_registry(chrome_widget_registry)+ , m_device_pixels_per_css_pixel(device_pixels_per_css_pixel)+ , m_chrome_metrics(chrome_metrics)+ , m_visual_animation_sample_time_nanoseconds(visual_animation_sample_time_nanoseconds)+ {+ }++ [[nodiscard]] Layout::RustFFI::FfiHitTestQuery ffi_query() const;+ [[nodiscard]] HitTestResult hit_test_result_for(Layout::BegunRead const& read, Layout::RustFFI::FfiPointHit const&) const;+ [[nodiscard]] Optional<CaretPosition> caret_position_from(Layout::RustFFI::FfiCaretAt const&) const;++ NonnullRefPtr<Layout::NodeArena> m_arena;+ NonnullRefPtr<ChromeWidgetRegistry> m_chrome_widget_registry;+ double m_device_pixels_per_css_pixel { 1 };+ ChromeMetrics m_chrome_metrics;+ // The time the query reads the compositor's animations at, as a frame of the compositor's shows them, if any.+ Optional<i64> m_visual_animation_sample_time_nanoseconds;+};++}diff --git a/Libraries/LibWeb/Rust/src/painting/ffi/main_thread_entries.rs b/Libraries/LibWeb/Rust/src/painting/ffi/main_thread_entries.rsindex 752c9bcc19a..9a4dc1eec58 100644--- a/Libraries/LibWeb/Rust/src/painting/ffi/main_thread_entries.rs+++ b/Libraries/LibWeb/Rust/src/painting/ffi/main_thread_entries.rs@@ -998,7 +998,7 @@ fn tree_as_the_query_reads_it<'tree>(}#[unsafe(no_mangle)]-pub extern "C" fn layout_hit_test_list_generation(host: &crate::render_state::DocumentHost) -> u64 {+pub extern "C" fn layout_hit_test_items_on_screen_generation(host: &crate::render_state::DocumentHost) -> u64 {host.recording().hit_test_items_generation()}