Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / css / visual_viewport.lucb

#==============================================================================================
#
#   visual_viewport - CSS::VisualViewport: window.visualViewport, the pinch-zoom viewport
#                     over the layout viewport
#
#   DESCRIPTION:
#       Port of LibWeb/CSS/VisualViewport.cpp (lines 1-195) and the inline members of
#       VisualViewport.h (lines 17-61): offsets, size and scale, the event handlers, the
#       transform, zooming around a focal point and mapping back to the layout viewport.
#
#       Copyright (c) 2023, Andreas Kling <andreas@ladybird.org>
#       Copyright (c) 2025, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

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

## Ported from Libraries/LibWeb/CSS/VisualViewport.cpp:20 Web::CSS::VisualViewport::create.
pub func css_visual_viewport_create(document: DomDocument*) -> CssVisualViewport*:
    return realm_create_css_visual_viewport(bind_platform_object_realm(document.bind_platform_object()), document)

## Ported from Libraries/LibWeb/CSS/VisualViewport.cpp:25 Web::CSS::VisualViewport::VisualViewport.
pub func css_visual_viewport_construct(this: CssVisualViewport*, document: DomDocument*):
    dom_event_target_construct(this.dom_event_target(), bind_platform_object_realm(document.bind_platform_object()))
    css_visual_viewport_init_fields(this)
    this.m_document = document

## Ported from Libraries/LibWeb/CSS/VisualViewport.cpp:31 Web::CSS::VisualViewport::initialize (override).
pub func css_visual_viewport_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.visual_viewport)
    dom_event_target_initialize_impl(base, realm)

## Ported from Libraries/LibWeb/CSS/VisualViewport.cpp:37 Web::CSS::VisualViewport::visit_edges (override).
pub func css_visual_viewport_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (CssVisualViewport*)base
    dom_event_target_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t(visitor, this.m_document)

# mark: Offsets, size and scale =================================================================

## Ported from Libraries/LibWeb/CSS/VisualViewport.h:26 Web::CSS::VisualViewport::offset.
pub func css_visual_viewport_offset(this: CssVisualViewport*) -> gfx.CssPixelPoint:
    return this.m_offset

## Ported from Libraries/LibWeb/CSS/VisualViewport.cpp:44 Web::CSS::VisualViewport::offset_left.
# https://drafts.csswg.org/cssom-view/#dom-visualviewport-offsetleft
pub func css_visual_viewport_offset_left(this: CssVisualViewport*) -> f64:
    # 1. If the visual viewport’s associated document is not fully active, return 0.
    if not dom_document_is_fully_active(this.m_document):
        return 0.0

    # 2. Otherwise, return the offset of the left edge of the visual viewport from the left edge of the layout viewport.
    let x = gfx.css_pixel_point_x(&this.m_offset)
    return gfx.css_pixels_to_double_const(&x)

## Ported from Libraries/LibWeb/CSS/VisualViewport.cpp:55 Web::CSS::VisualViewport::offset_top.
# https://drafts.csswg.org/cssom-view/#dom-visualviewport-offsettop
pub func css_visual_viewport_offset_top(this: CssVisualViewport*) -> f64:
    # 1. If the visual viewport’s associated document is not fully active, return 0.
    if not dom_document_is_fully_active(this.m_document):
        return 0.0

    # 2. Otherwise, return the offset of the top edge of the visual viewport from the top edge of the layout viewport.
    let y = gfx.css_pixel_point_y(&this.m_offset)
    return gfx.css_pixels_to_double_const(&y)

## Ported from Libraries/LibWeb/CSS/VisualViewport.cpp:66 Web::CSS::VisualViewport::page_left.
# https://drafts.csswg.org/cssom-view/#dom-visualviewport-pageleft
pub func css_visual_viewport_page_left(this: CssVisualViewport*) -> f64:
    # 1. If the visual viewport’s associated document is not fully active, return 0.
    if not dom_document_is_fully_active(this.m_document):
        return 0.0

    # 2. Otherwise, return the offset of the left edge of the visual viewport from the
    #    left edge of the initial containing block of the layout viewport’s document.
    let viewport_rect = dom_document_viewport_rect(this.m_document)
    let x = gfx.css_pixel_rect_x(&viewport_rect)
    return gfx.css_pixels_to_double_const(&x) + css_visual_viewport_offset_left(this)

## Ported from Libraries/LibWeb/CSS/VisualViewport.cpp:78 Web::CSS::VisualViewport::page_top.
# https://drafts.csswg.org/cssom-view/#dom-visualviewport-pagetop
pub func css_visual_viewport_page_top(this: CssVisualViewport*) -> f64:
    # 1. If the visual viewport’s associated document is not fully active, return 0.
    if not dom_document_is_fully_active(this.m_document):
        return 0.0

    # 2. Otherwise, return the offset of the top edge of the visual viewport from the
    #    top edge of the initial containing block of the layout viewport’s document.
    let viewport_rect = dom_document_viewport_rect(this.m_document)
    let y = gfx.css_pixel_rect_y(&viewport_rect)
    return gfx.css_pixels_to_double_const(&y) + css_visual_viewport_offset_top(this)

