Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / layout / node_2.lucb

#==============================================================================================
#
#   node_2 - Layout::NodeWithStyle: construction, image observers and edges
#
#   DESCRIPTION:
#       Port of LibWeb/Layout/Node.cpp (lines 579-673): NodeWithStyle's two constructors, the
#       ImageObserver that repaints a node when one of its images updates,
#       rebuild_image_observers and NodeWithStyle::visit_edges.
#
#       An ImageObserver is a managed blob (`NonnullOwnPtr`, DESIGN.md ยง3.3). Its destructor
#       unregisters it from its image; the port does that where C++ destroys the observers:
#       when rebuild_image_observers replaces them, and when the node itself is destroyed
#       (~NodeWithStyle, the finalize every NodeWithStyle class's table names), so an image
#       that updates later finds no observer whose node is gone.
#
#       Copyright (c) 2018-2023, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2021-2025, Sam Atkins <sam@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: Construction ============================================================================

## `node && node == document.body()`: whether `node` is the body element of `document`.
func layout_node_with_style_node_is_body(document: DomDocument*, node: DomNode*?) -> bool:
    let dom_node = node else return false
    let body = dom_document_body(document) else return false
    return body.dom_node() == dom_node

## Ported from Libraries/LibWeb/Layout/Node.cpp:579 Web::Layout::NodeWithStyle::NodeWithStyle.
pub func layout_node_with_style_construct_css_computed_properties(this: LayoutNodeWithStyle*, document: DomDocument*, node: DomNode*?, computed_style: CssComputedProperties*):
    layout_node_construct(this.layout_node(), document, node)
    this.m_computed_values = css_computed_values_create()
    this.node.m_has_style = true
    this.node.m_is_body = layout_node_with_style_node_is_body(document, node)
    layout_node_with_style_apply_style(this, computed_style)

## Ported from Libraries/LibWeb/Layout/Node.cpp:588 Web::Layout::NodeWithStyle::NodeWithStyle.
pub func layout_node_with_style_construct_css_computed_values(this: LayoutNodeWithStyle*, document: DomDocument*, node: DomNode*?, computed_values: CssComputedValues*):
    layout_node_construct(this.layout_node(), document, node)
    this.m_computed_values = computed_values
    this.node.m_has_style = true
    this.node.m_is_body = layout_node_with_style_node_is_body(document, node)

# mark: ImageObserver ===========================================================================

## The ImageStyleValue::Client vtable of NodeWithStyle::ImageObserver.
let layout_node_with_style_image_observer_client_vtable = CssImageStyleValueClientVTable(class_info = &css_image_style_value_client_class, image_style_value_did_update = layout_node_with_style_image_observer_image_style_value_did_update_impl)

## Ported from Libraries/LibWeb/Layout/Node.cpp:596 Web::Layout::NodeWithStyle::ImageObserver::ImageObserver.
pub func layout_node_with_style_image_observer_construct(this: LayoutNodeWithStyleImageObserver*, owner: LayoutNodeWithStyle*, image: CssImageStyleValue*):
    this.client.vtable = &layout_node_with_style_image_observer_client_vtable
    css_image_style_value_client_construct(&this.client, image)
    gc.weak_construct(&this.m_owner, owner)
    this.m_image = image

## `make<ImageObserver>(owner, image)`: a managed observer, constructed.
func layout_node_with_style_image_observer_create(owner: LayoutNodeWithStyle*, image: CssImageStyleValue*) -> LayoutNodeWithStyleImageObserver*:
    let storage = (new LayoutNodeWithStyleImageObserver[1] ---) else trap("out of memory")
    let this = &storage[0]
    layout_node_with_style_image_observer_construct(this, owner, image)
    return this

## Ported from Libraries/LibWeb/Layout/Node.cpp:603 Web::Layout::NodeWithStyle::ImageObserver::~ImageObserver: the
## destructor's effect, run where C++ destroys an observer.
func layout_node_with_style_image_observer_destruct(this: LayoutNodeWithStyleImageObserver*):
    css_image_style_value_client_image_style_value_finalize(&this.client)

## ~NodeWithStyle (Libraries/LibWeb/Layout/Node.h: the implicit destructor), as the cell's finalize: destroying
## m_image_observers runs each observer's destructor.
pub func layout_node_with_style_finalize_impl(base: gc.Cell*):
    let this = (LayoutNodeWithStyle*)base
    gc.cell_finalize_impl(base)
    for observer in ak.vector_span_const(&this.m_image_observers):
        layout_node_with_style_image_observer_destruct(observer)
    ak.vector_clear(&this.m_image_observers)

