Truly independent web browser
/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/CSS/Enums.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/Layout/Node.h>
#include <LibWeb/Painting/BoxViews.h>
#include <LibWeb/Painting/PaintingRustBridge.h>
#include <LibWeb/Painting/ScrollSnap.h>
namespace Web::Painting {
// The element a snap area's box belongs to, which is the generating element of a pseudo-element's box.
static DOM::Element const* element_of_snap_area(Compositing::SnapAreaIdentity const& area)
{
if (!area.is_valid())
return nullptr;
return as_if<DOM::Element>(DOM::Node::from_unique_id(area.node_id));
}
// https://drafts.csswg.org/css-scroll-snap-1/#scroll-snap-container
bool is_scroll_snap_container(Layout::Node const& node)
{
auto const* node_with_style = as_if<Layout::NodeWithStyle>(node);
if (!node_with_style || !node_with_style->is_scroll_container())
return false;
// OPTIMIZATION: Only the viewport takes its snap type from another box's style, the document element's, so any
// other box without one snaps nothing, and nor does the viewport where the document element has
// none. Asking the render state what the box snaps measures its overflow.
if (!node.is_viewport() && node_with_style->style_group<CSS::ComputedValues::MiscInteractionValues>().scroll_snap_strictness_value() == CSS::ScrollSnapStrictness::None)
return false;
if (node.is_viewport()) {
auto const* document_element = node.document().document_element();
auto const* misc_interaction_values = document_element ? document_element->style_group<CSS::ComputedValues::MiscInteractionValues>() : nullptr;
if (!misc_interaction_values || misc_interaction_values->scroll_snap_strictness_value() == CSS::ScrollSnapStrictness::None)
return false;
}
auto axes = Layout::RustFFI::layout_row_scroll_snap_axes(node.document_host(), Layout::Node::slot_id(&node));
return axes.x || axes.y;
}
void take_built_scroll_container(Layout::BegunRead const& read, DOM::Document& document, Compositing::RustFFI::NodeSlotId slot, bool is_scroll_snap_container)
{
auto const* scroll_container = document.layout_node_arena().node_if_live(read, slot);
if (!scroll_container)
return;
if (is_scroll_snap_container) {
document.set_may_have_scroll_snap_areas();
document.register_scroll_snap_container(*scroll_container);
return;
}
// A box that does not snap is snapped to no snap areas, so that a scroll it is given while it does not snap is not
// undone by a re-snap once it snaps again.
document.forget_snapped_areas_of_scroll_container(*scroll_container);
}
static Compositing::SnapAreaIdentity snap_area_identity(Layout::RustFFI::FfiSnapAreaIdentity area)
{
return { UniqueNodeID { area.node_id }, area.pseudo_element_type };
}
static void receive_snap_destination(void* receiver, Layout::RustFFI::FfiSnapDestination const* destination)
{
Compositing::SnapDestination result {
.position = destination->position,
.snapped_x = destination->snapped_x,
.snapped_y = destination->snapped_y,
.evaluated_x = destination->evaluated_x,
.evaluated_y = destination->evaluated_y,
};
for (auto area : destination->snapped_areas_x)
result.snapped_areas.x.append(snap_area_identity(area));
for (auto area : destination->snapped_areas_y)
result.snapped_areas.y.append(snap_area_identity(area));
*static_cast<Compositing::SnapDestination*>(receiver) = move(result);
}
// https://drafts.csswg.org/css-scroll-snap-1/#choosing
Compositing::SnapDestination adjust_scroll_destination_for_snapping(Layout::Node const& snap_container, CSSPixelPoint destination, Compositing::SnapSelectionStrategy const& strategy)
{
Compositing::SnapDestination result { destination };
if (!has_committed_box(snap_container))
return result;
Layout::RustFFI::FfiSnapSelectionStrategy ffi_strategy {
.type = strategy.type,
.has_start_offset = strategy.start_offset.has_value(),
.start_offset = strategy.start_offset.value_or({}),
.displacement = strategy.displacement,
.has_starting_positions_boundary = strategy.starting_positions_boundary.has_value(),
.starting_positions_boundary = strategy.starting_positions_boundary.value_or({}),
};
Layout::RustFFI::render_state_select_snap_destination(snap_container.document_host(), committed_row_slot(snap_container), destination, &ffi_strategy, &result, receive_snap_destination);
return result;
}
static bool snap_area_contains_node(Compositing::SnapAreaIdentity const& area, DOM::Node const& node)
{
// A box generated by a pseudo-element has no content of its own that could be focused or targeted.
if (area.is_pseudo_element())
return false;
auto const* element = element_of_snap_area(area);
return element && element->is_inclusive_ancestor_of(node);
}
static bool area_contains_focused_node(void* context, Layout::RustFFI::FfiSnapAreaIdentity area)
{
auto const& selection = *static_cast<ResnapSelection const*>(context);
return selection.focused_node && snap_area_contains_node(snap_area_identity(area), *selection.focused_node);
}
static bool area_contains_targeted_element(void* context, Layout::RustFFI::FfiSnapAreaIdentity area)
{
auto const& selection = *static_cast<ResnapSelection const*>(context);
return selection.targeted_element && snap_area_contains_node(snap_area_identity(area), *selection.targeted_element);
}
static bool element_of_area_is_ancestor_of_element_of(void*, Layout::RustFFI::FfiSnapAreaIdentity ffi_area, Layout::RustFFI::FfiSnapAreaIdentity other_area)
{
auto area = snap_area_identity(ffi_area);
if (area.is_pseudo_element())
return false;
auto const* element = element_of_snap_area(area);
if (!element)
return false;
auto const* other_element = element_of_snap_area(snap_area_identity(other_area));
return other_element && other_element != element && element->is_inclusive_ancestor_of(*other_element);
}
Compositing::SnapDestination select_resnap_destination(Layout::Node const& snap_container, CSSPixelPoint current_offset, ResnapSelection const& selection)
{
Compositing::SnapDestination result { current_offset };
if (!has_committed_box(snap_container))
return result;
auto identities = [](auto const& areas) {
Vector<Layout::RustFFI::FfiSnapAreaIdentity> result;
for (auto const& area : areas)
result.append({ area.node_id.value(), area.pseudo_element_type });
return result;
};
auto x = identities(selection.snapped_areas.x);
auto y = identities(selection.snapped_areas.y);
Layout::RustFFI::FfiSnapAreaDomRelations relations {
.context = const_cast<ResnapSelection*>(&selection),
.area_contains_focused_node = area_contains_focused_node,
.area_contains_targeted_element = area_contains_targeted_element,
.element_of_area_is_ancestor_of_element_of = element_of_area_is_ancestor_of_element_of,
};
Layout::RustFFI::render_state_select_resnap_destination(snap_container.document_host(), committed_row_slot(snap_container), current_offset, x.span(), y.span(), &relations, &result, receive_snap_destination);
return result;
}
}