## Ported from Libraries/LibWeb/CSS/VisualViewport.cpp:90 Web::CSS::VisualViewport::width.
# https://drafts.csswg.org/cssom-view/#dom-visualviewport-width
pub func css_visual_viewport_width(this: CssVisualViewport*) -> f64:
    # 1. If the visual viewport’s associated document is not fully active, return 0.
    if not dom_document_is_fully_active(this.m_document):
        return 0.0

    # 2. Otherwise, return the width of the visual viewport
    #    FIXME: excluding the width of any rendered vertical classic scrollbar that is fixed to the visual viewport.
    let viewport_rect = dom_document_viewport_rect(this.m_document)
    let size = gfx.css_pixel_rect_size(&viewport_rect)
    let width = gfx.css_pixel_size_width(&size)
    return gfx.css_pixels_to_double_const(&width) / this.m_scale

## Ported from Libraries/LibWeb/CSS/VisualViewport.cpp:102 Web::CSS::VisualViewport::height.
# https://drafts.csswg.org/cssom-view/#dom-visualviewport-height
pub func css_visual_viewport_height(this: CssVisualViewport*) -> f64:
    # 1. If the visual viewport’s associated document is not fully active, return 0.
    if not dom_document_is_fully_active(this.m_document):
        return 0.0

    # 2. Otherwise, return the height of the visual viewport
    #    FIXME: excluding the height of any rendered vertical classic scrollbar that is fixed to the visual viewport.
    let viewport_rect = dom_document_viewport_rect(this.m_document)
    let size = gfx.css_pixel_rect_size(&viewport_rect)
    let height = gfx.css_pixel_size_height(&size)
    return gfx.css_pixels_to_double_const(&height) / this.m_scale

## Ported from Libraries/LibWeb/CSS/VisualViewport.cpp:114 Web::CSS::VisualViewport::scale.
# https://drafts.csswg.org/cssom-view/#dom-visualviewport-scale
pub func css_visual_viewport_scale(this: CssVisualViewport*) -> f64:
    return this.m_scale

# mark: Event handlers ==========================================================================

## Ported from Libraries/LibWeb/CSS/VisualViewport.cpp:119 Web::CSS::VisualViewport::set_onresize.
pub func css_visual_viewport_set_onresize(this: CssVisualViewport*, event_handler: IdlCallbackType*?):
    dom_event_target_set_event_handler_attribute(this.dom_event_target(), html_event_names_resize, event_handler)

## Ported from Libraries/LibWeb/CSS/VisualViewport.cpp:124 Web::CSS::VisualViewport::onresize.
pub func css_visual_viewport_onresize(this: CssVisualViewport*) -> IdlCallbackType*?:
    return dom_event_target_event_handler_attribute(this.dom_event_target(), html_event_names_resize)

## Ported from Libraries/LibWeb/CSS/VisualViewport.cpp:129 Web::CSS::VisualViewport::set_onscroll.
pub func css_visual_viewport_set_onscroll(this: CssVisualViewport*, event_handler: IdlCallbackType*?):
    dom_event_target_set_event_handler_attribute(this.dom_event_target(), html_event_names_scroll, event_handler)

## Ported from Libraries/LibWeb/CSS/VisualViewport.cpp:134 Web::CSS::VisualViewport::onscroll.
pub func css_visual_viewport_onscroll(this: CssVisualViewport*) -> IdlCallbackType*?:
    return dom_event_target_event_handler_attribute(this.dom_event_target(), html_event_names_scroll)

## Ported from Libraries/LibWeb/CSS/VisualViewport.cpp:139 Web::CSS::VisualViewport::set_onscrollend.
pub func css_visual_viewport_set_onscrollend(this: CssVisualViewport*, event_handler: IdlCallbackType*?):
    dom_event_target_set_event_handler_attribute(this.dom_event_target(), html_event_names_scrollend, event_handler)

## Ported from Libraries/LibWeb/CSS/VisualViewport.cpp:144 Web::CSS::VisualViewport::onscrollend.
pub func css_visual_viewport_onscrollend(this: CssVisualViewport*) -> IdlCallbackType*?:
    return dom_event_target_event_handler_attribute(this.dom_event_target(), html_event_names_scrollend)

# mark: Scrolling and zooming ===================================================================

## Ported from Libraries/LibWeb/CSS/VisualViewport.h:46 Web::CSS::VisualViewport::scroll_by.
pub func css_visual_viewport_scroll_by(this: CssVisualViewport*, delta: gfx.CssPixelPoint):
    _ = gfx.css_pixel_point_add_assign(&this.m_offset, delta)

