forked from aarzilli/nucular
/
testing.go
125 lines (101 loc) · 2.58 KB
/
testing.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package nucular
import (
"bytes"
"image"
"github.com/Varunram/nucular/rect"
nstyle "github.com/Varunram/nucular/style"
"golang.org/x/mobile/event/key"
"golang.org/x/mobile/event/mouse"
)
type TestWindow struct {
Img *image.RGBA
ctx *context
layout panel
}
func NewTestWindow(flags WindowFlags, size image.Point, updatefn UpdateFn) *TestWindow {
ctx := &context{}
ctx.Input.Mouse.valid = true
wnd := &TestWindow{ctx: ctx}
wnd.Img = image.NewRGBA(image.Rect(0, 0, size.X, size.Y))
wnd.layout.Flags = flags
ctx.setupMasterWindow(&wnd.layout, updatefn)
ctx.Windows[0].Bounds = rect.Rect{0, 0, size.X, size.Y}
ctx.mw = wnd
wnd.SetStyle(nstyle.FromTheme(nstyle.DefaultTheme, 1.0))
return wnd
}
func (w *TestWindow) context() *context {
return w.ctx
}
func (w *TestWindow) ActivateEditor(ed *TextEditor) {
w.ctx.activateEditor = ed
}
func (w *TestWindow) Close() {
}
func (w *TestWindow) Closed() bool {
return false
}
func (w *TestWindow) Changed() {
}
func (w *TestWindow) Main() {
}
func (w *TestWindow) SetStyle(style *nstyle.Style) {
w.ctx.Style = *style
w.ctx.Style.Defaults()
}
func (w *TestWindow) Style() *nstyle.Style {
return &w.ctx.Style
}
// Update runs the update function.
func (w *TestWindow) Update() {
in := &w.ctx.Input
in.Mouse.clip = nk_null_rect
w.ctx.Update()
contextAllCommands(w.ctx)
w.ctx.Draw(w.Img)
w.ctx.Reset()
}
func (w *TestWindow) GetPerf() bool {
return false
}
func (w *TestWindow) SetPerf(p bool) {
}
func (w *TestWindow) PopupOpen(title string, flags WindowFlags, rect rect.Rect, scale bool, updateFn UpdateFn) {
w.ctx.popupOpen(title, flags, rect, scale, updateFn)
}
// Click simulates a click at point p.
// The update function will be run as many times as needed, the window will
// be drawn every time.
func (w *TestWindow) Click(button mouse.Button, p image.Point) {
if button < 0 || int(button) >= len(w.ctx.Input.Mouse.Buttons) {
return
}
in := &w.ctx.Input
// Mouse move
in.Mouse.Pos = p
in.Mouse.Delta = in.Mouse.Pos.Sub(in.Mouse.Prev)
w.Update()
// Button press
btn := &w.ctx.Input.Mouse.Buttons[button]
btn.ClickedPos = p
btn.Clicked = true
btn.Down = true
w.Update()
// Button release
btn.Clicked = true
btn.Down = false
w.Update()
}
// Type simulates typing.
// The update function will be run as many times as needed, the window will
// be drawn every time.
func (w *TestWindow) Type(text string) {
w.ctx.Input.Keyboard.Text = text
w.Update()
}
func (w *TestWindow) TypeKey(e key.Event) {
var b bytes.Buffer
w.ctx.processKeyEvent(e, &b)
w.ctx.Input.Keyboard.Text = w.ctx.Input.Keyboard.Text + b.String()
w.Update()
}