From b3d14d2dd4c0a3e5571b48192acfcd27c91e3a26 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Sun, 7 Jun 2020 14:05:15 +0200 Subject: [PATCH] example/windows: add example that demonstrates multiple windows Updates gio#19 Signed-off-by: Elias Naur --- example/windows/windows.go | 59 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 example/windows/windows.go diff --git a/example/windows/windows.go b/example/windows/windows.go new file mode 100644 index 00000000..9a8fefc3 --- /dev/null +++ b/example/windows/windows.go @@ -0,0 +1,59 @@ +// SPDX-License-Identifier: Unlicense OR MIT + +package main + +// Multiple windows in Gio. + +import ( + "log" + + "gioui.org/app" + "gioui.org/io/event" + "gioui.org/io/system" + "gioui.org/layout" + "gioui.org/op" + "gioui.org/widget" + "gioui.org/widget/material" + + "gioui.org/font/gofont" +) + +type window struct { + btn widget.Clickable +} + +func main() { + newWindow() + app.Main() +} + +func newWindow() { + go func() { + w := new(window) + evts := app.NewWindow().Events() + if err := w.loop(evts); err != nil { + log.Fatal(err) + } + }() +} + +func (w *window) loop(events <-chan event.Event) error { + th := material.NewTheme(gofont.Collection()) + var ops op.Ops + for { + e := <-events + switch e := e.(type) { + case system.DestroyEvent: + return e.Err + case system.FrameEvent: + for w.btn.Clicked() { + newWindow() + } + gtx := layout.NewContext(&ops, e.Queue, e.Config, e.Size) + layout.Center.Layout(gtx, func(gtx layout.Context) layout.Dimensions { + return material.Button(th, &w.btn, "More!").Layout(gtx) + }) + e.Frame(gtx.Ops) + } + } +}