## Ported from Libraries/LibWeb/Layout/Node.cpp:608 Web::Layout::NodeWithStyle::ImageObserver::image_style_value_did_update (override).
pub func layout_node_with_style_image_observer_image_style_value_did_update_impl(base: CssImageStyleValueClient*, arg0: CssImageStyleValue*):
    _ = arg0
    let this = (LayoutNodeWithStyleImageObserver*)base
    let owner = gc.weak_ptr(&this.m_owner) else trap("VERIFY: m_owner")

    for paintable in ak.intrusive_list_begin(layout_node_paintables(owner.layout_node())):
        paint_paintable_set_needs_repaint(paintable, InvalidateDisplayList.yes)

    # The body's background propagates to the root element's paintable, which holds the cached draw commands.
    if layout_node_with_style_is_body(owner):
        if let html_element = dom_document_html_element(layout_node_document(owner.layout_node())):
            if let html_layout_node = dom_element_unsafe_layout_node(html_element.dom_element()):
                if html_html_element_should_use_body_background_properties(html_element):
                    for paintable in ak.intrusive_list_begin(layout_node_paintables(html_layout_node.layout_node())):
                        paint_paintable_set_needs_repaint(paintable, InvalidateDisplayList.yes)

## Ported from Libraries/LibWeb/Layout/Node.cpp:629 Web::Layout::NodeWithStyle::ImageObserver::visit_edges.
pub func layout_node_with_style_image_observer_visit_edges(this: LayoutNodeWithStyleImageObserver*, visitor: gc.CellVisitor*):
    css_style_value_visit_edges(this.m_image.css_style_value(), visitor)

## `add_observer_for`: the lambda of NodeWithStyle::rebuild_image_observers.
func layout_node_with_style_add_observer_for(this: LayoutNodeWithStyle*, abstract_image: CssAbstractImageStyleValue*?, observers: ak.Vector[LayoutNodeWithStyleImageObserver*]*):
    let image = abstract_image else return
    var image_to_observe: CssImageStyleValue*? = none
    if css_style_value_is_image(image.css_style_value()):
        image_to_observe = css_style_value_as_image(image.css_style_value())
    elif css_style_value_is_image_set(image.css_style_value()):
        if let selected = css_image_set_style_value_selected_image(css_style_value_as_image_set(image.css_style_value())):
            if css_style_value_is_image(selected.css_style_value()):
                image_to_observe = css_style_value_as_image(selected.css_style_value())
    let observed = image_to_observe else return
    ak.vector_append_t(observers, layout_node_with_style_image_observer_create(this, observed))

## Ported from Libraries/LibWeb/Layout/Node.cpp:634 Web::Layout::NodeWithStyle::rebuild_image_observers.
pub func layout_node_with_style_rebuild_image_observers(this: LayoutNodeWithStyle*):
    var new_observers: ak.Vector[LayoutNodeWithStyleImageObserver*]
    let computed_values = &layout_node_with_style_computed_values(this).computed_values
    let background_layers = css_computed_values_background_layers(computed_values)
    for layer in ak.vector_span_const(&background_layers):
        layout_node_with_style_add_observer_for(this, layer.background_image, &new_observers)
    layout_node_with_style_add_observer_for(this, this.m_list_style_image, &new_observers)
    layout_node_with_style_add_observer_for(this, css_computed_values_mask_image(computed_values), &new_observers)

    # move() destroys the old observers, which unregister from their images.
    for observer in ak.vector_span_const(&this.m_image_observers):
        layout_node_with_style_image_observer_destruct(observer)
    this.m_image_observers = new_observers

# mark: Edges ===================================================================================

## Ported from Libraries/LibWeb/Layout/Node.cpp:660 Web::Layout::NodeWithStyle::visit_edges (override).
pub func layout_node_with_style_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (LayoutNodeWithStyle*)base
    layout_node_visit_edges_impl(base, visitor)
    let background_layers = css_computed_values_background_layers(&layout_node_with_style_computed_values(this).computed_values)
    for layer in ak.vector_span_const(&background_layers):
        css_style_value_visit_edges(layer.background_image.css_style_value(), visitor)

    if let list_style_image = this.m_list_style_image:
        css_style_value_visit_edges(list_style_image.css_style_value(), visitor)

    css_computed_values_visit_edges(this.m_computed_values, visitor)

    for image_observer in ak.vector_span_const(&this.m_image_observers):
        layout_node_with_style_image_observer_visit_edges(image_observer, visitor)