Gio is a new open source Go library for writing immediate mode GUI programs that run on all the major platforms: Android, iOS/tvOS, macOS, Linux, Windows. The talk will cover Gio's unusual design and how it achieves simplicity, portability and performance.
Why GUIs?
Last year at GopherCon we asked what the biggest challenges that Go developers faced. Here are the results:
As you heard in the keynote, Modules, generics, and error handling is being handled by the Go team, so I wanted to focus on making writing GUIs in Go easy.
Scatter is a Gio program for end-to-end encrypted messaging over email.
Demo - Scatter
Scatter is a multi-platform messaging application for sending and receiving encrypted chat messages, implementing the Signal protocol over federated email.
Features
I wanted to be able to write a GUI program in GO that I could implement only once and have it work on every platform. This, to me, is the most interesting feature of Gio.
Features:
Immediate mode design.
UI state owned by program.
Only depends on lowest-level platform libraries.
Minimal dependency tree to keep things low level as possible.
Core is 100% Go. OS-specific native interfaces are optional.
Immediate mode UI
Some programs require you to maintain state for your widgetry. In Gio, you draw what you need to draw, you layout what you need to layout, and that’s it!
UI state is owned by the program. Even layout and widget tree.
No callbacks. Events are handled while drawing.
Blank window
This is all you need to render a simple blank window:
This is odd, because you’re doing the event loop in your go routine.
Hello, World
Slightly more advanced example, but in this case you are loading up some support structures and adding text.Label to display your label:
Running Gio programs
Linux, macOS, Windows
Enable modules
export GO111MODULE=on
I recommend you enable for convenience and also because I break the API often, so you will be shielded from updates that could break your application until you are ready to upgrade.
Build, install or run the program
Android
There is a tool to package your application as an APK that you can install through the ads tool to run on a device or simulator.
Install the gio tool:
Install on a connected device or emulator with adb:
iOS/tvOS
For iOS/tvOS devices:
Use the .app file extension for simulators:
Install on a running simulator:
Browsers
To output a directory ready to serve:
Use a webserver or goexec to serve it:
Compile directly with the Go tool or use the Gio tool to build as a web assembly module, but also add the necessary file to supply it to work in your browser.
Operations
The way you communicate each user interface update to gio. Gio has not state so you have to add it to every frame.
Operations buffer and type called ui ops and you add operations to that to your ops buffers which sends to window.draw method.
Operations
Serializing operations
Only the app package depends on platform libraries
Position other operations
Request a redraw
Drawing operations
Set current color or image
Draw with the current color or image
draw.DrawOp{Rect: …}.Add(ops)
Clip operations
Clip drawing to a rectangle
Or to an outline
Input operations
Keyboard and text input
Mouse and touch input
Drawing
Drawing (and animating)
Drawing and animating a clipped square
Layout
If you have non-trivial setup, you need some way to lay them out - you don’t want to use absolute coordinates for each item. Layout assembly helps you structure your user interface. As a result of calling their layout, widgets will give you their own size.
Constraints and dimensions
Constraints are input
Dimensions are output
Widgets accept constraints, output dimensions
Example - two labels
Layout helpers
Can layout to the compass directions or to specific place, like the center.
Aligning
Insetting
Flex layout
Lay out widgets on an axis.
Stack layout
List layout
Input
Input queue and handler keys
Pointer event handling
Takes all available events, updates it’s own state, system can know whether the events belong to this button or not is you register the area rectangle arc with a handler.
Window input queue
The Window’s Queue method returns an input.Queue for OS events.
Gestures
Detect clicks
Determine scroll distance from mouse wheel or touch drag/fling
Widgets
Widgets - the Editor
Complete implementation of a text area field. It’s a complicated widget, but is simple to use. You have to keep state somewhere, but you give it font and font size. Simply call the layout methods and
Initialize the editor
Draw, layout and handle input in one call.
Why Gio?
Gio is:
Simple. Immediate mode design, no hidden state.
Portable. The core of Gio is all Go.
Fast. GPU accelerated, very little per-frame garbage.
Convenient. Develop on desktop, deploy on mobile.
Public domain source (UNLICENCE). Dual licenced MIT to please your lawyers.
Most importantly, Gio needs your help to succeed!
I want to bring Go from a place where GUI programming is a fringe activity to a state where it’s normal to use. Maybe in the future we can bring it to a place where you will choose Go for your GUI programming even if you aren’t interested in Go as a programming language, but because the tooling is so good.
Get Cody, the AI coding assistant
Cody makes it easy to write, fix, and maintain code.