AeonGUI
A portable video game graphic user interface library.
Loading...
Searching...
No Matches
EventTarget.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_EVENTTARGET_H
17#define AEONGUI_DOM_EVENTTARGET_H
18#include <cstdint>
19#include <optional>
20#include <variant>
21#include <vector>
22#include <functional>
23#include <unordered_map>
24#include "aeongui/Platform.hpp"
25#include "AnyType.hpp"
26#include "DOMString.hpp"
27#include "EventListener.hpp"
28
29namespace AeonGUI
30{
31 namespace DOM
32 {
33 class AbortSignal;
34 class Event;
35 class EventTarget;
37 using EventHandler = std::function<void ( Event& ) >;
40 {
41 bool capture{false};
42 };
43
46 {
47 bool passive;
48 bool once{false};
49 AbortSignal* signal{nullptr};
50 };
51
58 {
59 public:
61 DLL virtual ~EventTarget() = 0;
67 DLL void addEventListener ( const DOMString& type, EventListener* callback, const std::variant<std::monostate, AddEventListenerOptions, bool>& options = {} );
73 DLL void removeEventListener ( const DOMString& type, EventListener* callback, const std::variant<std::monostate, EventListenerOptions, bool>& options = {} );
78 DLL virtual bool dispatchEvent ( Event& event );
79 private:
81 struct RegisteredListener
82 {
83 EventListener* callback{nullptr};
84 bool capture{false};
85 bool once{false};
86 bool passive{false};
87 };
92 void invokeListeners ( Event& event, uint16_t phase );
93 std::unordered_map<DOMString, std::vector<RegisteredListener>> mEventListeners{};
94 };
95
101 class AbortSignal :
103 {
104 public:
109 static AbortSignal abort ( const std::optional<AnyType>& reason = std::nullopt );
114 static AbortSignal timeout ( uint64_t milliseconds );
119 static AbortSignal any ( std::vector<AbortSignal> signals );
120
123 bool aborted() const;
126 const AnyType & reason() const;
129
130 EventHandler onabort;
132 virtual ~AbortSignal() = default;
133 private:
134 AbortSignal() = default;
135 bool m_aborted;
136 AnyType m_reason;
137 };
138 }
139}
140#endif
Platform-specific DLL import/export macros and compiler helpers.
Signal that can abort an asynchronous operation.
Definition EventTarget.hpp:103
const AnyType & reason() const
Get the reason for the abort.
static AbortSignal abort(const std::optional< AnyType > &reason=std::nullopt)
Create an already-aborted signal.
bool aborted() const
Check whether the signal has been aborted.
void throwIfAborted()
Throw if the signal has been aborted.
virtual ~AbortSignal()=default
Destructor.
static AbortSignal any(std::vector< AbortSignal > signals)
Create a signal that aborts when any of the given signals abort.
EventHandler onabort
Handler invoked when the signal is aborted.
Definition EventTarget.hpp:130
static AbortSignal timeout(uint64_t milliseconds)
Create a signal that will abort after a timeout.
Represents a DOM event.
Definition Event.hpp:47
Interface for objects that handle DOM events.
Definition EventListener.hpp:32
Base class for objects that can receive DOM events.
Definition EventTarget.hpp:58
virtual ~EventTarget()=0
Virtual destructor.
void removeEventListener(const DOMString &type, EventListener *callback, const std::variant< std::monostate, EventListenerOptions, bool > &options={})
Unregister an event listener.
Definition EventTarget.cpp:52
virtual bool dispatchEvent(Event &event)
Dispatch an event to this target.
Definition EventTarget.cpp:112
void addEventListener(const DOMString &type, EventListener *callback, const std::variant< std::monostate, AddEventListenerOptions, bool > &options={})
Register an event listener.
Definition EventTarget.cpp:25
Extended options for addEventListener.
Definition EventTarget.hpp:46
bool passive
If true, the listener will not call preventDefault.
Definition EventTarget.hpp:47
AbortSignal * signal
An AbortSignal that can remove the listener.
Definition EventTarget.hpp:49
bool once
If true, the listener is automatically removed after firing.
Definition EventTarget.hpp:48
Options for addEventListener / removeEventListener.
Definition EventTarget.hpp:40
bool capture
If true, listen during the capture phase.
Definition EventTarget.hpp:41