-
Notifications
You must be signed in to change notification settings - Fork 281
/
Copy pathapplication.go
240 lines (203 loc) · 7.09 KB
/
application.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
package flutter
import (
"fmt"
"os"
"path/filepath"
"runtime"
"unsafe"
"github.com/go-gl/glfw/v3.2/glfw"
"github.com/pkg/errors"
"github.com/go-flutter-desktop/go-flutter/embedder"
"github.com/go-flutter-desktop/go-flutter/internal/execpath"
"github.com/go-flutter-desktop/go-flutter/internal/tasker"
)
// Run executes a flutter application with the provided options.
// given limitations this method must be called by the main function directly.
//
// Run(opt) is short for NewApplication(opt).Run()
func Run(opt ...Option) (err error) {
return NewApplication(opt...).Run()
}
// Application provides the flutter engine in a user friendly matter.
type Application struct {
config config
engine *embedder.FlutterEngine
window *glfw.Window
}
// NewApplication creates a new application with provided options.
func NewApplication(opt ...Option) *Application {
app := &Application{
config: defaultApplicationConfig,
}
// The platformPlugin, textinputPlugin, etc. are currently hardcoded as we
// have a hard link with GLFW. The plugins must be singleton and are
// accessed directly from the flutter package to wire up with glfw. If
// there's going to be a renderer interface, it's init would replace this
// configuration.
opt = append(opt, AddPlugin(defaultNavigationPlugin))
opt = append(opt, AddPlugin(defaultPlatformPlugin))
opt = append(opt, AddPlugin(defaultTextinputPlugin))
opt = append(opt, AddPlugin(defaultLifecyclePlugin))
opt = append(opt, AddPlugin(defaultKeyeventsPlugin))
// apply all configs
for _, o := range opt {
o(&app.config)
}
return app
}
// Run starts the application and waits for it to finish.
func (a *Application) Run() error {
runtime.LockOSThread()
err := glfw.Init()
if err != nil {
return errors.Wrap(err, "glfw init")
}
defer glfw.Terminate()
var monitor *glfw.Monitor
switch a.config.windowMode {
case WindowModeDefault:
// nothing
case WindowModeBorderless:
glfw.WindowHint(glfw.Decorated, glfw.False)
case WindowModeBorderlessFullscreen:
monitor = glfw.GetPrimaryMonitor()
mode := monitor.GetVideoMode()
a.config.windowInitialDimensions.width = mode.Width
a.config.windowInitialDimensions.height = mode.Height
glfw.WindowHint(glfw.RedBits, mode.RedBits)
glfw.WindowHint(glfw.GreenBits, mode.GreenBits)
glfw.WindowHint(glfw.BlueBits, mode.BlueBits)
glfw.WindowHint(glfw.RefreshRate, mode.RefreshRate)
default:
return errors.Errorf("invalid window mode %T", a.config.windowMode)
}
a.window, err = glfw.CreateWindow(a.config.windowInitialDimensions.width, a.config.windowInitialDimensions.height, "Loading..", monitor, nil)
if err != nil {
return errors.Wrap(err, "creating glfw window")
}
glfw.DefaultWindowHints()
defer a.window.Destroy()
if a.config.windowIconProvider != nil {
images, err := a.config.windowIconProvider()
if err != nil {
return errors.Wrap(err, "getting images from icon provider")
}
a.window.SetIcon(images)
}
if a.config.windowInitializerDeprecated != nil {
err = a.config.windowInitializerDeprecated(a.window)
if err != nil {
return errors.Wrap(err, "executing window initializer")
}
}
if a.config.windowDimensionLimits.minWidth != 0 {
a.window.SetSizeLimits(
a.config.windowDimensionLimits.minWidth,
a.config.windowDimensionLimits.minHeight,
a.config.windowDimensionLimits.maxWidth,
a.config.windowDimensionLimits.maxHeight,
)
}
a.engine = embedder.NewFlutterEngine()
messenger := newMessenger(a.engine)
for _, p := range a.config.plugins {
err = p.InitPlugin(messenger)
if err != nil {
return errors.Wrap(err, "failed to initialize plugin "+fmt.Sprintf("%T", p))
}
// Extra init call for plugins that satisfy the PluginGLFW interface.
if glfwPlugin, ok := p.(PluginGLFW); ok {
err = glfwPlugin.InitPluginGLFW(a.window)
if err != nil {
return errors.Wrap(err, "failed to initialize glfw plugin"+fmt.Sprintf("%T", p))
}
}
}
if a.config.flutterAssetsPath != "" {
a.engine.AssetsPath = a.config.flutterAssetsPath
} else {
execPath, err := execpath.ExecPath()
if err != nil {
return errors.Wrap(err, "failed to resolve path for executable")
}
a.engine.AssetsPath = filepath.Join(filepath.Dir(execPath), "flutter_assets")
}
if a.config.icuDataPath != "" {
a.engine.IcuDataPath = a.config.icuDataPath
} else {
execPath, err := execpath.ExecPath()
if err != nil {
return errors.Wrap(err, "failed to resolve path for executable")
}
a.engine.IcuDataPath = filepath.Join(filepath.Dir(execPath), "icudtl.dat")
}
// Render callbacks
a.engine.GLMakeCurrent = func() bool {
a.window.MakeContextCurrent()
return true
}
a.engine.GLClearCurrent = func() bool {
glfw.DetachCurrentContext()
return true
}
a.engine.GLPresent = func() bool {
a.window.SwapBuffers()
return true
}
a.engine.GLFboCallback = func() int32 {
return 0
}
a.engine.GLMakeResourceCurrent = func() bool {
return false
}
a.engine.GLProcResolver = func(procName string) unsafe.Pointer {
return glfw.GetProcAddress(procName)
}
a.engine.PlatfromMessage = messenger.handlePlatformMessage
// Not very nice, but we can only really fix this when there's a pluggable
// renderer.
defaultTextinputPlugin.keyboardLayout = a.config.keyboardLayout
flutterEnginePointer := uintptr(unsafe.Pointer(a.engine))
defer func() {
runtime.KeepAlive(flutterEnginePointer)
}()
a.window.SetUserPointer(unsafe.Pointer(&flutterEnginePointer))
result := a.engine.Run(unsafe.Pointer(&flutterEnginePointer), a.config.vmArguments)
if result != embedder.ResultSuccess {
switch result {
case embedder.ResultInvalidLibraryVersion:
fmt.Printf("go-flutter: engine.Run() returned result code %d (invalid library version)\n", result)
case embedder.ResultInvalidArguments:
fmt.Printf("go-flutter: engine.Run() returned result code %d (invalid arguments)\n", result)
default:
fmt.Printf("go-flutter: engine.Run() returned result code %d (unknown result code)\n", result)
}
os.Exit(1)
}
defaultPlatformPlugin.glfwTasker = tasker.New()
m := newWindowManager()
m.forcedPixelRatio = a.config.forcePixelRatio
m.glfwRefreshCallback(a.window)
a.window.SetRefreshCallback(m.glfwRefreshCallback)
a.window.SetKeyCallback(
func(window *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
defaultTextinputPlugin.glfwKeyCallback(window, key, scancode, action, mods)
defaultKeyeventsPlugin.sendKeyEvent(window, key, scancode, action, mods)
})
a.window.SetCharCallback(defaultTextinputPlugin.glfwCharCallback)
a.window.SetIconifyCallback(defaultLifecyclePlugin.glfwIconifyCallback)
a.window.SetCursorEnterCallback(m.glfwCursorEnterCallback)
a.window.SetCursorPosCallback(m.glfwCursorPosCallback)
a.window.SetMouseButtonCallback(m.glfwMouseButtonCallback)
a.window.SetScrollCallback(m.glfwScrollCallback)
defer a.engine.Shutdown()
for !a.window.ShouldClose() {
glfw.WaitEventsTimeout(0.016) // timeout to get 60fps-ish iterations
embedder.FlutterEngineFlushPendingTasksNow()
defaultPlatformPlugin.glfwTasker.ExecuteTasks()
messenger.engineTasker.ExecuteTasks()
}
fmt.Println("go-flutter: closing application")
return nil
}
// TODO: app.Start(), app.Wait()?