android-go/examples/sensors/main.go

95 lines
1.9 KiB
Go

// +build darwin linux
package main
import (
"image/color"
"log"
"gioui.org/app"
"gioui.org/layout"
"gioui.org/text"
"gioui.org/text/shape"
"gioui.org/unit"
"golang.org/x/image/font/gofont/goregular"
"golang.org/x/image/font/sfnt"
)
var (
face text.Face
white = color.RGBA{A: 0xff, R: 0xff, G: 0xff, B: 0xff}
gray = color.RGBA{A: 0xff, R: 0xb0, G: 0xb0, B: 0xb0}
black = color.RGBA{A: 0xff, R: 0x00, G: 0x00, B: 0x00}
labchan chan string
)
func main() {
labchan = make(chan string)
log.Print("Staring event loop")
go eventloop()
app.Main()
log.Print("App closed")
}
func diffInsets(x, y app.Insets) bool {
return x.Top != y.Top ||
x.Bottom != y.Bottom ||
x.Left != y.Left ||
x.Right != y.Right
}
func eventloop() {
w := app.NewWindow(
app.WithWidth(unit.Dp(400)),
app.WithHeight(unit.Dp(400)),
app.WithTitle("Hello"))
gtx := &layout.Context{Queue: w.Queue()}
var faces shape.Faces
regular, err := sfnt.Parse(goregular.TTF)
if err != nil {
log.Fatal("Cannot parse font.")
}
face = faces.For(regular, unit.Sp(16))
sysinset := &layout.Inset{}
margin := layout.UniformInset(unit.Dp(10))
labels := []*text.Label{}
list := &layout.List{Axis: layout.Vertical}
resetSysinset := func(x app.Insets) {
sysinset.Top = x.Top
sysinset.Bottom = x.Bottom
sysinset.Left = x.Left
sysinset.Right = x.Right
}
for {
select {
case x := <-labchan:
labels = append(labels, &text.Label{Face: face, Text: x})
case e := <-w.Events():
switch e := e.(type) {
case app.DestroyEvent:
return
case app.UpdateEvent:
gtx.Reset(&e.Config, e.Size)
faces.Reset(&e.Config)
resetSysinset(e.Insets)
sysinset.Layout(gtx, func() {
margin.Layout(gtx, func() {
for i := 0; i < len(labels); i++ {
list.Layout(gtx, len(labels), func(i int) {
labels[i].Layout(gtx)
})
}
})
})
w.Update(gtx.Ops)
}
}
}
}