-
Notifications
You must be signed in to change notification settings - Fork 3
/
callbacks.go
296 lines (274 loc) · 7.83 KB
/
callbacks.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package glfw
import (
"image"
"runtime"
"time"
"unicode/utf8"
"github.com/kanryu/mado"
"github.com/kanryu/mado/app"
"github.com/kanryu/mado/f32"
"github.com/kanryu/mado/io/event"
"github.com/kanryu/mado/io/input"
"github.com/kanryu/mado/io/key"
"github.com/kanryu/mado/io/pointer"
"github.com/kanryu/mado/io/system"
"github.com/kanryu/mado/io/window"
)
var _ mado.Callbacks = (*Callbacks)(nil)
type Callbacks struct {
W *app.Window
Gw *Window
D mado.Driver
Busy bool
PrevWindowMode mado.WindowMode
PrevWindowStage mado.Stage
PrevScaling f32.Point
PrevWindowSize image.Point
PrevFramebufferSize image.Point
PrevCursorPos f32.Point
PrevModifiers key.Modifiers
Preedit string
WaitEvents []event.Event
WindowInitialized chan struct{}
}
func (c *Callbacks) GetWindow() mado.Window {
return c.W
}
func (c *Callbacks) SetWindow(w mado.Window) {
c.W = w.(*app.Window)
}
func (c *Callbacks) SetGlfwWindow(gw *Window) {
c.Gw = gw
}
func (c *Callbacks) SetDriver(d mado.Driver) {
c.D = d
var wakeup func()
if d != nil {
wakeup = d.Wakeup
}
c.W.WakeupFuncs <- wakeup
}
func (c *Callbacks) Event(e event.Event) bool {
if c.D == nil {
panic("event while no driver active")
}
c.WaitEvents = append(c.WaitEvents, e)
if c.Busy {
return true
}
c.Busy = true
var handled bool
for len(c.WaitEvents) > 0 {
e := c.WaitEvents[0]
// copy(c.WaitEvents, c.WaitEvents[1:])
// c.WaitEvents = c.WaitEvents[:len(c.WaitEvents)-1]
c.WaitEvents = append([]event.Event{}, c.WaitEvents[1:]...)
handled = c.W.ProcessEvent(c.D, e)
// POST events to glfw callbacks
switch e2 := e.(type) {
case mado.ViewEvent:
if c.WindowInitialized != nil {
close(c.WindowInitialized)
}
case mado.StageEvent:
switch e2.Stage {
case mado.StagePaused:
if e2.WindowMode == mado.Minimized {
c.Gw.fIconifyHolder(c.Gw, true)
c.PrevWindowMode = mado.Minimized
}
case mado.StageRunning:
if e2.WindowMode == mado.Maximized && c.PrevWindowMode != mado.Maximized {
c.Gw.fMaximizeHolder(c.Gw, true)
c.PrevWindowMode = mado.Maximized
} else if e2.WindowMode == mado.Fullscreen && c.PrevWindowMode != mado.Fullscreen {
c.Gw.fMaximizeHolder(c.Gw, true)
c.PrevWindowMode = mado.Fullscreen
}
if e2.WindowMode == mado.Windowed {
if c.PrevWindowMode == mado.Minimized {
c.Gw.fIconifyHolder(c.Gw, false)
} else if c.PrevWindowMode == mado.Maximized {
c.Gw.fMaximizeHolder(c.Gw, false)
} else if c.PrevWindowMode == mado.Fullscreen {
c.Gw.fMaximizeHolder(c.Gw, false)
} else if c.PrevWindowMode != mado.Windowed {
c.Gw.fMaximizeHolder(c.Gw, false)
}
c.PrevWindowMode = mado.Windowed
}
case mado.StageInactive:
c.PrevWindowStage = mado.StageInactive
}
case key.FocusEvent:
c.Gw.fFocusHolder(c.Gw, e2.Focus)
case window.FrameScaleEvent:
if c.PrevScaling != e2.Scaling {
c.PrevScaling = e2.Scaling
c.Gw.fContentScaleHolder(c.Gw, e2.Scaling.X, e2.Scaling.Y)
}
case mado.FrameEvent:
c.Gw.fRefreshHolder(c.Gw)
case window.MoveEvent:
c.Gw.fPosHolder(c.Gw, e2.Pos.X, e2.Pos.Y)
case window.SizeEvent:
if c.PrevWindowSize != e2.Size {
c.Gw.fSizeHolder(c.Gw, e2.Size.X, e2.Size.Y)
c.PrevWindowSize = e2.Size
}
case window.FramebufferSizeEvent:
if c.PrevFramebufferSize != e2.Size {
c.Gw.fFramebufferSizeHolder(c.Gw, e2.Size.X, e2.Size.Y)
c.PrevFramebufferSize = e2.Size
}
case window.CloseEvent:
c.Gw.fCloseHolder(c.Gw)
//c.Gw.shouldClose = true
c.Event(mado.DestroyEvent{})
case pointer.Event:
if e2.Kind == pointer.Scroll {
c.Gw.fScrollHolder(c.Gw, float64(e2.Scroll.X), float64(e2.Scroll.Y))
} else if c.PrevCursorPos != e2.Position {
c.Gw.fCursorPosHolder(c.Gw, float64(e2.Position.X), float64(e2.Position.Y))
c.PrevCursorPos = e2.Position
} else {
c.Gw.fMouseButtonHolder(c.Gw, MouseButton(e2.Buttons), Action(e2.Kind), ModifierKey(e2.Modifiers))
}
case pointer.CursorEnterEvent:
c.Gw.fCursorEnterHolder(c.Gw, e2.Entered)
case key.Event:
// IME with Preedit is confirmed by pressing Enter and POSTed as input token
if c.Preedit != "" && (e2.Name == key.NameReturn || e2.Name == key.NameEnter) {
c.Event(key.EditEvent{Text: c.Preedit})
break
}
c.PrevModifiers = e2.Modifiers
c.Gw.fKeyHolder(c.Gw, Key(e2.KeyCode), 0, Action(e2.State), ModifierKey(e2.Modifiers))
case key.EditEvent:
if e2.Preedit {
c.Preedit = e2.Text
} else {
// The IME returns to its initial state when the input character is confirmed, so initialize ImeState.
c.Preedit = ""
c.W.ImeState = mado.EditorState{}
}
for _, r := range e2.Text {
c.Gw.fCharModsHolder(c.Gw, r, ModifierKey(c.PrevModifiers))
if !e2.Preedit {
// [1] chars with IME off
// [2] Token whose input is confirmed (not preedit)
c.Gw.fCharHolder(c.Gw, r)
}
}
}
}
c.Busy = false
select {
case <-c.W.Destroy:
return handled
default:
}
c.W.UpdateState(c.D)
if _, ok := e.(mado.WakeupEvent); ok {
select {
case opts := <-c.W.Options:
cnf := mado.Config{Decorated: c.W.Decorations.Enabled}
for _, opt := range opts {
opt(c.W.Metric, &cnf)
}
c.W.Decorations.Enabled = cnf.Decorated
decoHeight := c.W.Decorations.Height
if !c.W.Decorations.Enabled {
decoHeight = 0
}
opts = append(opts, mado.DecoHeightOpt(decoHeight))
c.D.Configure(opts)
default:
}
select {
case acts := <-c.W.Actions:
c.D.Perform(acts)
default:
}
}
return handled
}
// SemanticRoot returns the ID of the semantic root.
func (c *Callbacks) SemanticRoot() input.SemanticID {
c.W.UpdateSemantics()
return c.W.Semantic.Root
}
// LookupSemantic looks up a semantic node from an ID. The zero ID denotes the root.
func (c *Callbacks) LookupSemantic(semID input.SemanticID) (input.SemanticNode, bool) {
c.W.UpdateSemantics()
n, found := c.W.Semantic.Ids[semID]
return n, found
}
func (c *Callbacks) AppendSemanticDiffs(diffs []input.SemanticID) []input.SemanticID {
c.W.UpdateSemantics()
if tree := c.W.Semantic.PrevTree; len(tree) > 0 {
c.W.CollectSemanticDiffs(&diffs, c.W.Semantic.PrevTree[0])
}
return diffs
}
func (c *Callbacks) SemanticAt(pos f32.Point) (input.SemanticID, bool) {
c.W.UpdateSemantics()
return c.W.Queue.SemanticAt(pos)
}
func (c *Callbacks) EditorState() mado.EditorState {
return c.W.ImeState
}
func (c *Callbacks) SetComposingRegion(r key.Range) {
c.W.ImeState.Compose = r
}
func (c *Callbacks) EditorInsert(text string, preedit bool) {
sel := c.W.ImeState.Selection.Range
c.EditorReplace(sel, text, preedit)
start := sel.Start
if sel.End < start {
start = sel.End
}
sel.Start = start + utf8.RuneCountInString(text)
sel.End = sel.Start
c.SetEditorSelection(sel)
}
func (c *Callbacks) EditorReplace(r key.Range, text string, preedit bool) {
c.W.ImeState.Replace(r, text)
c.Event(key.EditEvent{Range: r, Text: text, Preedit: preedit})
c.Event(key.SnippetEvent(c.W.ImeState.Snippet.Range))
}
func (c *Callbacks) SetEditorSelection(r key.Range) {
c.W.ImeState.Selection.Range = r
c.Event(key.SelectionEvent(r))
}
func (c *Callbacks) SetEditorSnippet(r key.Range) {
if sn := c.EditorState().Snippet.Range; sn == r {
// No need to expand.
return
}
c.Event(key.SnippetEvent(r))
}
func (c *Callbacks) ClickFocus() {
c.W.Queue.ClickFocus()
c.W.SetNextFrame(time.Time{})
c.W.UpdateAnimation(c.D)
}
func (c *Callbacks) ActionAt(p f32.Point) (system.Action, bool) {
return c.W.Queue.ActionAt(p)
}
// waitForInitialized waits until mado.Driver is initialized.
// It needs to poll some OS Events.
func (c *Callbacks) waitForInitialized() {
for {
runtime.Gosched()
t := time.NewTicker(time.Millisecond)
select {
case <-t.C:
PollEvents()
continue
case <-c.WindowInitialized:
c.WindowInitialized = nil
return
}
}
}