~eliasnaur/gio-example

10e912d534a79e35136599cfcf832ab5204e562b — Chris Waldon 7 months ago 8a1c432
gio-extras/haptic: add haptic feedback example

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
A gio-extras/haptic/example.go => gio-extras/haptic/example.go +60 -0
@@ 0,0 1,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"
	"git.sr.ht/~whereswaldon/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)
			}
		}
	}
}

A gio-extras/haptic/example_android.go => gio-extras/haptic/example_android.go +17 -0
@@ 0,0 1,17 @@
package main

import (
	"gioui.org/app"
	"gioui.org/io/event"
)

// ProcessPlatformEvent handles platform-specific event processing. If it
// consumed the provided event, it returns true. In this case, no further
// event processing should occur.
func ProcessPlatformEvent(event event.Event) bool {
	if ve, ok := event.(app.ViewEvent); ok {
		buzzer.SetView(ve.View)
		return true
	}
	return false
}

A gio-extras/haptic/example_nonandroid.go => gio-extras/haptic/example_nonandroid.go +9 -0
@@ 0,0 1,9 @@
//+build !android

package main

import "gioui.org/io/event"

func ProcessPlatformEvent(event event.Event) bool {
	return false
}

M go.mod => go.mod +1 -0
@@ 5,6 5,7 @@ go 1.13
require (
	gioui.org v0.0.0-20201211172859-bd7bb4d5d2f3
	git.sr.ht/~whereswaldon/colorpicker v0.0.0-20201207220634-905cd7cc7248
	git.sr.ht/~whereswaldon/haptic v0.0.0-20201207220958-78675dee81dd
	github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7
	github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4
	github.com/google/go-github/v24 v24.0.1

M go.sum => go.sum +6 -0
@@ 5,6 5,10 @@ gioui.org v0.0.0-20201211172859-bd7bb4d5d2f3 h1:MeGkzCHegIlIebxPbC+I1xwNQ7vjTYPx
gioui.org v0.0.0-20201211172859-bd7bb4d5d2f3/go.mod h1:Y+uS7hHMvku1Q+ooaoq6fYD5B2LGoT8JtFgvmYmRzTw=
git.sr.ht/~whereswaldon/colorpicker v0.0.0-20201207220634-905cd7cc7248 h1:sI70yHfHsuzyAL72ikrsOecjRT3dgQw9rCCbFd/goDA=
git.sr.ht/~whereswaldon/colorpicker v0.0.0-20201207220634-905cd7cc7248/go.mod h1:6dPWP8F87bsIhQuwg0l5hH0TSDyk414e1xe3q+8BUho=
git.sr.ht/~whereswaldon/haptic v0.0.0-20201207220958-78675dee81dd h1:xTijdESZL/kM3nS7v/N1yJ/X8eInbsyqDLOz9ZFHpsE=
git.sr.ht/~whereswaldon/haptic v0.0.0-20201207220958-78675dee81dd/go.mod h1:lFvegCF1P7IXfv5FpnnvKFdoAQWTgJZhx8aWOBgE0yg=
git.wow.st/gmp/jni v0.0.0-20200827154156-014cd5c7c4c0 h1:Ynp3h+TC8k1clvf45D28VFQlmy0bPx8M/MG5bB24Vj8=
git.wow.st/gmp/jni v0.0.0-20200827154156-014cd5c7c4c0/go.mod h1:+axXBRUTIDlCeE73IKeD/os7LoEnTKdkp8/gQOFjqyo=
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=


@@ 56,6 60,8 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20191001151750-bb3f8db39f24/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9 h1:1/DFK4b7JH8DmkqhUk48onnSfrPzImPoVxuomtbT2nk=
golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201204225414-ed752295db88 h1:KmZPnMocC93w341XZp26yTJg8Za7lhb2KhkYmixoeso=
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20190927191325-030b2cf1153e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=