/
main.go
64 lines (53 loc) · 1.63 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// SPDX-License-Identifier: Unlicense OR MIT
package main
import (
"image/color"
"log"
"os"
"gioui.org/app" // app contains Window handling.
"gioui.org/font/gofont" // gofont is used for loading the default font.
"gioui.org/io/system" // system is used for system events (e.g. closing the window).
"gioui.org/layout" // layout is used for layouting widgets.
"gioui.org/op" // op is used for recording different operations.
"gioui.org/text" // text contains constants for text layouting.
"gioui.org/widget/material" // material contains material design widgets.
)
func main() {
go func() {
w := app.NewWindow()
if err := loop(w); err != nil {
log.Println(err)
os.Exit(1)
}
os.Exit(0)
}()
app.Main()
}
func loop(w *app.Window) error {
// th contains constants for theming.
th := material.NewTheme(gofont.Collection())
// ops will be used to encode different operations.
var ops op.Ops
// listen for events happening on the window.
for e := range w.Events() {
// detect the type of the event.
switch e := e.(type) {
// this is sent when the application should re-render.
case system.FrameEvent:
// gtx is used to pass around rendering and event information.
gtx := layout.NewContext(&ops, e)
// handle all UI logic
l := material.H1(th, "Hello, Gio")
maroon := color.NRGBA{R: 127, G: 0, B: 0, A: 255}
l.Color = maroon
l.Alignment = text.Middle
l.Layout(gtx)
// render and handle the operations from the UI.
e.Frame(gtx.Ops)
// this is sent when the application is closed.
case system.DestroyEvent:
return e.Err
}
}
return nil
}