~eliasnaur/gio-example

ref: 8a3a65c8c0340253beaba1c2e5687a000fa74789 gio-example/x/haptic/example.go -rw-r--r-- 1.1 KiB
8a3a65c8Chris Waldon gio-extras/*,x: rename gio-extras demos and change imports 2 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
// SPDX-License-Identifier: Unlicense OR MIT

package main

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

import (
	"log"

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

	"gioui.org/font/gofont"
	"gioui.org/x/haptic"
)

var buzzer *haptic.Buzzer

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())
	btn := widget.Clickable{}
	buzzer = haptic.NewBuzzer(w)
	var ops op.Ops
	for {
		select {
		case e := <-w.Events():
			switch e := e.(type) {
			case system.DestroyEvent:
				return e.Err
			case system.FrameEvent:
				if btn.Clicked() {
					buzzer.Buzz()
				}
				gtx := layout.NewContext(&ops, e)
				layout.Center.Layout(gtx, material.Button(th, &btn, "buzz").Layout)
				e.Frame(gtx.Ops)
			default:
				ProcessPlatformEvent(e)
			}
		case err := <-buzzer.Errors():
			if err != nil {
				log.Printf("buzzer error: %v", err)
			}
		}
	}
}