Skip to content

Constants #

const no_result = unsafe { nil }

fn create #

fn create(opts CreateOptions) &Webview

create creates a new webview instance. Optionally, a debug and window parameter can be passed. If debug is true - developer tools will be enabled (if the platform supports them). The window parameter can be a pointer to the native window handle. If it's non-null, then the WebView is embedded into the given parent window. Otherwise a new window is created. Depending on the platform, a GtkWindow, NSWindow or HWND pointer can be passed here. Returns null on failure. Creation can fail for various reasons such as when required runtime dependencies are missing or when window creation fails.

fn open #

fn open(e &Event) !

open opens a path or URL with the system's default application. Use a bind function to make it available in JavaScript.

Example

w.bind_opt[voidptr]('open', ui.open)

enum Hint #

enum Hint {
	// Width and height are default size.
	@none = C.WEBVIEW_HINT_NONE
	// Window size can not be changed by a user.
	fixed = C.WEBVIEW_HINT_FIXED
	// Width and height are minimum bounds.
	min = C.WEBVIEW_HINT_MIN
	// Width and height are maximum bounds.
	max = C.WEBVIEW_HINT_MAX
}

A Hint that is passed to the Webview 'set_size' method to determine the window sizing behavior.

struct CreateOptions #

@[params]
struct CreateOptions {
pub:
	debug  ?bool
	window voidptr
}

struct Event #

struct Event {
pub:
	instance C.webview_t // Pointer to the events webview instance.
	event_id &char
	args     &char
}

An Event which a V function receives that is called by javascript.

fn (Event) dispatch #

fn (e &Event) dispatch(func fn ())

dispatch posts a function to be executed on the main thread. You normally do not need to call this function, unless you want to tweak the native window. This is a shorthand for e.instance.dispatch().

fn (Event) dispatch_ctx #

fn (e &Event) dispatch_ctx(func fn (ctx voidptr), ctx voidptr)

dispatch_ctx posts a function to be executed on the main thread. You normally do not need to call this function, unless you want to tweak the native window. This is a shorthand for e.instance.dispatch_ctx().

fn (Event) eval #

fn (e &Event) eval(code string)

eval evaluates arbitrary JavaScript code. Evaluation happens asynchronously, also the result of the expression is ignored. Use RPC bindings if you want to receive notifications about the results of the evaluation. This is a shorthand for e.instance.eval().

fn (Event) get_arg #

fn (e &Event) get_arg[T](idx int) !T

get_arg parses the JavaScript argument into a V data type.

struct ServeDevOptions #

@[params]
struct ServeDevOptions {
pub:
	pkg_manager serve.PackageManager // .node || .yarn || .pnpm
	script      string = 'dev' // name of the script(specified in package.json) that runs the dev instance.
}

struct ServeStaticOptions #

@[params]
struct ServeStaticOptions {
pub:
	port u16 = 4321
}

struct Webview #

@[heap]
struct Webview {
mut:
	w    C.webview_t // Pointer to a webview instance.
	proc &os.Process
}

fn (Webview) destroy #

fn (w Webview) destroy()

destroy destroys a webview and closes the native window.

fn (Webview) run #

fn (w &Webview) run()

run runs the main loop until it's terminated. After this function exits - you must destroy the webview.

fn (Webview) terminate #

fn (w &Webview) terminate()

terminate stops the main loop. It is safe to call this function from another other background thread.

fn (Webview) dispatch #

fn (w &Webview) dispatch(func fn ())

dispatch posts a function to be executed on the main thread. You normally do not need to call this function, unless you want to tweak the native window.

fn (Webview) dispatch_ctx #

fn (w &Webview) dispatch_ctx(func fn (ctx voidptr), ctx voidptr)

dispatch_ctx posts a function to be executed on the main thread. You normally do not need to call this function, unless you want to tweak the native window.

fn (Webview) get_window #

fn (w &Webview) get_window() voidptr

