Skip to content

Commit 550c9ce

Browse files
committed
Refactor window event classes into window_event.h
1 parent 166c1b5 commit 550c9ce

File tree

3 files changed

+330
-188
lines changed

3 files changed

+330
-188
lines changed

include/nativeapi.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
#include "../src/event.h"
99
#include "../src/event_dispatcher.h"
1010
#include "../src/geometry.h"
11+
#include "../src/keyboard_event.h"
1112
#include "../src/keyboard_monitor.h"
1213
#include "../src/menu.h"
1314
#include "../src/tray_icon.h"
1415
#include "../src/tray_manager.h"
1516
#include "../src/window.h"
17+
#include "../src/window_event.h"
1618
#include "../src/window_manager.h"
1719

1820
// Include necessary headers for C API

src/window_event.h

Lines changed: 327 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,327 @@
1+
#pragma once
2+
#include <string>
3+
#include <typeindex>
4+
#include "event.h"
5+
#include "geometry.h"
6+
#include "window.h"
7+
8+
namespace nativeapi {
9+
10+
/**
11+
* Base class for all window-related events
12+
*
13+
* This class provides common functionality for window events,
14+
* including access to the window ID that triggered the event.
15+
*/
16+
class WindowEvent : public Event {
17+
public:
18+
/**
19+
* Constructor for WindowEvent
20+
* @param window_id The window ID associated with this event
21+
*/
22+
explicit WindowEvent(WindowID window_id) : window_id_(window_id) {}
23+
24+
/**
25+
* Virtual destructor
26+
*/
27+
virtual ~WindowEvent() = default;
28+
29+
/**
30+
* Get the window ID associated with this event
31+
* @return The window ID
32+
*/
33+
WindowID GetWindowId() const { return window_id_; }
34+
35+
/**
36+
* Get a string representation of the event type (for debugging)
37+
* Default implementation returns "WindowEvent"
38+
*/
39+
std::string GetTypeName() const override { return "WindowEvent"; }
40+
41+
private:
42+
WindowID window_id_;
43+
};
44+
45+
/**
46+
* Event class for window creation
47+
*
48+
* This event is emitted when a new window is successfully created.
49+
*/
50+
class WindowCreatedEvent : public WindowEvent {
51+
public:
52+
explicit WindowCreatedEvent(WindowID window_id) : WindowEvent(window_id) {}
53+
54+
/**
55+
* Get a string representation of the event type
56+
*/
57+
std::string GetTypeName() const override { return "WindowCreatedEvent"; }
58+
59+
/**
60+
* Get the static type index for this event type
61+
*/
62+
static std::type_index GetStaticType() {
63+
return std::type_index(typeid(WindowCreatedEvent));
64+
}
65+
66+
/**
67+
* Get the type index for this event instance
68+
*/
69+
std::type_index GetType() const {
70+
return GetStaticType();
71+
}
72+
};
73+
74+
/**
75+
* Event class for window closure
76+
*
77+
* This event is emitted when a window is closed or destroyed.
78+
*/
79+
class WindowClosedEvent : public WindowEvent {
80+
public:
81+
explicit WindowClosedEvent(WindowID window_id) : WindowEvent(window_id) {}
82+
83+
/**
84+
* Get a string representation of the event type
85+
*/
86+
std::string GetTypeName() const override { return "WindowClosedEvent"; }
87+
88+
/**
89+
* Get the static type index for this event type
90+
*/
91+
static std::type_index GetStaticType() {
92+
return std::type_index(typeid(WindowClosedEvent));
93+
}
94+
95+
/**
96+
* Get the type index for this event instance
97+
*/
98+
std::type_index GetType() const {
99+
return GetStaticType();
100+
}
101+
};
102+
103+
/**
104+
* Event class for window focus gained
105+
*
106+
* This event is emitted when a window gains focus and becomes the active window.
107+
*/
108+
class WindowFocusedEvent : public WindowEvent {
109+
public:
110+
explicit WindowFocusedEvent(WindowID window_id) : WindowEvent(window_id) {}
111+
112+
/**
113+
* Get a string representation of the event type
114+
*/
115+
std::string GetTypeName() const override { return "WindowFocusedEvent"; }
116+
117+
/**
118+
* Get the static type index for this event type
119+
*/
120+
static std::type_index GetStaticType() {
121+
return std::type_index(typeid(WindowFocusedEvent));
122+
}
123+
124+
/**
125+
* Get the type index for this event instance
126+
*/
127+
std::type_index GetType() const {
128+
return GetStaticType();
129+
}
130+
};
131+
132+
/**
133+
* Event class for window focus lost
134+
*
135+
* This event is emitted when a window loses focus and is no longer the active window.
136+
*/
137+
class WindowBlurredEvent : public WindowEvent {
138+
public:
139+
explicit WindowBlurredEvent(WindowID window_id) : WindowEvent(window_id) {}
140+
141+
/**
142+
* Get a string representation of the event type
143+
*/
144+
std::string GetTypeName() const override { return "WindowBlurredEvent"; }
145+
146+
/**
147+
* Get the static type index for this event type
148+
*/
149+
static std::type_index GetStaticType() {
150+
return std::type_index(typeid(WindowBlurredEvent));
151+
}
152+
153+
/**
154+
* Get the type index for this event instance
155+
*/
156+
std::type_index GetType() const {
157+
return GetStaticType();
158+
}
159+
};
160+
161+
/**
162+
* Event class for window minimized
163+
*
164+
* This event is emitted when a window is minimized to the taskbar or dock.
165+
*/
166+
class WindowMinimizedEvent : public WindowEvent {
167+
public:
168+
explicit WindowMinimizedEvent(WindowID window_id) : WindowEvent(window_id) {}
169+
170+
/**
171+
* Get a string representation of the event type
172+
*/
173+
std::string GetTypeName() const override { return "WindowMinimizedEvent"; }
174+
175+
/**
176+
* Get the static type index for this event type
177+
*/
178+
static std::type_index GetStaticType() {
179+
return std::type_index(typeid(WindowMinimizedEvent));
180+
}
181+
182+
/**
183+
* Get the type index for this event instance
184+
*/
185+
std::type_index GetType() const {
186+
return GetStaticType();
187+
}
188+
};
189+
190+
/**
191+
* Event class for window maximized
192+
*
193+
* This event is emitted when a window is maximized to fill the entire screen.
194+
*/
195+
class WindowMaximizedEvent : public WindowEvent {
196+
public:
197+
explicit WindowMaximizedEvent(WindowID window_id) : WindowEvent(window_id) {}
198+
199+
/**
200+
* Get a string representation of the event type
201+
*/
202+
std::string GetTypeName() const override { return "WindowMaximizedEvent"; }
203+
204+
/**
205+
* Get the static type index for this event type
206+
*/
207+
static std::type_index GetStaticType() {
208+
return std::type_index(typeid(WindowMaximizedEvent));
209+
}
210+
211+
/**
212+
* Get the type index for this event instance
213+
*/
214+
std::type_index GetType() const {
215+
return GetStaticType();
216+
}
217+
};
218+
219+
/**
220+
* Event class for window restored
221+
*
222+
* This event is emitted when a window is restored from minimized or maximized state
223+
* to its normal windowed state.
224+
*/
225+
class WindowRestoredEvent : public WindowEvent {
226+
public:
227+
explicit WindowRestoredEvent(WindowID window_id) : WindowEvent(window_id) {}
228+
229+
/**
230+
* Get a string representation of the event type
231+
*/
232+
std::string GetTypeName() const override { return "WindowRestoredEvent"; }
233+
234+
/**
235+
* Get the static type index for this event type
236+
*/
237+
static std::type_index GetStaticType() {
238+
return std::type_index(typeid(WindowRestoredEvent));
239+
}
240+
241+
/**
242+
* Get the type index for this event instance
243+
*/
244+
std::type_index GetType() const {
245+
return GetStaticType();
246+
}
247+
};
248+
249+
/**
250+
* Event class for window moved
251+
*
252+
* This event is emitted when a window is moved to a new position on the screen.
253+
*/
254+
class WindowMovedEvent : public WindowEvent {
255+
public:
256+
WindowMovedEvent(WindowID window_id, Point new_position)
257+
: WindowEvent(window_id), new_position_(new_position) {}
258+
259+
/**
260+
* Get the new position of the window
261+
* @return The new position as a Point
262+
*/
263+
Point GetNewPosition() const { return new_position_; }
264+
265+
/**
266+
* Get a string representation of the event type
267+
*/
268+
std::string GetTypeName() const override { return "WindowMovedEvent"; }
269+
270+
/**
271+
* Get the static type index for this event type
272+
*/
273+
static std::type_index GetStaticType() {
274+
return std::type_index(typeid(WindowMovedEvent));
275+
}
276+
277+
/**
278+
* Get the type index for this event instance
279+
*/
280+
std::type_index GetType() const {
281+
return GetStaticType();
282+
}
283+
284+
private:
285+
Point new_position_;
286+
};
287+
288+
/**
289+
* Event class for window resized
290+
*
291+
* This event is emitted when a window is resized to a new size.
292+
*/
293+
class WindowResizedEvent : public WindowEvent {
294+
public:
295+
WindowResizedEvent(WindowID window_id, Size new_size)
296+
: WindowEvent(window_id), new_size_(new_size) {}
297+
298+
/**
299+
* Get the new size of the window
300+
* @return The new size as a Size object
301+
*/
302+
Size GetNewSize() const { return new_size_; }
303+
304+
/**
305+
* Get a string representation of the event type
306+
*/
307+
std::string GetTypeName() const override { return "WindowResizedEvent"; }
308+
309+
/**
310+
* Get the static type index for this event type
311+
*/
312+
static std::type_index GetStaticType() {
313+
return std::type_index(typeid(WindowResizedEvent));
314+
}
315+
316+
/**
317+
* Get the type index for this event instance
318+
*/
319+
std::type_index GetType() const {
320+
return GetStaticType();
321+
}
322+
323+
private:
324+
Size new_size_;
325+
};
326+
327+
} // namespace nativeapi

0 commit comments

Comments
 (0)