Skip to content

Truly independent web browser

LibWeb: Mark the scroll state stale where scroll offsets are stored

Aliaksandr Kalenik66aa91392ed0
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index b22b632d320..34b9320cc6d 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -10245,15 +10245,6 @@ GC::Ptr<HTML::HTMLElement> Document::topmost_auto_or_hint_popover()
return {};
}
-void Document::invalidate_scroll_state()
-{
- // NB: Propagating scroll state invalidation.
- // The scroll state is the render state's, and the invalidation a write queued for it: it needs no paint state,
- // which a task beside a frame in flight may not have yet, and waits for the frame like any other write.
- if (m_layout_node_arena)
- Layout::RustFFI::render_state_invalidate_scroll_state(m_layout_node_arena->host());
-}
-
Vector<GC::Root<Range>> Document::find_matching_text(Utf16View query, CaseSensitivity case_sensitivity)
{
Layout::ForcedReadScope read { *this };
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index 59a8d449145..f418b516ffd 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -1015,8 +1015,6 @@ public:
// root set; the layout node arena keeps the escape bit next to those roots.
void record_partial_relayout_escape(PartialRelayoutEscapeReason);
- void invalidate_scroll_state();
-
bool has_active_favicon() const { return !!m_active_favicon; }
void check_favicon_after_loading_link_resource();
diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cpp
index 87dba06b9ee..1f261a87231 100644
--- a/Libraries/LibWeb/DOM/Element.cpp
+++ b/Libraries/LibWeb/DOM/Element.cpp
@@ -6084,20 +6084,14 @@ void Element::set_last_relative_scroll_direction(u8 direction)
// That is why the unchecked layout node accessor is the right one here.
void Element::set_scroll_offset(Optional<CSS::PseudoElement> pseudo_element_type, CSSPixelPoint offset)
{
- // The document's scroll state mirrors these offsets, so it is invalidated here, next to the
- // store, rather than by each caller.
if (pseudo_element_type.has_value()) {
auto pseudo_element = get_synthetic_pseudo_element(*pseudo_element_type);
if (!pseudo_element.has_value())
return;
- if (pseudo_element->scroll_offset() != offset)
- document().invalidate_scroll_state();
pseudo_element->set_scroll_offset(offset);
return;
}
- if (scroll_offset({}) != offset)
- document().invalidate_scroll_state();
if (!offset.is_zero())
ensure_element_rare_data().scroll_offset = offset;
else if (auto* rare_data = element_rare_data())
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index d230c1f6f4d..575bdf7120c 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -4678,7 +4678,6 @@ void LocalNavigable::perform_scroll_of_viewport_scrolling_box(CSSPixelPoint new_
if (auto* arena = document->layout_node_arena_if_created())
Layout::RustFFI::render_state_set_viewport_scroll_offset(arena->host(), new_position);
document->set_needs_repaint(Badge<HTML::LocalNavigable> {}, InvalidateDisplayList::No);
- document->invalidate_scroll_state();
document->inform_all_viewport_clients_about_the_current_viewport_rect();
}
}
diff --git a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
index 6e70ef768d3..aced2ffb58b 100644
--- a/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
+++ b/Libraries/LibWeb/Rust/src/layout/layout_node_arena.rs
@@ -2217,13 +2217,16 @@ impl LayoutNodeArena {
/// an old row and its replacement both built for the element until the commit that retires the
/// old one, and both answer for the element meanwhile.
pub(crate) fn set_element_scroll_offset(&self, element: StyleNodeID, offset: CssPixelPoint) {
- {
+ let previous_offset = {
let mut offsets = self.style_node_tables.element_scroll_offsets.borrow_mut();
if offset == CssPixelPoint::default() {
- offsets.remove(&element);
+ offsets.remove(&element)
} else {
- offsets.insert(element, offset);
+ offsets.insert(element, offset)
}
+ };
+ if previous_offset.unwrap_or_default() != offset {
+ self.note_scroll_offsets_moved();
}
let bound_row = self.bound_row(element);
if !bound_row.is_invalid() {
@@ -2240,17 +2243,23 @@ impl LayoutNodeArena {
generated_for: u8,
offset: CssPixelPoint,
) {
- {
+ let previous_offset = {
let index = pseudo_element_scroll_offset_index(generated_for);
let mut offsets = self.style_node_tables.pseudo_element_scroll_offsets.borrow_mut();
if offset != CssPixelPoint::default() {
- offsets.entry(generator).or_default()[index] = offset;
+ std::mem::replace(&mut offsets.entry(generator).or_default()[index], offset)
} else if let Some(element_offsets) = offsets.get_mut(&generator) {
- element_offsets[index] = offset;
+ let previous_offset = std::mem::replace(&mut element_offsets[index], offset);
if element_offsets.iter().all(|&offset| offset == CssPixelPoint::default()) {
offsets.remove(&generator);
}
+ previous_offset
+ } else {
+ CssPixelPoint::default()
}
+ };
+ if previous_offset != offset {
+ self.note_scroll_offsets_moved();
}
let bound_row = self.bound_pseudo_element_row(generator, generated_for);
if !bound_row.is_invalid() {
@@ -2260,7 +2269,18 @@ impl LayoutNodeArena {
/// Records what the navigable has scrolled the viewport to.
pub(crate) fn set_viewport_scroll_offset(&self, offset: CssPixelPoint) {
- self.viewport_scroll_offset.set(offset);
+ if self.viewport_scroll_offset.replace(offset) != offset {
+ self.note_scroll_offsets_moved();
+ }
+ }
+
+ /// The scroll state reads the offsets the rows hold, so it is read again before it is published next whenever one
+ /// of them moves.
+ fn note_scroll_offsets_moved(&self) {
+ self.paint_state()
+ .borrow_mut()
+ .visual_context
+ .needs_to_refresh_scroll_state = true;
}
/// Record whether the node sits in the user agent shadow tree of the focused text control.
diff --git a/Libraries/LibWeb/Rust/src/painting/paint_changes.rs b/Libraries/LibWeb/Rust/src/painting/paint_changes.rs
index f43f4b20019..1db6129aca7 100644
--- a/Libraries/LibWeb/Rust/src/painting/paint_changes.rs
+++ b/Libraries/LibWeb/Rust/src/painting/paint_changes.rs
@@ -32,8 +32,6 @@ use std::sync::Arc;
pub(crate) enum PaintChange {
/// What the navigable has scrolled the viewport to.
SetViewportScrollOffset(CssPixelPoint),
- /// The scroll containers' offsets moved: the scroll state is read again before it is published next.
- InvalidateScrollState,
/// The selection covers `entries`, between the offsets in its start and end text.
ApplySelection {
viewport: NodeSlotId,
@@ -127,13 +125,6 @@ impl PaintChange {
pub(crate) fn apply(self, arena: &mut LayoutNodeArena) {
match self {
Self::SetViewportScrollOffset(offset) => arena.set_viewport_scroll_offset(offset),
- Self::InvalidateScrollState => {
- arena
- .paint_state()
- .borrow_mut()
- .visual_context
- .needs_to_refresh_scroll_state = true;
- }
Self::ApplySelection {
viewport,
entries,
@@ -228,11 +219,6 @@ pub extern "C" fn render_state_set_viewport_scroll_offset(host: &DocumentHost, o
queue(host, PaintChange::SetViewportScrollOffset(offset.into()));
}
-#[unsafe(no_mangle)]
-pub extern "C" fn render_state_invalidate_scroll_state(host: &DocumentHost) {
- queue(host, PaintChange::InvalidateScrollState);
-}
-
/// # Safety
///
/// `host` must be a live document host, on the document's thread, and `entries` must point at `entry_count` readable