@@ -3,7 +3,6 @@ package flutter
3
3
import (
4
4
"fmt"
5
5
"os"
6
- "path/filepath"
7
6
"runtime"
8
7
"time"
9
8
"unsafe"
@@ -15,10 +14,8 @@ import (
15
14
16
15
"github.com/go-flutter-desktop/go-flutter/embedder"
17
16
"github.com/go-flutter-desktop/go-flutter/internal/debounce"
18
- "github.com/go-flutter-desktop/go-flutter/internal/execpath"
19
17
"github.com/go-flutter-desktop/go-flutter/internal/opengl"
20
18
"github.com/go-flutter-desktop/go-flutter/internal/tasker"
21
- "github.com/go-flutter-desktop/go-flutter/plugin"
22
19
)
23
20
24
21
// Run executes a flutter application with the provided options.
@@ -40,7 +37,7 @@ type Application struct {
40
37
// NewApplication creates a new application with provided options.
41
38
func NewApplication (opt ... Option ) * Application {
42
39
app := & Application {
43
- config : defaultApplicationConfig ,
40
+ config : newApplicationConfig () ,
44
41
}
45
42
46
43
// The platformPlugin, textinputPlugin, etc. are currently hardcoded as we
@@ -54,6 +51,7 @@ func NewApplication(opt ...Option) *Application {
54
51
opt = append (opt , AddPlugin (defaultLifecyclePlugin ))
55
52
opt = append (opt , AddPlugin (defaultKeyeventsPlugin ))
56
53
opt = append (opt , AddPlugin (defaultAccessibilityPlugin ))
54
+ opt = append (opt , AddPlugin (defaultIsolatePlugin ))
57
55
opt = append (opt , AddPlugin (defaultMousecursorPlugin ))
58
56
59
57
// apply all configs
@@ -121,6 +119,13 @@ func (a *Application) Run() error {
121
119
122
120
opengl .GLFWWindowHint ()
123
121
122
+ {
123
+ // TODO(drakirus): Delete this when https://github.com/go-gl/glfw/issues/272 is resolved.
124
+ // Post an empty event from the main thread before it can happen in a non-main thread,
125
+ // to work around https://github.com/glfw/glfw/issues/1649.
126
+ glfw .PostEmptyEvent ()
127
+ }
128
+
124
129
if a .config .windowInitialLocation .xpos != 0 {
125
130
// To create the window at a specific position, make it initially invisible
126
131
// using the Visible window hint, set its position and then show it.
@@ -178,34 +183,32 @@ func (a *Application) Run() error {
178
183
)
179
184
}
180
185
186
+ // Create a empty FlutterEngine.
181
187
a .engine = embedder .NewFlutterEngine ()
182
188
189
+ // Set configuration values to engine.
190
+ a .engine .AssetsPath = a .config .flutterAssetsPath
191
+ a .engine .IcuDataPath = a .config .icuDataPath
192
+ a .engine .ElfSnapshotPath = a .config .elfSnapshotpath
193
+
183
194
// Create a messenger and init plugins
184
195
messenger := newMessenger (a .engine )
196
+ // Attach PlatformMessage callback function onto the engine
197
+ a .engine .PlatfromMessage = messenger .handlePlatformMessage
198
+
185
199
// Create a TextureRegistry
186
200
texturer := newTextureRegistry (a .engine , a .window )
201
+ // Attach TextureRegistry callback function onto the engine
202
+ a .engine .GLExternalTextureFrameCallback = texturer .handleExternalTexture
187
203
188
204
// Create a new eventloop
189
205
eventLoop := newEventLoop (
190
206
glfw .PostEmptyEvent , // Wakeup GLFW
191
207
a .engine .RunTask , // Flush tasks
192
208
)
193
-
194
- execPath , err := execpath .ExecPath ()
195
- if err != nil {
196
- return errors .Wrap (err , "failed to resolve path for executable" )
197
- }
198
- // Set configuration values to engine, with fallbacks to sane defaults.
199
- if a .config .flutterAssetsPath != "" {
200
- a .engine .AssetsPath = a .config .flutterAssetsPath
201
- } else {
202
- a .engine .AssetsPath = filepath .Join (filepath .Dir (execPath ), "flutter_assets" )
203
- }
204
- if a .config .icuDataPath != "" {
205
- a .engine .IcuDataPath = a .config .icuDataPath
206
- } else {
207
- a .engine .IcuDataPath = filepath .Join (filepath .Dir (execPath ), "icudtl.dat" )
208
- }
209
+ // Attach TaskRunner callback functions onto the engine
210
+ a .engine .TaskRunnerRunOnCurrentThread = eventLoop .RunOnCurrentThread
211
+ a .engine .TaskRunnerPostTask = eventLoop .PostTask
209
212
210
213
// Attach GL callback functions onto the engine
211
214
a .engine .GLMakeCurrent = func () bool {
@@ -233,14 +236,6 @@ func (a *Application) Run() error {
233
236
a .engine .GLProcResolver = func (procName string ) unsafe.Pointer {
234
237
return glfw .GetProcAddress (procName )
235
238
}
236
- a .engine .GLExternalTextureFrameCallback = texturer .handleExternalTexture
237
-
238
- // Attach TaskRunner callback functions onto the engine
239
- a .engine .TaskRunnerRunOnCurrentThread = eventLoop .RunOnCurrentThread
240
- a .engine .TaskRunnerPostTask = eventLoop .PostTask
241
-
242
- // Attach PlatformMessage callback functions onto the engine
243
- a .engine .PlatfromMessage = messenger .handlePlatformMessage
244
239
245
240
// Not very nice, but we can only really fix this when there's a pluggable
246
241
// renderer.
@@ -256,18 +251,9 @@ func (a *Application) Run() error {
256
251
a .window .SetUserPointer (unsafe .Pointer (& flutterEnginePointer ))
257
252
258
253
// Start the engine
259
- result := a .engine .Run (unsafe .Pointer (& flutterEnginePointer ), a .config .vmArguments )
260
- if result != embedder .ResultSuccess {
261
- switch result {
262
- case embedder .ResultInvalidLibraryVersion :
263
- fmt .Printf ("go-flutter: engine.Run() returned result code %d (invalid library version)\n " , result )
264
- case embedder .ResultInvalidArguments :
265
- fmt .Printf ("go-flutter: engine.Run() returned result code %d (invalid arguments)\n " , result )
266
- case embedder .ResultInternalInconsistency :
267
- fmt .Printf ("go-flutter: engine.Run() returned result code %d (internal inconsistency)\n " , result )
268
- default :
269
- fmt .Printf ("go-flutter: engine.Run() returned result code %d (unknown result code)\n " , result )
270
- }
254
+ err = a .engine .Run (unsafe .Pointer (& flutterEnginePointer ), a .config .vmArguments )
255
+ if err != nil {
256
+ fmt .Printf ("go-flutter: %v\n " , err )
271
257
os .Exit (1 )
272
258
}
273
259
@@ -279,9 +265,9 @@ func (a *Application) Run() error {
279
265
base , _ := languageTag .Base ()
280
266
region , _ := languageTag .Region ()
281
267
scriptCode , _ := languageTag .Script ()
282
- result = a .engine .UpdateSystemLocale (base .String (), region .String (), scriptCode .String ())
283
- if result != embedder . ResultSuccess {
284
- fmt .Printf ("go-flutter: engine.UpdateSystemLocale() returned result code %d \n " , result )
268
+ err = a .engine .UpdateSystemLocale (base .String (), region .String (), scriptCode .String ())
269
+ if err != nil {
270
+ fmt .Printf ("go-flutter: %v \n " , err )
285
271
}
286
272
287
273
// Register plugins
@@ -312,9 +298,8 @@ func (a *Application) Run() error {
312
298
initialRoute := os .Getenv ("GOFLUTTER_ROUTE" )
313
299
if initialRoute != "" {
314
300
defaultPlatformPlugin .addFrameworkReadyCallback (func () {
315
- plugin .
316
- NewMethodChannel (messenger , "flutter/navigation" , plugin.JSONMethodCodec {}).
317
- InvokeMethod ("pushRoute" , initialRoute )
301
+ defaultNavigationPlugin .
302
+ channel .InvokeMethod ("pushRoute" , initialRoute )
318
303
})
319
304
}
320
305
@@ -372,7 +357,6 @@ func (a *Application) Run() error {
372
357
373
358
// Execute tasks that MUST be run in the engine thread (!blocks rendering!)
374
359
glfwDebouceTasker .ExecuteTasks ()
375
- defaultPlatformPlugin .glfwTasker .ExecuteTasks ()
376
360
messenger .engineTasker .ExecuteTasks ()
377
361
texturer .engineTasker .ExecuteTasks ()
378
362
}
0 commit comments