Skip to content

Truly independent web browser

LibWeb: Record only the paint that reads window focus again

Aliaksandr Kalenik9f378753d9e7
diff --git a/Libraries/LibWeb/Painting/BoxViews.cpp b/Libraries/LibWeb/Painting/BoxViews.cpp
index 4fcbf5cc38f..30f82d24995 100644
--- a/Libraries/LibWeb/Painting/BoxViews.cpp
+++ b/Libraries/LibWeb/Painting/BoxViews.cpp
@@ -536,11 +536,8 @@ Layout::RustFFI::FfiCaretPaint resolve_document_caret_paint(Layout::BegunRead co
auto cursor_position = document.cursor_position();
if (!cursor_position)
return caret;
- // The caret paints only while the window has focus and the cursor node is editable (or a mutable text
- // control has focus); every candidate box below has a committed box.
- auto navigable = document.navigable();
- if (!navigable || !navigable->is_focused())
- return caret;
+ // The caret paints only while the cursor node is editable (or a mutable text control has focus), and the recorder
+ // paints it only while the window has focus; every candidate box below has a committed box.
auto const* cursor_node = cursor_position->node().ptr();
if (!cursor_node)
return caret;
diff --git a/Libraries/LibWeb/Painting/DocumentPaintState.cpp b/Libraries/LibWeb/Painting/DocumentPaintState.cpp
index 7323f9ec812..90fae02e476 100644
--- a/Libraries/LibWeb/Painting/DocumentPaintState.cpp
+++ b/Libraries/LibWeb/Painting/DocumentPaintState.cpp
@@ -134,13 +134,6 @@ void DocumentPaintState::append_paint_command_cache_source_resources(Compositing
retained_resources.include(m_paint_command_cache_source_referenced_resources);
}
-void DocumentPaintState::invalidate_all_cached_paint(DOM::Document& document)
-{
- Layout::ForcedReadScope read { document };
- Layout::RustFFI::render_state_invalidate_all_paint_caches(m_layout_node_arena->host());
- Painting::set_needs_repaint(*document.unsafe_layout_node(read));
-}
-
void DocumentPaintState::refresh_scroll_state(Layout::BegunRead const& read, DOM::Document& document)
{
// The render state hands over a new snapshot only where something invalidated the one kept.
diff --git a/Libraries/LibWeb/Painting/DocumentPaintState.h b/Libraries/LibWeb/Painting/DocumentPaintState.h
index a4f4be3bd6f..14ab1cf94f2 100644
--- a/Libraries/LibWeb/Painting/DocumentPaintState.h
+++ b/Libraries/LibWeb/Painting/DocumentPaintState.h
@@ -64,8 +64,6 @@ public:
void recompute_spelling_error_states(Layout::BegunRead const& read, DOM::Document&, Editing::SpellingMarkers&);
void reset_spelling_error_states();
- void invalidate_all_cached_paint(DOM::Document&);
-
Compositing::ScrollStateSnapshot const& scroll_state_snapshot() const { return m_scroll_state_snapshot; }
void set_boxes_with_auto_content_visibility(Vector<Compositing::RustFFI::NodeSlotId> boxes) { m_boxes_with_auto_content_visibility = move(boxes); }
diff --git a/Libraries/LibWeb/Rust/src/painting/paint_changes.rs b/Libraries/LibWeb/Rust/src/painting/paint_changes.rs
index 11a4a560ca3..d2bf4ead6ef 100644
--- a/Libraries/LibWeb/Rust/src/painting/paint_changes.rs
+++ b/Libraries/LibWeb/Rust/src/painting/paint_changes.rs
@@ -99,8 +99,6 @@ pub(crate) enum PaintChange {
node: NodeSlotId,
kind: VisualContextBoxDirtyKind,
},
- /// Every row paints again.
- InvalidateAllPaintCaches,
/// The visual context tree is built again whole before it is published next.
RequestFullVisualContextRebuild(super::visual_context::dirty::VisualContextUpdateScope),
/// The SVG paint resources the rows enrolled may have changed: they are synced again before they are painted next.
@@ -217,7 +215,6 @@ impl PaintChange {
arena.note_visual_context_box_dirty(node, kind);
}
}
- Self::InvalidateAllPaintCaches => arena.push_all_paint_damage(),
Self::RequestFullVisualContextRebuild(scope) => arena.request_full_visual_context_rebuild(scope),
Self::SvgPaintResourcesChanged => {
arena.svg_paint_resources_mut().note_changed();
@@ -536,11 +533,6 @@ pub extern "C" fn render_state_note_visual_context_style_change(
queue(host, PaintChange::NoteVisualContextBoxDirty { node, kind });
}
-#[unsafe(no_mangle)]
-pub extern "C" fn render_state_invalidate_all_paint_caches(host: &DocumentHost) {
- queue(host, PaintChange::InvalidateAllPaintCaches);
-}
-
#[unsafe(no_mangle)]
pub extern "C" fn render_state_visual_context_request_full_rebuild(
host: &DocumentHost,
diff --git a/Libraries/LibWeb/Rust/src/painting/record/assemble.rs b/Libraries/LibWeb/Rust/src/painting/record/assemble.rs
index c068b71c1e9..df24a1f2736 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/assemble.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/assemble.rs
@@ -54,6 +54,8 @@ fn producer_kinds(producer: PaintProducer) -> SmallVec<[ProducerKind; 5]> {
pub(crate) struct ProducerOutcome {
// Recorded again every frame; the entry and every scope above it stay live.
pub live: bool,
+ // Recorded again when the window gains or loses focus.
+ pub reads_window_focus: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -130,9 +132,10 @@ struct PendingCopy {
}
/// Whether the published recording can be returned as it is: nothing was pushed, nothing in it is
-/// recorded every frame, and there is a published tree at all.
-pub(crate) fn frame_is_unchanged(tree: &PaintOrderTree, has_damage: bool) -> bool {
- !tree.root().is_none() && !has_damage && !tree.root_entry().is_live()
+/// recorded every frame or read a window focus that changed, and there is a published tree at all.
+pub(crate) fn frame_is_unchanged(tree: &PaintOrderTree, has_damage: bool, window_focus_changed: bool) -> bool {
+ let root = tree.root_entry();
+ !tree.root().is_none() && !has_damage && !root.is_live() && !(window_focus_changed && root.reads_window_focus())
}
pub(crate) struct Assembler<'a, H: AssemblyHost> {
@@ -143,6 +146,8 @@ pub(crate) struct Assembler<'a, H: AssemblyHost> {
// Off for a frame that publishes nothing: it still marks damaged paths, but the tree's
// structure keeps describing the published recording.
rewrites_tree: bool,
+ // The published recording was made in the other window focus state.
+ window_focus_changed: bool,
pending_copy: Option<PendingCopy>,
}
@@ -152,16 +157,24 @@ impl<'a, H: AssemblyHost> Assembler<'a, H> {
tree: &'a mut PaintOrderTree,
source_prologue_bytes: Option<u32>,
rewrites_tree: bool,
+ window_focus_changed: bool,
) -> Self {
Self {
host,
tree,
source_prologue_bytes,
rewrites_tree,
+ window_focus_changed,
pending_copy: None,
}
}
+ // Copying an entry would keep output that is recorded every frame, or that read the window
+ // focus before it changed.
+ fn records_again(&self, entry: &ChildEntry) -> bool {
+ entry.is_live() || (self.window_focus_changed && entry.reads_window_focus())
+ }
+
/// Records the frame's root scope after the prologue and returns its output. A frame that
/// rewrites the tree leaves the assembled structure there as pending changes for the caller
/// to publish or discard.
@@ -227,7 +240,7 @@ impl<'a, H: AssemblyHost> Assembler<'a, H> {
let published_output = entry.output();
let assembled = match entry.key() {
ChildKey::Scope(child) => {
- if self.tree.is_on_damaged_path(child) || entry.is_live() {
+ if self.tree.is_on_damaged_path(child) || self.records_again(&entry) {
self.assemble_scope(child, child_cursor)
} else {
self.copy(child_cursor, published_output);
@@ -265,7 +278,7 @@ impl<'a, H: AssemblyHost> Assembler<'a, H> {
entry: &ChildEntry,
) -> bool {
damage.intersects(kind.damage())
- || entry.is_live()
+ || self.records_again(entry)
|| (damage.contains(PaintDamage::DESCENDANT_READERS) && self.host.producer_reads_descendants(owner, kind))
}
@@ -335,7 +348,7 @@ impl<'a, H: AssemblyHost> Assembler<'a, H> {
let assembled = match found {
Some((child, index)) => {
matched[index] = true;
- if self.tree.is_on_damaged_path(child) || published[index].is_live() {
+ if self.tree.is_on_damaged_path(child) || self.records_again(&published[index]) {
self.assemble_scope(child, published_cursors[index])
} else {
self.copy(published_cursors[index], published[index].output());
@@ -452,6 +465,7 @@ impl<'a, H: AssemblyHost> Assembler<'a, H> {
hits: after.hits - before.hits,
blocking_wheel_event_regions: after.blocking_wheel_event_regions - before.blocking_wheel_event_regions,
live: outcome.live,
+ reads_window_focus: outcome.reads_window_focus,
};
ChildEntry::producer(kind, output)
}
@@ -642,7 +656,7 @@ mod tests {
fn record_producer(&mut self, owner: NodeSlotId, kind: ProducerKind) -> ProducerOutcome {
let Some(content) = self.contents.get(&(owner, kind)).copied() else {
- return ProducerOutcome { live: false };
+ return ProducerOutcome::default();
};
self.recorded.push((owner, kind));
let output = &mut self.output;
@@ -685,7 +699,10 @@ mod tests {
});
}
output.blocking_wheel_event_region_count += content.blocking;
- ProducerOutcome { live: content.live }
+ ProducerOutcome {
+ live: content.live,
+ ..Default::default()
+ }
}
fn output_position(&self) -> OutputPosition {
@@ -753,7 +770,7 @@ mod tests {
let prologue_bytes = host.output.recorder.byte_size() as u32;
let source_prologue_bytes = host.source.as_ref().map(|frame| frame.prologue_bytes);
let root_output =
- Assembler::new(host, tree, source_prologue_bytes, rewrites_tree).assemble_root(context(row(0)));
+ Assembler::new(host, tree, source_prologue_bytes, rewrites_tree, false).assemble_root(context(row(0)));
let output = std::mem::replace(&mut host.output, new_output());
let hit_test_items = std::sync::Arc::try_unwrap(output.hit_test_list.items).expect("items are unshared");
Frame {
@@ -841,7 +858,7 @@ mod tests {
fn a_quiet_frame_is_unchanged_and_a_fresh_frame_records_everything() {
let mut host = cards_host();
let mut tree = PaintOrderTree::default();
- assert!(!frame_is_unchanged(&tree, false));
+ assert!(!frame_is_unchanged(&tree, false, false));
let frame = record_frame(&mut host, &mut tree);
@@ -852,8 +869,8 @@ mod tests {
frame.root_output.bytes as usize,
frame.display_list.bytes.len() - frame.prologue_bytes as usize
);
- assert!(frame_is_unchanged(&tree, false));
- assert!(!frame_is_unchanged(&tree, true));
+ assert!(frame_is_unchanged(&tree, false, false));
+ assert!(!frame_is_unchanged(&tree, true, false));
}
#[test]
@@ -1187,7 +1204,7 @@ mod tests {
let first = record_frame(&mut host, &mut tree);
host.source = Some(first);
host.clear_damage();
- assert!(!frame_is_unchanged(&tree, false));
+ assert!(!frame_is_unchanged(&tree, false, false));
let second = record_next_frame(&mut host, &mut tree);
diff --git a/Libraries/LibWeb/Rust/src/painting/record/mod.rs b/Libraries/LibWeb/Rust/src/painting/record/mod.rs
index 505c45ed015..e229fef75db 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/mod.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/mod.rs
@@ -59,6 +59,8 @@ pub struct RecordingOutput {
pub has_blocking_wheel_event_listeners: bool,
pub wheel_event_listener_state_generation: u64,
pub is_identical_to_published_recording: bool,
+ // Whether the window was focused, which the output of the producers that read it reflects.
+ pub(crate) window_is_focused: bool,
pub(crate) capture_log: Option<trace::CaptureLog>,
/// The SVG images whose display lists, which the host rendered, the recording draws, and the ids of those lists.
pub(crate) vector_image_display_lists: Vec<(vector_images::VectorImageRenderRequest, u64)>,
@@ -114,6 +116,8 @@ pub struct PaintRecorder<'a, O: Observer> {
pub(crate) source_items: Option<Arc<PublishedHitTestItems>>,
// Set while a producer records output that must record again every frame.
live_producer: bool,
+ // Set while a producer records output that depends on whether the window is focused.
+ producer_reads_window_focus: std::cell::Cell<bool>,
// Rows of the layout subtrees whose root moved: every producer of theirs records again.
pub(crate) moved_expansion: FastSet<NodeSlotId>,
pub(crate) blocking_wheel_event_region_count: u32,
@@ -143,6 +147,12 @@ impl<O: Observer> PaintRecorder<'_, O> {
self.live_producer = true;
}
+ /// Whether the window is focused. The producer that asks records again when that changes.
+ pub(crate) fn window_is_focused(&self) -> bool {
+ self.producer_reads_window_focus.set(true);
+ self.inputs.window_is_focused
+ }
+
pub(crate) fn line_root_positions(
&mut self,
root: NodeSlotId,
@@ -297,6 +307,8 @@ impl<O: Observer> PaintRecorder<'_, O> {
&mut self,
node: crate::layout::node_data::NodeSlotId,
) -> Arc<paint::text::SelectionStyleAnswer> {
+ // Read before the cache, so that each producer asking notes that it depends on it.
+ let window_is_focused = self.window_is_focused();
let key = node.index;
if let Some(answer) = self.scratch.selection_style_cache.get(&key) {
return answer.clone();
@@ -305,7 +317,9 @@ impl<O: Observer> PaintRecorder<'_, O> {
let committed = self.first_non_anonymous_ancestor_row(node).and_then(|element_row| {
self.committed_highlight_pseudo_style(HighlightPseudoElement::Selection, node, element_row)
});
- let answer = Self::highlight_style_answer(committed, || self.default_selection_style(node, style_source));
+ let answer = Self::highlight_style_answer(committed, || {
+ self.default_selection_style(node, style_source, window_is_focused)
+ });
self.scratch.selection_style_cache.insert(key, answer.clone());
answer
}
@@ -314,13 +328,16 @@ impl<O: Observer> PaintRecorder<'_, O> {
&mut self,
element_row: crate::layout::node_data::NodeSlotId,
) -> Arc<paint::text::SelectionStyleAnswer> {
+ let window_is_focused = self.window_is_focused();
let key = element_row.index;
if let Some(answer) = self.scratch.selection_style_cache.get(&key) {
return answer.clone();
}
let committed =
self.committed_highlight_pseudo_style(HighlightPseudoElement::Selection, element_row, element_row);
- let answer = Self::highlight_style_answer(committed, || self.default_selection_style(element_row, element_row));
+ let answer = Self::highlight_style_answer(committed, || {
+ self.default_selection_style(element_row, element_row, window_is_focused)
+ });
self.scratch.selection_style_cache.insert(key, answer.clone());
answer
}
@@ -441,8 +458,9 @@ impl<O: Observer> PaintRecorder<'_, O> {
&self,
node: crate::layout::node_data::NodeSlotId,
style_source: crate::layout::node_data::NodeSlotId,
+ window_is_focused: bool,
) -> paint::text::SelectionStyleAnswer {
- let background_color = self.default_selection_background(node, style_source, self.inputs.window_is_focused);
+ let background_color = self.default_selection_background(node, style_source, window_is_focused);
paint::text::SelectionStyleAnswer {
facts: crate::painting::host::FfiSelectionStyleFacts {
background_color,
diff --git a/Libraries/LibWeb/Rust/src/painting/record/order_tree.rs b/Libraries/LibWeb/Rust/src/painting/record/order_tree.rs
index 3bf4f8329a8..d97c14c4cb2 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/order_tree.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/order_tree.rs
@@ -155,6 +155,9 @@ pub(crate) struct OutputSize {
// Recorded again every frame: scroll-offset-dependent content and resources without
// retained output. A scope is live when any child is.
pub live: bool,
+ // Recorded again when the window gains or loses focus. A scope reads the window focus when
+ // any child does.
+ pub reads_window_focus: bool,
}
impl OutputSize {
@@ -163,6 +166,7 @@ impl OutputSize {
self.hits += other.hits;
self.blocking_wheel_event_regions += other.blocking_wheel_event_regions;
self.live |= other.live;
+ self.reads_window_focus |= other.reads_window_focus;
}
}
@@ -185,6 +189,7 @@ const _: () = assert!(std::mem::size_of::<ChildEntry>() == 16);
const SCOPE_KEY_BIT: u32 = 1 << 31;
const META_LIVE: u32 = 1;
+const META_READS_WINDOW_FOCUS: u32 = 2;
const META_BLOCKING_REGIONS_SHIFT: u32 = 8;
impl ChildEntry {
@@ -199,7 +204,12 @@ impl ChildEntry {
fn with_key(key: u32, output: OutputSize) -> Self {
let meta = (output.blocking_wheel_event_regions << META_BLOCKING_REGIONS_SHIFT)
- | if output.live { META_LIVE } else { 0 };
+ | if output.live { META_LIVE } else { 0 }
+ | if output.reads_window_focus {
+ META_READS_WINDOW_FOCUS
+ } else {
+ 0
+ };
Self {
key,
bytes: output.bytes,
@@ -222,12 +232,17 @@ impl ChildEntry {
hits: self.hits,
blocking_wheel_event_regions: self.meta >> META_BLOCKING_REGIONS_SHIFT,
live: self.meta & META_LIVE != 0,
+ reads_window_focus: self.reads_window_focus(),
}
}
pub(crate) fn is_live(&self) -> bool {
self.meta & META_LIVE != 0
}
+
+ pub(crate) fn reads_window_focus(&self) -> bool {
+ self.meta & META_READS_WINDOW_FOCUS != 0
+ }
}
impl Default for ChildEntry {
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/inline_box.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/inline_box.rs
index 70c4389df3b..167b6ee63c9 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/inline_box.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/inline_box.rs
@@ -175,6 +175,7 @@ fn paint_pieces<O: Observer>(
if facts.is_visible
&& let Some(caret) = recorder.inputs.caret
&& caret.target == crate::painting::record::inputs::CaretTarget::EmptyInline(paintable)
+ && recorder.window_is_focused()
{
let color = caret.color;
if color.alpha() != 0 {
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/outline.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/outline.rs
index 8b00a136596..93e304ca764 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/outline.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/outline.rs
@@ -43,7 +43,7 @@ pub(crate) fn outline_data_for_paint<O: Observer>(
crate::painting::style_queries::outline_data(
recorder.source,
node,
- recorder.inputs.window_is_focused,
+ || recorder.window_is_focused(),
recorder.inputs.outline_auto_color.0,
)
}
@@ -142,13 +142,10 @@ fn paint_focused_area_outline<O: Observer>(recorder: &mut PaintRecorder<'_, O>,
// focusable area paints the focus outline along that shape.
// AD-HOC: Only the user agent focus ring is painted, as an `outline: auto` would paint it. Other engines do not let
// author outline values style the focus indicator of an image map area.
- if !recorder.inputs.window_is_focused {
- return;
- }
let Some(outline) = &recorder.inputs.focused_area_outline else {
return;
};
- if outline.image != paintable {
+ if outline.image != paintable || !recorder.window_is_focused() {
return;
}
let image_rect = paintable_geometry::absolute_rect(recorder.source, paintable);
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/text.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/text.rs
index 7343fa7abaa..42e775e13c7 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/text.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/text.rs
@@ -745,7 +745,7 @@ pub(crate) fn paint_cursor<O: Observer>(
let Some(caret) = recorder.inputs.caret else {
return;
};
- if caret.target != (CaretTarget::InBlock { block, owner }) {
+ if caret.target != (CaretTarget::InBlock { block, owner }) || !recorder.window_is_focused() {
return;
}
let color = caret.color;
diff --git a/Libraries/LibWeb/Rust/src/painting/record/producers.rs b/Libraries/LibWeb/Rust/src/painting/record/producers.rs
index cac02e0fd9d..2ce36af7ca6 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/producers.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/producers.rs
@@ -182,9 +182,10 @@ impl<O: Observer> AssemblyHost for PaintRecorder<'_, O> {
if self.producer_is_skipped(owner, kind) {
self.observer
.observe(|log| log.leaf(Operation::Producer(owner, kind), Action::Skip, true));
- return ProducerOutcome { live: false };
+ return ProducerOutcome::default();
}
self.live_producer = false;
+ self.producer_reads_window_focus.set(false);
let start = self.output_position();
self.observer
.observe(|log| log.begin(Operation::Producer(owner, kind), Action::Record));
@@ -224,6 +225,7 @@ impl<O: Observer> AssemblyHost for PaintRecorder<'_, O> {
);
ProducerOutcome {
live: self.live_producer,
+ reads_window_focus: self.producer_reads_window_focus.get(),
}
}
diff --git a/Libraries/LibWeb/Rust/src/painting/record/traversal.rs b/Libraries/LibWeb/Rust/src/painting/record/traversal.rs
index f222f7280e4..5e0e8946642 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/traversal.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/traversal.rs
@@ -94,6 +94,9 @@ fn record_display_list_impl<O: Observer>(
} else {
(None, None)
};
+ let window_focus_changed = source_recording
+ .as_ref()
+ .is_some_and(|frame| frame.window_is_focused != inputs.window_is_focused);
let force_dark_settings = inputs.force_dark_enabled.then_some(inputs.force_dark_settings);
let mut recorder = PaintRecorder {
source,
@@ -106,6 +109,7 @@ fn record_display_list_impl<O: Observer>(
source_recording,
source_items,
live_producer: false,
+ producer_reads_window_focus: std::cell::Cell::new(false),
line_root_positions: None,
moved_expansion: FastSet::default(),
blocking_wheel_event_region_count: 0,
@@ -135,7 +139,7 @@ fn record_display_list_impl<O: Observer>(
.as_ref()
.zip(recorder.source_items.as_ref())
.filter(|(frame, _)| {
- frame_is_unchanged(tree, !source.frame().damage().is_empty())
+ frame_is_unchanged(tree, !source.frame().damage().is_empty(), window_focus_changed)
&& !has_inspector_overlays
&& frame.prologue_bytes == prologue_bytes
&& recorder.recorder.bytes() == &frame.display_list.bytes[..prologue_bytes as usize]
@@ -152,8 +156,14 @@ fn record_display_list_impl<O: Observer>(
}
None => {
let source_prologue_bytes = recorder.source_recording.as_ref().map(|frame| frame.prologue_bytes);
- Assembler::new(&mut recorder, tree, source_prologue_bytes, inputs.publishes_recording)
- .assemble_root(root_scope);
+ Assembler::new(
+ &mut recorder,
+ tree,
+ source_prologue_bytes,
+ inputs.publishes_recording,
+ window_focus_changed,
+ )
+ .assemble_root(root_scope);
if has_inspector_overlays {
recorder.trace_paint(
Operation::Named(None, "inspector-overlays"),
@@ -175,6 +185,7 @@ fn record_display_list_impl<O: Observer>(
has_blocking_wheel_event_listeners: recorder.blocking_wheel_event_region_count > 0,
wheel_event_listener_state_generation: inputs.wheel_event_listener_state_generation,
is_identical_to_published_recording: false,
+ window_is_focused: inputs.window_is_focused,
capture_log: recorder.observer.finish(),
vector_image_display_lists: Vec::new(),
};
diff --git a/Libraries/LibWeb/Rust/src/painting/style_queries.rs b/Libraries/LibWeb/Rust/src/painting/style_queries.rs
index 9ae427a1808..d9519925e64 100644
--- a/Libraries/LibWeb/Rust/src/painting/style_queries.rs
+++ b/Libraries/LibWeb/Rust/src/painting/style_queries.rs
@@ -526,12 +526,12 @@ pub(crate) fn auto_outline_data(auto_outline_color: u32) -> OutlineData {
pub(crate) fn outline_data(
arena: &impl PaintRead,
node: NodeSlotId,
- window_is_focused: bool,
+ window_is_focused: impl FnOnce() -> bool,
auto_outline_color: u32,
) -> Option<OutlineData> {
let style = arena.node_style_if_live(node)?;
let is_auto = style.misc_reset().outline_style == outline_style::AUTO;
- if is_auto && !window_is_focused {
+ if is_auto && !window_is_focused() {
return None;
}
let geometry = outline_geometry(style)?;
diff --git a/Services/WebContent/PageClient.cpp b/Services/WebContent/PageClient.cpp
index 5d011965a0a..84227b89ecc 100644
--- a/Services/WebContent/PageClient.cpp
+++ b/Services/WebContent/PageClient.cpp
@@ -61,7 +61,6 @@
#include <LibWeb/InvalidateDisplayList.h>
#include <LibWeb/Layout/Viewport.h>
#include <LibWeb/Painting/BoxViews.h>
-#include <LibWeb/Painting/DocumentPaintState.h>
#include <LibWeb/Platform/EventLoopPlugin.h>
#include <LibWeb/Streams/ReadableStreamDefaultReader.h>
#include <LibWeb/WebDriver/BiDi/BiDi.h>
@@ -211,8 +210,8 @@ void PageClient::set_has_focus(bool has_focus)
page().invalidate_compositor_keyboard_scroll_state();
m_has_focus = has_focus;
- // The focus ring, the text caret, and selection highlight colors all depend on the window focus state, so
- // nothing painted before the change can be reused; repaint every document the page hosts.
+ // The focus ring, the text caret, and selection highlight colors all depend on the window focus state, so every
+ // document the page hosts records again, which records the output that read the focus state again.
for (auto const& navigable : page().hosted_navigables()) {
if (!has_focus)
navigable->event_handler().release_held_scroll_key();
@@ -224,10 +223,7 @@ void PageClient::set_has_focus(bool has_focus)
// A writing suggestion is for the user typing in the window, so leaving the window drops it.
if (auto* controller = document->writing_suggestion_controller_if_exists(); controller && !has_focus)
controller->dismiss();
- // Focus changes can arrive while layout is invalidated. We only need to invalidate cached paint
- // on the current paintable tree here, without requiring layout to be up to date first.
- if (document->has_committed_viewport_box())
- document->paint_state().invalidate_all_cached_paint(*document);
+ Web::Painting::request_document_repaint(*document, Web::InvalidateDisplayList::PaintCommands);
}
}
diff --git a/Tests/LibWeb/Text/expected/display_list/window-focus-change-records-caret.txt b/Tests/LibWeb/Text/expected/display_list/window-focus-change-records-caret.txt
new file mode 100644
index 00000000000..6b47f83a23b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/display_list/window-focus-change-records-caret.txt
@@ -0,0 +1,3 @@
+PASS: quiet before focus changes (work)
+PASS: window loses focus (work)
+PASS: window regains focus (work)
diff --git a/Tests/LibWeb/Text/expected/display_list/window-focus-change-records-focus-dependent-output.txt b/Tests/LibWeb/Text/expected/display_list/window-focus-change-records-focus-dependent-output.txt
new file mode 100644
index 00000000000..051b8eda62d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/display_list/window-focus-change-records-focus-dependent-output.txt
@@ -0,0 +1,4 @@
+PASS: quiet before focus changes (work)
+PASS: window loses focus (work)
+PASS: quiet while unfocused (work)
+PASS: window regains focus (work)
diff --git a/Tests/LibWeb/Text/input/display_list/window-focus-change-records-caret.html b/Tests/LibWeb/Text/input/display_list/window-focus-change-records-caret.html
new file mode 100644
index 00000000000..9ec47892e3e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/display_list/window-focus-change-records-caret.html
@@ -0,0 +1,81 @@
+<!doctype html>
+<html id="html">
+<script src="../include.js"></script>
+<script src="recording-test.js"></script>
+<style>
+ body { margin: 0; font: 16px/20px monospace; }
+ #editor, #plain { width: 200px; height: 20px; }
+ #editor { outline: none; }
+</style>
+<body id="body">
+<div id="editor" contenteditable>Editable</div><div id="plain">Plain</div>
+<script>
+// The caret paints only while the window is focused, so a focus change records the block that paints it
+// again and copies everything else.
+const focusChangeTrace = `
+ damage: rows=0 moved=0 order=0 eligibility=0 all=false
+ @canvas RECORD (empty)
+ @viewport ASSEMBLE
+ @viewport/preamble COPY
+ @viewport/background/hit-test COPY
+ @viewport/scroll-metadata COPY
+ @viewport/background COPY
+ @viewport/border COPY
+ BlockContainer<HTML>#html/descendants(background-and-borders) COPY
+ @viewport/foreground/hit-test COPY
+ @viewport/foreground COPY
+ BlockContainer<HTML>#html/descendants(foreground) COPY
+ BlockContainer<HTML>#html ASSEMBLE
+ BlockContainer<HTML>#html/preamble COPY
+ BlockContainer<HTML>#html/background/hit-test COPY
+ BlockContainer<HTML>#html/scroll-metadata COPY
+ BlockContainer<HTML>#html/background COPY
+ BlockContainer<HTML>#html/border COPY
+ BlockContainer<BODY>#body/descendants(background-and-borders) COPY
+ BlockContainer<HTML>#html/foreground/hit-test COPY
+ BlockContainer<HTML>#html/foreground COPY
+ BlockContainer<BODY>#body/descendants(foreground) ASSEMBLE
+ BlockContainer<BODY>#body/foreground/hit-test COPY
+ BlockContainer<BODY>#body/foreground COPY
+ BlockContainer<DIV>#editor/descendants(foreground) ASSEMBLE
+ BlockContainer<DIV>#editor/foreground/hit-test COPY
+ BlockContainer<DIV>#editor/foreground RECORD
+ BlockContainer<DIV>#editor/outline COPY
+ BlockContainer<DIV>#editor/overlay/hit-test COPY
+ BlockContainer<DIV>#editor/overlay COPY
+ BlockContainer<DIV>#plain/descendants(foreground) COPY
+ BlockContainer<BODY>#body/outline COPY
+ BlockContainer<BODY>#body/overlay/hit-test COPY
+ BlockContainer<BODY>#body/overlay COPY
+ BlockContainer<HTML>#html/outline COPY
+ @viewport/outline COPY
+`;
+displayListTest({
+ setup: () => {
+ removeBodyWhitespace();
+ editor.focus();
+ getSelection().collapse(editor.firstChild, 3);
+ },
+ steps: [
+ {
+ name: "quiet before focus changes",
+ expect: `
+ damage: rows=0 moved=0 order=0 eligibility=0 all=false
+ @canvas RECORD (empty)
+ @viewport COPY
+ `,
+ },
+ {
+ name: "window loses focus",
+ mutate: () => internals.setPageFocus(false),
+ expect: focusChangeTrace,
+ },
+ {
+ name: "window regains focus",
+ mutate: () => internals.setPageFocus(true),
+ expect: focusChangeTrace,
+ },
+ ],
+ cleanup: () => internals.setPageFocus(true),
+});
+</script>
diff --git a/Tests/LibWeb/Text/input/display_list/window-focus-change-records-focus-dependent-output.html b/Tests/LibWeb/Text/input/display_list/window-focus-change-records-focus-dependent-output.html
new file mode 100644
index 00000000000..4b1b19be7ac
--- /dev/null
+++ b/Tests/LibWeb/Text/input/display_list/window-focus-change-records-focus-dependent-output.html
@@ -0,0 +1,94 @@
+<!doctype html>
+<html id="html">
+<script src="../include.js"></script>
+<script src="recording-test.js"></script>
+<style>
+ body { margin: 0; font: 16px/20px monospace; }
+ #ring { width: 100px; height: 20px; outline: auto; }
+ #selected, #plain { width: 200px; height: 20px; }
+</style>
+<body id="body">
+<div id="ring"></div><div id="selected">Selected</div><div id="plain">Plain</div>
+<script>
+// Only an outline: auto ring and the selection paint differently while the window is unfocused, so a
+// focus change records them again and copies everything else.
+const focusChangeTrace = ringOutline => `
+ damage: rows=0 moved=0 order=0 eligibility=0 all=false
+ @canvas RECORD (empty)
+ @viewport ASSEMBLE
+ @viewport/preamble COPY
+ @viewport/background/hit-test COPY
+ @viewport/scroll-metadata COPY
+ @viewport/background COPY
+ @viewport/border COPY
+ BlockContainer<HTML>#html/descendants(background-and-borders) COPY
+ @viewport/foreground/hit-test COPY
+ @viewport/foreground COPY
+ BlockContainer<HTML>#html/descendants(foreground) COPY
+ BlockContainer<HTML>#html ASSEMBLE
+ BlockContainer<HTML>#html/preamble COPY
+ BlockContainer<HTML>#html/background/hit-test COPY
+ BlockContainer<HTML>#html/scroll-metadata COPY
+ BlockContainer<HTML>#html/background COPY
+ BlockContainer<HTML>#html/border COPY
+ BlockContainer<BODY>#body/descendants(background-and-borders) COPY
+ BlockContainer<HTML>#html/foreground/hit-test COPY
+ BlockContainer<HTML>#html/foreground COPY
+ BlockContainer<BODY>#body/descendants(foreground) ASSEMBLE
+ BlockContainer<BODY>#body/foreground/hit-test COPY
+ BlockContainer<BODY>#body/foreground COPY
+ BlockContainer<DIV>#ring/descendants(foreground) ASSEMBLE
+ BlockContainer<DIV>#ring/foreground/hit-test COPY
+ BlockContainer<DIV>#ring/foreground COPY
+ BlockContainer<DIV>#ring/outline ${ringOutline}
+ BlockContainer<DIV>#ring/overlay/hit-test COPY
+ BlockContainer<DIV>#ring/overlay COPY
+ BlockContainer<DIV>#selected/descendants(foreground) ASSEMBLE
+ BlockContainer<DIV>#selected/foreground/hit-test COPY
+ BlockContainer<DIV>#selected/foreground RECORD
+ BlockContainer<DIV>#selected/outline COPY
+ BlockContainer<DIV>#selected/overlay/hit-test COPY
+ BlockContainer<DIV>#selected/overlay COPY
+ BlockContainer<DIV>#plain/descendants(foreground) COPY
+ BlockContainer<BODY>#body/outline COPY
+ BlockContainer<BODY>#body/overlay/hit-test COPY
+ BlockContainer<BODY>#body/overlay COPY
+ BlockContainer<HTML>#html/outline COPY
+ @viewport/outline COPY
+`;
+displayListTest({
+ setup: () => {
+ removeBodyWhitespace();
+ getSelection().selectAllChildren(selected);
+ },
+ steps: [
+ {
+ name: "quiet before focus changes",
+ expect: `
+ damage: rows=0 moved=0 order=0 eligibility=0 all=false
+ @canvas RECORD (empty)
+ @viewport COPY
+ `,
+ },
+ {
+ name: "window loses focus",
+ mutate: () => internals.setPageFocus(false),
+ expect: focusChangeTrace("RECORD (empty)"),
+ },
+ {
+ name: "quiet while unfocused",
+ expect: `
+ damage: rows=0 moved=0 order=0 eligibility=0 all=false
+ @canvas RECORD (empty)
+ @viewport COPY
+ `,
+ },
+ {
+ name: "window regains focus",
+ mutate: () => internals.setPageFocus(true),
+ expect: focusChangeTrace("RECORD"),
+ },
+ ],
+ cleanup: () => internals.setPageFocus(true),
+});
+</script>