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 virtual ~EventTarget() = 0;
67 void addEventListener ( const DOMString& type, EventListener* callback, const std::variant<std::monostate, AddEventListenerOptions, bool>& options = {} );
73 void removeEventListener ( const DOMString& type, EventListener* callback, const std::variant<std::monostate, EventListenerOptions, bool>& options = {} );
78 virtual bool dispatchEvent ( Event& event );
79 private:
80 std::unordered_map<DOMString, std::vector<EventListener* >> mEventListeners{};
81 };
82
88 class AbortSignal :
90 {
91 public:
96 static AbortSignal abort ( const std::optional<AnyType>& reason = std::nullopt );
101 static AbortSignal timeout ( uint64_t milliseconds );
106 static AbortSignal any ( std::vector<AbortSignal> signals );
107
110 bool aborted() const;
113 const AnyType & reason() const;
116
117 EventHandler onabort;
119 virtual ~AbortSignal() = default;
120 private:
121 AbortSignal() = default;
122 bool m_aborted;
123 AnyType m_reason;
124 };
125 }
126}
127#endif
Platform-specific DLL import/export macros and compiler helpers.
Signal that can abort an asynchronous operation.
Definition EventTarget.hpp:90
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:117
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:31
virtual bool dispatchEvent(Event &event)
Dispatch an event to this target.
Definition EventTarget.cpp:44
void addEventListener(const DOMString &type, EventListener *callback, const std::variant< std::monostate, AddEventListenerOptions, bool > &options={})
Register an event listener.
Definition EventTarget.cpp:23
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