Loading...
Searching...
No Matches
View.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
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 ///
31 /// A user-generated id for the display (monitor, TV, or screen) that this View will be shown on.
32 ///
33 /// Animations are driven based on the physical refresh rate of the display. Multiple Views can
34 /// share the same display.
35 ///
36 /// @note This is automatically managed for you when App::Create() is used.
37 ///
38 /// @see Renderer::RefreshDisplay.
39 ///
40 uint32_t display_id = 0;
41
42 ///
43 /// Whether to render using the GPU renderer (accelerated) or the CPU renderer (unaccelerated).
44 ///
45 /// When true, the View will be rendered to an offscreen GPU texture using the GPU driver set in
46 /// Platform::set_gpu_driver. You can fetch details for the texture via View::render_target.
47 ///
48 /// When false (the default), the View will be rendered to an offscreen pixel buffer using the
49 /// multithreaded CPU renderer. This pixel buffer can optionally be provided by the user--
50 /// for more info see Platform::set_surface_factory and View::surface.
51 ///
52 /// @note This is automatically managed for you when App::Create() is used.
53 ///
54 bool is_accelerated = false;
55
56 ///
57 /// The initial device scale, ie. the amount to scale page units to screen pixels. This should
58 /// be set to the scaling factor of the device that the View is displayed on.
59 ///
60 /// @note 1.0 is equal to 100% zoom (no scaling), 2.0 is equal to 200% zoom (2x scaling)
61 ///
62 /// @note This is automatically managed for you when App::Create() is used.
63 ///
64 double initial_device_scale = 1.0;
65
66 ///
67 /// Whether or not this View should support transparency.
68 ///
69 /// @note Make sure to also set the following CSS on the page:
70 ///
71 /// html, body { background: transparent; }
72 ///
73 bool is_transparent = false;
74
75 ///
76 /// Whether or not the View should initially have input focus, @see View::Focus()
77 ///
78 bool initial_focus = true;
79
80 ///
81 /// Whether or not images should be enabled.
82 ///
83 bool enable_images = true;
84
85 ///
86 /// Whether or not JavaScript should be enabled.
87 ///
88 bool enable_javascript = true;
89
90 ///
91 /// Whether or not compositing should be enabled.
92 ///
93 bool enable_compositor = false;
94
95 ///
96 /// Default font-family to use.
97 ///
98 String font_family_standard = "Times New Roman";
99
100 ///
101 /// Default font-family to use for fixed fonts. (pre/code)
102 ///
103 String font_family_fixed = "Courier New";
104
105 ///
106 /// Default font-family to use for serif fonts.
107 ///
108 String font_family_serif = "Times New Roman";
109
110 ///
111 /// Default font-family to use for sans-serif fonts.
112 ///
113 String font_family_sans_serif = "Arial";
114
115 ///
116 /// Default user-agent string.
117 ///
118 String user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
119 "AppleWebKit/615.1.18.100.1 (KHTML, like Gecko) "
120 "Ultralight/1.4.0 Version/16.4.1 Safari/615.1.18.100.1";
121};
122
123///
124/// Web-page container rendered to an offscreen surface.
125///
126/// The View class is responsible for loading and rendering web-pages to an offscreen surface. It
127/// is completely isolated from the OS windowing system, you must forward all input events to it
128/// from your application.
129///
130/// ## Creating a View
131///
132/// You can create a View using Renderer::CreateView.
133///
134/// ```
135/// // Create a ViewConfig with the desired settings
136/// ViewConfig view_config;
137///
138/// // Create a View, 500 by 500 pixels in size, using the default Session
139/// RefPtr<View> view = renderer->CreateView(500, 500, view_config, nullptr);
140/// ```
141///
142/// @note When using App::Create, the library will automatically create a View for you when you
143/// call Overlay::Create.
144///
145/// ## Loading Content into a View
146///
147/// You can load content asynchronously into a View using View::LoadURL().
148///
149/// ```
150/// // Load a URL into the View
151/// view->LoadURL("https://en.wikipedia.org/wiki/Main_Page");
152/// ```
153///
154/// ### Local File URLs
155///
156/// Local file URLs (eg, `file:///page.html`) will be loaded via FileSystem. You can provide your
157/// own FileSystem implementation so these files can be loaded from your application's resources.
158///
159/// ### Displaying Views in Your Application
160///
161/// Views are rendered either to a pixel-buffer (View::surface) or a GPU texture
162/// (View::render_target) depending on whether CPU or GPU rendering is used (see
163/// ViewConfig::is_accelerated).
164///
165/// You can use the Surface or RenderTarget to display the View in your application.
166///
167/// ```
168/// // Get the Surface for the View (assuming CPU rendering)
169/// Surface* surface = view->surface();
170///
171/// // Check if the Surface is dirty (pixels have changed)
172/// if (!surface->dirty_bounds().IsEmpty()) {
173/// // Cast to the default Surface implementation (BitmapSurface) and get
174/// // the underlying Bitmap.
175/// RefPtr<Bitmap> bitmap = static_cast<BitmapSurface*>(surface)->bitmap();
176///
177/// // Use the bitmap pixels here...
178///
179/// // Clear the dirty bounds after you're done displaying the pixels
180/// surface->ClearDirtyBounds();
181/// }
182/// ```
183///
184/// ## Input Events
185///
186/// You must forward all input events to the View from your application. This includes keyboard,
187/// mouse, and scroll events.
188///
189/// ```
190/// // Forward a mouse-move event to the View
191/// MouseEvent evt;
192/// evt.type = MouseEvent::kType_MouseMoved;
193/// evt.x = 100;
194/// evt.y = 100;
195/// evt.button = MouseEvent::kButton_None;
196/// view->FireMouseEvent(evt);
197/// ```
198///
199/// @note The View API is not thread-safe, all calls must be made on the same thread that the
200/// Renderer or App was created on.
201///
202class UExport View : public RefCounted {
203 public:
204 ///
205 /// Get the URL of the current page loaded into this View, if any.
206 ///
207 virtual String url() = 0;
208
209 ///
210 /// Get the title of the current page loaded into this View, if any.
211 ///
212 virtual String title() = 0;
213
214 ///
215 /// Get the width of the View, in pixels.
216 ///
217 virtual uint32_t width() const = 0;
218
219 ///
220 /// Get the height of the View, in pixels.
221 ///
222 virtual uint32_t height() const = 0;
223
224 ///
225 /// Get the display id of the View.
226 ///
227 /// @see ViewConfig::display_id
228 ///
229 virtual uint32_t display_id() const = 0;
230
231 ///
232 /// Set the display id of the View.
233 ///
234 /// This should be called when the View is moved to another display.
235 ///
236 virtual void set_display_id(uint32_t id) = 0;
237
238 ///
239 /// Get the device scale, ie. the amount to scale page units to screen pixels.
240 ///
241 /// For example, a value of 1.0 is equivalent to 100% zoom. A value of 2.0 is 200% zoom.
242 ///
243 virtual double device_scale() const = 0;
244
245 ///
246 /// Set the device scale.
247 ///
248 virtual void set_device_scale(double scale) = 0;
249
250 ///
251 /// Whether or not the View is GPU-accelerated. If this is false, the page will be rendered
252 /// via the CPU renderer.
253 ///
254 virtual bool is_accelerated() const = 0;
255
256 ///
257 /// Whether or not the View supports transparent backgrounds.
258 ///
259 virtual bool is_transparent() const = 0;
260
261 ///
262 /// Check if the main frame of the page is currently loading.
263 ///
264 virtual bool is_loading() = 0;
265
266 ///
267 /// Get the RenderTarget for the View.
268 ///
269 /// @pre Only valid if this View is using the GPU renderer (see ViewConfig::is_accelerated).
270 ///
271 /// @note You can use this with your GPUDriver implementation to bind and display the
272 /// corresponding texture in your application.
273 ///
275
276 ///
277 /// Get the Surface for the View (native pixel buffer that the CPU renderer draws into).
278 ///
279 /// @pre This operation is only valid if the View is using the CPU renderer, (eg, it is
280 /// **not** GPU accelerated, see ViewConfig::is_accelerated). This function will return
281 /// return nullptr if the View is using the GPU renderer.
282 ///
283 /// @note The default Surface is BitmapSurface but you can provide your own Surface
284 /// implementation via Platform::set_surface_factory().
285 ///
286 virtual Surface* surface() = 0;
287
288 ///
289 /// Load a raw string of HTML, the View will navigate to it as a new page.
290 ///
291 /// @param html The raw HTML string to load.
292 ///
293 /// @param url An optional URL for this load (to make it appear as if we we loaded this HTML
294 /// from a certain URL). Can be used for resolving relative URLs and cross-origin
295 /// rules.
296 ///
297 /// @param add_to_history Whether or not this load should be added to the session's history
298 /// (eg, the back/forward list).
299 ///
300 virtual void LoadHTML(const String& html, const String& url = "", bool add_to_history = false)
301 = 0;
302
303 ///
304 /// Load a URL, the View will navigate to it as a new page.
305 ///
306 /// @note You can use File URLs (eg, file:///page.html) but you must define your own FileSystem
307 /// implementation if you are not using AppCore. @see Platform::set_file_system
308 ///
309 virtual void LoadURL(const String& url) = 0;
310
311 ///
312 /// Resize View to a certain size.
313 ///
314 /// @param width The initial width, in pixels.
315 ///
316 /// @param height The initial height, in pixels.
317 ///
318 ///
319 virtual void Resize(uint32_t width, uint32_t height) = 0;
320
321 ///
322 /// Acquire the page's JSContext for use with the JavaScriptCore API
323 ///
324 /// @note You can use the underlying JSContextRef with the JavaScriptCore C API. This allows you
325 /// to marshall C/C++ objects to/from JavaScript, bind callbacks, and call JS functions
326 /// directly.
327 ///
328 /// @note The JSContextRef gets reset after each page navigation. You should initialize your
329 /// JavaScript state within the OnWindowObjectReady and OnDOMReady events,
330 /// @see ViewListener.
331 ///
332 /// @note This call locks the internal context for the current thread. It will be unlocked when
333 /// the returned JSContext's ref-count goes to zero. The lock is recursive, you can call
334 /// this multiple times.
335 ///
337
338 ///
339 /// Get a handle to the internal JavaScriptCore VM.
340 ///
341 virtual void* JavaScriptVM() = 0;
342
343 ///
344 /// Helper function to evaluate a raw string of JavaScript and return the result as a String.
345 ///
346 /// @param script A string of JavaScript to evaluate in the main frame.
347 ///
348 /// @param exception A string to store the exception in, if any. Pass a nullptr if you don't
349 /// care about exceptions.
350 ///
351 /// @return Returns the JavaScript result typecast to a String.
352 ///
353 ///
354 /// @note You do not need to lock the JS context, it is done automatically.
355 ///
356 /// @note If you need lower-level access to native JavaScript values, you should instead lock
357 /// the JS context and call JSEvaluateScript() in the JavaScriptCore C API.
358 /// @see <JavaScriptCore/JSBase.h>
359 ///
360 virtual String EvaluateScript(const String& script, String* exception = nullptr) = 0;
361
362 ///
363 /// Whether or not we can navigate backwards in history
364 ///
365 virtual bool CanGoBack() = 0;
366
367 ///
368 /// Whether or not we can navigate forwards in history
369 ///
370 virtual bool CanGoForward() = 0;
371
372 ///
373 /// Navigate backwards in history
374 ///
375 virtual void GoBack() = 0;
376
377 ///
378 /// Navigate forwards in history
379 ///
380 virtual void GoForward() = 0;
381
382 ///
383 /// Navigate to an arbitrary offset in history
384 ///
385 virtual void GoToHistoryOffset(int offset) = 0;
386
387 ///
388 /// Reload current page
389 ///
390 virtual void Reload() = 0;
391
392 ///
393 /// Stop all page loads
394 ///
395 virtual void Stop() = 0;
396
397 ///
398 /// Give focus to the View.
399 ///
400 /// You should call this to give visual indication that the View has input focus (changes active
401 /// text selection colors, for example).
402 ///
403 virtual void Focus() = 0;
404
405 ///
406 /// Remove focus from the View and unfocus any focused input elements.
407 ///
408 /// You should call this to give visual indication that the View has lost input focus.
409 ///
410 virtual void Unfocus() = 0;
411
412 ///
413 /// Whether or not the View has focus.
414 ///
415 virtual bool HasFocus() = 0;
416
417 ///
418 /// Whether or not the View has an input element with visible keyboard focus (indicated by a
419 /// blinking caret).
420 ///
421 /// You can use this to decide whether or not the View should consume keyboard input events
422 /// (useful in games with mixed UI and key handling).
423 ///
424 virtual bool HasInputFocus() = 0;
425
426 ///
427 /// Fire a keyboard event
428 ///
429 /// @note Only 'Char' events actually generate text in input fields.
430 ///
431 virtual void FireKeyEvent(const KeyEvent& evt) = 0;
432
433 ///
434 /// Fire a mouse event
435 ///
436 virtual void FireMouseEvent(const MouseEvent& evt) = 0;
437
438 ///
439 /// Fire a scroll event
440 ///
441 virtual void FireScrollEvent(const ScrollEvent& evt) = 0;
442
443 ///
444 /// Set a ViewListener to receive callbacks for View-related events.
445 ///
446 /// @note Ownership remains with the caller.
447 ///
448 virtual void set_view_listener(ViewListener* listener) = 0;
449
450 ///
451 /// Get the active ViewListener, if any
452 ///
453 virtual ViewListener* view_listener() const = 0;
454
455 ///
456 /// Set a LoadListener to receive callbacks for Load-related events.
457 ///
458 /// @note Ownership remains with the caller.
459 ///
460 virtual void set_load_listener(LoadListener* listener) = 0;
461
462 ///
463 /// Get the active LoadListener, if any
464 ///
465 virtual LoadListener* load_listener() const = 0;
466
467 ///
468 /// Set a DownloadListener to receive callbacks for download-related events.
469 ///
470 /// @note Ownership remains with the caller.
471 ///
472 virtual void set_download_listener(DownloadListener* listener) = 0;
473
474 ///
475 /// Get the active DownloadListener, if any
476 ///
478
479 ///
480 /// Cancel an active download.
481 ///
482 virtual void CancelDownload(DownloadId id) = 0;
483
484 ///
485 /// Set a NetworkListener to receive callbacks for network-related events.
486 ///
487 /// @note Ownership remains with the caller.
488 ///
489 virtual void set_network_listener(NetworkListener* listener) = 0;
490
491 ///
492 /// Get the active NetworkListener, if any
493 ///
494 virtual NetworkListener* network_listener() const = 0;
495
496 ///
497 /// Set whether or not this View should be repainted during the next call to Renderer::Render
498 ///
499 /// @note This flag is automatically set whenever the page content changes but you can set it
500 /// directly in case you need to force a repaint.
501 ///
502 virtual void set_needs_paint(bool needs_paint) = 0;
503
504 ///
505 /// Whether or not this View should be repainted during the next call to
506 /// Renderer::Render.
507 ///
508 virtual bool needs_paint() const = 0;
509
510 ///
511 /// Create an Inspector View to inspect / debug this View locally.
512 ///
513 /// This will only succeed if you have the inspector assets in your filesystem-- the inspector
514 /// will look for file:///inspector/Main.html when it first loads.
515 ///
516 /// You must handle ViewListener::OnCreateInspectorView so that the library has a View to display
517 /// the inspector in. This function will call this event only if an inspector view is not
518 /// currently active.
519 ///
520 virtual void CreateLocalInspectorView() = 0;
521
522 protected:
523 virtual ~View();
524};
525
526} // namespace ultralight
#define UExport
Definition Exports.h:25
User-defined interface to handle download-related events for a View.
Definition Listener.h:276
Keyboard event representing a change in keyboard state.
Definition KeyEvent.h:23
User-defined interface to handle load-related events for a View.
Definition Listener.h:160
Mouse event representing a change in mouse state.
Definition MouseEvent.h:18
User-defined interface to handle network-related events for a View.
Definition Listener.h:334
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
Unicode string container with conversions for UTF-8, UTF-16, and UTF-32.
Definition String.h:34
User-defined pixel buffer surface.
Definition Surface.h:47
Web-page container rendered to an offscreen surface.
Definition View.h:202
virtual String url()=0
Get the URL of the current page loaded into this View, if any.
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 RefPtr< JSContext > LockJSContext()=0
Acquire the page's JSContext for use with the JavaScriptCore API.
virtual uint32_t height() const =0
Get the height of the View, in pixels.
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 void LoadURL(const String &url)=0
Load a URL, the View will navigate to it as a new page.
virtual void set_view_listener(ViewListener *listener)=0
Set a ViewListener to receive callbacks for View-related events.
virtual String title()=0
Get the title of the current page loaded into this View, if any.
virtual bool needs_paint() const =0
Whether or not this View should be repainted during the next call to Renderer::Render.
virtual void Resize(uint32_t width, uint32_t height)=0
Resize View to a certain size.
virtual NetworkListener * network_listener() const =0
Get the active NetworkListener, if any.
virtual void set_device_scale(double scale)=0
Set the device scale.
virtual RenderTarget render_target()=0
Get the RenderTarget for the View.
virtual void Stop()=0
Stop all page loads.
virtual ViewListener * view_listener() const =0
Get the active ViewListener, if any.
virtual uint32_t width() const =0
Get the width of the View, in pixels.
virtual void CreateLocalInspectorView()=0
Create an Inspector View to inspect / debug this View locally.
virtual void FireScrollEvent(const ScrollEvent &evt)=0
Fire a scroll event.
virtual void set_download_listener(DownloadListener *listener)=0
Set a DownloadListener to receive callbacks for download-related events.
virtual bool CanGoBack()=0
Whether or not we can navigate backwards in history.
virtual Surface * surface()=0
Get the Surface for the View (native pixel buffer that the CPU renderer draws into).
virtual void set_network_listener(NetworkListener *listener)=0
Set a NetworkListener to receive callbacks for network-related events.
virtual void FireKeyEvent(const KeyEvent &evt)=0
Fire a keyboard event.
virtual LoadListener * load_listener() const =0
Get the active LoadListener, if any.
virtual void FireMouseEvent(const MouseEvent &evt)=0
Fire a mouse event.
virtual double device_scale() const =0
Get the device scale, ie.
virtual bool CanGoForward()=0
Whether or not we can navigate forwards in history.
virtual bool HasInputFocus()=0
Whether or not the View has an input element with visible keyboard focus (indicated by a blinking car...
virtual bool HasFocus()=0
Whether or not the View has focus.
virtual void CancelDownload(DownloadId id)=0
Cancel an active download.
virtual void GoToHistoryOffset(int offset)=0
Navigate to an arbitrary offset in history.
virtual void * JavaScriptVM()=0
Get a handle to the internal JavaScriptCore VM.
virtual void Focus()=0
Give focus to the View.
virtual bool is_loading()=0
Check if the main frame of the page is currently loading.
virtual void Unfocus()=0
Remove focus from the View and unfocus any focused input elements.
virtual DownloadListener * download_listener() const =0
Get the active DownloadListener, if any.
virtual void GoForward()=0
Navigate forwards in history.
virtual void GoBack()=0
Navigate backwards in history.
virtual void set_display_id(uint32_t id)=0
Set the display id of the View.
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 set_load_listener(LoadListener *listener)=0
Set a LoadListener to receive callbacks for Load-related events.
virtual uint32_t display_id() const =0
Get the display id of the View.
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 Reload()=0
Reload current page.
User-defined interface to handle general events for a View.
Definition Listener.h:77
Definition App.h:14
uint32_t DownloadId
A unique identifier representing an active download.
Definition Listener.h:267
Offscreen render target, used when rendering Views via the GPU renderer.
Definition RenderTarget.h:34
View-specific configuration settings.
Definition View.h:28