Loading...
Searching...
No Matches
App.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/Renderer.h>
13
14namespace ultralight {
15
16class Monitor;
17class Window;
18
19///
20/// Interface for all App-related events. @see App::set_listener
21///
23public:
24 virtual ~AppListener() {}
25
26 ///
27 /// Called whenever the App updates. You should update all app logic here.
28 ///
29 /// @note This event is fired right before the run loop calls
30 /// Renderer::Update and Renderer::Render.
31 ///
32 virtual void OnUpdate() {}
33};
34
35///
36/// App-specific settings.
37///
39 ///
40 /// The name of the developer of this app.
41 ///
42 /// This is used to generate a unique path to store local application data
43 /// on the user's machine.
44 ///
45 String developer_name = "MyCompany";
46
47 ///
48 /// The name of this app.
49 ///
50 /// This is used to generate a unique path to store local application data
51 /// on the user's machine.
52 ///
53 String app_name = "MyApp";
54
55 ///
56 /// The root file path for our file system. You should set this to the
57 /// relative path where all of your app data is.
58 ///
59 /// This will be used to resolve all file URLs, eg file:///page.html
60 ///
61 /// @note This relative path is resolved using the following logic:
62 /// - Windows: relative to the executable path
63 /// - Linux: relative to the executable path
64 /// - macOS: relative to YourApp.app/Contents/Resources/
65 ///
66 String file_system_path = "./assets/";
67
68 ///
69 /// Whether or not we should load and compile shaders from the file system
70 /// (eg, from the /shaders/ path, relative to file_system_path).
71 ///
72 /// If this is false (the default), we will instead load pre-compiled shaders
73 /// from memory which speeds up application startup time.
74 ///
75 bool load_shaders_from_file_system = false;
76
77 ///
78 /// We try to use the GPU renderer when a compatible GPU is detected.
79 ///
80 /// Set this to true to force the engine to always use the CPU renderer.
81 ///
82 bool force_cpu_renderer = false;
83};
84
85///
86/// Main application singleton (use this if you want to let the library manage window creation).
87///
88/// This convenience class sets up everything you need to display web-based content in a
89/// desktop application.
90///
91/// The App class initializes the Platform singleton with OS-specific defaults, creates a Renderer,
92/// and automatically manages window creation, run loop, input events, and painting.
93///
94/// ## Creating the App
95///
96/// Call App::Create() to initialize the library and create the App singleton.
97///
98/// ```
99/// auto app = App::Create();
100/// ```
101///
102/// ## Creating a Window
103///
104/// Call Window::Create() to create one or more windows during the lifetime of your app.
105///
106/// ```
107/// auto window = Window::Create(app->main_monitor(), 1024, 768, false,
108/// kWindowFlags_Titled | kWindowFlags_Resizable);
109/// ```
110///
111/// ### Creating an Overlay in a Window
112///
113/// Each Window can have one or more Overlay instances. Overlays are used to display web-based
114/// content in a portion of the window.
115///
116/// Call Overlay::Create() to create an overlay in a window.
117///
118/// ```
119/// auto overlay = Overlay::Create(window, 1024, 768, 0, 0);
120/// ```
121///
122/// Each Overlay has a View instance that you can use to load web content into.
123///
124/// ```
125/// overlay->view()->LoadURL("https://google.com");
126/// ```
127///
128/// ## Running the App
129///
130/// Call App::Run() to start the main run loop.
131///
132/// ```
133/// #include <AppCore/AppCore.h>
134///
135/// using namespace ultralight;
136///
137/// int main() {
138/// // Initialize app, window, overlay, etc. here...
139///
140/// app->Run();
141///
142/// return 0;
143/// }
144/// ```
145///
146/// ## Shutting Down the App
147///
148/// Call App::Quit() to stop the main run loop and shut down the app.
149///
150/// ```
151/// app->Quit();
152/// ```
153
154/// @note This is optional, you can use the Renderer class directly if you want to manage your
155/// own windows and run loop.
156///
157class AExport App : public RefCounted {
158public:
159 ///
160 /// Create the App singleton.
161 ///
162 /// @param settings Settings to customize App runtime behavior.
163 ///
164 /// @param config Config options for the Ultralight renderer.
165 ///
166 /// @return Returns a ref-pointer to the created App instance.
167 ///
168 /// @note You should only create one of these per application lifetime.
169 ///
170 /// @note Certain Config options may be overridden during App creation,
171 /// most commonly Config::face_winding and Config::cache_path.
172 ///
173 static RefPtr<App> Create(Settings settings = Settings(), Config config = Config());
174
175 ///
176 /// Get the App singleton.
177 ///
178 static App* instance();
179
180 ///
181 /// Get the settings this App was created with.
182 ///
183 virtual const Settings& settings() const = 0;
184
185 ///
186 /// Set an AppListener to receive callbacks for app-related events.
187 ///
188 /// @note Ownership remains with the caller.
189 ///
190 virtual void set_listener(AppListener* listener) = 0;
191
192 ///
193 /// Get the AppListener, if any.
194 ///
195 virtual AppListener* listener() = 0;
196
197 ///
198 /// Whether or not the App is running.
199 ///
200 virtual bool is_running() const = 0;
201
202 ///
203 /// Get the main monitor (this is never NULL).
204 ///
205 /// @note We'll add monitor enumeration later.
206 ///
207 virtual Monitor* main_monitor() = 0;
208
209 ///
210 /// Get the underlying Renderer instance.
211 ///
213
214 ///
215 /// Run the main loop.
216 ///
217 virtual void Run() = 0;
218
219 ///
220 /// Quit the application.
221 ///
222 virtual void Quit() = 0;
223
224protected:
225 virtual ~App();
226};
227
228} // namespace ultralight
#define AExport
Definition Defines.h:42
Main application singleton (use this if you want to let the library manage window creation).
Definition App.h:157
virtual ~App()
virtual RefPtr< Renderer > renderer()=0
Get the underlying Renderer instance.
virtual const Settings & settings() const =0
Get the settings this App was created with.
static App * instance()
Get the App singleton.
static RefPtr< App > Create(Settings settings=Settings(), Config config=Config())
Create the App singleton.
virtual Monitor * main_monitor()=0
Get the main monitor (this is never NULL).
virtual void Quit()=0
Quit the application.
virtual void Run()=0
Run the main loop.
virtual AppListener * listener()=0
Get the AppListener, if any.
virtual bool is_running() const =0
Whether or not the App is running.
virtual void set_listener(AppListener *listener)=0
Set an AppListener to receive callbacks for app-related events.
Interface for all App-related events.
Definition App.h:22
virtual ~AppListener()
Definition App.h:24
virtual void OnUpdate()
Called whenever the App updates.
Definition App.h:32
A platform-specific monitor.
Definition Monitor.h:16
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
Unicode string container with conversions for UTF-8, UTF-16, and UTF-32.
Definition String.h:34
A platform-specific window.
Definition Window.h:123
Definition App.h:14
Core configuration for the renderer.
Definition Config.h:95
App-specific settings.
Definition App.h:38