android-go/examples/sensors/main.go

81 lines
1.4 KiB
Go
Raw Normal View History

2019-09-17 10:42:01 -04:00
// +build darwin linux
package main
import (
2019-10-07 10:41:51 -04:00
"log"
2019-09-17 10:42:01 -04:00
2019-10-03 11:32:15 -04:00
"gioui.org/app"
2019-10-18 14:40:35 -04:00
"gioui.org/io/system"
2019-10-03 11:32:15 -04:00
"gioui.org/layout"
"gioui.org/unit"
2019-10-18 14:40:35 -04:00
"gioui.org/widget/material"
2019-09-17 10:42:01 -04:00
2019-10-18 14:40:35 -04:00
_ "gioui.org/font/gofont"
2019-09-17 10:42:01 -04:00
)
var (
labchan chan string
)
func main() {
labchan = make(chan string)
log.Print("Staring event loop")
go eventloop()
app.Main()
log.Print("App closed")
}
2019-10-18 14:40:35 -04:00
func diffInsets(x, y system.Insets) bool {
2019-10-07 10:41:51 -04:00
return x.Top != y.Top ||
2019-09-17 10:42:01 -04:00
x.Bottom != y.Bottom ||
x.Left != y.Left ||
x.Right != y.Right
}
func eventloop() {
w := app.NewWindow(
2019-10-18 14:40:35 -04:00
app.Size(unit.Dp(400), unit.Dp(400)),
app.Title("Hello"))
th := material.NewTheme()
2019-10-03 11:32:15 -04:00
gtx := &layout.Context{Queue: w.Queue()}
2019-09-17 10:42:01 -04:00
sysinset := &layout.Inset{}
2019-10-03 11:32:15 -04:00
margin := layout.UniformInset(unit.Dp(10))
2019-10-18 14:40:35 -04:00
labels := []material.Label{}
2019-09-17 10:42:01 -04:00
list := &layout.List{Axis: layout.Vertical}
2019-10-18 14:40:35 -04:00
resetSysinset := func(x system.Insets) {
2019-09-17 10:42:01 -04:00
sysinset.Top = x.Top
sysinset.Bottom = x.Bottom
sysinset.Left = x.Left
sysinset.Right = x.Right
}
2019-10-18 14:40:35 -04:00
go func() {
labchan <- "Starting"
}()
2019-09-17 10:42:01 -04:00
for {
select {
case x := <-labchan:
2019-10-18 14:40:35 -04:00
labels = append(labels, th.Body1(x))
2019-09-17 10:42:01 -04:00
case e := <-w.Events():
switch e := e.(type) {
2019-10-18 14:40:35 -04:00
case system.DestroyEvent:
2019-09-17 10:42:01 -04:00
return
2019-10-18 14:40:35 -04:00
case system.FrameEvent:
gtx.Reset(e.Config, e.Size)
2019-09-17 10:42:01 -04:00
resetSysinset(e.Insets)
2019-10-03 11:32:15 -04:00
sysinset.Layout(gtx, func() {
margin.Layout(gtx, func() {
2019-10-10 13:10:58 -04:00
list.Layout(gtx, len(labels), func(i int) {
labels[i].Layout(gtx)
})
2019-10-03 11:32:15 -04:00
})
})
2019-10-18 14:40:35 -04:00
e.Frame(gtx.Ops)
2019-09-17 10:42:01 -04:00
}
}
}
}