Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.1

0.6.1 / src / web / ui_events / keyboard_event_2.lucb

#==============================================================================================
#
#   keyboard_event_2 - UIEvents::KeyboardEvent: the code value, the event and its accessors
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/UIEvents/KeyboardEvent.cpp lines 346-829 (get_event_code,
#       get_event_location, create_from_platform_event, get_modifier_state,
#       init_keyboard_event, the constructor and initialize) with KeyboardEvent.h's inline
#       accessors. KeyboardEvent::create_from_platform_event keeps the signature its callers had
#       while the class was a stub (the event as its DOM::Event).
#
#       Copyright (c) 2021-2022, Andreas Kling <andreas@ladybird.org>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

# mark: code and location =======================================================================

## Ported from KeyboardEvent.cpp:347 get_event_code.
## 3. Keyboard Event code Value Tables, https://www.w3.org/TR/uievents-code/#code-value-tables
func ui_events_get_event_code(platform_key: UiEventsKeyCode, modifiers: u32) -> ak.String:
    # 3.4. Numpad Section, https://www.w3.org/TR/uievents-code/#key-numpad-section
    if (modifiers & (u32)UiEventsKeyModifier.mod_keypad) != 0:
        let numpad: ak.String? = match platform_key:
            .key0 => ak.string_from_str("Numpad0")
            .key1 => ak.string_from_str("Numpad1")
            .key2 => ak.string_from_str("Numpad2")
            .key3 => ak.string_from_str("Numpad3")
            .key4 => ak.string_from_str("Numpad4")
            .key5 => ak.string_from_str("Numpad5")
            .key6 => ak.string_from_str("Numpad6")
            .key7 => ak.string_from_str("Numpad7")
            .key8 => ak.string_from_str("Numpad8")
            .key9 => ak.string_from_str("Numpad9")
            .key_plus => ak.string_from_str("NumpadAdd")
            .key_comma => ak.string_from_str("NumpadComma")
            .key_period, .key_delete => ak.string_from_str("NumpadDecimal")
            .key_slash => ak.string_from_str("NumpadDivide")
            .key_return => ak.string_from_str("NumpadEnter")
            .key_asterisk => ak.string_from_str("NumpadMultiply")
            .key_minus => ak.string_from_str("NumpadSubtract")
            .key_equal => ak.string_from_str("NumpadEqual")
            .key_hashtag => ak.string_from_str("NumpadHash")
            .key_left_paren => ak.string_from_str("NumpadParenLeft")
            .key_right_paren => ak.string_from_str("NumpadParenRight")
            _ => none
        if let found = numpad:
            return found

    # (The donor's comments name the spec's sections: 3.1.1 Writing System Keys, 3.1.2 Functional Keys,
    # 3.2 Control Pad, 3.3 Arrow Pad, 3.4 Numpad, 3.5 Function, 3.6 Media, 3.7 Legacy, Non-Standard and
    # Special Keys; its FIXMEs: IntlBackslash, IntlRo, IntlYen, Help, and "Are these correct?" for the
    # GUI keys.)
    return match platform_key:
        .key_backtick, .key_tilde => ak.string_from_str("Backquote")
        .key_backslash, .key_pipe => ak.string_from_str("Backslash")
        .key_left_brace, .key_left_bracket => ak.string_from_str("BracketLeft")
        .key_right_brace, .key_right_bracket => ak.string_from_str("BracketRight")
        .key_comma, .key_less_than => ak.string_from_str("Comma")
        .key0, .key_right_paren => ak.string_from_str("Digit0")
        .key1, .key_exclamation_point => ak.string_from_str("Digit1")
        .key2, .key_at_sign => ak.string_from_str("Digit2")
        .key3, .key_hashtag => ak.string_from_str("Digit3")
        .key4, .key_dollar => ak.string_from_str("Digit4")
        .key5, .key_percent => ak.string_from_str("Digit5")
        .key6, .key_circumflex => ak.string_from_str("Digit6")
        .key7, .key_ampersand => ak.string_from_str("Digit7")
        .key8, .key_asterisk => ak.string_from_str("Digit8")
        .key9, .key_left_paren => ak.string_from_str("Digit9")
        .key_equal, .key_plus => ak.string_from_str("Equal")
        .key_a => ak.string_from_str("KeyA")
        .key_b => ak.string_from_str("KeyB")
        .key_c => ak.string_from_str("KeyC")
        .key_d => ak.string_from_str("KeyD")
        .key_e => ak.string_from_str("KeyE")
        .key_f => ak.string_from_str("KeyF")
        .key_g => ak.string_from_str("KeyG")
        .key_h => ak.string_from_str("KeyH")
        .key_i => ak.string_from_str("KeyI")
        .key_j => ak.string_from_str("KeyJ")
        .key_k => ak.string_from_str("KeyK")
        .key_l => ak.string_from_str("KeyL")
        .key_m => ak.string_from_str("KeyM")
        .key_n => ak.string_from_str("KeyN")
        .key_o => ak.string_from_str("KeyO")
        .key_p => ak.string_from_str("KeyP")
        .key_q => ak.string_from_str("KeyQ")
        .key_r => ak.string_from_str("KeyR")
        .key_s => ak.string_from_str("KeyS")
        .key_t => ak.string_from_str("KeyT")
        .key_u => ak.string_from_str("KeyU")
        .key_v => ak.string_from_str("KeyV")
        .key_w => ak.string_from_str("KeyW")
        .key_x => ak.string_from_str("KeyX")
        .key_y => ak.string_from_str("KeyY")
        .key_z => ak.string_from_str("KeyZ")
        .key_minus, .key_underscore => ak.string_from_str("Minus")
        .key_period, .key_greater_than => ak.string_from_str("Period")
        .key_apostrophe, .key_double_quote => ak.string_from_str("Quote")
        .key_semicolon, .key_colon => ak.string_from_str("Semicolon")
        .key_slash, .key_question_mark => ak.string_from_str("Slash")
        .key_left_alt => ak.string_from_str("AltLeft")
        .key_right_alt => ak.string_from_str("AltRight")
        .key_alt_gr => ak.string_from_str("AltGraph")
        .key_backspace => ak.string_from_str("Backspace")
        .key_caps_lock => ak.string_from_str("CapsLock")
        .key_menu => ak.string_from_str("ContextMenu")
        .key_left_control => ak.string_from_str("ControlLeft")
        .key_right_control => ak.string_from_str("ControlRight")
        .key_return => ak.string_from_str("Enter")
        .key_left_super => ak.string_from_str("MetaLeft")
        .key_right_super => ak.string_from_str("MetaRight")
        .key_left_shift => ak.string_from_str("ShiftLeft")
        .key_right_shift => ak.string_from_str("ShiftRight")
        .key_space => ak.string_from_str("Space")
        .key_tab => ak.string_from_str("Tab")
        .key_delete => ak.string_from_str("Delete")
        .key_end => ak.string_from_str("End")
        .key_home => ak.string_from_str("Home")
        .key_insert => ak.string_from_str("Insert")
        .key_page_down => ak.string_from_str("PageDown")
        .key_page_up => ak.string_from_str("PageUp")
        .key_down => ak.string_from_str("ArrowDown")
        .key_left => ak.string_from_str("ArrowLeft")
        .key_right => ak.string_from_str("ArrowRight")
        .key_up => ak.string_from_str("ArrowUp")
        .key_num_lock => ak.string_from_str("NumLock")
        .key_escape => ak.string_from_str("Escape")
        .key_f1 => ak.string_from_str("F1")
        .key_f2 => ak.string_from_str("F2")
        .key_f3 => ak.string_from_str("F3")
        .key_f4 => ak.string_from_str("F4")
        .key_f5 => ak.string_from_str("F5")
        .key_f6 => ak.string_from_str("F6")
        .key_f7 => ak.string_from_str("F7")
        .key_f8 => ak.string_from_str("F8")
        .key_f9 => ak.string_from_str("F9")
        .key_f10 => ak.string_from_str("F10")
        .key_f11 => ak.string_from_str("F11")
        .key_f12 => ak.string_from_str("F12")
        .key_print_screen, .key_sys_rq => ak.string_from_str("PrintScreen")
        .key_scroll_lock => ak.string_from_str("ScrollLock")
        .key_pause_break => ak.string_from_str("Pause")
        .key_browser_search => ak.string_from_str("BrowserSearch")
        .key_browser_favorites => ak.string_from_str("BrowserFavorites")
        .key_browser_home => ak.string_from_str("BrowserHome")
        .key_previous_track => ak.string_from_str("PreviousTrack")
        .key_browser_back => ak.string_from_str("BrowserBack")
        .key_browser_forward => ak.string_from_str("BrowserForward")
        .key_browser_refresh => ak.string_from_str("BrowserRefresh")
        .key_browser_stop => ak.string_from_str("BrowserStop")
        .key_volume_down => ak.string_from_str("AudioVolumeDown")
        .key_volume_up => ak.string_from_str("AudioVolumeUp")
        .key_wake => ak.string_from_str("WakeUp")
        .key_sleep => ak.string_from_str("Sleep")
        .key_next_track => ak.string_from_str("NextTrack")
        .key_media_select => ak.string_from_str("MediaSelect")
        .key_email => ak.string_from_str("LaunchMail")
        .key_power => ak.string_from_str("Power")
        .key_stop => ak.string_from_str("MediaStop")
        .key_play_pause => ak.string_from_str("MediaPlayPause")
        .key_mute => ak.string_from_str("AudioVolumeMute")
        .key_calculator => ak.string_from_str("LaunchApp2")
        .key_my_computer => ak.string_from_str("LaunchApp1")
        .key_left_gui => ak.string_from_str("LaunchApp2")
        .key_right_gui, .key_apps => ak.string_from_str("LaunchApp1")
        .key_invalid => ak.string_from_str("Unidentified")
        _ => trap("VERIFY_NOT_REACHED")

## Ported from KeyboardEvent.cpp:678 get_event_location.
## 5.6.2. Keyboard Event Key Location, https://www.w3.org/TR/uievents/#events-keyboard-key-location
func ui_events_get_event_location(platform_key: UiEventsKeyCode, modifiers: u32) -> UiEventsDomKeyLocation:
    if (modifiers & (u32)UiEventsKeyModifier.mod_keypad) != 0:
        return UiEventsDomKeyLocation.numpad

    return match platform_key:
        .key_left_alt, .key_left_control, .key_left_shift, .key_left_super => UiEventsDomKeyLocation.left
        .key_right_alt, .key_right_control, .key_right_shift, .key_right_super => UiEventsDomKeyLocation.right
        _ => UiEventsDomKeyLocation.standard

# mark: The event ===============================================================================

## Ported from KeyboardEvent.cpp:701 Web::UIEvents::KeyboardEvent::create_from_platform_event (the event as its DOM::Event).
pub func ui_events_keyboard_event_create_from_platform_event(realm: Realm*, event_name: ak.FlyString, platform_key: UiEventsKeyCode, modifiers: u32, code_point: u32, repeat: bool) -> DomEvent*:
    let event_key = ui_events_get_event_key(platform_key, code_point, modifiers)
    let event_code = ui_events_get_event_code(platform_key, modifiers)
    let key_code = ui_events_determine_key_code(platform_key, code_point)
    let char_code = ui_events_determine_char_code(event_name, code_point)

    var event_init = UiEventsKeyboardEventInit()
    event_init.key = event_key
    event_init.code = event_code
    event_init.location = (u32)ui_events_get_event_location(platform_key, modifiers)
    event_init.event_modifier_init.ctrl_key = ui_events_has_modifier(modifiers, UiEventsKeyModifier.mod_ctrl)
    event_init.event_modifier_init.shift_key = ui_events_has_modifier(modifiers, UiEventsKeyModifier.mod_shift)
    event_init.event_modifier_init.alt_key = ui_events_has_modifier(modifiers, UiEventsKeyModifier.mod_alt)
    event_init.event_modifier_init.meta_key = ui_events_has_modifier(modifiers, UiEventsKeyModifier.mod_super)
    event_init.repeat = repeat
    event_init.is_composing = false
    event_init.key_code = key_code
    event_init.char_code = char_code
    event_init.event_modifier_init.ui_event_init.event_init.bubbles = true
    event_init.event_modifier_init.ui_event_init.event_init.cancelable = true
    event_init.event_modifier_init.ui_event_init.event_init.composed = true

    let event = ui_events_keyboard_event_create(realm, event_name, event_init)
    dom_event_set_is_trusted(event.dom_event(), true)
    return event.dom_event()

## Ported from KeyboardEvent.cpp:729 Web::UIEvents::KeyboardEvent::get_modifier_state.
pub func ui_events_keyboard_event_get_modifier_state(this: UiEventsKeyboardEvent*, key_arg: const ak.String*) -> bool:
    if ak.string_eq_string_view(key_arg, ak.sv("Control")): return this.m_ctrl_key
    if ak.string_eq_string_view(key_arg, ak.sv("Shift")): return this.m_shift_key
    if ak.string_eq_string_view(key_arg, ak.sv("Alt")): return this.m_alt_key
    if ak.string_eq_string_view(key_arg, ak.sv("Meta")): return this.m_meta_key
    if ak.string_eq_string_view(key_arg, ak.sv("AltGraph")): return this.m_modifier_alt_graph
    if ak.string_eq_string_view(key_arg, ak.sv("CapsLock")): return this.m_modifier_caps_lock
    if ak.string_eq_string_view(key_arg, ak.sv("Fn")): return this.m_modifier_fn
    if ak.string_eq_string_view(key_arg, ak.sv("FnLock")): return this.m_modifier_fn_lock
    if ak.string_eq_string_view(key_arg, ak.sv("Hyper")): return this.m_modifier_hyper
    if ak.string_eq_string_view(key_arg, ak.sv("NumLock")): return this.m_modifier_num_lock
    if ak.string_eq_string_view(key_arg, ak.sv("ScrollLock")): return this.m_modifier_scroll_lock
    if ak.string_eq_string_view(key_arg, ak.sv("Super")): return this.m_modifier_super
    if ak.string_eq_string_view(key_arg, ak.sv("Symbol")): return this.m_modifier_symbol
    if ak.string_eq_string_view(key_arg, ak.sv("SymbolLock")): return this.m_modifier_symbol_lock
    return false

## Ported from KeyboardEvent.cpp:763 Web::UIEvents::KeyboardEvent::init_keyboard_event.
## https://w3c.github.io/uievents/#dom-keyboardevent-initkeyboardevent
pub func ui_events_keyboard_event_init_keyboard_event(this: UiEventsKeyboardEvent*, type_: ak.String, bubbles: bool, cancelable: bool, view: HtmlWindowProxy*?, key: ak.String, location: u32,
                                                      ctrl_key: bool, alt_key: bool, shift_key: bool, meta_key: bool):
    # Initializes attributes of a KeyboardEvent object. This method has the same behavior as UIEvent.initUIEvent().
    # The value of detail remains undefined.

    # 1. If this’s dispatch flag is set, then return.
    if dom_event_dispatched(this.dom_event()):
        return

    # 2. Initialize this with type, bubbles, and cancelable.
    dom_event_initialize_event(this.dom_event(), type_, bubbles, cancelable)

    # Implementation Defined: Initialise other values.
    this.ui_event.m_view = view
    this.m_key = key
    this.m_location = location
    this.m_ctrl_key = ctrl_key
    this.m_alt_key = alt_key
    this.m_shift_key = shift_key
    this.m_meta_key = meta_key

## Ported from KeyboardEvent.cpp:785 Web::UIEvents::KeyboardEvent::create.
pub func ui_events_keyboard_event_create(realm: Realm*, event_name: ak.FlyString, event_init: UiEventsKeyboardEventInit = UiEventsKeyboardEventInit()) -> UiEventsKeyboardEvent*:
    return realm_create_ui_events_keyboard_event(realm, realm, event_name, event_init)

## Ported from KeyboardEvent.cpp:790 Web::UIEvents::KeyboardEvent::construct_impl.
pub func ui_events_keyboard_event_construct_impl(realm: Realm*, event_name: ak.FlyString, event_init: UiEventsKeyboardEventInit) -> UiEventsKeyboardEvent*!:
    return ui_events_keyboard_event_create(realm, event_name, event_init)

## Ported from KeyboardEvent.cpp:795 Web::UIEvents::KeyboardEvent::KeyboardEvent.
pub func ui_events_keyboard_event_construct(this: UiEventsKeyboardEvent*, realm: Realm*, event_name: ak.FlyString, event_init: UiEventsKeyboardEventInit):
    let modifier_init = event_init.event_modifier_init
    ui_events_ui_event_construct_ui_event_init(this.ui_events_ui_event(), realm, event_name, modifier_init.ui_event_init)
    this.m_key = event_init.key
    this.m_code = event_init.code
    this.m_location = event_init.location
    this.m_ctrl_key = modifier_init.ctrl_key
    this.m_shift_key = modifier_init.shift_key
    this.m_alt_key = modifier_init.alt_key
    this.m_meta_key = modifier_init.meta_key
    this.m_modifier_alt_graph = modifier_init.modifier_alt_graph
    this.m_modifier_caps_lock = modifier_init.modifier_caps_lock
    this.m_modifier_fn = modifier_init.modifier_fn
    this.m_modifier_fn_lock = modifier_init.modifier_fn_lock
    this.m_modifier_hyper = modifier_init.modifier_hyper
    this.m_modifier_num_lock = modifier_init.modifier_num_lock
    this.m_modifier_scroll_lock = modifier_init.modifier_scroll_lock
    this.m_modifier_super = modifier_init.modifier_super
    this.m_modifier_symbol = modifier_init.modifier_symbol
    this.m_modifier_symbol_lock = modifier_init.modifier_symbol_lock
    this.m_repeat = event_init.repeat
    this.m_is_composing = event_init.is_composing
    this.m_key_code = event_init.key_code
    this.m_char_code = event_init.char_code

## Ported from KeyboardEvent.cpp:823 Web::UIEvents::KeyboardEvent::initialize (override).
pub func ui_events_keyboard_event_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.keyboard_event)
    ui_events_ui_event_initialize_impl(base, realm)

