dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / html / close_watcher_manager.lucb
#==============================================================================================
#
# close_watcher_manager - HTML::CloseWatcherManager: a Window's groups of close watchers
#
# DESCRIPTION:
# Port of Libraries/LibWeb/HTML/CloseWatcherManager.cpp (lines 1-121): adding and
# removing close watchers, processing them (the Escape key), the user activation that
# allows a new group, and whether a cancel action may prevent the close.
#
# Copyright (c) 2024, the Ladybird developers.
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
## Ported from Libraries/LibWeb/HTML/CloseWatcherManager.cpp:19 Web::HTML::CloseWatcherManager::create.
pub func html_close_watcher_manager_create(realm: Realm*) -> HtmlCloseWatcherManager*:
return realm_create_html_close_watcher_manager(realm, realm)
## Ported from Libraries/LibWeb/HTML/CloseWatcherManager.cpp:24 Web::HTML::CloseWatcherManager::CloseWatcherManager.
pub func html_close_watcher_manager_construct(this: HtmlCloseWatcherManager*, realm: Realm*):
bind_platform_object_construct_realm(this.bind_platform_object(), realm)
html_close_watcher_manager_init_fields(this)
## Ported from Libraries/LibWeb/HTML/CloseWatcherManager.cpp:29 Web::HTML::CloseWatcherManager::add.
pub func html_close_watcher_manager_add(this: HtmlCloseWatcherManager*, close_watcher: HtmlCloseWatcher*):
# If manager's groups's size is less than manager's allowed number of groups
if ak.vector_size(&this.m_groups) < (usize)this.m_allowed_number_of_groups:
# then append « closeWatcher » to manager's groups.
var new_group: ak.Vector[HtmlCloseWatcher*]
ak.vector_append_t(&new_group, close_watcher)
ak.vector_append_t(&this.m_groups, new_group)
else:
# Assert: manager's groups's size is at least 1 in this branch, since manager's allowed number of groups is always at least 1.
assert(not ak.vector_is_empty(&this.m_groups))
# Append closeWatcher to manager's groups's last item.
ak.vector_append_t(ak.vector_last(&this.m_groups), close_watcher)
# Set manager's next user interaction allows a new group to true.
this.m_next_user_interaction_allows_a_new_group = true
## remove's `entry.ptr() == &close_watcher` (its context is the close watcher).
func html_close_watcher_manager_is_close_watcher(context: void*?, entry: const (HtmlCloseWatcher*)*) -> bool:
return (void*?)*entry == context
## remove's `group.is_empty()`.
func html_close_watcher_manager_is_empty_group(context: void*?, group: const ak.Vector[HtmlCloseWatcher*]*) -> bool:
return ak.vector_is_empty(group)
## Ported from Libraries/LibWeb/HTML/CloseWatcherManager.cpp:48 Web::HTML::CloseWatcherManager::remove.
pub func html_close_watcher_manager_remove(this: HtmlCloseWatcherManager*, close_watcher: HtmlCloseWatcher*):
# 2. For each group of manager's groups: remove closeWatcher from group
for i in 0..<ak.vector_size(&this.m_groups):
_ = ak.vector_remove_first_matching(ak.vector_at(&this.m_groups, i), ak.Function1[const (HtmlCloseWatcher*)*, bool](call = html_close_watcher_manager_is_close_watcher, context = close_watcher))
# 3. Remove any item from manager's group that is empty
_ = ak.vector_remove_all_matching(&this.m_groups, ak.Function1[const ak.Vector[HtmlCloseWatcher*]*, bool](call = html_close_watcher_manager_is_empty_group, context = none))
## Ported from Libraries/LibWeb/HTML/CloseWatcherManager.cpp:61 Web::HTML::CloseWatcherManager::process_close_watchers.
# https://html.spec.whatwg.org/multipage/interaction.html#process-close-watchers
pub func html_close_watcher_manager_process_close_watchers(this: HtmlCloseWatcherManager*) -> bool:
# 1. Let processedACloseWatcher be false.
var processed_a_close_watcher = false
# 2. If window's close watcher manager's groups is not empty:
if not ak.vector_is_empty(&this.m_groups):
# 2.1 Let group be the last item in window's close watcher manager's groups.
let group = ak.vector_last(&this.m_groups)
# Ambiguous spec wording. We copy the groups to avoid modifying the original while iterating.
# See https://github.com/whatwg/html/issues/10240
var group_copy = ak.vector_clone(group)
# 2.2 For each closeWatcher of group, in reverse order:
var i = ak.vector_size(&group_copy)
while i > 0:
i -= 1
let close_watcher = *ak.vector_at(&group_copy, i)
# 2.2.1 If the result of running closeWatcher's get enabled state is true, set processedACloseWatcher to true.
if html_close_watcher_get_enabled_state(close_watcher):
processed_a_close_watcher = true
# 2.2.2 Let shouldProceed be the result of requesting to close closeWatcher with true.
let should_proceed = html_close_watcher_request_close(close_watcher, true)
# 2.2.3 If shouldProceed is false, then break;
if not should_proceed:
break
# 3. If window's close watcher manager's allowed number of groups is greater than 1, decrement it by 1.
if this.m_allowed_number_of_groups > 1:
this.m_allowed_number_of_groups -= 1
# 4. Return processedACloseWatcher.
return processed_a_close_watcher
## Ported from Libraries/LibWeb/HTML/CloseWatcherManager.cpp:97 Web::HTML::CloseWatcherManager::notify_about_user_activation.
# https://html.spec.whatwg.org/multipage/interaction.html#notify-the-close-watcher-manager-about-user-activation
pub func html_close_watcher_manager_notify_about_user_activation(this: HtmlCloseWatcherManager*):
# 1. Let manager be window's close watcher manager.
# 2. If manager's next user interaction allows a new group is true, then increment manager's allowed number of groups.
if this.m_next_user_interaction_allows_a_new_group:
this.m_allowed_number_of_groups += 1
# 3. Set manager's next user interaction allows a new group to false.
this.m_next_user_interaction_allows_a_new_group = false
## Ported from Libraries/LibWeb/HTML/CloseWatcherManager.cpp:108 Web::HTML::CloseWatcherManager::can_prevent_close.
# https://html.spec.whatwg.org/multipage/interaction.html#close-watcher-request-close
pub func html_close_watcher_manager_can_prevent_close(this: HtmlCloseWatcherManager*) -> bool:
# 5. Let canPreventClose be true if window's close watcher manager's groups's size is less than window's close watcher manager's allowed number of groups...
return ak.vector_size(&this.m_groups) < (usize)this.m_allowed_number_of_groups
## Ported from Libraries/LibWeb/HTML/CloseWatcherManager.cpp:114 Web::HTML::CloseWatcherManager::visit_edges (override).
pub func html_close_watcher_manager_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
let this = (HtmlCloseWatcherManager*)base
bind_platform_object_visit_edges_impl(base, visitor)
for i in 0..<ak.vector_size(&this.m_groups):
gc.cell_visitor_visit_vector(visitor, ak.vector_at(&this.m_groups, i))