docs

Creating Views

Create offscreen Views, configure display and rendering settings, and update them at runtime.

On this page

A View loads and renders web-pages to an offscreen target. It is completely isolated from the OS windowing system and must be forwarded all input events.

You create Views through the Renderer, drawing content either to a CPU pixel buffer or GPU render-target.

📘 Views in AppCore

When using AppCore, each Panel creates a View for you. See Laying Out Panels.

Creating a View

To create a View, call Renderer::CreateView() on the Renderer's thread:

C++
RefPtr<View> view;

void CreateView() {
  ///
  /// An 800x600 View with default settings, using the default
  /// Session (nullptr).
  ///
  view = renderer->CreateView(800, 600, ViewConfig(), nullptr);

  view->LoadURL("file:///app.html");
}

Dimensions are specified in device pixels.

Renderer::CreateView() returns a RefPtr<View> (a ref-counted pointer to the View). You should retain the pointer to keep the View alive.

Call View::LoadURL() or View::LoadHTML() to begin loading content into the View. To know when the page is ready, see Handling View Events.

📘 Sessions and Site Data

Site data like cookies, local storage, and cached resources are stored in the View's Session. You should specify a Session during creation or pass nullptr to use the default Session. See Sessions and Site Data.

Configuring a View

A ViewConfig struct defines initial settings for a View and remain fixed unless the View provides a runtime setter.

Most applications configure three common settings: is_accelerated for GPU rendering, initial_device_scale for high-DPI displays, and is_transparent for transparent overlays.

When using is_transparent, the page must also clear its own background in CSS:

CSS
html, body { background: transparent; }

The following function configures an accelerated, transparent View for a 2x display:

C++
void CreateHudView() {
  ViewConfig config;
  config.is_accelerated = true;
  config.initial_device_scale = 2.0;
  config.is_transparent = true;

  ///
  /// The page lays out at 640x360 and renders 1280x720 pixels.
  ///
  view = renderer->CreateView(1280, 720, config, nullptr);
}

🚧 Register a GPU Driver First

You must register a GPUDriver on Platform before creating an accelerated View. Creating an accelerated View without a GPU driver terminates the process with an error. See GPU Renderer Overview.

ViewConfig Options

The table below lists every option available on ViewConfig and its default value.

Field Default Description
display_id 0 Display identifier whose Renderer::RefreshDisplay() call advances this View's animations. See Updating and Rendering.
is_accelerated false Renders on the GPU to a texture instead of the CPU to a Surface.
initial_device_scale 1.0 Number of device pixels per CSS pixel.
is_transparent false Enables a transparent background for the View.
background_color Unset (opaque white) Base color drawn beneath the page while it loads and wherever the page paints nothing. See Dark Mode and Color Schemes.
preferred_color_scheme ColorScheme::Auto Color scheme reported to the page through prefers-color-scheme. ColorScheme::Auto follows the system scheme, which remains Light until you call Renderer::set_system_color_scheme(). See Dark Mode and Color Schemes.
initial_focus true Starts the View with input focus. See Keyboard Focus and Editable State.
enable_images true Enables loading and displaying images.
enable_javascript true Runs scripts on the page. The DOM API and data bindings work without it. See About the DOM API.
enable_compositor true Separates 3D transforms, animated transforms, opacity changes, video, and will-change elements into composited layers.
enable_compositor_debug_info false Displays layer borders, tile boundaries, and repaint counters. Requires enable_compositor. See Profiling and Tracing.
enable_canvas_filters true Enables the HTML5 2D canvas filter property.
match_native_editing_behavior false Follows host OS text editing and selection conventions. See Text Editing and the Clipboard.
clipboard_read_policy ClipboardReadPolicy::AllowForAppContent Controls which pages may read the clipboard from JavaScript. See Text Editing and the Clipboard.
enable_hidden_timer_throttling false Throttles repeating JavaScript timers to approximately 1 Hz while the View is hidden. See Optimizing Performance.
max_render_fps 0 (no per-View cap) Caps the animation and repaint frame rate for this View. The edition's maximum frame rate still applies. See Optimizing Performance.
javascript_can_open_windows_automatically false Allows scripts to call window.open() without a user gesture. See Handling View Events.
allow_universal_access_from_file_urls true Allows local file:/// pages to bypass cross-origin restrictions. See Shipping Your App.
font_family_standard "Times New Roman" Default font family for text that does not specify one. See Text Rendering and Fonts.
font_family_fixed "Courier New" Default font family for monospace text such as pre and code.
font_family_serif "Times New Roman" Default font family for the CSS serif generic.
font_family_sans_serif "Arial" Default font family for the CSS sans-serif generic.
font_family_cursive "Comic Sans MS" Default font family for the CSS cursive generic.
font_family_fantasy "Impact" Default font family for the CSS fantasy generic.
font_family_pictograph Empty Default font family for the CSS -webkit-pictograph generic. When empty, falls back to font_family_standard.
font_size_default 16 Default font size in pixels.
font_size_fixed 13 Default font size for monospace text in pixels.
user_agent Library default Custom User-Agent header string. The Free edition always uses the default string.

Updating Settings at Runtime

You can update several View properties after creation using dedicated methods on View.

Method Description
View::Resize(width, height) Resizes the View to new dimensions in device pixels.
View::Resize(width, height, device_scale) Updates dimensions and device scale in a single pass.
View::set_device_scale() Updates the device scale factor.
View::set_display_id() Updates the display ID when moving the View to another monitor.
View::set_visible() Shows or hides the View. Hiding stops painting and pauses animations.
View::set_max_render_fps() Sets an animation and repaint frame-rate cap, or 0 to remove it.
View::set_preferred_color_scheme() Updates the preferred color scheme reported to the page.
View::set_compositor_debug_info_enabled() Toggles the compositor debug overlay.

Every other ViewConfig property is fixed at creation. To change any other setting, you must destroy the View and create a new one.