Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / page / element_resize_action.lucb

#==============================================================================================
#
#   element_resize_action - Web::ElementResizeAction: resizing an element with `resize` by its
#   gripper, and the copies of the page's input events without their browser data
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/Page/ElementResizeAction.cpp (lines 1-105) and InputEvent.cpp
#       lines 14-27 (the clone_without_browser_data functions; the IPC encoders and decoders
#       after them belong to the IPC layer, which phase 1 does not have).
#
#       Copyright (c) 2025, Jonathan Gamble <gamblej@gmail.com>
#       Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: ElementResizeAction =====================================================================

## Ported from ElementResizeAction.cpp:18 Web::containing_block_padding_box_size.
# https://drafts.csswg.org/css-ui#resize
func containing_block_padding_box_size(layout_node: LayoutNode*) -> gfx.CssPixelSize?:
    let parent_box = layout_node_containing_block(layout_node) else return none
    let first_paintable = layout_node_first_paintable(parent_box.layout_node()) else return none
    if let paintable_box = as_if_paint_paintable_box(first_paintable):
        let rect = paint_paintable_box_absolute_padding_box_rect(paintable_box)
        return gfx.css_pixel_rect_size(&rect)
    return none

## Ported from ElementResizeAction.cpp:28 Web::ElementResizeAction::ElementResizeAction.
pub func element_resize_action_construct(this: ElementResizeAction*, element: DomElement*, pointer_down_origin: gfx.CssPixelPoint):
    this.m_element = element
    this.m_pointer_down_origin = pointer_down_origin
    this.m_initial_border_box_size = gfx.CssPixelSize()
    if let paintable_box = dom_node_paintable_box(element.dom_node()):
        let rect = paint_paintable_box_absolute_border_box_rect(paintable_box)
        this.m_initial_border_box_size = gfx.css_pixel_rect_size(&rect)

## `String::formatted("{:.2f}px", value)`.
func element_resize_action_px_string(value: f64) -> ak.String:
    var builder = ak.string_builder_new()
    var format_builder = ak.FormatBuilder(m_builder = &builder)
    ak.format_builder_put_f32_or_f64_f64(&format_builder, value, precision = 2, display_mode = ak.FormatBuilderRealNumberDisplayMode.fixed_point) else trap("MUST: String::formatted")
    ak.string_builder_append(&builder, "px")
    return ak.string_from_string_builder(&builder) else trap("MUST: String::formatted")

