Skip to main content

drift

Package drift provides the main entry point for Drift applications.

func Dispatch

func Dispatch(callback func())

Dispatch schedules a callback to run on the UI thread during the next frame and is safe to call from any goroutine.

Example:

This example shows how to dispatch work to the UI thread from a background goroutine. Use Dispatch when you need to update UI state from async operations like network calls.

package main

import (
"github.com/go-drift/drift/pkg/drift"
)

func main() {
// Simulating an async operation that needs to update UI
go func() {
// ... do some work in the background ...

// Schedule UI update on the main thread
drift.Dispatch(func() {
// This code runs on the UI thread and can safely update state
})
}()
}

func Run

func Run(app App)

Run initializes the Drift engine with the given App configuration.

type App

App defines the configuration for a Drift application.

type App struct {
// Root is the root widget of the application.
Root core.Widget
// Theme is the application theme. Defaults to DefaultLightTheme if nil.
Theme *theme.ThemeData
// DeviceScale is the device pixel ratio. Defaults to 1.0 if zero.
DeviceScale float64
// Diagnostics enables the performance diagnostics HUD overlay.
// Use engine.DefaultDiagnosticsConfig() for sensible defaults.
Diagnostics *engine.DiagnosticsConfig
// OnInit is called once in a background goroutine before the root widget
// is mounted. Use it for one-time setup such as opening a database,
// loading configuration, or restoring authentication state.
//
// While OnInit executes, the engine defers root mounting so the
// platform's native splash screen stays visible. When OnInit returns
// nil, the root widget mounts on the next frame. If OnInit returns a
// non-nil error, a [widgets.DebugErrorScreen] is shown instead.
// Tapping "Restart" on the error screen mounts the root widget
// directly; OnInit is not retried.
//
// The provided context is cancelled when the app is disposed (see
// OnDispose). Long-running work inside OnInit should select on
// ctx.Done() to exit promptly during teardown.
OnInit func(ctx context.Context) error

// OnDispose is called once when the app lifecycle reaches
// [platform.LifecycleStateDetached]. Use it to release resources
// acquired in OnInit, such as closing database connections or
// flushing caches.
//
// The context passed to OnInit is cancelled before OnDispose runs,
// so any in-flight OnInit work observes cancellation first.
// OnDispose is guaranteed to run at most once, even if the
// lifecycle transitions to Detached multiple times.
OnDispose func()
}

Example (With On Init):

This example shows how to run async setup before the widget tree mounts. OnInit runs in a background goroutine; the splash screen stays visible until it completes. OnDispose runs when the app is detached.

package main

import (
"context"

"github.com/go-drift/drift/pkg/drift"
"github.com/go-drift/drift/pkg/widgets"
)

func main() {
root := widgets.Center{
Child: widgets.Text{Content: "My App"},
}

app := drift.NewApp(root)
app.OnInit = func(ctx context.Context) error {
// Open database, load config, restore auth, etc.
return nil
}
app.OnDispose = func() {
// Close database, flush caches, etc.
}
_ = app
}

Example (With Theme):

This example shows how to create an app with a custom theme.

package main

import (
"github.com/go-drift/drift/pkg/drift"
"github.com/go-drift/drift/pkg/theme"
"github.com/go-drift/drift/pkg/widgets"
)

func main() {
root := widgets.Center{
Child: widgets.Text{Content: "Dark Mode App"},
}

app := drift.App{
Root: root,
Theme: theme.DefaultDarkTheme(),
DeviceScale: 2.0, // For high-DPI displays
}
_ = app
}

func NewApp

func NewApp(root core.Widget) App

NewApp creates a default App with the given root widget.

Example:

This example shows how to create and configure a Drift application.

package main

import (
"github.com/go-drift/drift/pkg/drift"
"github.com/go-drift/drift/pkg/widgets"
)

func main() {
// Create the root widget for the application
root := widgets.Center{
Child: widgets.Text{Content: "Hello, Drift!"},
}

// Create an app with default settings
app := drift.NewApp(root)
_ = app
}

func (App) Run

func (app App) Run()

Run starts the app using the package-level runtime.

Generated by gomarkdoc