~eliasnaur/gio-example

gio-example/notify/main.go -rw-r--r-- 1.9 KiB
a9116b22Chris Waldon go.*: update to latest gio{,/x} and prune deps 3 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
// SPDX-License-Identifier: Unlicense OR MIT

package main

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

import (
	"fmt"
	"os"

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

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

	"gioui.org/font/gofont"
)

func main() {
	go func() {
		w := app.NewWindow(
			app.Title("notify"),
			app.Size(unit.Dp(800), unit.Dp(600)))

		var ops op.Ops
		for event := range w.Events() {
			switch event := event.(type) {
			case system.DestroyEvent:
				os.Exit(0)
			case system.FrameEvent:
				event.Frame(frame(layout.NewContext(&ops, event)))
			}
		}
	}()
	app.Main()
}

type (
	// C quick alias for Context.
	C = layout.Context
	// D quick alias for Dimensions.
	D = layout.Dimensions
)

var (
	th       = material.NewTheme(gofont.Collection())
	notifier = func() notify.Notifier {
		n, err := notify.NewNotifier()
		if err != nil {
			panic(fmt.Errorf("init notification manager: %w", err))
		}
		return n
	}()
	editor    component.TextField
	notifyBtn widget.Clickable
)

// frame lays out the entire frame and returns the reusltant ops buffer.
func frame(gtx C) *op.Ops {
	if notifyBtn.Clicked() {
		msg := "This is a notification send from gio."
		if txt := editor.Text(); txt != "" {
			msg = txt
		}
		go notifier.CreateNotification("Hello Gio!", msg)
	}
	layout.Center.Layout(gtx, func(gtx C) D {
		gtx.Constraints.Max.X = gtx.Dp(unit.Dp(300))
		return layout.Flex{Axis: layout.Vertical}.Layout(gtx,
			layout.Rigid(func(gtx C) D {
				return editor.Layout(gtx, th, "enter a notification message")
			}),
			layout.Rigid(func(gtx C) D {
				return layout.Spacer{Height: unit.Dp(10)}.Layout(gtx)
			}),
			layout.Rigid(func(gtx C) D {
				return material.Button(th, &notifyBtn, "notify").Layout(gtx)
			}),
		)
	})
	return gtx.Ops
}