Documentation
¶
Overview ¶
Package desktop provides a native desktop window for dark applications. Import this package only when building desktop apps — it pulls in the WebView native library dependency.
Simple usage with convenience Run:
func init() { runtime.LockOSThread() }
func main() {
app, _ := dark.New(...)
desktop.Run(app.MustHandler(), desktop.WithTitle("My App"))
}
Full usage with Go↔JS bridge and events:
func init() { runtime.LockOSThread() }
func main() {
app, _ := dark.New(...)
dsk := desktop.New(app.MustHandler(),
desktop.WithTitle("My App"),
desktop.WithSize(1280, 800),
desktop.WithMinSize(640, 480),
desktop.WithDebug(true),
)
dsk.Bind("greet", func(name string) string {
return "Hello, " + name
})
dsk.On("save", func(data any) {
fmt.Println("save:", data)
})
dsk.Run()
}
Index ¶
- func Run(handler http.Handler, opts ...Option) error
- type App
- func (a *App) Bind(name string, fn any) error
- func (a *App) BindMethods(prefix string, obj any) error
- func (a *App) Emit(event string, data any)
- func (a *App) Eval(js string)
- func (a *App) On(event string, fn func(data any))
- func (a *App) Ready() <-chan struct{}
- func (a *App) Run() error
- func (a *App) SetSize(w, h int)
- func (a *App) SetTitle(title string)
- func (a *App) Terminate()
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
App wraps an http.Handler in a native desktop window with optional Go↔JS bindings and a bidirectional event system.
func New ¶
New creates a desktop App. Call Bind, BindMethods, and On to configure the Go↔JS bridge, then call Run to open the window.
func (*App) Bind ¶
Bind exposes a Go function as a global JavaScript function. The function appears as window.<name>(...) and returns a Promise.
The function may accept JSON-serializable arguments and return:
- nothing
- a value
- an error
- (value, error)
Call Bind before Run to register bindings. Calling after Run starts applies the binding immediately (thread-safe).
func (*App) BindMethods ¶
BindMethods exposes all exported methods of obj as global JavaScript functions named window.<prefix>_<method_name>(). Method names are converted to snake_case (e.g., GetUser → get_user).
Methods must follow the same signature rules as Bind.
func (*App) Emit ¶
Emit sends a named event with data to the frontend. Listeners registered via window.dark.on(event, callback) will be invoked. Safe to call from any goroutine.
func (*App) On ¶
On registers a handler for events emitted from the frontend via window.dark.emit(event, data). Safe to call before or during Run.
func (*App) Ready ¶
func (a *App) Ready() <-chan struct{}
Ready returns a channel that is closed once the WebView and JS bridge are initialized. Use this to safely wait before calling Emit or other methods from background goroutines:
go func() {
<-dsk.Ready()
dsk.Emit("started", nil)
}()
func (*App) Run ¶
Run starts the internal HTTP server, opens a native WebView window, and blocks until the window is closed.
The caller must pin the main goroutine to the main OS thread:
func init() { runtime.LockOSThread() }
func (*App) SetSize ¶
SetSize changes the window dimensions at runtime. Safe to call from any goroutine.
type Option ¶
type Option func(*config)
Option configures the desktop App.
func WithAddr ¶
WithAddr sets the listen address for the internal HTTP server. Defaults to "127.0.0.1:0" (random port).
func WithMaxSize ¶
WithMaxSize sets the maximum window dimensions.
func WithMinSize ¶
WithMinSize sets the minimum window dimensions.
func WithOnReady ¶
WithOnReady registers a callback invoked with the local URL once the HTTP server is listening.