webview

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 16 Imported by: 0

README

Glaze

Go bindings for webview/webview using purego, with no CGO, and prebuilt native libraries for Windows, macOS, and Linux.

Features

  • No CGO
  • Cross-platform (Windows, macOS, Linux)
  • Prebuilt dynamic libraries included
  • Bind Go functions to JavaScript
  • Fully embeddable in pure Go projects

Basic Example

package main

import (
 "log"

 "github.com/crgimenes/glaze"
 _ "github.com/crgimenes/glaze/embedded" // embed native library
)

func main() {
 w, err := glaze.New(true)
 if err != nil {
  log.Fatal(err)
 }
 defer w.Destroy()

 w.SetTitle("Greetings")
 w.SetSize(480, 320, glaze.HintNone)
 w.SetHtml("Hello World!")
 w.Run()
}

See ./examples/bind for an example binding Go functions to JavaScript.

Building for Windows

When building Windows apps, set the following flag: -ldflags="-H windowsgui".

go build -ldflags="-H windowsgui" .

Testing

Run unit tests (default, headless-safe):

go test ./...

Run GUI integration test (requires desktop session and window support):

go test -tags=integration -run TestWebview ./...

Embedded Libraries

This package requires native WebView libraries per-platform. To embed them in your app import the embedded package.

import _ "github.com/crgimenes/glaze/embedded"

Or you can ship your application with .dll, .so, or .dylib files. Ensure these are discoverable at runtime by placing them in the same folder as your executable. For MacOS .app bundles, place the .dylib file into the Frameworks folder.

See the embedded folder for pre-built libraries you can ship with your application.

Helpers for Desktop Applications

BindMethods

BindMethods automatically binds all exported methods of a Go struct as JavaScript functions. Method names are converted from CamelCase to snake_case with a prefix.

type Store struct{}
func (s *Store) GetItems() []string   { return []string{"a", "b"} }
func (s *Store) AddItem(name string)  { /* ... */ }

// Binds: window.api_get_items(), window.api_add_item(name)
bound, err := glaze.BindMethods(w, "api", &Store{})
RenderHTML

RenderHTML renders a Go html/template to a string, suitable for SetHtml(). This lets you reuse Go template definitions without an HTTP server.

tpl := template.Must(template.ParseFiles("ui.html"))
html, err := glaze.RenderHTML(tpl, "main", data)
w.SetHtml(html)
AppWindow

AppWindow wraps an http.Handler in a native window with a local loopback server. This is the easiest way to turn a web application (e.g. a devengine app) into a desktop app:

err := glaze.AppWindow(glaze.AppOptions{
    Title:   "My App",
    Width:   1280,
    Height:  800,
    Debug:   true,
    Handler: mux, // your http.ServeMux
    OnReady: func(addr string) { fmt.Println("Serving on", addr) },
})

The server starts on a random port, the window opens, and when the user closes it the server shuts down automatically.

Local-First Desktop Pattern

For local desktop apps without an HTTP server, expose Go services directly to JavaScript via Bind:

store := &MyStore{}
w, _ := glaze.New(true)
glaze.BindMethods(w, "store", store) // JS calls Go directly
w.SetHtml(myHTML)
w.Run()

See ./examples/desktop and ./examples/filorepl for complete working examples.

Acknowledgements

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppWindow

func AppWindow(opts AppOptions) error

AppWindow creates a native window backed by a local HTTP server.

It starts the server on a random loopback port (or the address specified in opts.Addr), opens a webview pointing to it, and runs the UI event loop. When the user closes the window, the server is shut down and AppWindow returns.

This is the recommended way to wrap a full devengine application as a desktop app — pass the configured http.ServeMux as opts.Handler and everything (templates, assets, routes) works unmodified.

func BindMethods

func BindMethods(w WebView, prefix string, obj any) ([]string, error)

BindMethods binds all exported methods of obj as JavaScript functions. Each method is exposed as window.{prefix}_{MethodName}(args...). Methods must follow the same signature rules as Bind:

  • Return either nothing, a value, an error, or (value, error).

Returns the list of bound function names and the first error encountered.

func RenderHTML

func RenderHTML(tpl *template.Template, name string, data any) (string, error)

RenderHTML executes a named template to a string, suitable for SetHtml(). This allows reusing Go html/template definitions without an HTTP server.

Types

type AppOptions

type AppOptions struct {
	// Title is the window title.
	Title string

	// Width and Height set the initial window dimensions.
	Width  int
	Height int

	// Hint controls window resize behaviour (HintNone, HintMin, HintMax, HintFixed).
	Hint Hint

	// Debug enables the browser developer tools.
	Debug bool

	// Addr is the listen address for the local HTTP server.
	// Defaults to "127.0.0.1:0" (random port on loopback).
	Addr string

	// Handler is the HTTP handler to serve (typically an http.ServeMux).
	Handler http.Handler

	// OnReady is called once the server is listening, with the base URL.
	// Use it to log the address or perform additional setup.
	OnReady func(addr string)
}

AppOptions configures an AppWindow.

type Hint

type Hint int

Hints are used to configure window sizing and resizing.

const (
	// Width and height are default size.
	HintNone Hint = iota

	// Width and height are minimum bounds.
	HintMin

	// Width and height are maximum bounds.
	HintMax

	// Window size can not be changed by a user.
	HintFixed
)

type WebView

type WebView interface {
	// Run runs the main loop until it's terminated. After this function exits -
	// you must destroy the webview.
	Run()

	// Terminate stops the main loop. It is safe to call this function from
	// a background thread.
	Terminate()

	// 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.
	Dispatch(f func())

	// Destroy destroys a webview and closes the native window.
	Destroy()

	// Window returns a native window handle pointer. When using GTK backend the
	// pointer is GtkWindow pointer, when using Cocoa backend the pointer is
	// NSWindow pointer, when using Win32 backend the pointer is HWND pointer.
	Window() unsafe.Pointer

	// SetTitle updates the title of the native window. Must be called from the UI
	// thread.
	SetTitle(title string)

	// SetSize updates native window size. See Hint constants.
	SetSize(w, h int, hint Hint)

	// 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("data:text/html;base64,PGgxPkhlbGxvPC9oMT4=")
	Navigate(url string)

	// SetHtml sets the webview HTML directly.
	// Example: w.SetHtml(w, "<h1>Hello</h1>");
	SetHtml(html string)

	// Init injects JavaScript code at the initialization of the new page. Every
	// time the webview will open a the new page - this initialization code will
	// be executed. It is guaranteed that code is executed before window.onload.
	Init(js 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.
	Eval(js string)

	// Bind binds a callback function so that it will appear under the given name
	// as a global JavaScript function. Internally it uses webview_init().
	// Callback receives a request string and a user-provided argument pointer.
	// Request string is a JSON array of all the arguments passed to the
	// JavaScript function.
	//
	// f must be a function
	// f must return either value and error or just error
	Bind(name string, f any) error

	// Removes a callback that was previously set by Bind.
	Unbind(name string) error
}

func New

func New(debug bool) (WebView, error)

New calls NewWindow to create a new window and a new webview instance. If debug is non-zero - developer tools will be enabled (if the platform supports them).

func NewWindow

func NewWindow(debug bool, window unsafe.Pointer) (WebView, error)

NewWindow creates a new webview instance. If debug is non-zero - developer tools will be enabled (if the platform supports them). Window parameter can be a pointer to the native window handle. If it's non-null - then child 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.

Directories

Path Synopsis
examples
bind command
simple command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL