WebviewOwebview — OCaml bindings for webview 0.12.
Open a native window backed by the operating system's web engine, load HTML/CSS/JS, and call back and forth between the page's JavaScript and OCaml — no Electron, no bundler.
let () =
let w = Webview.create () in
Webview.set_title w "Hello";
Webview.set_size w ~width:480 ~height:320 Webview.Hint_none;
Webview.set_html w "<h1>Hello from OCaml</h1>";
Webview.run w;
Webview.destroy wval create : ?debug:bool -> unit -> tcreate ?debug () creates a new webview. When debug is true the developer tools are enabled (default false).
val destroy : t -> unitDestroy the webview and free associated resources.
val run : t -> unitRun the main loop. Blocks the calling thread until the window is closed and must be called on the main thread. The OCaml runtime lock is released for the duration so other threads keep running.
val set_title : t -> string -> unitset_title w title sets the window title.
set_size w ~width ~height hint sets the window size in pixels; hint controls how the size is interpreted (see hint).
Navigate to a URL (supports http://, https://, file://, data:).
val set_html : t -> string -> unitLoad the given HTML string as the document.
val init : t -> string -> unitInject JS to be run on every page load, before page scripts.
val eval : t -> string -> unitEvaluate JS in the current page.
Expose OCaml functions to the page as window.<name>(...) and resolve the JavaScript promises they return.
val bind : t -> string -> (string -> string -> unit) -> unitbind w name f exposes a JS function window.name(...) that calls back into f id req, where req is a JSON array string of the JS arguments. The callback must eventually answer with return (using id).
The closure is kept alive as a GC root until the binding is removed with unbind or the webview is destroyed. Raises Failure if a binding with the same name already exists.
val unbind : t -> string -> unitunbind w name removes the binding name created with bind, releasing the closure's GC root. Raises Failure if no such binding exists.
val return : t -> string -> error:bool -> result:string -> unitreturn w id ~error ~result resolves (or rejects, if error) the JS promise associated with the call id. result must be a JSON value.
type version_info = {major : int;minor : int;patch : int;version_number : string;SemVer "MAJOR.MINOR.PATCH" string
pre_release : string;SemVer pre-release labels, or ""
build_metadata : string;SemVer build metadata, or ""
}The library's version information, as returned by version.
val version : unit -> version_infoThe webview library's version information.
The kind of native handle to retrieve with get_native_handle, mirrors WEBVIEW_NATIVE_HANDLE_KIND_*.
val get_window : t -> nativeintget_window w returns the native top-level window handle as a pointer (0n if unavailable). Interpret it with platform-specific FFI.
val get_native_handle : t -> native_handle_kind -> nativeintget_native_handle w kind returns the requested native handle as a pointer (0n if unavailable).
val set_app_icon : t -> string -> unitset_app_icon w path sets the application/window icon from an image file, so a plain executable shows a custom icon instead of the generic one:
w is ignored).WM_SETICON. The file is decoded with WIC, so any format Windows imaging supports works (.png, .jpg, .bmp, .gif, .tif, .ico); it is rescaled to the system icon sizes.Raises Failure if the image cannot be loaded; a no-op on other backends.
On macOS the process only becomes a regular (Dock-visible) app once run has started, so call this once the app is active — e.g. from a dispatch callback — otherwise the Dock ignores it.
Linux/Wayland: this alone is not enough to get a custom Dock icon. set_app_icon only sets the X11 _NET_WM_ICON property, which does not exist under native Wayland (the default on e.g. GNOME/Fedora); the Dock there resolves an app's icon from an installed .desktop file instead. Pair this with set_app_id and an installed .desktop file whose id (or StartupWMClass=) matches.
set_app_id id sets the process-wide application id used by the windowing system to associate this process's windows with an installed .desktop file:
g_set_prgname), which becomes the X11 WM_CLASS class name, and, under Wayland, the toplevel's app_id. Desktop shells (e.g. GNOME) match this id against an installed .desktop file's id or its StartupWMClass= to decide which icon to show in the Dock/taskbar.Call this once, before create, so it applies to the first window realized. See set_app_icon for why this matters on Linux/Wayland.
module Utils : sig ... endFilesystem helpers for locating on-disk assets (HTML/CSS/JS) relative to the running executable, independently of the current working directory.