forked from go-flutter-desktop/go-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flutter.go
409 lines (339 loc) · 11.5 KB
/
flutter.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package flutter
import (
"encoding/json"
"fmt"
"log"
"runtime"
"time"
"unsafe"
"github.com/go-flutter-desktop/go-flutter/embedder"
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/pkg/errors"
)
// dpPerInch defines the amount of display pixels per inch as defined for Flutter.
const dpPerInch = 160.0
// Run executes a flutter application with the provided options.
// given limitations this method must be called by the main function directly.
func Run(options ...Option) (err error) {
var (
window *glfw.Window
c config
)
// The Windows Title Handler and the TextInput handler come by default
options = append(options, addHandlerWindowTitle())
options = append(options, addHandlerTextInput())
options = append(options, addHandlerClipboard())
c = c.merge(options...)
err = glfw.Init()
if err != nil {
return errors.Wrap(err, "glfw init")
}
defer glfw.Terminate()
window, err = glfw.CreateWindow(c.WindowDimension.x, c.WindowDimension.y, "Loading..", nil, nil)
if err != nil {
return errors.Wrap(err, "creating glfw window")
}
defer window.Destroy()
if c.WindowIconProvider != nil {
images, err := c.WindowIconProvider()
if err != nil {
return errors.Wrap(err, "getting images from icon provider")
}
window.SetIcon(images)
}
if c.WindowInitializerDeprecated != nil {
err = c.WindowInitializerDeprecated(window)
if err != nil {
return errors.Wrap(err, "executing window initializer")
}
}
flu := runFlutter(window, c)
defer flu.Shutdown()
for !window.ShouldClose() {
// glfw.WaitEvents()
glfw.PollEvents()
embedder.FlutterEngineFlushPendingTasksNow()
}
return nil
}
// GLFW callbacks to the Flutter Engine
func glfwCursorPositionCallbackAtPhase(
window *glfw.Window, phase embedder.PointerPhase,
x float64, y float64,
) {
winWidth, _ := window.GetSize()
frameBuffWidth, _ := window.GetFramebufferSize()
contentScale := float64(frameBuffWidth / winWidth)
event := embedder.PointerEvent{
Phase: phase,
X: x * contentScale,
Y: y * contentScale,
Timestamp: time.Now().UnixNano() / int64(time.Millisecond),
}
index := *(*int)(window.GetUserPointer())
flutterEngine := embedder.FlutterEngineByIndex(index)
flutterEngine.SendPointerEvent(event)
}
func glfwMouseButtonCallback(window *glfw.Window, key glfw.MouseButton, action glfw.Action, mods glfw.ModifierKey) {
if key == glfw.MouseButton1 {
x, y := window.GetCursorPos()
// recalculate x and y from screen cordinates to pixels
widthPx, _ := window.GetFramebufferSize()
width, _ := window.GetSize()
pixelsPerScreenCoordinate := float64(widthPx) / float64(width)
x = x * pixelsPerScreenCoordinate
y = y * pixelsPerScreenCoordinate
if action == glfw.Press {
glfwCursorPositionCallbackAtPhase(window, embedder.KDown, x, y)
window.SetCursorPosCallback(func(window *glfw.Window, x float64, y float64) {
x = x * pixelsPerScreenCoordinate
y = y * pixelsPerScreenCoordinate
glfwCursorPositionCallbackAtPhase(window, embedder.KMove, x, y)
})
}
if action == glfw.Release {
glfwCursorPositionCallbackAtPhase(window, embedder.KUp, x, y)
window.SetCursorPosCallback(nil)
}
}
}
var state = textModel{}
func glfwKey(keyboardLayout KeyboardShortcuts) func(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
var modifierKey glfw.ModifierKey
var wordTravellerKey int
var wordTravellerKeyShift int
switch runtime.GOOS {
case "darwin":
modifierKey = glfw.ModSuper
wordTravellerKey = ModAlt
wordTravellerKeyShift = ModShiftAlt
default:
modifierKey = glfw.ModControl
wordTravellerKey = ModControl
wordTravellerKeyShift = ModShiftControl
}
return func(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
var modsIsModfifier = false
var modsIsShift = false
var modsIsWordModifierShift = false
var modsIsWordModifier = false
switch {
case int(mods) == wordTravellerKeyShift:
modsIsWordModifierShift = true
case int(mods) == wordTravellerKey:
modsIsWordModifier = true
case mods == modifierKey:
modsIsModfifier = true
case int(mods) == ModShift:
modsIsShift = true
}
if key == glfw.KeyEscape && action == glfw.Press {
w.SetShouldClose(true)
}
if action == glfw.Repeat || action == glfw.Press {
if state.clientID != 0 {
switch key {
case glfw.KeyEnter:
if mods == modifierKey {
performAction(w, "done")
} else {
state.addChar([]rune{'\n'})
performAction(w, "newline")
}
case glfw.KeyHome:
state.MoveCursorHome(modsIsModfifier, modsIsShift, modsIsWordModifierShift, modsIsWordModifier)
case glfw.KeyEnd:
state.MoveCursorEnd(modsIsModfifier, modsIsShift, modsIsWordModifierShift, modsIsWordModifier)
case glfw.KeyLeft:
state.MoveCursorLeft(modsIsModfifier, modsIsShift, modsIsWordModifierShift, modsIsWordModifier)
case glfw.KeyRight:
state.MoveCursorRight(modsIsModfifier, modsIsShift, modsIsWordModifierShift, modsIsWordModifier)
case glfw.KeyDelete:
state.Delete(modsIsModfifier, modsIsShift, modsIsWordModifierShift, modsIsWordModifier)
case glfw.KeyBackspace:
state.Backspace(modsIsModfifier, modsIsShift, modsIsWordModifierShift, modsIsWordModifier)
case keyboardLayout.SelectAll:
if mods == modifierKey {
state.SelectAll()
}
case keyboardLayout.Copy:
if mods == modifierKey && state.isSelected() {
_, _, selectedContent := state.GetSelectedText()
w.SetClipboardString(selectedContent)
}
case keyboardLayout.Cut:
if mods == modifierKey && state.isSelected() {
_, _, selectedContent := state.GetSelectedText()
w.SetClipboardString(selectedContent)
state.RemoveSelectedText()
}
case keyboardLayout.Paste:
if mods == modifierKey {
var clpString, err = w.GetClipboardString()
if err != nil {
log.Printf("unable to get the clipboard content: %v\n", err)
} else {
state.addChar([]rune(clpString))
}
}
}
}
}
}
}
// newGLFWFramebufferSizeCallback creates a func that is called on framebuffer resizes.
// When pixelRatio is set, the pixelRatio communicated to the Flutter embedder is not calculated.
func newGLFWFramebufferSizeCallback(pixelRatio float64, monitorScreenCoordinatesPerInch float64) func(*glfw.Window, int, int) {
return func(window *glfw.Window, widthPx int, heightPx int) {
index := *(*int)(window.GetUserPointer())
flutterEngine := embedder.FlutterEngineByIndex(index)
// calculate pixelRatio when it has not been forced.
if pixelRatio == 0 {
width, _ := window.GetSize()
pixelsPerScreenCoordinate := float64(widthPx) / float64(width)
dpi := pixelsPerScreenCoordinate * monitorScreenCoordinatesPerInch
pixelRatio = dpi / dpPerInch
// Limit the ratio to 1 to avoid rendering a smaller UI in standard resolution monitors.
if pixelRatio < 1.0 {
fmt.Println("calculated pixelRatio limited to a minimum of 1.0")
pixelRatio = 1.0
}
}
event := embedder.WindowMetricsEvent{
Width: widthPx,
Height: heightPx,
PixelRatio: pixelRatio,
}
flutterEngine.SendWindowMetricsEvent(event)
}
}
func glfwCharCallback(w *glfw.Window, char rune) {
if state.clientID != 0 {
state.addChar([]rune{char})
}
}
// Flutter Engine
func runFlutter(window *glfw.Window, c config) *embedder.FlutterEngine {
flutterEngine := embedder.NewFlutterEngine()
// Engine arguments
flutterEngine.AssetsPath = c.AssetsPath
flutterEngine.IcuDataPath = c.ICUDataPath
// Render callbacks
flutterEngine.FMakeCurrent = func(v unsafe.Pointer) bool {
w := glfw.GoWindow(v)
w.MakeContextCurrent()
return true
}
flutterEngine.FClearCurrent = func(v unsafe.Pointer) bool {
glfw.DetachCurrentContext()
return true
}
flutterEngine.FPresent = func(v unsafe.Pointer) bool {
w := glfw.GoWindow(v)
w.SwapBuffers()
return true
}
flutterEngine.FFboCallback = func(v unsafe.Pointer) int32 {
return 0
}
flutterEngine.FMakeResourceCurrent = func(v unsafe.Pointer) bool {
return false
}
// PlatformMessage
flutterEngine.FPlatfromMessage = func(platMessage *embedder.PlatformMessage, window unsafe.Pointer) bool {
windows := glfw.GoWindow(window)
hasDispatched := false
// Dispatch the message from the Flutter Engine, to all of the PluginReceivers
// having the same embedder.PlatformMessage.Channel name
for _, receivers := range c.PlatformMessageReceivers[platMessage.Channel] {
hasDispatched = receivers(platMessage, flutterEngine, windows) || hasDispatched
}
return hasDispatched
}
state.notifyState = func() {
// log.Printf("Text: Sending to the flutter engine %v", state)
updateEditingState(window)
}
flutterEngineIndex := flutterEngine.Index()
window.SetUserPointer(unsafe.Pointer(&flutterEngineIndex))
result := flutterEngine.Run(window.GLFWWindow(), c.VMArguments)
if result != embedder.KSuccess {
window.Destroy()
panic("Couldn't launch the FlutterEngine")
}
glfwFramebufferSizeCallback := newGLFWFramebufferSizeCallback(c.ForcePixelRatio, getScreenCoordinatesPerInch())
width, height := window.GetFramebufferSize()
glfwFramebufferSizeCallback(window, width, height)
var glfwKeyCallback func(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey)
if c.KeyboardLayout != nil {
glfwKeyCallback = glfwKey(*c.KeyboardLayout)
} else {
glfwKeyCallback = glfwKey(KeyboardQwertyLayout)
}
window.SetKeyCallback(glfwKeyCallback)
window.SetFramebufferSizeCallback(glfwFramebufferSizeCallback)
window.SetMouseButtonCallback(glfwMouseButtonCallback)
window.SetCharCallback(glfwCharCallback)
return flutterEngine
}
// getScreenCoordinatesPerInch returns the number of screen coordinates per
// inch for the main monitor. If the information is unavailable it returns
// a default value that assumes that a screen coordinate is one dp.
func getScreenCoordinatesPerInch() float64 {
// TODO: multi-monitor support (#74)
primaryMonitor := glfw.GetPrimaryMonitor()
if primaryMonitor == nil {
return dpPerInch
}
primaryMonitorMode := primaryMonitor.GetVideoMode()
if primaryMonitorMode == nil {
return dpPerInch
}
primaryMonitorWidthMM, _ := primaryMonitor.GetPhysicalSize()
if primaryMonitorWidthMM == 0 {
return dpPerInch
}
return float64(primaryMonitorMode.Width) / (float64(primaryMonitorWidthMM) / 25.4)
}
// Update the TextInput with the current state
func updateEditingState(window *glfw.Window) {
editingState := argsEditingState{
Text: string(state.word),
SelectionAffinity: "TextAffinity.downstream",
SelectionBase: state.selectionBase,
SelectionExtent: state.selectionExtent,
SelectionIsDirectional: false,
}
editingStateMarchalled, _ := json.Marshal([]interface{}{
state.clientID,
editingState,
})
message := embedder.Message{
Args: editingStateMarchalled,
Method: textUpdateStateMethod,
}
var mess = &embedder.PlatformMessage{
Channel: textInputChannel,
Message: message,
}
index := *(*int)(window.GetUserPointer())
flutterEngine := embedder.FlutterEngineByIndex(index)
flutterEngine.SendPlatformMessage(mess)
}
func performAction(window *glfw.Window, action string) {
actionArgs, _ := json.Marshal([]interface{}{
state.clientID,
"TextInputAction." + action,
})
message := embedder.Message{
Args: actionArgs,
Method: "TextInputClient.performAction",
}
var mess = &embedder.PlatformMessage{
Channel: textInputChannel,
Message: message,
}
index := *(*int)(window.GetUserPointer())
flutterEngine := embedder.FlutterEngineByIndex(index)
flutterEngine.SendPlatformMessage(mess)
}