# mark: Accessors ===============================================================================

## Ported from KeyboardEvent.h:44 Web::UIEvents::KeyboardEvent::key_code.
pub func ui_events_keyboard_event_key_code(this: UiEventsKeyboardEvent*) -> u32:
    return this.m_key_code

## Ported from KeyboardEvent.h:45 Web::UIEvents::KeyboardEvent::char_code.
pub func ui_events_keyboard_event_char_code(this: UiEventsKeyboardEvent*) -> u32:
    return this.m_char_code

## Ported from KeyboardEvent.h:47 Web::UIEvents::KeyboardEvent::key.
pub func ui_events_keyboard_event_key(this: UiEventsKeyboardEvent*) -> ak.String:
    return this.m_key

## Ported from KeyboardEvent.h:48 Web::UIEvents::KeyboardEvent::code.
pub func ui_events_keyboard_event_code(this: UiEventsKeyboardEvent*) -> ak.String:
    return this.m_code

## Ported from KeyboardEvent.h:50 Web::UIEvents::KeyboardEvent::location.
pub func ui_events_keyboard_event_location(this: UiEventsKeyboardEvent*) -> u32:
    return this.m_location

## Ported from KeyboardEvent.h:52 Web::UIEvents::KeyboardEvent::ctrl_key.
pub func ui_events_keyboard_event_ctrl_key(this: UiEventsKeyboardEvent*) -> bool:
    return this.m_ctrl_key

