Loading...
Searching...
No Matches
Window.h
Go to the documentation of this file.
1/**************************************************************************************************
2 * This file is a part of Ultralight. *
3 * *
4 * See <https://ultralig.ht> for licensing and more. *
5 * *
6 * (C) 2024 Ultralight, Inc. *
7 **************************************************************************************************/
8#pragma once
9#include "Defines.h"
10#include <Ultralight/RefPtr.h>
11#include <Ultralight/Listener.h>
12#include <Ultralight/Bitmap.h>
13#include <Ultralight/KeyEvent.h>
16
17namespace ultralight {
18
19class Monitor;
20class OverlayManager;
21class Surface;
22class Window;
23
24///
25/// Interface for all Window-related events. @see Window::set_listener
26///
28public:
29 virtual ~WindowListener() {}
30
31 ///
32 /// Called when the Window is closed.
33 ///
34 virtual void OnClose(ultralight::Window* window) { }
35
36 ///
37 /// Called when the Window is resized.
38 ///
39 /// @param width The new width (in pixels).
40 ///
41 /// @param height The new height (in pixels).
42 ///
43 virtual void OnResize(ultralight::Window* window, uint32_t width_px, uint32_t height_px) { }
44
45 ///
46 /// Called when a keyboard event is fired.
47 ///
48 /// @param evt Details for the event.
49 ///
50 /// @return Return false to consume the event and prevent it from propagating further.
51 ///
52 virtual bool OnKeyEvent(const ultralight::KeyEvent& evt) { return true; }
53
54 ///
55 /// Called when a mouse event is fired.
56 ///
57 /// @param evt Details for the event.
58 ///
59 /// @return Return false to consume the event and prevent it from propagating further.
60 ///
61 virtual bool OnMouseEvent(const ultralight::MouseEvent& evt) { return true; }
62
63 ///
64 /// Called when a scroll event is fired.
65 ///
66 /// @param evt Details for the event.
67 ///
68 /// @return Return false to consume the event and prevent it from propagating further.
69 ///
70 virtual bool OnScrollEvent(const ultralight::ScrollEvent& evt) { return true; }
71};
72
73///
74/// Window creation flags. @see Window::Create
75///
83
84///
85/// A platform-specific window.
86///
87/// This class describes a platform-specific window and provides methods for managing it.
88///
89/// ## Creating a Window
90///
91/// To create a new Window, use the static Window::Create method:
92///
93/// ```
94/// auto window = Window::Create(monitor, 1024, 768, false, kWindowFlags_Titled);
95/// ```
96///
97/// ## Setting a WindowListener
98///
99/// To receive callbacks for window-related events, set a WindowListener:
100///
101/// ```
102/// class MyWindowListener : public WindowListener {
103/// virtual void OnClose(Window* window) override {
104/// printf("Window closed!\n");
105/// }
106/// };
107///
108/// auto listener = new MyWindowListener();
109/// window->set_listener(listener);
110/// ```
111///
112/// ## Coordinate Systems
113///
114/// Monitors and may windows may have a device scale applied by the OS (for example, a Retina
115/// display on macOS may have a 2x or 3x DPI scale).
116///
117/// To convert between screen coordinates and pixel coordinates, use the following equation:
118///
119/// ```
120/// pixel_coordinate = round(screen_coordinate * scale)
121/// ```
122///
123class AExport Window : public RefCounted {
124public:
125 ///
126 /// Create a new Window.
127 ///
128 /// @param monitor The monitor to create the Window on.
129 ///
130 /// @param width The width (in screen coordinates).
131 ///
132 /// @param height The height (in screen coordinates).
133 ///
134 /// @param fullscreen Whether or not the window is fullscreen.
135 ///
136 /// @param window_flags Various window flags.
137 ///
138 /// @note
139 /// \parblock
140 ///
141 /// Windows are immediately shown by default unless kWindowFlags_Hidden is set in the
142 /// window_flags parameter. (They can be shown later via Window::Show())
143 ///
144 /// \endparblock
145 ///
146 /// @note
147 /// \parblock
148 ///
149 /// Screen coordinates are device-scale-independent and have the following relationship
150 /// to pixel coordinates:
151 ///
152 /// \code
153 /// pixel_coordinate = round(screen_coordinate * scale)
154 /// \endcode
155 ///
156 /// \endparblock
157 ///
158 static RefPtr<Window> Create(Monitor* monitor, uint32_t width, uint32_t height,
159 bool fullscreen, unsigned int window_flags);
160
161 ///
162 /// Set a WindowListener to receive callbacks for window-related events.
163 ///
164 /// @note Ownership remains with the caller.
165 ///
166 virtual void set_listener(WindowListener* listener) = 0;
167
168 ///
169 /// Get the WindowListener, if any.
170 ///
171 virtual WindowListener* listener() = 0;
172
173 ///
174 /// Get the window width (in screen coordinates).
175 ///
176 virtual uint32_t screen_width() const = 0;
177
178 ///
179 /// Get the window width (in pixels).
180 ///
181 virtual uint32_t width() const = 0;
182
183 ///
184 /// Get the window height (in screen coordinates).
185 ///
186 virtual uint32_t screen_height() const = 0;
187
188 ///
189 /// Get the window height (in pixels).
190 ///
191 virtual uint32_t height() const = 0;
192
193 ///
194 /// Move the window to a new position (in screen coordinates) relative to the top-left of the
195 /// monitor area.
196 ///
197 virtual void MoveTo(int x, int y) = 0;
198
199 ///
200 /// Move the window to the center of the monitor.
201 ///
202 virtual void MoveToCenter() = 0;
203
204 ///
205 /// Get the x-position of the window (in screen coordinates) relative to the top-left of the
206 /// monitor area.
207 ///
208 virtual int x() const = 0;
209
210 ///
211 /// Get the y-position of the window (in screen coordinates) relative to the top-left of the
212 /// monitor area.
213 ///
214 virtual int y() const = 0;
215
216 ///
217 /// Whether or not the window is fullscreen.
218 ///
219 virtual bool is_fullscreen() const = 0;
220
221 ///
222 /// Whether or not the window is GPU accelerated.
223 ///
224 virtual bool is_accelerated() const = 0;
225
226 ///
227 /// The render buffer id of the the window's backing texture.
228 /// (This will be 0 if the window is not accelerated).
229 ///
230 virtual uint32_t render_buffer_id() const = 0;
231
232 ///
233 /// The DPI scale of the window.
234 ///
235 virtual double scale() const = 0;
236
237 ///
238 /// Set the window title.
239 ///
240 virtual void SetTitle(const char* title) = 0;
241
242 ///
243 /// Set the cursor.
244 ///
245 virtual void SetCursor(ultralight::Cursor cursor) = 0;
246
247 ///
248 /// Show the window (if it was previously hidden).
249 ///
250 virtual void Show() = 0;
251
252 ///
253 /// Hide the window.
254 ///
255 virtual void Hide() = 0;
256
257 ///
258 /// Whether or not the window is currently visible (not hidden).
259 ///
260 virtual bool is_visible() const = 0;
261
262 ///
263 /// Close the window.
264 ///
265 virtual void Close() = 0;
266
267 ///
268 /// Convert screen coordinates to pixels using the current DPI scale.
269 ///
270 virtual int ScreenToPixels(int val) const = 0;
271
272 ///
273 /// Convert pixels to screen coordinates using the current DPI scale.
274 ///
275 virtual int PixelsToScreen(int val) const = 0;
276
277 ///
278 /// Draw a surface directly to window, used only by CPU renderer
279 ///
280 virtual void DrawSurface(int x, int y, Surface* surface) {}
281
282 ///
283 /// Get the underlying native window handle.
284 ///
285 /// @note
286 /// This is:
287 /// - HWND on Windows
288 /// - NSWindow* on macOS
289 /// - GLFWwindow* on Linux
290 ///
291 virtual void* native_handle() const = 0;
292
293 ///
294 /// Display frame statistics (FPS, frame times, etc.) in the titlebar.
295 ///
296 virtual void EnableFrameStatistics() {}
297
298protected:
299 virtual ~Window();
300 virtual bool platform_always_uses_cpu_renderer() const = 0;
301 virtual OverlayManager* overlay_manager() const = 0;
302
303 friend class OverlayImpl;
304};
305
306} // namespace ultralight
#define AExport
Definition Defines.h:42
Keyboard event representing a change in keyboard state.
Definition KeyEvent.h:23
A platform-specific monitor.
Definition Monitor.h:16
Mouse event representing a change in mouse state.
Definition MouseEvent.h:18
Interface for all ref-counted objects that will be managed using the RefPtr<> smart pointer.
Definition RefPtr.h:47
A nullable smart pointer.
Definition RefPtr.h:79
Scroll event representing a change in scroll state.
Definition ScrollEvent.h:18
User-defined pixel buffer surface.
Definition Surface.h:47
A platform-specific window.
Definition Window.h:123
virtual bool is_accelerated() const =0
Whether or not the window is GPU accelerated.
static RefPtr< Window > Create(Monitor *monitor, uint32_t width, uint32_t height, bool fullscreen, unsigned int window_flags)
Create a new Window.
virtual uint32_t height() const =0
Get the window height (in pixels).
virtual void SetTitle(const char *title)=0
Set the window title.
virtual bool is_visible() const =0
Whether or not the window is currently visible (not hidden).
virtual double scale() const =0
The DPI scale of the window.
virtual bool is_fullscreen() const =0
Whether or not the window is fullscreen.
virtual uint32_t width() const =0
Get the window width (in pixels).
virtual void set_listener(WindowListener *listener)=0
Set a WindowListener to receive callbacks for window-related events.
virtual void MoveToCenter()=0
Move the window to the center of the monitor.
virtual uint32_t render_buffer_id() const =0
The render buffer id of the the window's backing texture.
virtual int y() const =0
Get the y-position of the window (in screen coordinates) relative to the top-left of the monitor area...
virtual int x() const =0
Get the x-position of the window (in screen coordinates) relative to the top-left of the monitor area...
virtual void * native_handle() const =0
Get the underlying native window handle.
virtual bool platform_always_uses_cpu_renderer() const =0
virtual void Show()=0
Show the window (if it was previously hidden).
virtual uint32_t screen_height() const =0
Get the window height (in screen coordinates).
virtual void Hide()=0
Hide the window.
virtual void Close()=0
Close the window.
virtual int PixelsToScreen(int val) const =0
Convert pixels to screen coordinates using the current DPI scale.
virtual int ScreenToPixels(int val) const =0
Convert screen coordinates to pixels using the current DPI scale.
virtual void EnableFrameStatistics()
Display frame statistics (FPS, frame times, etc.) in the titlebar.
Definition Window.h:296
virtual OverlayManager * overlay_manager() const =0
virtual void DrawSurface(int x, int y, Surface *surface)
Draw a surface directly to window, used only by CPU renderer.
Definition Window.h:280
virtual uint32_t screen_width() const =0
Get the window width (in screen coordinates).
virtual void SetCursor(ultralight::Cursor cursor)=0
Set the cursor.
virtual void MoveTo(int x, int y)=0
Move the window to a new position (in screen coordinates) relative to the top-left of the monitor are...
virtual WindowListener * listener()=0
Get the WindowListener, if any.
Interface for all Window-related events.
Definition Window.h:27
virtual void OnResize(ultralight::Window *window, uint32_t width_px, uint32_t height_px)
Called when the Window is resized.
Definition Window.h:43
virtual bool OnKeyEvent(const ultralight::KeyEvent &evt)
Called when a keyboard event is fired.
Definition Window.h:52
virtual bool OnScrollEvent(const ultralight::ScrollEvent &evt)
Called when a scroll event is fired.
Definition Window.h:70
virtual ~WindowListener()
Definition Window.h:29
virtual bool OnMouseEvent(const ultralight::MouseEvent &evt)
Called when a mouse event is fired.
Definition Window.h:61
virtual void OnClose(ultralight::Window *window)
Called when the Window is closed.
Definition Window.h:34
Definition App.h:14
WindowFlags
Window creation flags.
Definition Window.h:76
@ kWindowFlags_Titled
Definition Window.h:78
@ kWindowFlags_Hidden
Definition Window.h:81
@ kWindowFlags_Borderless
Definition Window.h:77
@ kWindowFlags_Maximizable
Definition Window.h:80
@ kWindowFlags_Resizable
Definition Window.h:79
Cursor
Cursor types,.
Definition Listener.h:25