Native UI
Native UI
A window can show a user interface that MyGo draws itself instead of a web
page. You write it in Go with package ui: there is no HTML, no JavaScript
and no frontend build, and the window starts no webview, so it opens at
once and uses little memory. MyGo draws it on the GPU, with Metal on macOS,
Direct3D 11 on Windows and OpenGL on Linux.
package main
import (
"fmt"
"log"
"github.com/egoist/mygo"
"github.com/egoist/mygo/ui"
)
// counter is the app's state.
type counter struct{ n int }
// view builds the window's interface from the state, for every frame.
func (app *counter) view(c *ui.Context) {
ui.Column(c).Fill().Center().Gap(12).Children(func() {
ui.Text(c, fmt.Sprint(app.n)).FontSize(40).Bold()
if ui.PrimaryButton(c, "Increment").Clicked() {
app.n++
}
})
}
func main() {
app := &counter{}
mygo.App.WhenReady(func() {
mygo.NewWindow(mygo.WindowOptions{
Title: "Counter",
Width: 320,
Height: 240,
Content: ui.View(app.view),
})
})
if err := mygo.App.Run(); err != nil {
log.Fatal(err)
}
}Reading the examples. The examples in these guides are parts of a view
such as counter.view: c is the view's *ui.Context, and app its
receiver, the value of your own type that holds the state. A field such as
app.volume or a method such as app.save() is one you declare on that
type, as counter declares n; Views says more.
mygo init -template native my-app starts a project of native UI (see
the CLI). In a clone of the repository,
examples/counter-native is a counter with a test of its view
(go run ./examples/counter-native), and go run ./examples/gallery
tours what the toolkit does.
One app can have windows of both kinds. Native UI suits tools, settings, inspectors and utilities, and apps that must start instantly; a web page suits rich documents, existing web code and anything that needs what only a browser has. Screen readers and other assistive technology read native UI as they read other apps (see Accessibility).