From efce78d414f3adf866b81bf6c149aa6aee8302c6 Mon Sep 17 00:00:00 2001 From: metaclips Date: Thu, 2 Apr 2020 22:50:58 +0100 Subject: [PATCH] example/kitchen: add ProgressBar example Updates the kitchen example codebase showing ProgressBar functionality. Signed-off-by: metaclips --- example/go.mod | 2 +- example/go.sum | 4 +-- example/kitchen/kitchen.go | 50 ++++++++++++++++++++++++++++---------- 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/example/go.mod b/example/go.mod index 000dbf79..14ead805 100644 --- a/example/go.mod +++ b/example/go.mod @@ -3,7 +3,7 @@ module gioui.org/example go 1.13 require ( - gioui.org v0.0.0-20200401111706-a08674dbcaca + gioui.org v0.0.0-20200402190513-da01fbdea784 github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7 github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72 github.com/google/go-github/v24 v24.0.1 diff --git a/example/go.sum b/example/go.sum index a54df423..669115b1 100644 --- a/example/go.sum +++ b/example/go.sum @@ -1,7 +1,7 @@ cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= -gioui.org v0.0.0-20200401111706-a08674dbcaca h1:3yyshrXY2S0bXVRpzm2agRLzKnuwSDRCedi67WwqalQ= -gioui.org v0.0.0-20200401111706-a08674dbcaca/go.mod h1:AHI9rFr6AEEHCb8EPVtb/p5M+NMJRKH58IOp8O3Je04= +gioui.org v0.0.0-20200402190513-da01fbdea784 h1:gH0+2HCtuzxAdK6sd0BeCxZLAkCbhptF607odwS5QY0= +gioui.org v0.0.0-20200402190513-da01fbdea784/go.mod h1:AHI9rFr6AEEHCb8EPVtb/p5M+NMJRKH58IOp8O3Je04= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7 h1:SCYMcCJ89LjRGwEa0tRluNRiMjZHalQZrVrvTbPh+qw= github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7/go.mod h1:482civXOzJJCPzJ4ZOX/pwvXBWSnzD4OKMdH4ClKGbk= diff --git a/example/kitchen/kitchen.go b/example/kitchen/kitchen.go index 50ff8464..5c972277 100644 --- a/example/kitchen/kitchen.go +++ b/example/kitchen/kitchen.go @@ -48,6 +48,7 @@ func main() { log.Fatal(err) } icon = ic + progressIncrementer = make(chan int) gofont.Register() if *screenshot != "" { if err := saveScreenshot(*screenshot); err != nil { @@ -56,8 +57,16 @@ func main() { } os.Exit(0) } + go func() { - w := app.NewWindow() + for { + time.Sleep(time.Second) + progressIncrementer <- 10 + } + }() + + go func() { + w := app.NewWindow(app.Size(unit.Dp(800), unit.Dp(650))) if err := loop(w); err != nil { log.Fatal(err) } @@ -91,15 +100,24 @@ func saveScreenshot(f string) error { func loop(w *app.Window) error { th := material.NewTheme() gtx := layout.NewContext(w.Queue()) + for { - e := <-w.Events() - switch e := e.(type) { - case system.DestroyEvent: - return e.Err - case system.FrameEvent: - gtx.Reset(e.Config, e.Size) - kitchen(gtx, th) - e.Frame(gtx.Ops) + select { + case e := <-w.Events(): + switch e := e.(type) { + case system.DestroyEvent: + return e.Err + case system.FrameEvent: + gtx.Reset(e.Config, e.Size) + kitchen(gtx, th) + e.Frame(gtx.Ops) + } + case p := <-progressIncrementer: + progress += p + if progress > 100 { + progress = 0 + } + w.Invalidate() } } } @@ -118,10 +136,12 @@ var ( list = &layout.List{ Axis: layout.Vertical, } - green = true - topLabel = "Hello, Gio" - icon *material.Icon - checkbox = new(widget.CheckBox) + progress = 0 + progressIncrementer chan int + green = true + topLabel = "Hello, Gio" + icon *material.Icon + checkbox = new(widget.CheckBox) ) func (b iconAndTextButton) Layout(gtx *layout.Context, button *widget.Button, icon *material.Icon, word string) { @@ -204,6 +224,9 @@ func kitchen(gtx *layout.Context, th *material.Theme) { }), ) }, + func() { + th.ProgressBar().Layout(gtx, progress) + }, func() { th.CheckBox("Checkbox").Layout(gtx, checkbox) }, @@ -221,6 +244,7 @@ func kitchen(gtx *layout.Context, th *material.Theme) { ) }, } + list.Layout(gtx, len(widgets), func(i int) { layout.UniformInset(unit.Dp(16)).Layout(gtx, widgets[i]) })