dymokomi/luce-browser-engine
Source of release 0.6.1
0.6.1 / src / web / page / auto_scroll_handler.lucb
#==============================================================================================
#
# auto_scroll_handler - Web::AutoScrollHandler: scrolling a container while a mouse selection
# is dragged past its edges, and Web::MiddleButtonScrollHandler: middle-button autoscroll
#
# DESCRIPTION:
# Port of Libraries/LibWeb/Page/AutoScrollHandler.cpp (lines 1-215) and
# MiddleButtonScrollHandler.cpp (lines 1-94), with the inline functions of their headers.
# MiddleButtonScrollHandler's destructor (an OwnPtr releasing it) is
# middle_button_scroll_handler_destruct, which EventHandler calls where it releases one.
#
# Copyright (c) 2026, Tim Flynn <trflynn89@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Auto scroll geometry ====================================================================
## compute_effective_auto_scroll_edge's effective lambda.
func auto_scroll_effective_edge(distance_to_viewport_edge: gfx.CssPixels) -> gfx.CssPixels:
return auto_scroll_edge_threshold.add(ak.max(gfx.CssPixels.from_int(0), viewport_edge_inset.sub(distance_to_viewport_edge)))
## Ported from AutoScrollHandler.cpp:27 Web::compute_effective_auto_scroll_edge.
# Returns the scrollport shrunk by per-side effective auto scroll edge thresholds. Sides close to a viewport edge get
# a larger inset so the distance-based speed ramp works predictably, even if the user's mouse is limited in reach (e.g.
# by the window/screen boundary).
func compute_effective_auto_scroll_edge(scrollport: gfx.CssPixelRect, viewport_rect: gfx.CssPixelRect) -> gfx.CssPixelRect:
return gfx.css_pixel_rect_shrunken_css_pixels_css_pixels_css_pixels(&scrollport, auto_scroll_effective_edge(gfx.css_pixel_rect_top(&scrollport).sub(gfx.css_pixel_rect_top(&viewport_rect))), auto_scroll_effective_edge(gfx.css_pixel_rect_right(&viewport_rect).sub(gfx.css_pixel_rect_right(&scrollport))), auto_scroll_effective_edge(gfx.css_pixel_rect_bottom(&viewport_rect).sub(gfx.css_pixel_rect_bottom(&scrollport))), auto_scroll_effective_edge(gfx.css_pixel_rect_left(&scrollport).sub(gfx.css_pixel_rect_left(&viewport_rect))))
## Ported from AutoScrollHandler.cpp:39 Web::scrollport_rect_in_viewport.
func scrollport_rect_in_viewport(paintable_box: PaintPaintableBox*) -> gfx.CssPixelRect?:
let scrollport = paint_paintable_box_absolute_padding_box_rect(paintable_box)
# The viewport's scrollport is already in viewport coordinates.
if paint_paintable_is_viewport_paintable(paintable_box.paint_paintable()):
return scrollport
if paint_paintable_box_accumulated_visual_context_index(paintable_box) == 0:
return none
return paint_paintable_box_transform_rect_to_viewport(paintable_box, scrollport)
## compute_auto_scroll_speed's distance_to_speed_factor and max_speed_per_second.
let auto_scroll_distance_to_speed_factor: i64 = 100
let auto_scroll_max_speed_per_second: i64 = 5000
## compute_auto_scroll_speed's compute_axis_speed lambda.
func auto_scroll_compute_axis_speed(mouse_pos: gfx.CssPixels, edge_start: gfx.CssPixels, edge_end: gfx.CssPixels) -> gfx.CssPixels:
let factor = gfx.CssPixels.from_int(auto_scroll_distance_to_speed_factor)
let max_speed = gfx.CssPixels.from_int(auto_scroll_max_speed_per_second)
if mouse_pos.compare(edge_start) < 0:
let distance = edge_start.sub(mouse_pos)
return gfx.CssPixels.from_int(0).sub(ak.min(distance.mul(factor), max_speed))
if mouse_pos.compare(edge_end) > 0:
let distance = mouse_pos.sub(edge_end)
return ak.min(distance.mul(factor), max_speed)
return gfx.CssPixels.from_int(0)
## Ported from AutoScrollHandler.cpp:53 Web::compute_auto_scroll_speed.
# Returns scroll speed in CSS pixels per second for each axis, based on how far the mouse is past the auto scroll edge.
func compute_auto_scroll_speed(mouse: gfx.CssPixelPoint, edge: gfx.CssPixelRect) -> gfx.CssPixelPoint:
let x = gfx.css_pixel_rect_x(&edge)
let y = gfx.css_pixel_rect_y(&edge)
return gfx.CssPixelPoint(m_x = auto_scroll_compute_axis_speed(mouse.m_x, x, x.add(gfx.css_pixel_rect_width(&edge))), m_y = auto_scroll_compute_axis_speed(mouse.m_y, y, y.add(gfx.css_pixel_rect_height(&edge))))
## Ported from AutoScrollHandler.cpp:76 Web::is_in_form_associated_text_control.
func is_in_form_associated_text_control(element: DomElement*) -> bool:
var host = element
if let shadow_root = dom_node_containing_shadow_root(element.dom_node()):
host = dom_document_fragment_host(shadow_root.dom_document_fragment()) else trap("no shadow host")
return as_if_form_associated_text_control_element(host) != none
# mark: AutoScrollHandler =======================================================================
## Ported from AutoScrollHandler.cpp:82 Web::AutoScrollHandler::AutoScrollHandler, with the default
## member initializers.
pub func auto_scroll_handler_construct(this: AutoScrollHandler*, navigable: HtmlNavigable*, container: DomElement*):
this.m_navigable = navigable
this.m_container_element = container
this.m_mouse_position = gfx.CssPixelPoint()
this.m_fractional_delta = gfx.CssPixelPoint()
this.m_active = false
## Ported from AutoScrollHandler.cpp:88 Web::AutoScrollHandler::visit_edges.
pub func auto_scroll_handler_visit_edges(this: const AutoScrollHandler*, visitor: gc.CellVisitor*):
gc.cell_visitor_visit_t_2(visitor, this.m_navigable)
gc.cell_visitor_visit_t_2(visitor, this.m_container_element)
## `CSSPixelRect viewport_rect { {}, m_navigable->viewport_size() }`.
func auto_scroll_handler_viewport_rect(this: AutoScrollHandler*) -> gfx.CssPixelRect:
var viewport_rect: gfx.CssPixelRect = ---
gfx.css_pixel_rect_construct_css_pixel_point(&viewport_rect, gfx.CssPixelPoint(), html_navigable_viewport_size(this.m_navigable))
return viewport_rect
## Ported from AutoScrollHandler.cpp:94 Web::AutoScrollHandler::process.
pub func auto_scroll_handler_process(this: AutoScrollHandler*, mouse_position: gfx.CssPixelPoint) -> gfx.CssPixelPoint:
this.m_mouse_position = mouse_position
dom_document_update_layout(dom_node_document(this.m_container_element.dom_node()), DomUpdateLayoutReason.auto_scroll_selection)
let paintable_box = auto_scroll_handler_auto_scroll_paintable(this.m_container_element) else return mouse_position
let scrollport = scrollport_rect_in_viewport(paintable_box) else return mouse_position
let viewport_rect = auto_scroll_handler_viewport_rect(this)
let effective_edge = compute_effective_auto_scroll_edge(scrollport, viewport_rect)
if gfx.css_pixel_rect_contains_css_pixel_point(&effective_edge, mouse_position):
auto_scroll_handler_deactivate(this)
return mouse_position
auto_scroll_handler_activate(this)
if is_in_form_associated_text_control(this.m_container_element):
return gfx.constrained(mouse_position, scrollport)
return mouse_position
## Ported from AutoScrollHandler.cpp:121 Web::AutoScrollHandler::find_scrollable_ancestor.
pub func auto_scroll_handler_find_scrollable_ancestor(paintable: PaintPaintable*) -> DomElement*?:
var paintable_box = paint_paintable_containing_block(paintable)
while let current = paintable_box:
if paint_paintable_box_could_be_scrolled_by_wheel_event(current):
if let node = paint_paintable_dom_node(current.paint_paintable()):
if let element = as_if_dom_element(node):
return element
# The viewport is always a potential scroll container, but may not report has_scrollable_overflow() and its DOM
# node is Document (not Element).
if paint_paintable_is_viewport_paintable(current.paint_paintable()) and paint_paintable_box_could_be_scrolled_by_wheel_event(current):
if let scrolling_element = dom_document_scrolling_element(paint_paintable_document(current.paint_paintable())):
return scrolling_element
paintable_box = paint_paintable_containing_block(current.paint_paintable())
return none
## Ported from AutoScrollHandler.cpp:144 Web::AutoScrollHandler::auto_scroll_paintable.
# Returns the paintable box that manages the scrollport for an auto-scroll container element. When the element is the
# document's scrolling element, the viewport paintable is the scroll container.
pub func auto_scroll_handler_auto_scroll_paintable(element: DomElement*) -> PaintPaintableBox*?:
let document = dom_node_document(element.dom_node())
if dom_document_scrolling_element(document) == (DomElement*?)element:
let viewport_paintable = dom_document_paintable(document) else return none
return viewport_paintable.paint_paintable_box()
return dom_node_paintable_box(element.dom_node())
## Ported from AutoScrollHandler.cpp:151 Web::AutoScrollHandler::activate.
func auto_scroll_handler_activate(this: AutoScrollHandler*):
this.m_active = true
## Ported from AutoScrollHandler.cpp:156 Web::AutoScrollHandler::deactivate.
func auto_scroll_handler_deactivate(this: AutoScrollHandler*):
this.m_active = false
this.m_fractional_delta = gfx.CssPixelPoint()
## Ported from AutoScrollHandler.cpp:162 Web::AutoScrollHandler::perform_tick.
pub func auto_scroll_handler_perform_tick(this: AutoScrollHandler*):
if not this.m_active:
return
if not event_handler_is_handling_mouse_selection(html_navigable_event_handler(this.m_navigable)):
auto_scroll_handler_deactivate(this)
return
let document = html_navigable_active_document(this.m_navigable) else trap("no active document")
dom_document_update_layout(document, DomUpdateLayoutReason.auto_scroll_selection)
let paintable_box = auto_scroll_handler_auto_scroll_paintable(this.m_container_element)
if paintable_box == none or dom_document_paintable(document) == none:
auto_scroll_handler_deactivate(this)
return
let scroll_container = paintable_box else trap("unreachable")
let scrollport = scrollport_rect_in_viewport(scroll_container)
if scrollport == none:
auto_scroll_handler_deactivate(this)
return
let scrollport_rect = scrollport else trap("unreachable")
let viewport_rect = auto_scroll_handler_viewport_rect(this)
let effective_edge = compute_effective_auto_scroll_edge(scrollport_rect, viewport_rect)
if gfx.css_pixel_rect_contains_css_pixel_point(&effective_edge, this.m_mouse_position):
auto_scroll_handler_deactivate(this)
return
let elapsed_seconds = (f64)auto_scroll_interval_ms / 1000.0
let speed = compute_auto_scroll_speed(this.m_mouse_position, effective_edge)
# Accumulate sub-pixel deltas across ticks, since scroll_by() only accepts whole pixels.
let elapsed = gfx.CssPixels.from_f64(elapsed_seconds)
this.m_fractional_delta = gfx.css_pixel_point_add(&this.m_fractional_delta, gfx.CssPixelPoint(m_x = speed.m_x.mul(elapsed), m_y = speed.m_y.mul(elapsed)))
let scroll_x = this.m_fractional_delta.m_x.to_int()
let scroll_y = this.m_fractional_delta.m_y.to_int()
this.m_fractional_delta = gfx.css_pixel_point_sub(&this.m_fractional_delta, gfx.CssPixelPoint(m_x = gfx.CssPixels.from_int((i64)scroll_x), m_y = gfx.CssPixels.from_int((i64)scroll_y)))
if paint_paintable_box_scroll_by(scroll_container, scroll_x, scroll_y) == PaintPaintableBoxScrollHandled.no:
return
var selection_position = this.m_mouse_position
if is_in_form_associated_text_control(this.m_container_element):
selection_position = gfx.constrained(this.m_mouse_position, scrollport_rect)
event_handler_apply_mouse_selection(html_navigable_event_handler(this.m_navigable), selection_position)
## Ported from AutoScrollHandler.h:25 Web::AutoScrollHandler::is_active.
pub func auto_scroll_handler_is_active(this: const AutoScrollHandler*) -> bool:
return this.m_active
# mark: MiddleButtonScrollHandler ===============================================================
## Ported from MiddleButtonScrollHandler.cpp:23 Web::MiddleButtonScrollHandler::MiddleButtonScrollHandler,
## with the default member initializers.
pub func middle_button_scroll_handler_construct(this: MiddleButtonScrollHandler*, container: DomElement*, origin: gfx.CssPixelPoint):
this.m_container_element = container
this.m_origin = origin
this.m_mouse_position = origin
this.m_fractional_delta = gfx.CssPixelPoint()
this.m_mouse_has_moved_beyond_dead_zone = false
if let paintable = dom_document_paintable(dom_node_document(container.dom_node())):
paint_paintable_set_needs_repaint(paintable.paint_paintable(), InvalidateDisplayList.yes)
## Ported from MiddleButtonScrollHandler.cpp:32 Web::MiddleButtonScrollHandler::~MiddleButtonScrollHandler.
pub func middle_button_scroll_handler_destruct(this: MiddleButtonScrollHandler*):
let document = dom_node_document(this.m_container_element.dom_node())
if not dom_document_layout_is_up_to_date(document):
return
if let paintable = dom_document_paintable(document):
paint_paintable_set_needs_repaint(paintable.paint_paintable(), InvalidateDisplayList.yes)
## Ported from MiddleButtonScrollHandler.cpp:40 Web::MiddleButtonScrollHandler::visit_edges.
pub func middle_button_scroll_handler_visit_edges(this: const MiddleButtonScrollHandler*, visitor: gc.CellVisitor*):
gc.cell_visitor_visit_t_2(visitor, this.m_container_element)
## Ported from MiddleButtonScrollHandler.cpp:45 Web::MiddleButtonScrollHandler::find_scrollable_ancestor.
pub func middle_button_scroll_handler_find_scrollable_ancestor(document: DomDocument*, paintable: PaintPaintable*) -> DomElement*?:
# AutoScrollHandler::find_scrollable_ancestor begins with the paintable's containing block. For middle mouse
# scrolling, we want to include the paintable itself. This allows clicking in dead space to being scrolling.
if let paintable_box = as_if_paint_paintable_box(paintable):
if paint_paintable_box_could_be_scrolled_by_wheel_event(paintable_box):
if let node = paint_paintable_dom_node(paintable):
if let element = as_if_dom_element(node):
return element
if let container = auto_scroll_handler_find_scrollable_ancestor(paintable):
return container
if let scrolling_element = dom_document_scrolling_element(document):
return scrolling_element
return none
## Ported from MiddleButtonScrollHandler.cpp:63 Web::MiddleButtonScrollHandler::perform_tick.
pub func middle_button_scroll_handler_perform_tick(this: MiddleButtonScrollHandler*):
let distance_x = this.m_mouse_position.m_x.sub(this.m_origin.m_x).to_double()
let distance_y = this.m_mouse_position.m_y.sub(this.m_origin.m_y).to_double()
let distance = math.hypot(distance_x, distance_y)
if distance < dead_zone_radius:
return
dom_document_update_layout(dom_node_document(this.m_container_element.dom_node()), DomUpdateLayoutReason.auto_scroll_selection)
this.m_mouse_has_moved_beyond_dead_zone = true
let paintable_box = auto_scroll_handler_auto_scroll_paintable(this.m_container_element) else return
let speed_x = ak.clamp_f64(distance_x * speed_factor, -max_speed_per_second, max_speed_per_second)
let speed_y = ak.clamp_f64(distance_y * speed_factor, -max_speed_per_second, max_speed_per_second)
let elapsed_seconds = scroll_interval_ms / 1000.0
this.m_fractional_delta = gfx.css_pixel_point_add(&this.m_fractional_delta, gfx.CssPixelPoint(m_x = gfx.CssPixels.from_f64(speed_x * elapsed_seconds), m_y = gfx.CssPixels.from_f64(speed_y * elapsed_seconds)))
let scroll_x = this.m_fractional_delta.m_x.to_int()
let scroll_y = this.m_fractional_delta.m_y.to_int()
this.m_fractional_delta = gfx.css_pixel_point_sub(&this.m_fractional_delta, gfx.CssPixelPoint(m_x = gfx.CssPixels.from_int((i64)scroll_x), m_y = gfx.CssPixels.from_int((i64)scroll_y)))
_ = paint_paintable_box_scroll_by(paintable_box, scroll_x, scroll_y)
## Ported from MiddleButtonScrollHandler.h:25 Web::MiddleButtonScrollHandler::update_mouse_position.
pub func middle_button_scroll_handler_update_mouse_position(this: MiddleButtonScrollHandler*, position: gfx.CssPixelPoint):
this.m_mouse_position = position
## Ported from MiddleButtonScrollHandler.h:28 Web::MiddleButtonScrollHandler::origin.
pub func middle_button_scroll_handler_origin(this: const MiddleButtonScrollHandler*) -> gfx.CssPixelPoint:
return this.m_origin
## Ported from MiddleButtonScrollHandler.h:29 Web::MiddleButtonScrollHandler::mouse_has_moved_beyond_dead_zone.
pub func middle_button_scroll_handler_mouse_has_moved_beyond_dead_zone(this: const MiddleButtonScrollHandler*) -> bool:
return this.m_mouse_has_moved_beyond_dead_zone