Skip to content
beetlebugorgPublic

About

Native GUI apps in Go: your program compiles to WebAssembly and runs in a ztk window. The Go binding of the ztk wasm interface.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

Repository files navigation

ztk-go

Native GUI apps in Go. Your Go program compiles to a WebAssembly module, and ztk runs it in a native window on macOS, Windows, and Linux. The widgets are the widgets of Tk: labels, buttons, entries, a canvas, a text widget, and the rest.

The spreadsheet example

package main

import "github.com/beetlebugorg/ztk-go/tk"

//go:wasmexport app_init
func appInit() {
	root := tk.Root()
	root.SetTitle("Hello from Go")
	msg := tk.NewLabel(root, tk.Opts{Text: "Type a name."})
	name := tk.NewEntry(root, tk.Opts{Width: 24})
	greet := tk.NewButton(root, tk.Opts{Text: "Greet", Command: func() {
		msg.SetText("Hello, " + name.Get() + ", from Go!")
	}})
	pad := tk.Pack{PadX: 16, PadY: 8}
	msg.Pack(pad)
	name.Pack(pad)
	greet.Pack(pad)
}

func main() {}
tinygo build -target=wasm-unknown -gc=conservative -opt=z -no-debug -o hello.wasm .
ztk hello.wasm

The module of this app has 7,869 bytes.

How it works

ztk has a WebAssembly interface with the name tk: 91 typed functions for the widgets, the options, the geometry, the canvas, the events, and the timers. A widget is a handle, an option is a case of an enum, and a number goes as a number. No Tcl text crosses the boundary. The package tk of this repository puts these functions in the form of Go: one type for each widget class, option structs, and Go functions as callbacks.

What you need

  • TinyGo 0.44 or later, with wasm-opt of Binaryen on the PATH or in WASMOPT. The standard Go compiler makes a module for WASI or for a browser, with a runtime that this interface does not use.
  • ztk with ztk::wasm::run (the command of ztk#429).

Use the package

go mod init example.com/myapp
go get github.com/beetlebugorg/ztk-go

An app exports app_init, which builds the widgets. The package exports app_event, which runs your callbacks. main stays empty.

The examples

Example What it shows Module
hello A label, an entry, and a button 7,869 bytes
sheet A spreadsheet with formulas, on a canvas with an entry 76,351 bytes
vector A drawing app: shapes, a selection, a drag 16,342 bytes
escape Text with each special character of Tcl, as plain text 9,138 bytes
make                 # builds dist/hello.wasm and the others
make run APP=sheet   # runs one in a window
make test            # runs each example headless with its checks

make test runs the check scripts of tests/: each one drives its app with pointer and key events and reads the widgets. The four examples have 52 checks.

hello vector

The generated file

tk/raw.go has the imports and the enums. The tool tools/wasm-tk-gen.py of ztk writes it from tk.wit, so the file changes only with the interface. tk/tk.go is the part written by hand.

License

MIT. See LICENSE.

About

Native GUI apps in Go: your program compiles to WebAssembly and runs in a ztk window. The Go binding of the ztk wasm interface.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages