-
Notifications
You must be signed in to change notification settings - Fork 1
/
pointer.go
71 lines (62 loc) · 1.41 KB
/
pointer.go
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
package main
import (
"image"
"image/color"
"gioui.org/app"
"gioui.org/io/pointer"
"gioui.org/io/system"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
)
// START QUEUE OMIT
func main() {
go func() {
w := app.NewWindow()
button := new(Button)
var ops op.Ops
for e := range w.Events() {
if e, ok := e.(system.FrameEvent); ok {
gtx := layout.NewContext(&ops, e)
button.Layout(gtx)
e.Frame(gtx.Ops)
}
}
}()
app.Main()
}
// END QUEUE OMIT
type Button struct {
pressed bool
}
// START OMIT
func (b *Button) Layout(gtx layout.Context) layout.Dimensions {
for _, e := range gtx.Events(b) { // HLevent
if e, ok := e.(pointer.Event); ok { // HLevent
switch e.Type { // HLevent
case pointer.Press: // HLevent
b.pressed = true // HLevent
case pointer.Release: // HLevent
b.pressed = false // HLevent
}
}
}
col := color.RGBA{A: 0xff, R: 0xff}
if b.pressed {
col = color.RGBA{A: 0xff, G: 0xff}
}
pointer.Rect( // HLevent
image.Rectangle{Max: image.Point{X: 500, Y: 500}}, // HLevent
).Add(gtx.Ops) // HLevent
pointer.InputOp{Tag: b}.Add(gtx.Ops) // HLevent
return drawSquare(gtx.Ops, col)
}
// END OMIT
func drawSquare(ops *op.Ops, color color.RGBA) layout.Dimensions {
square := image.Rectangle{
Max: image.Point{X: 500, Y: 500},
}
paint.FillShape(ops, color, clip.Rect(square).Op())
return layout.Dimensions{Size: image.Pt(500, 500)}
}