AeonGUI
A portable video game graphic user interface library.
Loading...
Searching...
No Matches
Event.hpp
1/*
2Copyright (C) 2025,2026 Rodrigo Jose Hernandez Cordoba
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16#ifndef AEONGUI_DOM_EVENT_H
17#define AEONGUI_DOM_EVENT_H
18#include <cstdint>
19#include <optional>
20#include <vector>
21#include <chrono>
22#include "aeongui/Platform.hpp"
23#include "DOMString.hpp"
24#include "EventTarget.hpp"
25
26namespace AeonGUI
27{
28 namespace DOM
29 {
31 using DOMHighResTimeStamp = std::chrono::time_point<std::chrono::high_resolution_clock>;
33 struct EventInit
34 {
35 bool bubbles{false};
36 bool cancelable{false};
37 bool composed{false};
38 };
39
46 class Event
47 {
48 public:
53 DLL Event ( const DOMString& type, const std::optional<EventInit>& eventInitDict = {} );
55 virtual ~Event() = default;
58 DLL const std::vector<EventTarget*>& composedPath() const;
60 DLL void stopPropagation();
62 DLL void stopImmediatePropagation();
64 DLL void preventDefault();
65
66 const uint16_t NONE{0};
67 const uint16_t CAPTURING_PHASE{1};
68 const uint16_t AT_TARGET{2};
69 const uint16_t BUBBLING_PHASE{3};
70
73 constexpr const DOMString& type() const
74 {
75 return m_type;
76 }
77
79 constexpr const EventTarget* const target() const
80 {
81 return m_target;
82 }
83
85 constexpr const EventTarget* const currentTarget() const
86 {
87 return m_currentTarget;
88 }
89
91 constexpr uint16_t eventPhase() const
92 {
93 return m_eventPhase;
94 }
95
97 constexpr bool bubbles() const
98 {
99 return m_bubbles;
100 }
101
103 constexpr bool cancelable() const
104 {
105 return m_cancelable;
106 }
107
109 constexpr bool defaultPrevented() const
110 {
111 return m_defaultPrevented;
112 }
113
115 constexpr bool composed() const
116 {
117 return m_composed;
118 }
119
121 constexpr bool isTrusted() const
122 {
123 return m_isTrusted;
124 }
125
127 constexpr const DOMHighResTimeStamp& timeStamp() const
128 {
129 return m_timeStamp;
130 }
131
132 protected:
136 DLL void setTrusted ( bool trusted );
137 private:
138 friend class EventTarget;
139 DOMString m_type;
140 EventTarget* m_target{};
141 EventTarget* m_currentTarget{};
142 std::vector<EventTarget*> m_composedPath{};
143 uint16_t m_eventPhase{NONE};
144 bool m_bubbles{};
145 bool m_cancelable{};
146 bool m_defaultPrevented{};
147 bool m_composed{};
148 bool m_isTrusted{};
149 bool m_stopPropagation{false};
150 bool m_stopImmediatePropagation{false};
151 DOMHighResTimeStamp m_timeStamp{};
152 };
153 }
154}
155#endif
Platform-specific DLL import/export macros and compiler helpers.
constexpr bool composed() const
Check whether the event is composed.
Definition Event.hpp:115
const uint16_t AT_TARGET
At-target phase.
Definition Event.hpp:68
const uint16_t CAPTURING_PHASE
Capture phase.
Definition Event.hpp:67
constexpr uint16_t eventPhase() const
Get the current event phase.
Definition Event.hpp:91
constexpr bool bubbles() const
Check whether the event bubbles.
Definition Event.hpp:97
virtual ~Event()=default
Virtual destructor for proper polymorphic cleanup.
const std::vector< EventTarget * > & composedPath() const
Build the composed path for this event.
Definition Event.cpp:29
constexpr const EventTarget *const currentTarget() const
Get the current target during dispatch.
Definition Event.hpp:85
void stopPropagation()
Stop the event from propagating further.
Definition Event.cpp:33
constexpr bool isTrusted() const
Check whether the event was generated by the user agent.
Definition Event.hpp:121
constexpr const DOMString & type() const
Get the event type.
Definition Event.hpp:73
const uint16_t NONE
No phase.
Definition Event.hpp:66
const uint16_t BUBBLING_PHASE
Bubble phase.
Definition Event.hpp:69
constexpr const EventTarget *const target() const
Get the target of the event.
Definition Event.hpp:79
void stopImmediatePropagation()
Stop the event from propagating and prevent other listeners on the same target.
Definition Event.cpp:37
constexpr const DOMHighResTimeStamp & timeStamp() const
Get the event's creation timestamp.
Definition Event.hpp:127
constexpr bool defaultPrevented() const
Check whether the default action was prevented.
Definition Event.hpp:109
void preventDefault()
Cancel the event's default action, if cancelable.
Definition Event.cpp:42
void setTrusted(bool trusted)
Set the trusted flag (for user-agent generated events).
Definition Event.cpp:49
constexpr bool cancelable() const
Check whether the event is cancelable.
Definition Event.hpp:103
Event(const DOMString &type, const std::optional< EventInit > &eventInitDict={})
Construct an event of the given type.
Definition Event.cpp:21
Base class for objects that can receive DOM events.
Definition EventTarget.hpp:58
Initialization dictionary for Event construction.
Definition Event.hpp:34
bool cancelable
Whether the event can be cancelled.
Definition Event.hpp:36
bool bubbles
Whether the event bubbles up the DOM tree.
Definition Event.hpp:35
bool composed
Whether the event crosses shadow DOM boundaries.
Definition Event.hpp:37