## Ported from Libraries/LibWeb/CSS/VisualViewport.cpp:149 Web::CSS::VisualViewport::transform.
pub func css_visual_viewport_transform(this: CssVisualViewport*) -> gfx.AffineTransform:
    var transform: gfx.AffineTransform = ---
    gfx.affine_transform_construct(&transform)
    # auto offset = m_offset.to_type<double>() * m_scale;
    let offset_x_value = gfx.css_pixel_point_x(&this.m_offset)
    let offset_y_value = gfx.css_pixel_point_y(&this.m_offset)
    let offset_x = gfx.css_pixels_to_double_const(&offset_x_value) * this.m_scale
    let offset_y = gfx.css_pixels_to_double_const(&offset_y_value) * this.m_scale
    _ = gfx.affine_transform_translate_f32(&transform, (f32)(-offset_x), (f32)(-offset_y))
    _ = gfx.affine_transform_scale_float_point(&transform, gfx.FloatPoint(m_x = (f32)this.m_scale, m_y = (f32)this.m_scale))
    return transform

## Ported from Libraries/LibWeb/CSS/VisualViewport.cpp:158 Web::CSS::VisualViewport::zoom.
pub func css_visual_viewport_zoom(this: CssVisualViewport*, position: gfx.CssPixelPoint, scale_delta: f64):
    let min_allowed_scale: f64 = 1.0
    let max_allowed_scale: f64 = 5.0
    let new_scale = ak.clamp_f64(this.m_scale * (1.0 + scale_delta), min_allowed_scale, max_allowed_scale)
    let applied_delta = new_scale / this.m_scale

    # For pinch zoom we want focal_point to stay put on screen:
    # scale_new * (focal_point - offset_new) = scale_old * (focal_point - offset_old)
    # (Point<double> arithmetic, coordinate by coordinate.)
    let offset_x = gfx.css_pixel_point_x(&this.m_offset)
    let offset_y = gfx.css_pixel_point_y(&this.m_offset)
    var new_offset_x = gfx.css_pixels_to_double_const(&offset_x) * this.m_scale * applied_delta
    var new_offset_y = gfx.css_pixels_to_double_const(&offset_y) * this.m_scale * applied_delta
    let position_int = gfx.css_pixel_point_to_type_i32(&position)
    new_offset_x += f64(position_int.m_x) * (applied_delta - 1.0)
    new_offset_y += f64(position_int.m_y) * (applied_delta - 1.0)

    let navigable = dom_document_navigable(this.m_document) else trap("VERIFY: navigable")
    let viewport_rect = html_navigable_viewport_rect(navigable)
    let viewport_size = gfx.css_pixel_rect_size(&viewport_rect)
    let viewport_width = gfx.css_pixel_size_width(&viewport_size)
    let viewport_height = gfx.css_pixel_size_height(&viewport_size)
    let max_x_offset = ak.max_f64(0.0, gfx.css_pixels_to_double_const(&viewport_width) * (new_scale - 1.0))
    let max_y_offset = ak.max_f64(0.0, gfx.css_pixels_to_double_const(&viewport_height) * (new_scale - 1.0))
    new_offset_x = ak.clamp_f64(new_offset_x, 0.0, max_x_offset)
    new_offset_y = ak.clamp_f64(new_offset_y, 0.0, max_y_offset)

    this.m_scale = new_scale
    # m_offset = (new_offset / m_scale).to_type<CSSPixels>();
    gfx.css_pixel_point_construct(&this.m_offset, gfx.css_pixels_nearest_value_for_f64(new_offset_x / this.m_scale), gfx.css_pixels_nearest_value_for_f64(new_offset_y / this.m_scale))
    dom_document_set_needs_accumulated_visual_contexts_update(this.m_document, true)
    dom_document_set_needs_repaint_t(this.m_document, InvalidateDisplayList.yes)

## Ported from Libraries/LibWeb/CSS/VisualViewport.cpp:181 Web::CSS::VisualViewport::map_to_layout_viewport.
pub func css_visual_viewport_map_to_layout_viewport(this: CssVisualViewport*, position: gfx.CssPixelPoint) -> gfx.CssPixelPoint:
    let transform = css_visual_viewport_transform(this)
    var inverse: gfx.AffineTransform = ---
    if let transform_inverse = gfx.affine_transform_inverse(&transform):
        inverse = transform_inverse
    else:
        gfx.affine_transform_construct(&inverse)
    let mapped = gfx.affine_transform_map_i32_int_point(&inverse, gfx.css_pixel_point_to_type_i32(&position))
    var result: gfx.CssPixelPoint = ---
    gfx.css_pixel_point_construct_int(&result, i64(mapped.m_x), i64(mapped.m_y))
    return result

## Ported from Libraries/LibWeb/CSS/VisualViewport.cpp:187 Web::CSS::VisualViewport::reset.
pub func css_visual_viewport_reset(this: CssVisualViewport*):
    this.m_scale = 1.0
    gfx.css_pixel_point_construct_int(&this.m_offset, 0, 0)
    dom_document_set_needs_accumulated_visual_contexts_update(this.m_document, true)
    dom_document_set_needs_repaint_t(this.m_document, InvalidateDisplayList.yes)