zig-webui
zig-webui

webui-dev/zig-webui

MIT

Use any web browser or WebView as GUI, with Zig in the backend and modern web technologies in the frontend, all in a lightweight portable library.

426 426 15 6
9
build.zig.zon  build.zig 
View on Github  
Updated: 4:16:45 AM Fri Nov 08 2024 Size: 1831KB Created: 8:21:26 PM Fri May 12 2023
Dependencies:
webui
zig  fetch  --save  git+https://github.com/webui-dev/zig-webui

WebUI Zig v2.5.0-beta.2

Use any web browser or WebView as GUI, with Zig in the backend and modern web technologies in the frontend, all in a lightweight portable library.

Features

  • Portable (Needs only a web browser or a WebView at runtime)
  • One header file
  • Lightweight (Few Kb library) & Small memory footprint
  • Fast binary communication protocol
  • Multi-platform & Multi-Browser
  • Using private profile for safety
  • Cross-platform WebView

API Documentation

Examples

There are several examples for newbies, they are in the examples directory.

You can use zig build --help to view all buildable examples.

Like zig build run_minimal, this will build and run the minimal example.

Installation

Zig 0.11

  1. Add this to build.zig.zon
.@"zig-webui" = .{
        // It is recommended to replace the following branch with commit id
        .url = "https://github.com/webui-dev/zig-webui/archive/main.tar.gz",
        .hash = <hash value>,
    },

This tells zig to fetch zig-webui from a tarball provided by GitHub. Make sure to replace the COMMIT part with an actual commit SHA in long form, like 219faa2a5cd5a268a865a1100e92805df4b84610. Every time you want to update zig-webui you'll have to update this commit.

  1. Config build.zig

Add this:

const zig_webui = b.dependency("zig-webui", .{
    .target = target,
    .optimize = optimize,
    .enable_tls = false, // whether enable tls support
    .is_static = true, // whether static link
});

// add module
exe.addModule("webui", zig_webui.module("webui"));

// link library
exe.linkLibrary(zig_webui.artifact("webui"));

Zig 0.12 \ 0.13.0 \ nightly

To be honest, I don’t recommend using the nightly version because the API of the build system is not yet stable, which means that there may be problems with not being able to build after nightly is updated.

  1. Add to build.zig.zon
# It is recommended to replace the following branch with commit id
zig fetch --save https://github.com/webui-dev/zig-webui/archive/main.tar.gz
# Of course, you can also use git+https to fetch this package!
  1. Config build.zig

Add this:

// To standardize development, maybe you should use `lazyDependency()` instead of `dependency()`
// more info to see: https://ziglang.org/download/0.12.0/release-notes.html#toc-Lazy-Dependencies
const zig_webui = b.dependency("zig-webui", .{
    .target = target,
    .optimize = optimize,
    .enable_tls = false, // whether enable tls support
    .is_static = true, // whether static link
});

// add module
exe.root_module.addImport("webui", zig_webui.module("webui"));

It is not recommended to dynamically link libraries under Windows, which may cause some symbol duplication problems. see this issue: https://github.com/ziglang/zig/issues/15107

Windows without console

For hide console window, you can set exe.subsystem = .Windows;!

UI & The Web Technologies

Borislav Stanimirov discusses using HTML5 in the web browser as GUI at the C++ Conference 2019 (YouTube).

Web application UI design is not just about how a product looks but how it works. Using web technologies in your UI makes your product modern and professional, And a well-designed web application will help you make a solid first impression on potential customers. Great web application design also assists you in nurturing leads and increasing conversions. In addition, it makes navigating and using your web app easier for your users.

Why Use Web Browsers?

Today's web browsers have everything a modern UI needs. Web browsers are very sophisticated and optimized. Therefore, using it as a GUI will be an excellent choice. While old legacy GUI lib is complex and outdated, a WebView-based app is still an option. However, a WebView needs a huge SDK to build and many dependencies to run, and it can only provide some features like a real web browser. That is why WebUI uses real web browsers to give you full features of comprehensive web technologies while keeping your software lightweight and portable.

How Does it Work?

Think of WebUI like a WebView controller, but instead of embedding the WebView controller in your program, which makes the final program big in size, and non-portable as it needs the WebView runtimes. Instead, by using WebUI, you use a tiny static/dynamic library to run any installed web browser and use it as GUI, which makes your program small, fast, and portable. All it needs is a web browser.

Runtime Dependencies Comparison

Tauri / WebView Qt WebUI
Runtime Dependencies on Windows WebView2 QtCore, QtGui, QtWidgets A Web Browser
Runtime Dependencies on Linux GTK3, WebKitGTK QtCore, QtGui, QtWidgets A Web Browser
Runtime Dependencies on macOS Cocoa, WebKit QtCore, QtGui, QtWidgets A Web Browser

Supported Web Browsers

Browser Windows macOS Linux
Mozilla Firefox ✔️ ✔️ ✔️
Google Chrome ✔️ ✔️ ✔️
Microsoft Edge ✔️ ✔️ ✔️
Chromium ✔️ ✔️ ✔️
Yandex ✔️ ✔️ ✔️
Brave ✔️ ✔️ ✔️
Vivaldi ✔️ ✔️ ✔️
Epic ✔️ ✔️ not available
Apple Safari not available coming soon not available
Opera coming soon coming soon coming soon

Supported WebView

WebView Status
Windows WebView2 ✔️
Linux GTK WebView ✔️
macOS WKWebView ✔️

License

Licensed under the MIT License.