Skip to content

Truly independent web browser

9.4 KB
Raw Blame
/*
 * Copyright (c) 2026-present, the Ladybird developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/AllOf.h>
#include <LibWeb/CSS/ComputedValues.h>
#include <LibWeb/CSS/RustQueryHandle.h>
#include <LibWeb/CSS/ScrollStateContainerQuery.h>
#include <LibWeb/CSS/StyleComputer.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/Layout/Box.h>
#include <LibWeb/Layout/Viewport.h>
#include <LibWeb/Painting/BoxViews.h>
#include <LibWeb/Painting/DocumentPaintState.h>
#include <LibWeb/Painting/ScrollSnap.h>
#include <LibWeb/Painting/Scrolling.h>

namespace Web::CSS {

using namespace Parser::ValueParserFFI;

static bool is_scroll_state_container(DOM::Element const& element)
{
    auto const* box_values = element.style_group<ComputedValues::BoxValues>();
    return box_values && box_values->is_scroll_state_container;
}

// The scrolling box a scroll-state(scrollable) or scroll-state(scrolled) query asks about. The root element's is the
// viewport's.
static Layout::Node* scrolling_box_of(Layout::BegunRead const& read, DOM::Document& document, DOM::Element& element)
{
    if (&element == document.document_element())
        return document.unsafe_layout_node(read);
    auto* layout_node = element.unsafe_layout_node(read);
    if (!layout_node || !layout_node->is_scroll_container())
        return nullptr;
    return layout_node;
}

// https://drafts.csswg.org/css-conditional-5/#stuck
static u8 stuck_edges(Layout::Node const& layout_node)
{
    auto const* node_with_style = as_if<Layout::NodeWithStyle>(layout_node);
    if (!node_with_style || !node_with_style->is_sticky_position())
        return 0;
    // A sticky box is stuck to the edge whose inset moved it from its normal position.
    Gfx::FloatPoint offset;
    if (!Layout::RustFFI::render_state_sticky_device_offset(layout_node.document_host(), Painting::committed_row_slot(layout_node), &offset))
        return 0;
    u8 edges = 0;
    if (offset.y() > 0)
        edges |= SCROLL_STATE_EDGE_TOP;
    else if (offset.y() < 0)
        edges |= SCROLL_STATE_EDGE_BOTTOM;
    if (offset.x() > 0)
        edges |= SCROLL_STATE_EDGE_LEFT;
    else if (offset.x() < 0)
        edges |= SCROLL_STATE_EDGE_RIGHT;
    return edges;
}

// https://drafts.csswg.org/css-conditional-5/#scrollable
static u8 scrollable_edges(Layout::Node const& scrolling_box)
{
    if (!Painting::has_committed_box(scrolling_box) || !Painting::scrollable_overflow_rect(scrolling_box).has_value())
        return 0;

    // Only the directions the user can scroll in count. A box with hidden overflow in an axis can still be scrolled
    // programmatically in it, so its scroll range alone does not say that.
    auto user_scrollable_axes = Painting::wheel_scrollable_axes(scrolling_box);

    auto offset = Painting::scroll_offset(scrolling_box);
    auto minimum_offset = Painting::minimum_scroll_offset(scrolling_box);
    auto maximum_offset = Painting::maximum_scroll_offset(scrolling_box);
    u8 edges = 0;
    if (user_scrollable_axes.vertical) {
        if (offset.y() > minimum_offset.y())
            edges |= SCROLL_STATE_EDGE_TOP;
        if (offset.y() < maximum_offset.y())
            edges |= SCROLL_STATE_EDGE_BOTTOM;
    }
    if (user_scrollable_axes.horizontal) {
        if (offset.x() > minimum_offset.x())
            edges |= SCROLL_STATE_EDGE_LEFT;
        if (offset.x() < maximum_offset.x())
            edges |= SCROLL_STATE_EDGE_RIGHT;
    }
    return edges;
}

// https://drafts.csswg.org/css-conditional-5/#snapped
static u8 snapped_axes(DOM::Document& document, DOM::Element& element, Layout::Node const& layout_node)
{
    // A snap area is snapped by its nearest ancestor scroll container, if that is a snap container.
    Layout::Box const* snap_container = layout_node.containing_block();
    while (snap_container && !snap_container->is_scroll_container())
        snap_container = snap_container->containing_block();
    if (!snap_container || !Painting::is_scroll_snap_container(*snap_container))
        return 0;

    auto stable_node_id = Painting::async_scroll_node_stable_id(*snap_container);
    if (!stable_node_id.has_value())
        return 0;

    auto const& snapped_areas = document.snapped_areas_of_scroll_container(*stable_node_id);
    Compositing::SnapAreaIdentity identity { .node_id = element.unique_id(), .pseudo_element_type = 0 };
    u8 axes = 0;
    if (snapped_areas.x.contains_slow(identity))
        axes |= SCROLL_STATE_SNAPPED_X;
    if (snapped_areas.y.contains_slow(identity))
        axes |= SCROLL_STATE_SNAPPED_Y;
    return axes;
}

// The style engine's container queries read the same snapshot; a container it knows of takes its
// box geometry from every layout commit as well.
static StyleNodeID publish_scroll_state(DOM::Element& element, ScrollStateSnapshot const& snapshot)
{
    if (element.style_node_id() == 0)
        return {};
    StyleEngineFFI::style_engine_set_element_scroll_state(element.document().style_computer().style_engine().host(), element.style_node_id(), snapshot.stuck, snapshot.snapped, snapshot.scrollable, snapshot.scrolled);
    return element.style_node_id();
}

ScrollStateSnapshot ScrollStateQueryContainers::snapshot_for_query(DOM::Element& container)
{
    auto& entry = m_containers.ensure(container);
    if (entry.published_style_node_id != container.style_node_id())
        entry.published_style_node_id = publish_scroll_state(container, entry.snapshot);
    return entry.snapshot;
}

void ScrollStateQueryContainers::did_scroll_relatively(Layout::Node const& scrolling_box, CSSPixelPoint delta)
{
    DOM::Element* element = nullptr;
    if (!scrolling_box.is_viewport()) {
        if (scrolling_box.generated_for_pseudo_element().has_value())
            return;
        element = const_cast<DOM::Element*>(as_if<DOM::Element>(scrolling_box.dom_node()));
        if (!element)
            return;
    }

    // https://drafts.csswg.org/css-conditional-5/#scrolled
    // The most recent scroll in each axis decides that axis, and a scroll in one axis keeps the other's.
    u8 direction = element ? element->last_relative_scroll_direction() : m_viewport_last_relative_scroll_direction;
    if (delta.x() != 0) {
        direction &= ~(SCROLL_STATE_EDGE_LEFT | SCROLL_STATE_EDGE_RIGHT);
        direction |= delta.x() > 0 ? SCROLL_STATE_EDGE_RIGHT : SCROLL_STATE_EDGE_LEFT;
    }
    if (delta.y() != 0) {
        direction &= ~(SCROLL_STATE_EDGE_TOP | SCROLL_STATE_EDGE_BOTTOM);
        direction |= delta.y() > 0 ? SCROLL_STATE_EDGE_BOTTOM : SCROLL_STATE_EDGE_TOP;
    }
    if (element)
        element->set_last_relative_scroll_direction(direction);
    else
        m_viewport_last_relative_scroll_direction = direction;
}

bool ScrollStateQueryContainers::snapshot_post_layout_state(Layout::BegunRead const& read, DOM::Document& document, Snapshot which)
{
    if (m_containers.is_empty())
        return false;
    if (which == Snapshot::NewContainersOnly && all_of(m_containers, [](auto const& it) { return it.value.has_been_snapshotted; }))
        return false;

    GC::RootVector<GC::Ref<DOM::Element>> containers_to_forget;
    bool any_state_changed = false;
    for (auto& [element, container] : m_containers) {
        if (which == Snapshot::NewContainersOnly && container.has_been_snapshotted)
            continue;
        container.has_been_snapshotted = true;
        if (!element->is_connected() || &element->document() != &document || !is_scroll_state_container(element)) {
            containers_to_forget.append(element);
            continue;
        }

        ScrollStateSnapshot snapshot;
        if (auto* layout_node = element->unsafe_layout_node(read); layout_node && Painting::has_committed_box(*layout_node)) {
            snapshot.stuck = stuck_edges(*layout_node);
            snapshot.snapped = snapped_axes(document, element, *layout_node);
        }
        if (auto* scrolling_box = scrolling_box_of(read, document, element)) {
            snapshot.scrollable = scrollable_edges(*scrolling_box);
            snapshot.scrolled = scrolling_box->is_viewport() ? m_viewport_last_relative_scroll_direction : element->last_relative_scroll_direction();
        }

        if (snapshot == container.snapshot) {
            // A moved element's state is published again under its new identity. The state did not change, so
            // neither did anything that read it.
            if (container.published_style_node_id != element->style_node_id())
                container.published_style_node_id = publish_scroll_state(element, snapshot);
            continue;
        }
        container.snapshot = snapshot;
        container.published_style_node_id = publish_scroll_state(element, snapshot);
        any_state_changed = true;
        document.style_computer().style_engine().record_size_container_query_dependents(element->style_node_id());
    }

    for (auto& element : containers_to_forget) {
        auto container = m_containers.take(element);
        // A container that goes away with state still has styles that read it, and they read no state now.
        if (container.has_value() && container->snapshot != ScrollStateSnapshot {} && element->is_connected()) {
            publish_scroll_state(element, {});
            any_state_changed = true;
            document.style_computer().style_engine().record_size_container_query_dependents(element->style_node_id());
        }
    }
    return any_state_changed;
}

void ScrollStateQueryContainers::visit_edges(GC::Cell::Visitor& visitor)
{
    for (auto& it : m_containers)
        visitor.visit(it.key);
}

}