forked from go-flutter-desktop/go-flutter
-
Notifications
You must be signed in to change notification settings - Fork 7
/
gutter.go
362 lines (296 loc) · 9.8 KB
/
gutter.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
package gutter
import (
"encoding/json"
"fmt"
"log"
"time"
"unsafe"
"github.com/Drakirus/go-flutter-desktop-embedder/flutter"
"github.com/go-gl/glfw/v3.2/glfw"
)
// 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) {
fmt.Println("WARNING: The go-flutter-desktop-embedder package has moved,\n please read https://github.com/go-flutter-desktop/go-flutter/wiki/Package-has-moved-to-an-organization\n")
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...)
if err = glfw.Init(); err != nil {
return err
}
defer glfw.Terminate()
window, err = glfw.CreateWindow(c.WindowDimension.x, c.WindowDimension.y, "Loading..", nil, nil)
if err != nil {
return err
}
defer window.Destroy()
if err = c.WindowInitializer(window); err != nil {
return err
}
engine := runFlutter(window, c)
defer engine.Shutdown()
for !window.ShouldClose() {
// glfw.WaitEvents()
glfw.PollEvents()
flutter.EngineFlushPendingTasksNow()
}
return nil
}
// GLFW callbacks to the Flutter Engine
func glfwCursorPositionCallbackAtPhase(
window *glfw.Window, phase flutter.PointerPhase,
x float64, y float64,
) {
winWidth, _ := window.GetSize()
frameBuffWidth, _ := window.GetFramebufferSize()
contentScale := float64(frameBuffWidth / winWidth)
event := flutter.PointerEvent{
Phase: phase,
X: x * contentScale,
Y: y * contentScale,
Timestamp: time.Now().UnixNano() / int64(time.Millisecond),
}
index := *(*int)(window.GetUserPointer())
flutterOGL := flutter.SelectEngine(index)
flutterOGL.EngineSendPointerEvent(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, flutter.KDown, x, y)
window.SetCursorPosCallback(func(window *glfw.Window, x float64, y float64) {
glfwCursorPositionCallbackAtPhase(window, flutter.KMove, x, y)
})
}
if action == glfw.Release {
x, y := window.GetCursorPos()
glfwCursorPositionCallbackAtPhase(window, flutter.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) {
return func(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
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 == glfw.ModControl {
performAction(w, "done")
} else {
state.addChar([]rune{'\n'})
performAction(w, "newline")
}
case glfw.KeyHome:
state.MoveCursorHome(int(mods))
case glfw.KeyEnd:
state.MoveCursorEnd(int(mods))
case glfw.KeyLeft:
state.MoveCursorLeft(int(mods))
case glfw.KeyRight:
state.MoveCursorRight(int(mods))
case glfw.KeyDelete:
state.Delete(int(mods))
case glfw.KeyBackspace:
state.Backspace(int(mods))
case keyboardLayout.SelectAll:
if mods == glfw.ModControl {
state.SelectAll()
}
case keyboardLayout.Copy:
if mods == glfw.ModControl && state.isSelected() {
_, _, selectedContent := state.GetSelectedText()
w.SetClipboardString(selectedContent)
}
case keyboardLayout.Cut:
if mods == glfw.ModControl && state.isSelected() {
_, _, selectedContent := state.GetSelectedText()
w.SetClipboardString(selectedContent)
state.RemoveSelectedText()
}
case keyboardLayout.Paste:
if mods == glfw.ModControl {
var clpString, err = w.GetClipboardString()
if err != nil {
log.Printf("unable to get the clipboard content: %v\n", err)
} else {
state.addChar([]rune(clpString))
}
}
}
}
}
}
}
func newGLFWFramebufferSizeCallback(pixelRatio float64, monitorScreenCoordinatesPerInch float64) func(*glfw.Window, int, int) {
return func(window *glfw.Window, widthPx int, heightPx int) {
index := *(*int)(window.GetUserPointer())
flutterOGL := flutter.SelectEngine(index)
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 := flutter.WindowMetricsEvent{
Width: widthPx,
Height: heightPx,
PixelRatio: pixelRatio,
}
flutterOGL.EngineSendWindowMetricsEvent(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) *flutter.EngineOpenGL {
flutterOGL := flutter.NewEngineOpenGL()
// Engine arguments
flutterOGL.AssetsPath = c.AssetPath
flutterOGL.IcuDataPath = c.ICUDataPath
// Render callbacks
flutterOGL.FMakeCurrent = func(v unsafe.Pointer) bool {
w := glfw.GoWindow(v)
w.MakeContextCurrent()
return true
}
flutterOGL.FClearCurrent = func(v unsafe.Pointer) bool {
glfw.DetachCurrentContext()
return true
}
flutterOGL.FPresent = func(v unsafe.Pointer) bool {
w := glfw.GoWindow(v)
w.SwapBuffers()
return true
}
flutterOGL.FFboCallback = func(v unsafe.Pointer) int32 {
return 0
}
flutterOGL.FMakeResourceCurrent = func(v unsafe.Pointer) bool {
return false
}
// PlatformMessage
flutterOGL.FPlatfromMessage = func(platMessage *flutter.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 flutter.PlatformMessage.Channel name
for _, receivers := range c.PlatformMessageReceivers[platMessage.Channel] {
hasDispatched = receivers(platMessage, flutterOGL, windows) || hasDispatched
}
return hasDispatched
}
state.notifyState = func() {
// log.Printf("Text: Sending to the flutter engine %v", state)
updateEditingState(window)
}
flutterOGLIndex := flutterOGL.Index()
window.SetUserPointer(unsafe.Pointer(&flutterOGLIndex))
result := flutterOGL.Run(window.GLFWWindow(), c.VMArguments)
if result != flutter.KSuccess {
window.Destroy()
panic("Couldn't launch the FlutterEngine")
}
glfwFramebufferSizeCallback := newGLFWFramebufferSizeCallback(c.PixelRatio, 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 flutterOGL
}
// 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 := flutter.Message{
Args: editingStateMarchalled,
Method: textUpdateStateMethod,
}
var mess = &flutter.PlatformMessage{
Channel: textInputChannel,
Message: message,
}
index := *(*int)(window.GetUserPointer())
flutterOGL := flutter.SelectEngine(index)
flutterOGL.SendPlatformMessage(mess)
}
func performAction(window *glfw.Window, action string) {
actionArgs, _ := json.Marshal([]interface{}{
state.clientID,
"TextInputAction." + action,
})
message := flutter.Message{
Args: actionArgs,
Method: "TextInputClient.performAction",
}
var mess = &flutter.PlatformMessage{
Channel: textInputChannel,
Message: message,
}
index := *(*int)(window.GetUserPointer())
flutterOGL := flutter.SelectEngine(index)
flutterOGL.SendPlatformMessage(mess)
}