## Ported from ElementResizeAction.cpp:37 Web::ElementResizeAction::handle_pointer_move.
pub func element_resize_action_handle_pointer_move(this: ElementResizeAction*, pointer_position: gfx.CssPixelPoint):
    let paintable_box = dom_node_paintable_box(this.m_element.dom_node()) else return
    let layout_node = paint_paintable_layout_node(paintable_box.paint_paintable())
    let computed = &layout_node_computed_values(layout_node).computed_values
    let resize = css_computed_values_resize(computed)
    if resize == css_data.Resize.none_:
        return

    let horizontal_writing_mode = css_computed_values_writing_mode(computed) == css_data.WritingMode.horizontal_tb
    let resize_x = resize == css_data.Resize.both or resize == css_data.Resize.horizontal or (resize == css_data.Resize.inline and horizontal_writing_mode) or (resize == css_data.Resize.block and not horizontal_writing_mode)

    let resize_y = resize == css_data.Resize.both or resize == css_data.Resize.vertical or (resize == css_data.Resize.inline and not horizontal_writing_mode) or (resize == css_data.Resize.block and horizontal_writing_mode)

    var dx = gfx.CssPixels.from_int(0)
    if resize_x:
        dx = pointer_position.m_x.sub(this.m_pointer_down_origin.m_x)
    var dy = gfx.CssPixels.from_int(0)
    if resize_y:
        dy = pointer_position.m_y.sub(this.m_pointer_down_origin.m_y)
    let writing_mode = css_computed_values_writing_mode(computed)
    if (writing_mode == css_data.WritingMode.horizontal_tb and css_computed_values_direction(computed) == css_data.Direction.rtl) or writing_mode == css_data.WritingMode.vertical_rl or writing_mode == css_data.WritingMode.sideways_rl:
        dx = gfx.CssPixels.from_int(0).sub(dx)
    var css_width = ak.max(chrome_metrics_zoom_invariant_resize_gripper_size, this.m_initial_border_box_size.m_width.add(dx))
    var css_height = ak.max(chrome_metrics_zoom_invariant_resize_gripper_size, this.m_initial_border_box_size.m_height.add(dy))

    let reference_basis = containing_block_padding_box_size(layout_node)

    if let basis = reference_basis:
        let min_width = css_computed_values_min_width(computed)
        if not css_size_is_auto(&min_width):
            css_width = ak.max(css_width, css_size_to_px(&min_width, layout_node, basis.m_width))
        let max_width = css_computed_values_max_width(computed)
        if not css_size_is_none(&max_width):
            css_width = ak.min(css_width, css_size_to_px(&max_width, layout_node, basis.m_width))
        let min_height = css_computed_values_min_height(computed)
        if not css_size_is_auto(&min_height):
            css_height = ak.max(css_height, css_size_to_px(&min_height, layout_node, basis.m_height))
        let max_height = css_computed_values_max_height(computed)
        if not css_size_is_none(&max_height):
            css_height = ak.min(css_height, css_size_to_px(&max_height, layout_node, basis.m_height))
    if css_computed_values_box_sizing(computed) == css_data.BoxSizing.content_box:
        let metrics = paint_paintable_box_box_model(paintable_box)
        css_width = css_width.sub(metrics.padding.left.add(metrics.padding.right).add(css_computed_values_border_left(computed).width).add(css_computed_values_border_right(computed).width))
        css_height = css_height.sub(metrics.padding.top.add(metrics.padding.bottom).add(css_computed_values_border_top(computed).width).add(css_computed_values_border_bottom(computed).width))

    let style = dom_element_style_for_bindings(this.m_element)
    let width_str = element_resize_action_px_string(ak.max_f64(0.0, css_width.to_double()))
    let height_str = element_resize_action_px_string(ak.max_f64(0.0, css_height.to_double()))

    css_style_properties_set_property_property_id(style, css_data.PropertyId.width, ak.string_bytes_as_string_view(&width_str), ak.sv("")) else trap("MUST: CSSStyleProperties::set_property")
    css_style_properties_set_property_property_id(style, css_data.PropertyId.height, ak.string_bytes_as_string_view(&height_str), ak.sv("")) else trap("MUST: CSSStyleProperties::set_property")

## Ported from ElementResizeAction.cpp:100 Web::ElementResizeAction::visit_edges.
pub func element_resize_action_visit_edges(this: const ElementResizeAction*, visitor: gc.CellVisitor*):
    gc.cell_visitor_visit_t_2(visitor, this.m_element)

# mark: Input events without their browser data =================================================

## Ported from InputEvent.cpp:14 Web::KeyEvent::clone_without_browser_data.
pub func key_event_clone_without_browser_data(this: const KeyEvent*) -> KeyEvent:
    return KeyEvent(type_ = this.type_, key = this.key, modifiers = this.modifiers, code_point = this.code_point, repeat = this.repeat, browser_data = none)

## Ported from InputEvent.cpp:19 Web::MouseEvent::clone_without_browser_data.
pub func mouse_event_clone_without_browser_data(this: const MouseEvent*) -> MouseEvent:
    return MouseEvent(type_ = this.type_, position = this.position, screen_position = this.screen_position, button = this.button, buttons = this.buttons, modifiers = this.modifiers, wheel_delta_x = this.wheel_delta_x, wheel_delta_y = this.wheel_delta_y, click_count = this.click_count, browser_data = none)

## Ported from InputEvent.cpp:24 Web::DragEvent::clone_without_browser_data.
pub func drag_event_clone_without_browser_data(this: const DragEvent*) -> DragEvent:
    return DragEvent(type_ = this.type_, position = this.position, screen_position = this.screen_position, button = this.button, buttons = this.buttons, modifiers = this.modifiers, files = ak.Vector[HtmlSelectedFile](), browser_data = none)