~eliasnaur/gio-example

ref: 8a3a65c8c0340253beaba1c2e5687a000fa74789 gio-example/x/notify/hello.go -rw-r--r-- 2.2 KiB
8a3a65c8Chris Waldon gio-extras/*,x: rename gio-extras demos and change imports 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// SPDX-License-Identifier: Unlicense OR MIT

package main

// A simple Gio program. See https://gioui.org for more information.

import (
	//	"image/color"
	"log"
	"time"

	"gioui.org/app"
	"gioui.org/io/system"
	"gioui.org/layout"
	"gioui.org/op"

	//	"gioui.org/text"
	"gioui.org/widget"
	"gioui.org/widget/material"
	"gioui.org/x/notify"

	"gioui.org/font/gofont"
)

func main() {
	go func() {
		w := app.NewWindow()
		if err := loop(w); err != nil {
			log.Fatal(err)
		}
	}()
	app.Main()
}

func loop(w *app.Window) error {
	th := material.NewTheme(gofont.Collection())
	var ops op.Ops
	first := true
	notificationRequests := make(chan struct{})
	var button widget.Clickable
	var err error
	for {
		e := <-w.Events()
		switch e := e.(type) {
		case system.DestroyEvent:
			return e.Err
		case system.FrameEvent:
			if button.Clicked() {
				notificationRequests <- struct{}{}
			}
			gtx := layout.NewContext(&ops, e)
			layout.Inset{
				Top:    e.Insets.Top,
				Bottom: e.Insets.Bottom,
				Left:   e.Insets.Left,
				Right:  e.Insets.Right,
			}.Layout(gtx, func(gtx layout.Context) layout.Dimensions {

				return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
					layout.Rigid(func(gtx layout.Context) layout.Dimensions {
						text := "notification errors will appear here"
						if err != nil {
							text = err.Error()
						}
						return material.Body1(th, text).Layout(gtx)
					}),
					layout.Flexed(1, func(gtx layout.Context) layout.Dimensions {
						return material.Button(th, &button, "Send Notification").Layout(gtx)
					}),
				)
			})
			e.Frame(gtx.Ops)
			if first {
				first = false
				go func() {
					mgr, e := notify.NewManager()
					if e != nil {
						log.Printf("manager creation failed: %v", e)
						err = e
					}
					for range notificationRequests {
						log.Println("trying to send notification")
						notif, e := mgr.CreateNotification("hello!", "IS GIO OUT THERE?")
						if e != nil {
							log.Printf("notification send failed: %v", e)
							err = e
							continue
						}
						go func() {
							time.Sleep(time.Second * 10)
							if err = notif.Cancel(); err != nil {
								log.Printf("failed cancelling: %v", err)
							}
						}()
					}
				}()
			}
		}
	}
}