## Ported from KeyboardEvent.h:53 Web::UIEvents::KeyboardEvent::shift_key.
pub func ui_events_keyboard_event_shift_key(this: UiEventsKeyboardEvent*) -> bool:
    return this.m_shift_key

## Ported from KeyboardEvent.h:54 Web::UIEvents::KeyboardEvent::alt_key.
pub func ui_events_keyboard_event_alt_key(this: UiEventsKeyboardEvent*) -> bool:
    return this.m_alt_key

## Ported from KeyboardEvent.h:55 Web::UIEvents::KeyboardEvent::meta_key.
pub func ui_events_keyboard_event_meta_key(this: UiEventsKeyboardEvent*) -> bool:
    return this.m_meta_key

## Ported from KeyboardEvent.h:56 Web::UIEvents::KeyboardEvent::repeat.
pub func ui_events_keyboard_event_repeat(this: UiEventsKeyboardEvent*) -> bool:
    return this.m_repeat

## Ported from KeyboardEvent.h:57 Web::UIEvents::KeyboardEvent::is_composing.
pub func ui_events_keyboard_event_is_composing(this: UiEventsKeyboardEvent*) -> bool:
    return this.m_is_composing

## Ported from KeyboardEvent.h:61 Web::UIEvents::KeyboardEvent::which (override).
pub func ui_events_keyboard_event_which_impl(base: UiEventsUiEvent*) -> u32:
    return ((UiEventsKeyboardEvent*)base).m_key_code