get_window returns a native window handle pointer. When using a GTK backend the pointer is a GtkWindow pointer, when using a Cocoa backend the pointer is a NSWindow pointer, when using a Win32 backend the pointer is a HWND pointer.

fn (Webview) set_icon #

fn (w &Webview) set_icon(icon_file_path string) !

set_icon updates the icon of the native window. It supports Windows HWND windows and Linux GTK windows under X11 - under Wayland, window application mapping is based on the desktop file entry name.

Todo: add macOS support

fn (Webview) set_title #

fn (w &Webview) set_title(title string)

set_title updates the title of the native window. Must be called from the UI thread.

fn (Webview) set_size #

fn (w &Webview) set_size(width int, height int, hint Hint)

set_size updates the size of the native window. See WEBVIEW_HINT constants.

fn (Webview) navigate #

fn (w &Webview) navigate(url string)

navigate navigates webview to the given URL. URL may be a properly encoded data URI.

Examples

w.navigate('https://github.com/webview/webview')
w.navigate('data:text/html,%3Ch1%3EHello%3C%2Fh1%3E')
w.navigate('file://${@}/index.html')

fn (Webview) set_html #

fn (w &Webview) set_html(html string)

set_html set webview HTML directly.

fn (Webview) serve_dev #

fn (mut w Webview) serve_dev(ui_path string, opts ServeDevOptions) !

serve_dev uses the given package manger to run the given script name and navigates to the localhost address on which the application is served.

Example

// Runs `npm run dev` in the `ui` directory.
w.serve_dev('ui')!
// Runs `yarn run start` in the `ui` directory (specifying an absolute path).
w.serve_dev(os.join_path(@, 'ui'), pkg_manager: 'yarn', script: 'start')!

fn (Webview) serve_static #

fn (w &Webview) serve_static(ui_build_path string, opts ServeStaticOptions) !

serve_static serves a UI that has been built into a static site on localhost and navigates to it address. Optionally, a port can be specified to serve the site. By default, the next free port from 4321 is used.

fn (Webview) init #

fn (w &Webview) init(code string)

init injects JavaScript code at the initialization of the new page. Every time the webview will open a new page - this initialization code will be executed. It is guaranteed that code is executed before window.onload.

fn (Webview) eval #

fn (w &Webview) eval(code string)

eval evaluates arbitrary JavaScript code. Evaluation happens asynchronously, also the result of the expression is ignored. Use RPC bindings if you want to receive notifications about the results of the evaluation.

fn (Webview) bind #

fn (w &Webview) bind[T](name string, func fn (&Event) T)

bind binds a V callback to a global JavaScript function that will appear under the given name. The callback receives an &Event argument. Internally it uses webview_init().

fn (Webview) bind_opt #

fn (w &Webview) bind_opt[T](name string, func fn (&Event) !T)

bind_opt binds a V callback with a result return type to a global JavaScript function that will appear under the given name. The callback receives an &Event argument. The callback can return an error to the calling JavaScript function. Internally it uses webview_init().

fn (Webview) bind_with_ctx #

fn (w &Webview) bind_with_ctx[T](name string, func fn (e &Event, ctx voidptr) T, ctx voidptr)

bind_with_ctx binds a V callback to a global JavaScript function that will appear under the given name. The callback receives an &Event and a user-provided ctx pointer argument.

fn (Webview) bind_opt_with_ctx #

fn (w &Webview) bind_opt_with_ctx[T](name string, func fn (e &Event, ctx voidptr) T, ctx voidptr)

bind_opt_with_ctx binds a V callback with a result return type to a global JavaScript function that will appear under the given name. The callback receives an &Event and a user-provided ctx pointer argument. The callback can return an error to the calling JavaScript function. Internally it uses webview_init().

fn (Webview) unbind #

fn (w &Webview) unbind(name string)

unbind removes a native C callback that was previously set by webview_bind.