Documentation
¶
Index ¶
- func AppWindow(opts AppOptions) error
- func BindMethods(w WebView, prefix string, obj any) ([]string, error)
- func Init() error
- func RenderHTML(tpl *template.Template, name string, data any) (string, error)
- type AppOptions
- type AppReadyInfo
- type AppTransport
- type EventHandler
- type Events
- type FileDialogOptions
- type FileFilter
- type Hint
- type Options
- type SchemeHandler
- type SchemeRequest
- type SchemeResponse
- type WebView
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 ¶
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.
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
// Transport selects the backend transport.
// Defaults to AppTransportAuto.
Transport AppTransport
// Addr is the listen address for the local HTTP server.
// Used by AppTransportTCP and defaults to "127.0.0.1:0".
Addr string
// UnixSocketPath is an optional socket path used when Transport is unix.
// If empty, a temporary socket path is generated automatically.
UnixSocketPath string
// Handler is the HTTP handler to serve (typically an http.ServeMux).
Handler http.Handler
// OnReady is called once listeners are up, with the navigable base URL.
// Use it to log the address or perform additional setup.
OnReady func(addr string)
// OnReadyInfo is called once listeners are up, with transport details.
// This is useful to inspect whether backend transport is tcp or unix.
OnReadyInfo func(info AppReadyInfo)
}
AppOptions configures an AppWindow.
type AppReadyInfo ¶
type AppReadyInfo struct {
// URL is the navigable URL used by the embedded browser.
URL string
// Transport is the resolved backend transport in use.
Transport AppTransport
// Backend is the backend listener endpoint.
// - tcp: "ip:port"
// - unix: "/path/to/socket"
Backend string
// Gateway is the loopback gateway endpoint when unix transport is used.
// For tcp transport this matches Backend.
Gateway string
}
AppReadyInfo contains transport details once AppWindow listeners are ready.
type AppTransport ¶
type AppTransport string
AppTransport selects how AppWindow serves HTTP to the embedded browser.
const ( // AppTransportAuto chooses the recommended platform default. // - macOS/Linux: unix backend socket with loopback HTTP gateway. // - Windows: loopback TCP. AppTransportAuto AppTransport = "auto" // AppTransportTCP serves directly over loopback TCP. AppTransportTCP AppTransport = "tcp" // AppTransportUnix serves the application handler over a Unix domain socket. // A lightweight loopback HTTP gateway is created so the embedded browser can // still navigate with a standard http:// URL. AppTransportUnix AppTransport = "unix" )
type EventHandler ¶
type EventHandler func(args ...json.RawMessage)
EventHandler receives the event's arguments, each as the raw JSON the emitter sent, to unmarshal into whatever type the handler expects.
type Events ¶
type Events struct {
// contains filtered or unexported fields
}
Events is a lightweight publish/subscribe bridge between Go and JavaScript, layered entirely on the public Bind/Init/Eval primitives (no extra native code). Create one per WebView with NewEvents, then Emit and subscribe on either side: an event reaches every listener on both sides exactly once, and neither side echoes back to create a loop.
The matching JavaScript API is installed on the page as window.glaze.events:
glaze.events.on("app:ready", (info) => { ... });
glaze.events.emit("ui:save", "untitled.txt");
Events is safe for concurrent use.
func NewEvents ¶
NewEvents installs the events bridge on w and returns the handle used to emit and subscribe from Go. Call it once per WebView, before Run. The error is non-nil only if the underlying Bind fails.
func (*Events) Emit ¶
Emit publishes an event to every listener on both sides. Each value in data becomes one argument delivered to the handlers (Go handlers receive it as raw JSON, JS handlers as a decoded value). It is safe to call from any goroutine; the JS-side listeners are notified on the UI thread. Emit returns an error only if a value in data cannot be JSON-encoded, in which case nothing is published.
func (*Events) On ¶
func (e *Events) On(name string, handler EventHandler) (cancel func())
On subscribes handler to the named event and returns a function that cancels just this subscription. Handlers for a JS-originated event run on the binding goroutine; handlers for a Go-originated event run on the goroutine that called Emit. Re-enter the UI thread with Dispatch if a handler touches the window.
type FileDialogOptions ¶
type FileDialogOptions struct {
// Title overrides the dialog's title.
Title string
// Directory is the initial directory the dialog displays, as a filesystem
// path. Empty uses the platform default (usually the last-used directory).
Directory string
// Filename is the suggested file name. It is used by SaveFile and ignored
// by the open and directory dialogs.
Filename string
// Filters limits the selectable file types. An empty list shows all files.
// Filters are advisory: a platform may present them differently or let the
// user override them.
Filters []FileFilter
}
FileDialogOptions configures a native file dialog. The zero value is valid: it shows a default dialog rooted at the platform's default directory with no type filtering.
type FileFilter ¶
type FileFilter struct {
// Name is the human-readable label for this filter (e.g. "Images").
Name string
// Extensions lists the file extensions WITHOUT the leading dot
// (e.g. {"png", "jpg"}). An empty list, or an entry "*", matches any file.
Extensions []string
}
FileFilter restricts a file dialog to files of a given kind.
type Options ¶
type Options struct {
// Debug enables the platform web inspector / dev tools.
Debug bool
// Window, if non-nil, is an existing native window to embed into (a
// GtkWindow* / NSWindow* / HWND), mirroring NewWindow.
Window unsafe.Pointer
// SchemeHandlers maps a scheme name (without "://", e.g. "app") to its
// handler, registered as a secure context. Handlers must be installed before
// the web view is created, so they cannot be added later.
SchemeHandlers map[string]SchemeHandler
}
Options configures a web view created with NewWithOptions.
type SchemeHandler ¶
type SchemeHandler func(*SchemeRequest) *SchemeResponse
SchemeHandler serves responses for one registered scheme. It runs on the UI thread, so keep it fast (serve from an in-memory FS).
type SchemeRequest ¶
type SchemeRequest struct {
// URL is the full request URL, e.g. "app://host/index.html".
URL string
}
SchemeRequest describes an incoming request for a registered custom scheme.
type SchemeResponse ¶
type SchemeResponse struct {
// Body is the response payload. The backend copies or streams it before the
// handler returns, so it need not outlive the call.
Body []byte
// MIMEType defaults to "application/octet-stream" when empty.
MIMEType string
}
SchemeResponse is what a SchemeHandler returns for a request. A nil response is treated as "not found".
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)
// 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("<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)
// Focus moves keyboard focus into the web content, so typing - and a screen
// reader's cursor - lands inside the page without the user having to click it
// first. Each platform already does this when its window first appears and
// when the window is re-activated; Focus is the explicit, on-demand version
// for pulling focus back into the page. Call it from the UI thread.
Focus()
// 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
// OpenFile shows an "open file" dialog and returns the chosen path, or "" if
// the user cancelled.
OpenFile(opts FileDialogOptions) (string, error)
// OpenFiles shows an "open file" dialog that allows selecting multiple files
// and returns the chosen paths, or nil if the user cancelled.
OpenFiles(opts FileDialogOptions) ([]string, error)
// SaveFile shows a "save file" dialog and returns the chosen path, or "" if
// the user cancelled.
SaveFile(opts FileDialogOptions) (string, error)
// OpenDirectory shows a directory chooser and returns the chosen directory
// path, or "" if the user cancelled.
OpenDirectory(opts FileDialogOptions) (string, error)
}
WebView is the cross-platform handle returned by New and NewWindow. Its methods drive the native window and the embedded web view. Unless a method's own documentation says otherwise, call them from the UI thread (the goroutine that created the first window), and use Dispatch to re-enter that thread from background goroutines.
func NewWindow ¶
NewWindow creates a web view. If window is non-nil it must point to an existing GtkWindow to embed into; otherwise a new window is created.
The first successful call pins the calling goroutine to its OS thread.
func NewWithOptions ¶
NewWithOptions creates a web view configured by opts, including any custom SchemeHandlers (registered on the web view's context and marked as secure).







