/
main.go
86 lines (72 loc) · 1.49 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"image"
"image/color"
"log"
"os"
"gioui.org/app"
"gioui.org/font/gofont"
"gioui.org/io/key"
"gioui.org/io/system"
"gioui.org/layout"
"gioui.org/op"
"gioui.org/op/clip"
"gioui.org/op/paint"
"gioui.org/widget/material"
"github.com/arl/gioexp/component/zoomable"
)
type (
C = layout.Context
D = layout.Dimensions
)
// Zoomable: experiment at making a widget which content can be zoomed/panned
// using the mouse wheel, while keeping the point under the cursor immobile
// (i.e. using the mouse cursor as scale origin).
func main() {
ui := &UI{
Theme: material.NewTheme(gofont.Collection()),
}
go func() {
w := app.NewWindow(app.Title("Zoomable"))
if err := ui.Run(w); err != nil {
log.Println(err)
os.Exit(1)
}
os.Exit(0)
}()
app.Main()
}
type UI struct {
Theme *material.Theme
Zoomable zoomable.Zoomable
}
func (ui *UI) Run(w *app.Window) error {
var ops op.Ops
for e := range w.Events() {
switch e := e.(type) {
case system.FrameEvent:
gtx := layout.NewContext(&ops, e)
ui.Layout(gtx)
e.Frame(gtx.Ops)
case key.Event:
if e.Name == key.NameEscape {
return nil
}
case system.DestroyEvent:
return e.Err
}
}
return nil
}
var red = color.NRGBA{R: 200, A: 255}
func (ui *UI) Layout(gtx C) D {
return ui.Zoomable.Layout(gtx, func(gtx C) D {
rect := clip.Rect{
Min: image.Pt(100, 100),
Max: image.Pt(300, 300),
}
paint.FillShape(gtx.Ops, red, rect.Op())
d := image.Point{Y: 400}
return D{Size: d}
})
}