Ultralight C++ API 1.3.0
Loading...
Searching...
No Matches
View.h
Go to the documentation of this file.
1/******************************************************************************
2 * This file is a part of Ultralight, an ultra-portable web-browser engine. *
3 * *
4 * See <https://ultralig.ht> for licensing and more. *
5 * *
6 * (C) 2023 Ultralight, Inc. *
7 *****************************************************************************/
8#pragma once
10#include <Ultralight/RefPtr.h>
11#include <Ultralight/KeyEvent.h>
17#include <Ultralight/Bitmap.h>
18#include <Ultralight/Listener.h>
20
21namespace ultralight {
22
23///
24/// View-specific configuration settings.
25///
26/// @see Renderer::CreateView
27///
29 ///
30 /// Whether to render using the GPU renderer (accelerated) or the CPU renderer (unaccelerated).
31 ///
32 /// This option is only valid if you're managing the Renderer yourself (eg, you've previously
33 /// called Renderer::Create() instead of App::Create()).
34 ///
35 /// When true, the View will be rendered to an offscreen GPU texture using the GPU driver set in
36 /// Platform::set_gpu_driver. You can fetch details for the texture via View::render_target.
37 ///
38 /// When false (the default), the View will be rendered to an offscreen pixel buffer using the
39 /// multithreaded CPU renderer. This pixel buffer can optionally be provided by the user--
40 /// for more info see Platform::set_surface_factory and View::surface.
41 ///
42 bool is_accelerated = false;
43
44 ///
45 /// Whether or not this View should support transparency.
46 ///
47 /// @note Make sure to also set the following CSS on the page:
48 ///
49 /// html, body { background: transparent; }
50 ///
51 bool is_transparent = false;
52
53 ///
54 /// The initial device scale, ie. the amount to scale page units to screen pixels. This should
55 /// be set to the scaling factor of the device that the View is displayed on.
56 ///
57 /// @note 1.0 is equal to 100% zoom (no scaling), 2.0 is equal to 200% zoom (2x scaling)
58 ///
59 double initial_device_scale = 1.0;
60
61 ///
62 /// Whether or not the View should initially have input focus, @see View::Focus()
63 ///
64 bool initial_focus = true;
65
66 ///
67 /// Whether or not images should be enabled.
68 ///
69 bool enable_images = true;
70
71 ///
72 /// Whether or not JavaScript should be enabled.
73 ///
74 bool enable_javascript = true;
75
76 ///
77 /// Default font-family to use.
78 ///
79 String font_family_standard = "Times New Roman";
80
81 ///
82 /// Default font-family to use for fixed fonts. (pre/code)
83 ///
84 String font_family_fixed = "Courier New";
85
86 ///
87 /// Default font-family to use for serif fonts.
88 ///
89 String font_family_serif = "Times New Roman";
90
91 ///
92 /// Default font-family to use for sans-serif fonts.
93 ///
94 String font_family_sans_serif = "Arial";
95
96 ///
97 /// Default user-agent string.
98 ///
99 String user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
100 "AppleWebKit/610.4.3.1.4 (KHTML, like Gecko) "
101 "Ultralight/1.3.0 Version/14.0.3 Safari/610.4.3.1.4";
102};
103
104///
105/// Web-page container rendered to an offscreen surface that you display yourself.
106///
107/// The View class is responsible for loading and rendering web-pages to an offscreen surface. It
108/// is completely isolated from the OS windowing system, you must forward all input events to it
109/// from your application.
110///
111/// @note The API is not thread-safe, all calls must be made on the same thread that the
112/// Renderer/App was created on.
113///
114class UExport View : public RefCounted {
115 public:
116 ///
117 /// Get the URL of the current page loaded into this View, if any.
118 ///
119 virtual String url() = 0;
120
121 ///
122 /// Get the title of the current page loaded into this View, if any.
123 ///
124 virtual String title() = 0;
125
126 ///
127 /// Get the width of the View, in pixels.
128 ///
129 virtual uint32_t width() const = 0;
130
131 ///
132 /// Get the height of the View, in pixels.
133 ///
134 virtual uint32_t height() const = 0;
135
136 ///
137 /// Get the device scale, ie. the amount to scale page units to screen pixels.
138 ///
139 /// For example, a value of 1.0 is equivalent to 100% zoom. A value of 2.0 is 200% zoom.
140 ///
141 virtual double device_scale() const = 0;
142
143 ///
144 /// Set the device scale.
145 ///
146 virtual void set_device_scale(double scale) = 0;
147
148 ///
149 /// Whether or not the View is GPU-accelerated. If this is false, the page will be rendered
150 /// via the CPU renderer.
151 ///
152 virtual bool is_accelerated() const = 0;
153
154 ///
155 /// Whether or not the View supports transparent backgrounds.
156 ///
157 virtual bool is_transparent() const = 0;
158
159 ///
160 /// Check if the main frame of the page is currently loading.
161 ///
162 virtual bool is_loading() = 0;
163
164 ///
165 /// Get the RenderTarget for the View.
166 ///
167 /// @pre Only valid if this View is using the GPU renderer (see ViewConfig::is_accelerated).
168 ///
169 /// @note You can use this with your GPUDriver implementation to bind and display the
170 /// corresponding texture in your application.
171 ///
173
174 ///
175 /// Get the Surface for the View (native pixel buffer that the CPU renderer draws into).
176 ///
177 /// @pre This operation is only valid if the View is using the CPU renderer, (eg, it is
178 /// **not** GPU accelerated, see ViewConfig::is_accelerated). This function will return
179 /// return nullptr if the View is using the GPU renderer.
180 ///
181 /// @note The default Surface is BitmapSurface but you can provide your own Surface
182 /// implementation via Platform::set_surface_factory().
183 ///
184 virtual Surface* surface() = 0;
185
186 ///
187 /// Load a raw string of HTML, the View will navigate to it as a new page.
188 ///
189 /// @param html The raw HTML string to load.
190 ///
191 /// @param url An optional URL for this load (to make it appear as if we we loaded this HTML
192 /// from a certain URL). Can be used for resolving relative URLs and cross-origin
193 /// rules.
194 ///
195 /// @param add_to_history Whether or not this load should be added to the session's history
196 /// (eg, the back/forward list).
197 ///
198 virtual void LoadHTML(const String& html, const String& url = "", bool add_to_history = false)
199 = 0;
200
201 ///
202 /// Load a URL, the View will navigate to it as a new page.
203 ///
204 /// @note You can use File URLs (eg, file:///page.html) but you must define your own FileSystem
205 /// implementation if you are not using AppCore. @see Platform::set_file_system
206 ///
207 virtual void LoadURL(const String& url) = 0;
208
209 ///
210 /// Resize View to a certain size.
211 ///
212 /// @param width The initial width, in pixels.
213 ///
214 /// @param height The initial height, in pixels.
215 ///
216 ///
217 virtual void Resize(uint32_t width, uint32_t height) = 0;
218
219 ///
220 /// Acquire the page's JSContext for use with the JavaScriptCore API
221 ///
222 /// @note You can use the underlying JSContextRef with the JavaScriptCore C API. This allows you
223 /// to marshall C/C++ objects to/from JavaScript, bind callbacks, and call JS functions
224 /// directly.
225 ///
226 /// @note The JSContextRef gets reset after each page navigation. You should initialize your
227 /// JavaScript state within the OnWindowObjectReady and OnDOMReady events,
228 /// @see ViewListener.
229 ///
230 /// @note This call locks the internal context for the current thread. It will be unlocked when
231 /// the returned JSContext's ref-count goes to zero. The lock is recursive, you can call
232 /// this multiple times.
233 ///
235
236 ///
237 /// Get a handle to the internal JavaScriptCore VM.
238 ///
239 virtual void* JavaScriptVM() = 0;
240
241 ///
242 /// Helper function to evaluate a raw string of JavaScript and return the result as a String.
243 ///
244 /// @param script A string of JavaScript to evaluate in the main frame.
245 ///
246 /// @param exception A string to store the exception in, if any. Pass a nullptr if you don't
247 /// care about exceptions.
248 ///
249 /// @return Returns the JavaScript result typecast to a String.
250 ///
251 ///
252 /// @note You do not need to lock the JS context, it is done automatically.
253 ///
254 /// @note If you need lower-level access to native JavaScript values, you should instead lock
255 /// the JS context and call JSEvaluateScript() in the JavaScriptCore C API.
256 /// @see <JavaScriptCore/JSBase.h>
257 ///
258 virtual String EvaluateScript(const String& script, String* exception = nullptr) = 0;
259
260 ///
261 /// Whether or not we can navigate backwards in history
262 ///
263 virtual bool CanGoBack() = 0;
264
265 ///
266 /// Whether or not we can navigate forwards in history
267 ///
268 virtual bool CanGoForward() = 0;
269
270 ///
271 /// Navigate backwards in history
272 ///
273 virtual void GoBack() = 0;
274
275 ///
276 /// Navigate forwards in history
277 ///
278 virtual void GoForward() = 0;
279
280 ///
281 /// Navigate to an arbitrary offset in history
282 ///
283 virtual void GoToHistoryOffset(int offset) = 0;
284
285 ///
286 /// Reload current page
287 ///
288 virtual void Reload() = 0;
289
290 ///
291 /// Stop all page loads
292 ///
293 virtual void Stop() = 0;
294
295 ///
296 /// Give focus to the View.
297 ///
298 /// You should call this to give visual indication that the View has input focus (changes active
299 /// text selection colors, for example).
300 ///
301 virtual void Focus() = 0;
302
303 ///
304 /// Remove focus from the View and unfocus any focused input elements.
305 ///
306 /// You should call this to give visual indication that the View has lost input focus.
307 ///
308 virtual void Unfocus() = 0;
309
310 ///
311 /// Whether or not the View has focus.
312 ///
313 virtual bool HasFocus() = 0;
314
315 ///
316 /// Whether or not the View has an input element with visible keyboard focus (indicated by a
317 /// blinking caret).
318 ///
319 /// You can use this to decide whether or not the View should consume keyboard input events
320 /// (useful in games with mixed UI and key handling).
321 ///
322 virtual bool HasInputFocus() = 0;
323
324 ///
325 /// Fire a keyboard event
326 ///
327 /// @note Only 'Char' events actually generate text in input fields.
328 ///
329 virtual void FireKeyEvent(const KeyEvent& evt) = 0;
330
331 ///
332 /// Fire a mouse event
333 ///
334 virtual void FireMouseEvent(const MouseEvent& evt) = 0;
335
336 ///
337 /// Fire a scroll event
338 ///
339 virtual void FireScrollEvent(const ScrollEvent& evt) = 0;
340
341 ///
342 /// Set a ViewListener to receive callbacks for View-related events.
343 ///
344 /// @note Ownership remains with the caller.
345 ///
346 virtual void set_view_listener(ViewListener* listener) = 0;
347
348 ///
349 /// Get the active ViewListener, if any
350 ///
351 virtual ViewListener* view_listener() const = 0;
352
353 ///
354 /// Set a LoadListener to receive callbacks for Load-related events.
355 ///
356 /// @note Ownership remains with the caller.
357 ///
358 virtual void set_load_listener(LoadListener* listener) = 0;
359
360 ///
361 /// Get the active LoadListener, if any
362 ///
363 virtual LoadListener* load_listener() const = 0;
364
365 ///
366 /// Set whether or not this View should be repainted during the next call to Renderer::Render
367 ///
368 /// @note This flag is automatically set whenever the page content changes but you can set it
369 /// directly in case you need to force a repaint.
370 ///
371 virtual void set_needs_paint(bool needs_paint) = 0;
372
373 ///
374 /// Whether or not this View should be repainted during the next call to
375 /// Renderer::Render.
376 ///
377 virtual bool needs_paint() const = 0;
378
379 ///
380 /// Create an Inspector View to inspect / debug this View locally.
381 ///
382 /// This will only succeed if you have the inspector assets in your filesystem-- the inspector
383 /// will look for file:///inspector/Main.html when it first loads.
384 ///
385 /// You must handle ViewListener::OnCreateInspectorView so that the library has a View to display
386 /// the inspector in. This function will call this event only if an inspector view is not
387 /// currently active.
388 ///
389 virtual void CreateLocalInspectorView() = 0;
390
391 protected:
392 virtual ~View();
393};
394
395} // namespace ultralight
#define UExport
Definition Defines.h:65
Generic keyboard event representing a change in keyboard state.
Definition KeyEvent.h:23
User-defined interface to listen for load-related events for a View.
Definition Listener.h:185
Generic 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
Generic scroll event representing a change in scroll state.
Definition ScrollEvent.h:18
UTF-8 String container with conversions for UTF-16 and UTF-32.
Definition String.h:21
Offscreen pixel buffer surface.
Definition Surface.h:34
Web-page container rendered to an offscreen surface that you display yourself.
Definition View.h:114
virtual void set_load_listener(LoadListener *listener)=0
Set a LoadListener to receive callbacks for Load-related events.
virtual uint32_t height() const =0
Get the height of the View, in pixels.
virtual bool HasInputFocus()=0
Whether or not the View has an input element with visible keyboard focus (indicated by a blinking car...
virtual void Unfocus()=0
Remove focus from the View and unfocus any focused input elements.
virtual bool CanGoForward()=0
Whether or not we can navigate forwards in history.
virtual void Stop()=0
Stop all page loads.
virtual void CreateLocalInspectorView()=0
Create an Inspector View to inspect / debug this View locally.
virtual String EvaluateScript(const String &script, String *exception=nullptr)=0
Helper function to evaluate a raw string of JavaScript and return the result as a String.
virtual void Resize(uint32_t width, uint32_t height)=0
Resize View to a certain size.
virtual void GoBack()=0
Navigate backwards in history.
virtual RenderTarget render_target()=0
Get the RenderTarget for the View.
virtual bool CanGoBack()=0
Whether or not we can navigate backwards in history.
virtual uint32_t width() const =0
Get the width of the View, in pixels.
virtual void Reload()=0
Reload current page.
virtual LoadListener * load_listener() const =0
Get the active LoadListener, if any.
virtual void FireMouseEvent(const MouseEvent &evt)=0
Fire a mouse event.
virtual String url()=0
Get the URL of the current page loaded into this View, if any.
virtual void GoForward()=0
Navigate forwards in history.
virtual double device_scale() const =0
Get the device scale, ie.
virtual void LoadHTML(const String &html, const String &url="", bool add_to_history=false)=0
Load a raw string of HTML, the View will navigate to it as a new page.
virtual ViewListener * view_listener() const =0
Get the active ViewListener, if any.
virtual void set_view_listener(ViewListener *listener)=0
Set a ViewListener to receive callbacks for View-related events.
virtual void Focus()=0
Give focus to the View.
virtual bool needs_paint() const =0
Whether or not this View should be repainted during the next call to Renderer::Render.
virtual void set_needs_paint(bool needs_paint)=0
Set whether or not this View should be repainted during the next call to Renderer::Render.
virtual void FireScrollEvent(const ScrollEvent &evt)=0
Fire a scroll event.
virtual bool HasFocus()=0
Whether or not the View has focus.
virtual bool is_loading()=0
Check if the main frame of the page is currently loading.
virtual RefPtr< JSContext > LockJSContext()=0
Acquire the page's JSContext for use with the JavaScriptCore API.
virtual void * JavaScriptVM()=0
Get a handle to the internal JavaScriptCore VM.
virtual String title()=0
Get the title of the current page loaded into this View, if any.
virtual void GoToHistoryOffset(int offset)=0
Navigate to an arbitrary offset in history.
virtual bool is_transparent() const =0
Whether or not the View supports transparent backgrounds.
virtual bool is_accelerated() const =0
Whether or not the View is GPU-accelerated.
virtual void LoadURL(const String &url)=0
Load a URL, the View will navigate to it as a new page.
virtual void set_device_scale(double scale)=0
Set the device scale.
virtual void FireKeyEvent(const KeyEvent &evt)=0
Fire a keyboard event.
virtual Surface * surface()=0
Get the Surface for the View (native pixel buffer that the CPU renderer draws into).
User-defined interface to listen for View-specific events.
Definition Listener.h:101
Definition App.h:14
Rendering details for a View, to be used with your own GPUDriver.
Definition RenderTarget.h:24
View-specific configuration settings.
Definition View.h:28