The BlinkWebView widget

Author: BlinkGTK Project
Version: 1.2.0-build2

BlinkWebView is the GTK4 widget that displays what Chromium/Blink renders.
It derives from GtkWidget, so you handle it like any other GTK widget — put it
in a container, connect signals, read and write properties.

This page covers the widget as a whole. Argument-by-argument detail lives in the
C API reference; task-oriented recipes live in the
How do I…? index.

The type

#define BLINK_TYPE_WEB_VIEW (blink_web_view_get_type())
G_DECLARE_FINAL_TYPE(BlinkWebView, blink_web_view, BLINK, WEB_VIEW, GtkWidget)

It is a final type, not a derivable one. You shape its behaviour through
signals and handlers rather than by subclassing.

The cast macro BLINK_WEB_VIEW() and the type check BLINK_IS_WEB_VIEW() are
available.

Parent class GtkWidget
Header #include <blink_gtk/blink_gtk.h>
pkg-config blinkgtk-0.1

Creating one

Three constructors. Pick by what you need.

Function Returns When to use it
blink_web_view_new() BlinkWebView (GtkWidget*) The usual choice. You place it yourself
blink_web_view_new_container() GtkOverlay wrapping a BlinkWebView When you would rather not wire up the overlay yourself
blink_web_view_new_with_gpu_mode() BlinkWebView (GtkWidget*) To pin the rendering path explicitly

Note that blink_web_view_new_container() returns a GtkOverlay. Get the
BlinkWebView inside it with:

GtkWidget *overlay = blink_web_view_new_container();
BlinkWebView *view = g_object_get_data(G_OBJECT(overlay), "blinkgtk-webview");

For the rendering path, see the Settings API.

The smallest working program

#include <gtk/gtk.h>
#include <blink_gtk/blink_gtk.h>

int main(int argc, char **argv) {
    /* Initialise the engine before GTK. Chromium starts here */
    if (!blink_gtk_init(&argc, &argv)) {
        g_printerr("blink_gtk_init() failed\n");
        return 1;
    }

    GtkWidget *window = gtk_window_new();
    gtk_window_set_default_size(GTK_WINDOW(window), 1024, 768);

    GtkWidget *view = blink_web_view_new();
    gtk_window_set_child(GTK_WINDOW(window), view);

    blink_web_view_load_uri(BLINK_WEB_VIEW(view), "https://example.com/");
    gtk_window_present(GTK_WINDOW(window));

    /* Run this, not GTK's own main loop.
     * g_application_run() will not start rendering */
    return blink_gtk_run_main_loop();
}
gcc -o app app.c $(pkg-config --cflags --libs blinkgtk-0.1)

The point to remember is running blink_gtk_run_main_loop(). A
GtkApplication built around g_application_run() never turns Chromium's loop,
so the navigation never commits and nothing appears. It compiles and starts
cleanly, which is what makes it hard to spot. For the GtkApplication shape,
see Integrate into your application.

Signals

Seven, all implemented.

Signal Emitted when Detail
load-changed The load reaches a new stage Signals API
load-failed The load fails Signals API
uri-changed The displayed URI changes Signals API
title-changed The page title changes Signals API
message-received The page's JavaScript sends a message App integration API
new-window-requested A new window is requested UI handlers API
permission-request Geolocation, notifications and the like are requested UI handlers API
static void on_title(BlinkWebView *view, const char *title, gpointer data) {
    gtk_window_set_title(GTK_WINDOW(data), title);
}

g_signal_connect(view, "title-changed", G_CALLBACK(on_title), window);

All but permission-request appear in the GObject Introspection data, so Python
and Rust connect to them under the same names.

Properties

Six, readable and writable through g_object_get() / g_object_set().

Property Type Access Equivalent function
uri gchar* read blink_web_view_get_uri()
title gchar* read blink_web_view_get_title()
is-loading gboolean read blink_web_view_is_loading()
zoom-level gdouble read/write blink_web_view_set_zoom_level()
gpu-mode enum read blink_web_view_get_gpu_mode()
devtools-locale gchar* read/write blink_gtk_set_devtools_locale()
g_autofree char *uri = NULL;
g_object_get(view, "uri", &uri, NULL);

g_object_set(view, "zoom-level", 1.25, NULL);

Being properties, they bind to other widgets with g_object_bind_property().

/* Keep the window title in step with the page */
g_object_bind_property(view, "title", window, "title", G_BINDING_SYNC_CREATE);

What it can do

There are 73 public functions that operate on a BlinkWebView. Entry points by
subject:

What you want Where to look
Open a URL, go back and forward, reload Navigation API
Run JavaScript, exchange messages with the page, serve a custom URL scheme App integration API
In-page search, zoom Page search & zoom API
Provide your own dialogs, file choosers, downloads and permission prompts UI handlers API
Fonts, User-Agent, content protection, rendering path Settings API
Attach DevTools DevTools API
Print to PDF, screenshots, cookies C API reference

To go from a goal to a function name, the How do I…? index
is quicker.

Coming from WebKitGTK

As a widget it sits where WebKitWebView sits, and the rhythm of placing it in
a container and connecting signals is the same. The function names and the
objects they act on differ
, however. For the mapping table and rewritten
examples, see
Migrating from WebKitGTK.

Startup is where the two genuinely diverge: BlinkGTK boots Chromium in
blink_gtk_init() and turns the loop in blink_gtk_run_main_loop().

History

Version Change
1.2.0-build2 Rewritten against the public C API. The previous text documented an internal C++ class (LoadURL() and friends), so code written from it would not build. Also corrected the claim that the seven signals and six properties were "planned for a future version" — they are implemented