android-go/examples/sensors/main.go
2019-09-17 10:42:01 -04:00

97 lines
2.0 KiB
Go

// +build darwin linux
package main
import (
"log"
"image/color"
"gioui.org/ui"
"gioui.org/ui/app"
"gioui.org/ui/layout"
"gioui.org/ui/measure"
"gioui.org/ui/text"
"golang.org/x/image/font/sfnt"
"golang.org/x/image/font/gofont/goregular"
)
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(ui.Dp(400)),
app.WithHeight(ui.Dp(400)),
app.WithTitle("Hello"))
q := w.Queue()
var c ui.Config
ops := new(ui.Ops)
//var dims layout.Dimensions
var cs layout.Constraints
var faces measure.Faces
regular, err := sfnt.Parse(goregular.TTF)
if err != nil { log.Fatal("Cannot parse font.") }
face = faces.For(regular, ui.Sp(16))
sysinset := &layout.Inset{}
margin := layout.UniformInset(ui.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:
c = &e.Config
ops.Reset()
faces.Reset(c)
resetSysinset(e.Insets)
cs = layout.RigidConstraints(e.Size)
cs = sysinset.Begin(c, ops, cs)
cs = margin.Begin(c, ops, cs)
for list.Init(c, q, ops, cs, len(labels)); list.More(); list.Next() {
list.End(labels[list.Index()].Layout(ops, list.Constraints()))
}
sysinset.End(margin.End(list.Layout()))
w.Update(ops)
}
}
}
}