2023-08-18 15:52:40 +02:00
|
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
|
* Copyright (c) 2023, Andreas Kling <andreas@ladybird.org>
|
2024-10-13 21:29:47 +02:00
|
|
|
|
* Copyright (c) 2024, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
|
2023-08-18 15:52:40 +02:00
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2024-03-18 07:42:38 +01:00
|
|
|
|
#include <LibWeb/DOM/Range.h>
|
2024-10-14 16:07:56 +02:00
|
|
|
|
#include <LibWeb/Layout/TextNode.h>
|
2023-08-18 15:52:40 +02:00
|
|
|
|
#include <LibWeb/Layout/Viewport.h>
|
2025-03-30 20:26:44 +02:00
|
|
|
|
#include <LibWeb/Painting/DisplayListRecorder.h>
|
2023-08-19 08:38:51 +02:00
|
|
|
|
#include <LibWeb/Painting/StackingContext.h>
|
2023-08-18 15:52:40 +02:00
|
|
|
|
#include <LibWeb/Painting/ViewportPaintable.h>
|
2024-03-18 07:42:38 +01:00
|
|
|
|
#include <LibWeb/Selection/Selection.h>
|
2023-08-18 15:52:40 +02:00
|
|
|
|
|
|
|
|
|
namespace Web::Painting {
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC_DEFINE_ALLOCATOR(ViewportPaintable);
|
2024-04-06 10:16:04 -07:00
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ref<ViewportPaintable> ViewportPaintable::create(Layout::Viewport const& layout_viewport)
|
2023-08-18 15:52:40 +02:00
|
|
|
|
{
|
2024-11-14 06:13:46 +13:00
|
|
|
|
return layout_viewport.heap().allocate<ViewportPaintable>(layout_viewport);
|
2023-08-18 15:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ViewportPaintable::ViewportPaintable(Layout::Viewport const& layout_viewport)
|
|
|
|
|
: PaintableWithLines(layout_viewport)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ViewportPaintable::~ViewportPaintable() = default;
|
|
|
|
|
|
2023-08-19 08:38:51 +02:00
|
|
|
|
void ViewportPaintable::build_stacking_context_tree_if_needed()
|
|
|
|
|
{
|
|
|
|
|
if (stacking_context())
|
|
|
|
|
return;
|
|
|
|
|
build_stacking_context_tree();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ViewportPaintable::build_stacking_context_tree()
|
|
|
|
|
{
|
2023-08-19 15:20:45 +02:00
|
|
|
|
set_stacking_context(make<StackingContext>(*this, nullptr, 0));
|
2023-08-19 08:38:51 +02:00
|
|
|
|
|
|
|
|
|
size_t index_in_tree_order = 1;
|
2024-11-18 16:05:22 +01:00
|
|
|
|
for_each_in_subtree_of_type<PaintableBox>([&](auto const& paintable_box) {
|
|
|
|
|
const_cast<PaintableBox&>(paintable_box).invalidate_stacking_context();
|
|
|
|
|
auto* parent_context = const_cast<PaintableBox&>(paintable_box).enclosing_stacking_context();
|
|
|
|
|
auto establishes_stacking_context = paintable_box.layout_node().establishes_stacking_context();
|
|
|
|
|
if ((paintable_box.is_positioned() || establishes_stacking_context) && paintable_box.computed_values().z_index().value_or(0) == 0)
|
|
|
|
|
parent_context->m_positioned_descendants_and_stacking_contexts_with_stack_level_0.append(paintable_box);
|
|
|
|
|
if (!paintable_box.is_positioned() && paintable_box.is_floating())
|
|
|
|
|
parent_context->m_non_positioned_floating_descendants.append(paintable_box);
|
2024-03-01 11:54:44 +01:00
|
|
|
|
if (!establishes_stacking_context) {
|
2024-11-18 16:05:22 +01:00
|
|
|
|
VERIFY(!paintable_box.stacking_context());
|
2023-08-19 08:38:51 +02:00
|
|
|
|
return TraversalDecision::Continue;
|
|
|
|
|
}
|
|
|
|
|
VERIFY(parent_context);
|
2024-11-18 16:05:22 +01:00
|
|
|
|
const_cast<PaintableBox&>(paintable_box).set_stacking_context(make<Painting::StackingContext>(const_cast<PaintableBox&>(paintable_box), parent_context, index_in_tree_order++));
|
2023-08-19 08:38:51 +02:00
|
|
|
|
return TraversalDecision::Continue;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
stacking_context()->sort();
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-31 23:07:26 +02:00
|
|
|
|
void ViewportPaintable::paint_all_phases(DisplayListRecordingContext& context)
|
2023-08-19 08:38:51 +02:00
|
|
|
|
{
|
|
|
|
|
build_stacking_context_tree_if_needed();
|
2025-03-30 20:26:44 +02:00
|
|
|
|
context.display_list_recorder().save_layer();
|
2023-08-19 08:38:51 +02:00
|
|
|
|
stacking_context()->paint(context);
|
2025-03-30 20:26:44 +02:00
|
|
|
|
context.display_list_recorder().restore();
|
2023-08-19 08:38:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-08 17:30:07 +01:00
|
|
|
|
void ViewportPaintable::assign_scroll_frames()
|
2023-12-29 06:10:32 +01:00
|
|
|
|
{
|
2024-08-17 18:57:17 +02:00
|
|
|
|
for_each_in_inclusive_subtree_of_type<PaintableBox>([&](auto& paintable_box) {
|
2024-08-24 19:20:31 +02:00
|
|
|
|
RefPtr<ScrollFrame> sticky_scroll_frame;
|
|
|
|
|
if (paintable_box.is_sticky_position()) {
|
|
|
|
|
auto const* nearest_scrollable_ancestor = paintable_box.nearest_scrollable_ancestor();
|
2024-09-11 22:01:44 +02:00
|
|
|
|
RefPtr<ScrollFrame const> parent_scroll_frame;
|
2024-08-24 19:20:31 +02:00
|
|
|
|
if (nearest_scrollable_ancestor) {
|
2024-09-11 22:01:44 +02:00
|
|
|
|
parent_scroll_frame = nearest_scrollable_ancestor->nearest_scroll_frame();
|
2024-08-24 19:20:31 +02:00
|
|
|
|
}
|
2024-10-11 19:32:32 +02:00
|
|
|
|
sticky_scroll_frame = m_scroll_state.create_sticky_frame_for(paintable_box, parent_scroll_frame);
|
2024-09-11 22:01:44 +02:00
|
|
|
|
|
2025-06-17 16:30:30 +02:00
|
|
|
|
paintable_box.set_enclosing_scroll_frame(sticky_scroll_frame);
|
|
|
|
|
paintable_box.set_own_scroll_frame(sticky_scroll_frame);
|
2024-08-24 19:20:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-17 18:57:17 +02:00
|
|
|
|
if (paintable_box.has_scrollable_overflow() || is<ViewportPaintable>(paintable_box)) {
|
2024-09-11 22:01:44 +02:00
|
|
|
|
RefPtr<ScrollFrame const> parent_scroll_frame;
|
2024-08-24 19:20:31 +02:00
|
|
|
|
if (sticky_scroll_frame) {
|
2024-09-11 22:01:44 +02:00
|
|
|
|
parent_scroll_frame = sticky_scroll_frame;
|
2024-08-24 19:20:31 +02:00
|
|
|
|
} else {
|
2024-09-11 22:01:44 +02:00
|
|
|
|
parent_scroll_frame = paintable_box.nearest_scroll_frame();
|
2024-08-24 19:20:31 +02:00
|
|
|
|
}
|
2024-10-11 19:32:32 +02:00
|
|
|
|
auto scroll_frame = m_scroll_state.create_scroll_frame_for(paintable_box, parent_scroll_frame);
|
2024-08-14 21:52:10 +02:00
|
|
|
|
paintable_box.set_own_scroll_frame(scroll_frame);
|
LibWeb: Move clip rect calculation to happen before painting
With this change, clip rectangles for boxes with hidden overflow or the
clip property are no longer calculated during the recording of painting
commands. Instead, it has moved to the "pre-paint" phase, along with
the assignment of scrolling offsets, and works in the following way:
1. The paintable tree is traversed to collect all paintable boxes that
have hidden overflow or use the CSS clip property. For each of these
boxes, the "final" clip rectangle is calculated by intersecting clip
rectangles in the containing block chain for a box.
2. The paintable tree is traversed another time, and a clip rectangle
is assigned for each paintable box contained by a node with hidden
overflow or the clip property.
This way, clipping becomes much easier during the painting commands
recording phase, as it only concerns the use of already assigned clip
rectangles. The same approach is applied to handle scrolling offsets.
Also, clip rectangle calculation is now implemented more correctly, as
we no longer stop at the stacking context boundary while intersecting
clip rectangles in the containing block chain.
Fixes:
https://github.com/SerenityOS/serenity/issues/22932
https://github.com/SerenityOS/serenity/issues/22883
https://github.com/SerenityOS/serenity/issues/22679
https://github.com/SerenityOS/serenity/issues/22534
2024-01-26 21:47:26 +01:00
|
|
|
|
}
|
2024-08-24 19:20:31 +02:00
|
|
|
|
|
LibWeb: Move clip rect calculation to happen before painting
With this change, clip rectangles for boxes with hidden overflow or the
clip property are no longer calculated during the recording of painting
commands. Instead, it has moved to the "pre-paint" phase, along with
the assignment of scrolling offsets, and works in the following way:
1. The paintable tree is traversed to collect all paintable boxes that
have hidden overflow or use the CSS clip property. For each of these
boxes, the "final" clip rectangle is calculated by intersecting clip
rectangles in the containing block chain for a box.
2. The paintable tree is traversed another time, and a clip rectangle
is assigned for each paintable box contained by a node with hidden
overflow or the clip property.
This way, clipping becomes much easier during the painting commands
recording phase, as it only concerns the use of already assigned clip
rectangles. The same approach is applied to handle scrolling offsets.
Also, clip rectangle calculation is now implemented more correctly, as
we no longer stop at the stacking context boundary while intersecting
clip rectangles in the containing block chain.
Fixes:
https://github.com/SerenityOS/serenity/issues/22932
https://github.com/SerenityOS/serenity/issues/22883
https://github.com/SerenityOS/serenity/issues/22679
https://github.com/SerenityOS/serenity/issues/22534
2024-01-26 21:47:26 +01:00
|
|
|
|
return TraversalDecision::Continue;
|
|
|
|
|
});
|
|
|
|
|
|
2025-06-17 16:30:30 +02:00
|
|
|
|
for_each_in_subtree([&](auto& paintable) {
|
|
|
|
|
if (paintable.is_fixed_position() || paintable.is_sticky_position())
|
2024-08-06 15:32:31 +03:00
|
|
|
|
return TraversalDecision::Continue;
|
2025-06-17 16:30:30 +02:00
|
|
|
|
|
2024-08-06 15:32:31 +03:00
|
|
|
|
for (auto block = paintable.containing_block(); block; block = block->containing_block()) {
|
2024-08-24 19:11:01 +02:00
|
|
|
|
if (auto scroll_frame = block->own_scroll_frame(); scroll_frame) {
|
2025-06-17 16:30:30 +02:00
|
|
|
|
if (auto* paintable_box = as_if<PaintableBox>(paintable))
|
|
|
|
|
paintable_box->set_enclosing_scroll_frame(*scroll_frame);
|
|
|
|
|
|
2024-08-06 15:32:31 +03:00
|
|
|
|
return TraversalDecision::Continue;
|
|
|
|
|
}
|
|
|
|
|
if (block->is_fixed_position()) {
|
|
|
|
|
return TraversalDecision::Continue;
|
LibWeb: Move clip rect calculation to happen before painting
With this change, clip rectangles for boxes with hidden overflow or the
clip property are no longer calculated during the recording of painting
commands. Instead, it has moved to the "pre-paint" phase, along with
the assignment of scrolling offsets, and works in the following way:
1. The paintable tree is traversed to collect all paintable boxes that
have hidden overflow or use the CSS clip property. For each of these
boxes, the "final" clip rectangle is calculated by intersecting clip
rectangles in the containing block chain for a box.
2. The paintable tree is traversed another time, and a clip rectangle
is assigned for each paintable box contained by a node with hidden
overflow or the clip property.
This way, clipping becomes much easier during the painting commands
recording phase, as it only concerns the use of already assigned clip
rectangles. The same approach is applied to handle scrolling offsets.
Also, clip rectangle calculation is now implemented more correctly, as
we no longer stop at the stacking context boundary while intersecting
clip rectangles in the containing block chain.
Fixes:
https://github.com/SerenityOS/serenity/issues/22932
https://github.com/SerenityOS/serenity/issues/22883
https://github.com/SerenityOS/serenity/issues/22679
https://github.com/SerenityOS/serenity/issues/22534
2024-01-26 21:47:26 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-06 15:32:31 +03:00
|
|
|
|
VERIFY_NOT_REACHED();
|
LibWeb: Move clip rect calculation to happen before painting
With this change, clip rectangles for boxes with hidden overflow or the
clip property are no longer calculated during the recording of painting
commands. Instead, it has moved to the "pre-paint" phase, along with
the assignment of scrolling offsets, and works in the following way:
1. The paintable tree is traversed to collect all paintable boxes that
have hidden overflow or use the CSS clip property. For each of these
boxes, the "final" clip rectangle is calculated by intersecting clip
rectangles in the containing block chain for a box.
2. The paintable tree is traversed another time, and a clip rectangle
is assigned for each paintable box contained by a node with hidden
overflow or the clip property.
This way, clipping becomes much easier during the painting commands
recording phase, as it only concerns the use of already assigned clip
rectangles. The same approach is applied to handle scrolling offsets.
Also, clip rectangle calculation is now implemented more correctly, as
we no longer stop at the stacking context boundary while intersecting
clip rectangles in the containing block chain.
Fixes:
https://github.com/SerenityOS/serenity/issues/22932
https://github.com/SerenityOS/serenity/issues/22883
https://github.com/SerenityOS/serenity/issues/22679
https://github.com/SerenityOS/serenity/issues/22534
2024-01-26 21:47:26 +01:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-08 17:30:07 +01:00
|
|
|
|
void ViewportPaintable::assign_clip_frames()
|
LibWeb: Move clip rect calculation to happen before painting
With this change, clip rectangles for boxes with hidden overflow or the
clip property are no longer calculated during the recording of painting
commands. Instead, it has moved to the "pre-paint" phase, along with
the assignment of scrolling offsets, and works in the following way:
1. The paintable tree is traversed to collect all paintable boxes that
have hidden overflow or use the CSS clip property. For each of these
boxes, the "final" clip rectangle is calculated by intersecting clip
rectangles in the containing block chain for a box.
2. The paintable tree is traversed another time, and a clip rectangle
is assigned for each paintable box contained by a node with hidden
overflow or the clip property.
This way, clipping becomes much easier during the painting commands
recording phase, as it only concerns the use of already assigned clip
rectangles. The same approach is applied to handle scrolling offsets.
Also, clip rectangle calculation is now implemented more correctly, as
we no longer stop at the stacking context boundary while intersecting
clip rectangles in the containing block chain.
Fixes:
https://github.com/SerenityOS/serenity/issues/22932
https://github.com/SerenityOS/serenity/issues/22883
https://github.com/SerenityOS/serenity/issues/22679
https://github.com/SerenityOS/serenity/issues/22534
2024-01-26 21:47:26 +01:00
|
|
|
|
{
|
|
|
|
|
for_each_in_subtree_of_type<PaintableBox>([&](auto const& paintable_box) {
|
2024-02-08 17:30:07 +01:00
|
|
|
|
auto overflow_x = paintable_box.computed_values().overflow_x();
|
|
|
|
|
auto overflow_y = paintable_box.computed_values().overflow_y();
|
2025-05-03 11:36:28 +02:00
|
|
|
|
// Note: Overflow may be clip on one axis and visible on the other.
|
|
|
|
|
auto has_hidden_overflow = overflow_x != CSS::Overflow::Visible || overflow_y != CSS::Overflow::Visible;
|
2025-05-09 21:34:47 +02:00
|
|
|
|
if (has_hidden_overflow || paintable_box.get_clip_rect().has_value() || paintable_box.layout_node().has_paint_containment()) {
|
2024-02-08 17:30:07 +01:00
|
|
|
|
auto clip_frame = adopt_ref(*new ClipFrame());
|
2024-04-05 13:47:48 -07:00
|
|
|
|
clip_state.set(paintable_box, move(clip_frame));
|
2024-02-08 17:30:07 +01:00
|
|
|
|
}
|
|
|
|
|
return TraversalDecision::Continue;
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-07 19:18:56 +02:00
|
|
|
|
for_each_in_subtree([&](auto& paintable) {
|
2025-05-03 11:22:14 +02:00
|
|
|
|
if (paintable.is_paintable_box()) {
|
2025-07-07 19:18:56 +02:00
|
|
|
|
auto& paintable_box = static_cast<PaintableBox&>(paintable);
|
2025-05-03 11:22:14 +02:00
|
|
|
|
if (auto clip_frame = clip_state.get(paintable_box); clip_frame.has_value()) {
|
2025-07-07 19:18:56 +02:00
|
|
|
|
paintable_box.set_own_clip_frame(clip_frame.value());
|
2025-05-03 11:22:14 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-10-13 16:58:57 +02:00
|
|
|
|
for (auto block = paintable.containing_block(); !block->is_viewport_paintable(); block = block->containing_block()) {
|
2024-03-01 15:30:44 +01:00
|
|
|
|
if (auto clip_frame = clip_state.get(block); clip_frame.has_value()) {
|
2024-02-08 17:30:07 +01:00
|
|
|
|
if (paintable.is_paintable_box()) {
|
2025-07-07 19:18:56 +02:00
|
|
|
|
auto& paintable_box = static_cast<PaintableBox&>(paintable);
|
|
|
|
|
paintable_box.set_enclosing_clip_frame(clip_frame.value());
|
2024-02-08 17:30:07 +01:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-09-23 21:43:44 +02:00
|
|
|
|
if (!block->transform().is_identity())
|
2024-07-31 22:26:43 +03:00
|
|
|
|
break;
|
2024-02-08 17:30:07 +01:00
|
|
|
|
}
|
|
|
|
|
return TraversalDecision::Continue;
|
|
|
|
|
});
|
2024-05-28 13:47:00 +02:00
|
|
|
|
|
2024-02-08 17:30:07 +01:00
|
|
|
|
for (auto& it : clip_state) {
|
|
|
|
|
auto const& paintable_box = *it.key;
|
|
|
|
|
auto& clip_frame = *it.value;
|
2024-10-13 21:29:47 +02:00
|
|
|
|
for (auto const* block = &paintable_box.layout_node_with_style_and_box_metrics(); !block->is_viewport(); block = block->containing_block()) {
|
|
|
|
|
auto const& paintable = block->first_paintable();
|
|
|
|
|
if (!paintable->is_paintable_box()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
auto const& block_paintable_box = static_cast<PaintableBox const&>(*paintable);
|
2025-05-03 11:36:28 +02:00
|
|
|
|
bool clip_x = paintable->computed_values().overflow_x() != CSS::Overflow::Visible;
|
|
|
|
|
bool clip_y = paintable->computed_values().overflow_y() != CSS::Overflow::Visible;
|
|
|
|
|
|
|
|
|
|
auto clip_rect = block_paintable_box.overflow_clip_edge_rect();
|
|
|
|
|
if (block_paintable_box.get_clip_rect().has_value()) {
|
|
|
|
|
clip_rect.intersect(block_paintable_box.get_clip_rect().value());
|
|
|
|
|
clip_x = true;
|
|
|
|
|
clip_y = true;
|
2024-08-14 18:13:01 +02:00
|
|
|
|
}
|
2025-05-03 11:36:28 +02:00
|
|
|
|
|
2025-05-03 11:46:39 +02:00
|
|
|
|
// https://drafts.csswg.org/css-contain-2/#paint-containment
|
|
|
|
|
// 1. The contents of the element including any ink or scrollable overflow must be clipped to the overflow clip
|
|
|
|
|
// edge of the paint containment box, taking corner clipping into account. This does not include the creation of
|
|
|
|
|
// any mechanism to access or indicate the presence of the clipped content; nor does it inhibit the creation of
|
|
|
|
|
// any such mechanism through other properties, such as overflow, resize, or text-overflow.
|
|
|
|
|
// NOTE: This clipping shape respects overflow-clip-margin, allowing an element with paint containment
|
|
|
|
|
// to still slightly overflow its normal bounds.
|
2025-05-09 21:34:47 +02:00
|
|
|
|
if (block->has_paint_containment()) {
|
|
|
|
|
// NOTE: Note: The behavior is described in this paragraph is equivalent to changing 'overflow-x: visible' into
|
|
|
|
|
// 'overflow-x: clip' and 'overflow-y: visible' into 'overflow-y: clip' at used value time, while leaving other
|
|
|
|
|
// values of 'overflow-x' and 'overflow-y' unchanged.
|
|
|
|
|
clip_x = true;
|
|
|
|
|
clip_y = true;
|
2025-05-03 11:46:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2025-05-03 11:36:28 +02:00
|
|
|
|
if (clip_x || clip_y) {
|
|
|
|
|
// https://drafts.csswg.org/css-overflow-3/#corner-clipping
|
|
|
|
|
// As mentioned in CSS Backgrounds 3 § 4.3 Corner Clipping, the clipping region established by overflow can be
|
|
|
|
|
// rounded:
|
|
|
|
|
if (clip_x && clip_y) {
|
|
|
|
|
// - When overflow-x and overflow-y compute to hidden, scroll, or auto, the clipping region is rounded
|
|
|
|
|
// based on the border radius, adjusted to the padding edge, as described in CSS Backgrounds 3 § 4.2 Corner
|
|
|
|
|
// Shaping.
|
|
|
|
|
// - When both overflow-x and overflow-y compute to clip, the clipping region is rounded as described in § 3.2
|
|
|
|
|
// Expanding Clipping Bounds: the overflow-clip-margin property.
|
|
|
|
|
// FIXME: Implement overflow-clip-margin
|
|
|
|
|
clip_frame.add_clip_rect(clip_rect, block_paintable_box.normalized_border_radii_data(ShrinkRadiiForBorders::Yes), block_paintable_box.enclosing_scroll_frame());
|
|
|
|
|
} else {
|
|
|
|
|
// - However, when one of overflow-x or overflow-y computes to clip and the other computes to visible, the
|
|
|
|
|
// clipping region is not rounded.
|
|
|
|
|
if (clip_x) {
|
|
|
|
|
clip_rect.set_top(0);
|
|
|
|
|
clip_rect.set_bottom(CSSPixels::max_integer_value);
|
|
|
|
|
} else {
|
|
|
|
|
clip_rect.set_left(0);
|
|
|
|
|
clip_rect.set_right(CSSPixels::max_integer_value);
|
|
|
|
|
}
|
|
|
|
|
clip_frame.add_clip_rect(clip_rect, {}, block_paintable_box.enclosing_scroll_frame());
|
|
|
|
|
}
|
2024-08-14 18:13:01 +02:00
|
|
|
|
}
|
2025-09-23 21:43:44 +02:00
|
|
|
|
if (!block_paintable_box.transform().is_identity())
|
2024-08-14 18:13:01 +02:00
|
|
|
|
break;
|
LibWeb: Move clip rect calculation to happen before painting
With this change, clip rectangles for boxes with hidden overflow or the
clip property are no longer calculated during the recording of painting
commands. Instead, it has moved to the "pre-paint" phase, along with
the assignment of scrolling offsets, and works in the following way:
1. The paintable tree is traversed to collect all paintable boxes that
have hidden overflow or use the CSS clip property. For each of these
boxes, the "final" clip rectangle is calculated by intersecting clip
rectangles in the containing block chain for a box.
2. The paintable tree is traversed another time, and a clip rectangle
is assigned for each paintable box contained by a node with hidden
overflow or the clip property.
This way, clipping becomes much easier during the painting commands
recording phase, as it only concerns the use of already assigned clip
rectangles. The same approach is applied to handle scrolling offsets.
Also, clip rectangle calculation is now implemented more correctly, as
we no longer stop at the stacking context boundary while intersecting
clip rectangles in the containing block chain.
Fixes:
https://github.com/SerenityOS/serenity/issues/22932
https://github.com/SerenityOS/serenity/issues/22883
https://github.com/SerenityOS/serenity/issues/22679
https://github.com/SerenityOS/serenity/issues/22534
2024-01-26 21:47:26 +01:00
|
|
|
|
}
|
2024-02-08 17:30:07 +01:00
|
|
|
|
}
|
2023-12-29 06:10:32 +01:00
|
|
|
|
}
|
|
|
|
|
|
2024-08-14 23:17:10 +02:00
|
|
|
|
void ViewportPaintable::refresh_scroll_state()
|
|
|
|
|
{
|
|
|
|
|
if (!m_needs_to_refresh_scroll_state)
|
|
|
|
|
return;
|
|
|
|
|
m_needs_to_refresh_scroll_state = false;
|
|
|
|
|
|
2024-10-11 20:31:19 +02:00
|
|
|
|
m_scroll_state.for_each_sticky_frame([&](auto& scroll_frame) {
|
2024-10-11 19:32:32 +02:00
|
|
|
|
auto const& sticky_box = scroll_frame->paintable_box();
|
2024-08-24 19:20:31 +02:00
|
|
|
|
auto const& sticky_insets = sticky_box.sticky_insets();
|
|
|
|
|
|
|
|
|
|
auto const* nearest_scrollable_ancestor = sticky_box.nearest_scrollable_ancestor();
|
|
|
|
|
if (!nearest_scrollable_ancestor) {
|
2024-10-11 20:31:19 +02:00
|
|
|
|
return;
|
2024-08-24 19:20:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Min and max offsets are needed to clamp the sticky box's position to stay within bounds of containing block.
|
|
|
|
|
CSSPixels min_y_offset_relative_to_nearest_scrollable_ancestor;
|
|
|
|
|
CSSPixels max_y_offset_relative_to_nearest_scrollable_ancestor;
|
|
|
|
|
CSSPixels min_x_offset_relative_to_nearest_scrollable_ancestor;
|
|
|
|
|
CSSPixels max_x_offset_relative_to_nearest_scrollable_ancestor;
|
|
|
|
|
auto const* containing_block_of_sticky_box = sticky_box.containing_block();
|
2025-01-30 15:47:09 +01:00
|
|
|
|
if (containing_block_of_sticky_box->could_be_scrolled_by_wheel_event()) {
|
2024-08-24 19:20:31 +02:00
|
|
|
|
min_y_offset_relative_to_nearest_scrollable_ancestor = 0;
|
2024-09-02 14:06:35 +02:00
|
|
|
|
max_y_offset_relative_to_nearest_scrollable_ancestor = containing_block_of_sticky_box->scrollable_overflow_rect()->height() - sticky_box.absolute_border_box_rect().height();
|
2024-08-24 19:20:31 +02:00
|
|
|
|
min_x_offset_relative_to_nearest_scrollable_ancestor = 0;
|
2024-09-02 14:06:35 +02:00
|
|
|
|
max_x_offset_relative_to_nearest_scrollable_ancestor = containing_block_of_sticky_box->scrollable_overflow_rect()->width() - sticky_box.absolute_border_box_rect().width();
|
2024-08-24 19:20:31 +02:00
|
|
|
|
} else {
|
2024-09-02 14:06:35 +02:00
|
|
|
|
auto containing_block_rect_relative_to_nearest_scrollable_ancestor = containing_block_of_sticky_box->absolute_border_box_rect().translated(-nearest_scrollable_ancestor->absolute_rect().top_left());
|
2024-08-24 19:20:31 +02:00
|
|
|
|
min_y_offset_relative_to_nearest_scrollable_ancestor = containing_block_rect_relative_to_nearest_scrollable_ancestor.top();
|
2024-09-02 14:06:35 +02:00
|
|
|
|
max_y_offset_relative_to_nearest_scrollable_ancestor = containing_block_rect_relative_to_nearest_scrollable_ancestor.bottom() - sticky_box.absolute_border_box_rect().height();
|
2024-08-24 19:20:31 +02:00
|
|
|
|
min_x_offset_relative_to_nearest_scrollable_ancestor = containing_block_rect_relative_to_nearest_scrollable_ancestor.left();
|
2024-09-02 14:06:35 +02:00
|
|
|
|
max_x_offset_relative_to_nearest_scrollable_ancestor = containing_block_rect_relative_to_nearest_scrollable_ancestor.right() - sticky_box.absolute_border_box_rect().width();
|
2024-08-24 19:20:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-02 14:06:35 +02:00
|
|
|
|
auto border_rect_of_sticky_box_relative_to_nearest_scrollable_ancestor = sticky_box.border_box_rect_relative_to_nearest_scrollable_ancestor();
|
2024-08-24 19:20:31 +02:00
|
|
|
|
|
|
|
|
|
// By default, the sticky box is shifted by the scroll offset of the nearest scrollable ancestor.
|
|
|
|
|
CSSPixelPoint sticky_offset = -nearest_scrollable_ancestor->scroll_offset();
|
|
|
|
|
CSSPixelRect const scrollport_rect { nearest_scrollable_ancestor->scroll_offset(), nearest_scrollable_ancestor->absolute_rect().size() };
|
|
|
|
|
|
|
|
|
|
if (sticky_insets.top.has_value()) {
|
|
|
|
|
auto top_inset = sticky_insets.top.value();
|
2024-09-02 14:06:35 +02:00
|
|
|
|
auto stick_to_top_scroll_offset_threshold = border_rect_of_sticky_box_relative_to_nearest_scrollable_ancestor.top() - top_inset;
|
2024-08-24 19:20:31 +02:00
|
|
|
|
if (scrollport_rect.top() > stick_to_top_scroll_offset_threshold) {
|
2024-09-02 14:06:35 +02:00
|
|
|
|
sticky_offset.translate_by({ 0, -border_rect_of_sticky_box_relative_to_nearest_scrollable_ancestor.top() });
|
2024-08-24 19:20:31 +02:00
|
|
|
|
sticky_offset.translate_by({ 0, min(scrollport_rect.top() + top_inset, max_y_offset_relative_to_nearest_scrollable_ancestor) });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sticky_insets.left.has_value()) {
|
|
|
|
|
auto left_inset = sticky_insets.left.value();
|
2024-09-02 14:06:35 +02:00
|
|
|
|
auto stick_to_left_scroll_offset_threshold = border_rect_of_sticky_box_relative_to_nearest_scrollable_ancestor.left() - left_inset;
|
2024-08-24 19:20:31 +02:00
|
|
|
|
if (scrollport_rect.left() > stick_to_left_scroll_offset_threshold) {
|
2024-09-02 14:06:35 +02:00
|
|
|
|
sticky_offset.translate_by({ -border_rect_of_sticky_box_relative_to_nearest_scrollable_ancestor.left(), 0 });
|
2024-08-24 19:20:31 +02:00
|
|
|
|
sticky_offset.translate_by({ min(scrollport_rect.left() + left_inset, max_x_offset_relative_to_nearest_scrollable_ancestor), 0 });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sticky_insets.bottom.has_value()) {
|
|
|
|
|
auto bottom_inset = sticky_insets.bottom.value();
|
2024-09-02 14:06:35 +02:00
|
|
|
|
auto stick_to_bottom_scroll_offset_threshold = border_rect_of_sticky_box_relative_to_nearest_scrollable_ancestor.bottom() + bottom_inset;
|
2024-08-24 19:20:31 +02:00
|
|
|
|
if (scrollport_rect.bottom() < stick_to_bottom_scroll_offset_threshold) {
|
2024-09-02 14:06:35 +02:00
|
|
|
|
sticky_offset.translate_by({ 0, -border_rect_of_sticky_box_relative_to_nearest_scrollable_ancestor.top() });
|
|
|
|
|
sticky_offset.translate_by({ 0, max(scrollport_rect.bottom() - sticky_box.absolute_border_box_rect().height() - bottom_inset, min_y_offset_relative_to_nearest_scrollable_ancestor) });
|
2024-08-24 19:20:31 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sticky_insets.right.has_value()) {
|
|
|
|
|
auto right_inset = sticky_insets.right.value();
|
2024-09-02 14:06:35 +02:00
|
|
|
|
auto stick_to_right_scroll_offset_threshold = border_rect_of_sticky_box_relative_to_nearest_scrollable_ancestor.right() + right_inset;
|
2024-08-24 19:20:31 +02:00
|
|
|
|
if (scrollport_rect.right() < stick_to_right_scroll_offset_threshold) {
|
2024-09-02 14:06:35 +02:00
|
|
|
|
sticky_offset.translate_by({ -border_rect_of_sticky_box_relative_to_nearest_scrollable_ancestor.left(), 0 });
|
|
|
|
|
sticky_offset.translate_by({ max(scrollport_rect.right() - sticky_box.absolute_border_box_rect().width() - right_inset, min_x_offset_relative_to_nearest_scrollable_ancestor), 0 });
|
2024-08-24 19:20:31 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-11 19:32:32 +02:00
|
|
|
|
scroll_frame->set_own_offset(sticky_offset);
|
2024-10-11 20:31:19 +02:00
|
|
|
|
});
|
2024-08-24 19:20:31 +02:00
|
|
|
|
|
2024-10-11 20:31:19 +02:00
|
|
|
|
m_scroll_state.for_each_scroll_frame([&](auto& scroll_frame) {
|
2024-10-11 19:32:32 +02:00
|
|
|
|
scroll_frame->set_own_offset(-scroll_frame->paintable_box().scroll_offset());
|
2024-10-11 20:31:19 +02:00
|
|
|
|
});
|
2024-08-14 23:17:10 +02:00
|
|
|
|
}
|
|
|
|
|
|
2025-09-23 23:28:11 +02:00
|
|
|
|
static void resolve_paint_only_properties_in_subtree(Paintable& root)
|
|
|
|
|
{
|
|
|
|
|
root.for_each_in_inclusive_subtree([&](auto& paintable) {
|
|
|
|
|
paintable.resolve_paint_properties();
|
|
|
|
|
paintable.set_needs_paint_only_properties_update(false);
|
|
|
|
|
return TraversalDecision::Continue;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-02 12:04:16 +01:00
|
|
|
|
void ViewportPaintable::resolve_paint_only_properties()
|
|
|
|
|
{
|
|
|
|
|
// Resolves layout-dependent properties not handled during layout and stores them in the paint tree.
|
|
|
|
|
// Properties resolved include:
|
|
|
|
|
// - Border radii
|
|
|
|
|
// - Box shadows
|
|
|
|
|
// - Text shadows
|
|
|
|
|
// - Transforms
|
|
|
|
|
// - Transform origins
|
2024-02-11 01:56:39 +01:00
|
|
|
|
// - Outlines
|
2024-02-02 12:04:16 +01:00
|
|
|
|
for_each_in_inclusive_subtree([&](Paintable& paintable) {
|
2025-09-23 23:28:11 +02:00
|
|
|
|
if (paintable.needs_paint_only_properties_update()) {
|
|
|
|
|
resolve_paint_only_properties_in_subtree(paintable);
|
|
|
|
|
return TraversalDecision::SkipChildrenAndContinue;
|
|
|
|
|
}
|
2024-02-02 12:04:16 +01:00
|
|
|
|
return TraversalDecision::Continue;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ptr<Selection::Selection> ViewportPaintable::selection() const
|
2024-03-18 07:42:38 +01:00
|
|
|
|
{
|
|
|
|
|
return const_cast<DOM::Document&>(document()).get_selection();
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 19:38:14 +08:00
|
|
|
|
void ViewportPaintable::recompute_selection_states(DOM::Range& range)
|
|
|
|
|
{
|
|
|
|
|
// 1. Start by resetting the selection state of all layout nodes to None.
|
|
|
|
|
for_each_in_inclusive_subtree([&](auto& layout_node) {
|
|
|
|
|
layout_node.set_selection_state(SelectionState::None);
|
|
|
|
|
return TraversalDecision::Continue;
|
|
|
|
|
});
|
|
|
|
|
|
2024-11-30 10:32:32 +01:00
|
|
|
|
auto start_container = range.start_container();
|
|
|
|
|
auto end_container = range.end_container();
|
2024-09-16 19:38:14 +08:00
|
|
|
|
|
|
|
|
|
// 2. If the selection starts and ends in the same node:
|
2024-03-18 07:42:38 +01:00
|
|
|
|
if (start_container == end_container) {
|
|
|
|
|
// 1. If the selection starts and ends at the same offset, return.
|
2024-09-16 19:38:14 +08:00
|
|
|
|
if (range.start_offset() == range.end_offset()) {
|
2024-03-18 07:42:38 +01:00
|
|
|
|
// NOTE: A zero-length selection should not be visible.
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. If it's a text node, mark it as StartAndEnd and return.
|
2025-02-05 23:41:40 +00:00
|
|
|
|
if (is<DOM::Text>(*start_container) && !range.start().node->is_inert()) {
|
2024-08-22 15:21:43 +02:00
|
|
|
|
if (auto* paintable = start_container->paintable())
|
2024-03-18 07:42:38 +01:00
|
|
|
|
paintable->set_selection_state(SelectionState::StartAndEnd);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 19:38:14 +08:00
|
|
|
|
// 3. Mark the selection start node as Start (if text) or Full (if anything else).
|
2025-02-05 23:41:40 +00:00
|
|
|
|
if (auto* paintable = start_container->paintable(); paintable && !range.start().node->is_inert()) {
|
2024-03-18 07:42:38 +01:00
|
|
|
|
if (is<DOM::Text>(*start_container))
|
|
|
|
|
paintable->set_selection_state(SelectionState::Start);
|
|
|
|
|
else
|
|
|
|
|
paintable->set_selection_state(SelectionState::Full);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 19:38:14 +08:00
|
|
|
|
// 4. Mark the selection end node as End (if text) or Full (if anything else).
|
2025-02-05 23:41:40 +00:00
|
|
|
|
if (auto* paintable = end_container->paintable(); paintable && !range.end().node->is_inert()) {
|
2025-07-03 18:33:11 +02:00
|
|
|
|
if (is<DOM::Text>(*end_container))
|
2024-03-18 07:42:38 +01:00
|
|
|
|
paintable->set_selection_state(SelectionState::End);
|
2025-07-03 18:33:11 +02:00
|
|
|
|
else
|
|
|
|
|
paintable->set_selection_state(SelectionState::Full);
|
2024-03-18 07:42:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 19:38:14 +08:00
|
|
|
|
// 5. Mark the nodes between start node and end node (in tree order) as Full.
|
2025-07-03 18:33:11 +02:00
|
|
|
|
// NOTE: If the start node is a descendant of the end node, we cannot traverse from it to the end node since the end node is before it in tree order.
|
|
|
|
|
// Instead, we need to stop traversal somewhere inside the end node, or right after it.
|
|
|
|
|
DOM::Node* stop_at;
|
|
|
|
|
if (start_container->is_descendant_of(end_container)) {
|
|
|
|
|
stop_at = end_container->child_at_index(range.end_offset());
|
|
|
|
|
} else {
|
|
|
|
|
stop_at = end_container;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto* node = start_container->next_in_pre_order(); node && node != stop_at; node = node->next_in_pre_order(end_container)) {
|
2025-02-05 23:41:40 +00:00
|
|
|
|
if (node->is_inert())
|
|
|
|
|
continue;
|
2024-03-18 07:42:38 +01:00
|
|
|
|
if (auto* paintable = node->paintable())
|
|
|
|
|
paintable->set_selection_state(SelectionState::Full);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-18 09:57:48 +02:00
|
|
|
|
bool ViewportPaintable::handle_mousewheel(Badge<EventHandler>, CSSPixelPoint, unsigned, unsigned, int, int)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-06 10:30:13 -07:00
|
|
|
|
void ViewportPaintable::visit_edges(Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
2024-04-15 13:58:21 +02:00
|
|
|
|
visitor.visit(clip_state);
|
2025-06-11 10:44:44 +02:00
|
|
|
|
visitor.visit(m_paintable_boxes_with_auto_content_visibility);
|
2024-04-06 10:30:13 -07:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-18 15:52:40 +02:00
|
|
|
|
}
|