Skip to content

Truly independent web browser

3.1 KB
Raw Blame
/*
 * Copyright (c) 2021-2022, Andreas Kling <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/TypeCasts.h>
#include <LibWeb/Bindings/KeyboardEvent.h>
#include <LibWeb/UIEvents/UIEvent.h>
#include <LibWebCommon/HighResolutionTime/DOMHighResTimeStamp.h>
#include <LibWebCommon/UIEvents/KeyCode.h>

namespace Web::HTML {

class Window;

}

namespace Web::UIEvents {

enum class DOMKeyLocation {
    Standard = 0,
    Left = 1,
    Right = 2,
    Numpad = 3,
};

using KeyboardEventInit = Bindings::KeyboardEventInit;

// https://www.w3.org/TR/uievents/#interface-keyboardevent
class KeyboardEvent final : public UIEvent {
    WEB_WRAPPABLE(KeyboardEvent, UIEvent);
    GC_DECLARE_ALLOCATOR(KeyboardEvent);

public:
    [[nodiscard]] static GC::Ref<KeyboardEvent> create(Utf16FlyString const& event_name, KeyboardEventInit const&, HighResolutionTime::DOMHighResTimeStamp);
    [[nodiscard]] static GC::Ref<KeyboardEvent> create_from_platform_event(JS::Object const& relevant_global_object, FlyString const& event_name, KeyCode, unsigned modifiers, u32 code_point, bool repeat);
    [[nodiscard]] static GC::Ref<KeyboardEvent> create_from_platform_event(JS::Object const& relevant_global_object, Utf16FlyString const& event_name, KeyCode, unsigned modifiers, u32 code_point, bool repeat);

    virtual ~KeyboardEvent() override;

    u32 key_code() const { return m_key_code; }
    void set_key_code(u32 key_code) { m_key_code = key_code; }
    u32 char_code() const { return m_char_code; }

    Utf16String const& key() const { return m_key; }
    Utf16FlyString const& code() const { return m_code; }
    u32 location() const { return m_location; }

    bool ctrl_key() const { return m_ctrl_key; }
    bool shift_key() const { return m_shift_key; }
    bool alt_key() const { return m_alt_key; }
    bool meta_key() const { return m_meta_key; }

    bool repeat() const { return m_repeat; }
    bool is_composing() const { return m_is_composing; }

    bool get_modifier_state(Utf16FlyString const& key_arg) const;

    virtual u32 which() const override { return m_key_code; }

    void init_keyboard_event(Utf16FlyString const& type, bool bubbles, bool cancelable, GC::Ptr<HTML::WindowProxy> view, Utf16String const& key, WebIDL::UnsignedLong location, bool ctrl_key, bool alt_key, bool shift_key, bool meta_key);

private:
    KeyboardEvent(Utf16FlyString const& event_name, KeyboardEventInit const& event_init, HighResolutionTime::DOMHighResTimeStamp);

    Utf16String m_key;
    Utf16FlyString m_code;
    u32 m_location { 0 };
    bool m_ctrl_key { false };
    bool m_shift_key { false };
    bool m_alt_key { false };
    bool m_meta_key { false };
    bool m_modifier_alt_graph { false };
    bool m_modifier_caps_lock { false };
    bool m_modifier_fn { false };
    bool m_modifier_fn_lock { false };
    bool m_modifier_hyper { false };
    bool m_modifier_num_lock { false };
    bool m_modifier_scroll_lock { false };
    bool m_modifier_super { false };
    bool m_modifier_symbol { false };
    bool m_modifier_symbol_lock { false };
    bool m_repeat { false };
    bool m_is_composing { false };
    u32 m_key_code { 0 };
    u32 m_char_code { 0 };
};

}