package owebview

  1. Overview
  2. Docs
OCaml bindings for the webview library

Install

dune-project
 Dependency

Authors

Maintainers

Sources

0.1.0.tar.gz
md5=96958382fcfef04cc489a87b392abd65
sha512=0494c253643890138099fb14023476d4fef95299449c118301461efa0d2e316bf37a326a76bcf8d29512cdb621c144d06deb76ac5e22adeab76f9fae8a443d24

Description

OCaml bindings for the webview library (https://github.com/webview/webview): create a native desktop window rendered by the system web engine, navigate to a URL or to inline HTML, and call back and forth between OCaml and JavaScript. The binding covers the full webview 0.12 C API and stays deliberately low-level; the C header is vendored, so nothing is fetched at build time.

The native dependencies are the ones of the system web engine: WebKit and Cocoa on macOS (provided by the system, nothing to install), gtk+-3.0 and webkit2gtk-4.1 on Linux (declared as depexts for common distributions), and WebView2 on Windows (the runtime ships with Windows 10 and 11; the SDK headers are located through the NuGet cache at build time).

README

Owebview

Build a tiny native desktop window with a web UI, straight from OCaml — powered by webview.

No Electron, no bundler: create a window, drop in some HTML, and you have an app. Here's the whole thing:

let () =
  let w = Webview.create () in
  Webview.set_title w "My first owebview app";
  Webview.set_size w ~width:480 ~height:320 Webview.Hint_none;
  Webview.set_html w
    {|<!doctype html>
      <html>
        <body style="font-family: system-ui; text-align: center">
          <h1>Hello from OCaml 👋</h1>
          <p>Rendered by webview.</p>
        </body>
      </html>|};
  Webview.run w;
  Webview.destroy w

See it run

Clone the repo and launch the bundled example, hellowv:

git clone https://github.com/korkorran/Owebview.git
cd Owebview

on Windows :

nuget install Microsoft.Web.WebView2
opam install . --deps-only
dune exec examples/hellowv/hellowv.exe

A window pops up with two buttons wired to OCaml: one adds two numbers, the other reports your OS. The example loads its UI from real .html / .css / .js files in examples/hellowv/web/ — peek at examples/hellowv/hellowv.ml to see how JavaScript calls back into OCaml.

Use it in your own project

Pin the library with opam (the webview.h header is vendored, nothing to fetch):

opam pin add owebview https://github.com/korkorran/Owebview.git

Then depend on it from your dune file:

(executable
 (name main)
 (libraries owebview))

Drop the example above into main.ml and run dune exec ./main.exe. That's it.

A little further

Once HTML rendering works, the fun part is the OCaml ↔ JavaScript bridge:

(* Expose window.add(a, b) to the page; the result is a JS Promise. *)
Webview.bind w "add" (fun id req ->
    let result =
      match Scanf.sscanf_opt req "[%d,%d]" (fun a b -> a + b) with
      | Some n -> string_of_int n
      | None -> "null"
    in
    Webview.return w id ~error:false ~result)

Other handy entry points: Webview.navigate (load a URL or a local file:// page), Webview.init / Webview.eval (inject JavaScript), and Webview.terminate (close the window from code). The full API lives in lib/webview.mli.

Native dependencies

webview uses the system web engine, so you need its native libraries:

  • macOS — WebKit / Cocoa, already provided by the system. Nothing to install.
  • Linuxgtk+-3.0 and webkit2gtk-4.1 (the -dev packages). They are declared as opam depexts, so opam pin will offer to install them.
  • Windows — WebView2. Compilation works via the MinGW toolchain: install the WebView2 SDK with NuGet (nuget install Microsoft.Web.WebView2) and the WebView2.h header is picked up automatically from the NuGet cache (or set MICROSOFT_WEB_WEBVIEW2 to the package directory). At run time the WebView2 Runtime must be present — it ships with Windows 10/11.

The platform-specific compile/link flags are detected automatically at build time — via pkg-config on Linux, and from the NuGet cache on Windows — so there's nothing to tweak by hand.

Contributing

Feedback is very welcome! This binding is developed and tested mainly on macOS, so reports about building and running it on Linux distributions are especially valuable — does it compile, do the depexts resolve, does the webkit2gtk-4.1 backend behave as expected on your distro?

If you give it a try on Linux, please open an issue with your distribution, what worked and what didn't (build logs welcome). Pull requests improving cross-platform support are happily accepted.

License

MIT.

Dependencies (5)

  1. conf-gtk3-webkit os != "macos" & os != "win32"
  2. conf-c++
  3. dune-configurator >= "3.0" & build
  4. ocaml >= "4.08"
  5. dune >= "3.0"

Dev Dependencies (1)

  1. odoc with-doc

Used by

None

Conflicts

None