Owebview – Cross-Platform Desktop Apps in OCaml, with a Web UI

Talk

SPEAKERS

Frédéric Lang
Frédéric Lang

ABSTRACT

OCaml offers plenty of ways to compute something and rather few to put the result in a window somebody can open. owebview takes the cheapest route available: a native window hosting the web engine the machine already has — WKWebView on macOS, WebKitGTK on Linux, WebView2 on Windows. Nothing is bundled, so an application weighs a few megabytes instead of a hundred and fifty, and the rendering is somebody else's problem.

This talk is about what that buys you, and what it costs.

One window, two languages — or one. Webview.bind publishes an OCaml function to the page, which calls it and gets a promise back. Arguments and results cross as JSON text, which sounds like a limitation and turns out to be the useful kind of primitive: bring the serialisation library you already like and a typed FFI is a dozen lines. The page can be JavaScript, or it can be OCaml compiled by js_of_ocaml — at which point the whole application is one language with a rendering engine in the middle. We will also look at the one rule the bridge imposes, that the UI loop owns the main thread, and at what it takes to run a real backend beside it.

A window is not yet an application. That is what owebview.desktop is for, and most of what the 0.2 release adds: native menu bars whose items are OCaml closures, with keyboard accelerators; modal dialogs on the platform's own widgets — confirm, open a file, choose a directory, save as; the application icon, and the application id, which is more subtle than it sounds the first time a user runs your program under Wayland; and locating your assets, which is not the same directory in a build tree, in an opam switch and inside a macOS bundle.

Under the window, briefly, because it is the part that every guide to C bindings already covers well: one OCaml API over three entirely different native backends, chosen at build time by dune-configurator; C++ stubs that CAMLprim will not save from name mangling; a vendored header that dune's sandbox refuses to let you include until you ask.

And out of it. An OCaml desktop application can now be handed over two ways: opam pin for anyone who has a switch, and a .deb, .rpm, .dmg or Windows installer for everyone else. They are not the same artefact, and the difference is not only in how it is delivered — an installer tells the desktop what the program is, while a binary dropped into a switch has to say so itself. That turns out to be where the desktop library earns its place.

What you will take away

  • When a web UI is the right answer for a desktop application in OCaml, and when it is not.
  • How the bridge between OCaml and the page works, what can cross it, and how to build something typed on top of a string.
  • What owebview.desktop provides — menus, dialogs, application icon and id, asset location — and the problem each one exists to solve.
  • The two ways to put an OCaml GUI application in somebody's hands, and what each of them expects your program to do for itself.

The example application

Sun notes is a note-taking application written entirely in OCaml — both sides of it: a native window driven by owebview, and a page compiled with js_of_ocaml and driven by ocaml-vdom. It serves throughout the talk as the concrete thing being shipped, and every example is taken from it.

It is MIT-licensed and installable today on every platform discussed: a .dmg for macOS, .deb and .rpm packages for Linux on x86-64 and ARM64, a Windows installer — and, for anyone who already has an opam switch, a single opam pin away.