Skip to content

Commit 85b3586

Browse files
authored
Clean up (#435)
* WIP refactor & aot support * fix test and 'error' everything * use helper to set CGO union type * feat: add plugin gomod isolate plugin * chore: rm code that require Flutter master channel * cleanup * fix HandleFuncSync * chore: adress review comments * fix go.mod * PostEmptyEvent faster platform message sent
1 parent f1c699d commit 85b3586

25 files changed

+335
-258
lines changed

Diff for: accessibility.go

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import "github.com/go-flutter-desktop/go-flutter/plugin"
44

55
type accessibilityPlugin struct{}
66

7-
var _ Plugin = &accessibilityPlugin{} // compile-time type check
8-
97
// hardcoded because there is no swappable renderer interface.
108
var defaultAccessibilityPlugin = &accessibilityPlugin{}
119

Diff for: application.go

+31-47
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package flutter
33
import (
44
"fmt"
55
"os"
6-
"path/filepath"
76
"runtime"
87
"time"
98
"unsafe"
@@ -15,10 +14,8 @@ import (
1514

1615
"github.com/go-flutter-desktop/go-flutter/embedder"
1716
"github.com/go-flutter-desktop/go-flutter/internal/debounce"
18-
"github.com/go-flutter-desktop/go-flutter/internal/execpath"
1917
"github.com/go-flutter-desktop/go-flutter/internal/opengl"
2018
"github.com/go-flutter-desktop/go-flutter/internal/tasker"
21-
"github.com/go-flutter-desktop/go-flutter/plugin"
2219
)
2320

2421
// Run executes a flutter application with the provided options.
@@ -40,7 +37,7 @@ type Application struct {
4037
// NewApplication creates a new application with provided options.
4138
func NewApplication(opt ...Option) *Application {
4239
app := &Application{
43-
config: defaultApplicationConfig,
40+
config: newApplicationConfig(),
4441
}
4542

4643
// The platformPlugin, textinputPlugin, etc. are currently hardcoded as we
@@ -54,6 +51,7 @@ func NewApplication(opt ...Option) *Application {
5451
opt = append(opt, AddPlugin(defaultLifecyclePlugin))
5552
opt = append(opt, AddPlugin(defaultKeyeventsPlugin))
5653
opt = append(opt, AddPlugin(defaultAccessibilityPlugin))
54+
opt = append(opt, AddPlugin(defaultIsolatePlugin))
5755
opt = append(opt, AddPlugin(defaultMousecursorPlugin))
5856

5957
// apply all configs
@@ -121,6 +119,13 @@ func (a *Application) Run() error {
121119

122120
opengl.GLFWWindowHint()
123121

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+
124129
if a.config.windowInitialLocation.xpos != 0 {
125130
// To create the window at a specific position, make it initially invisible
126131
// using the Visible window hint, set its position and then show it.
@@ -178,34 +183,32 @@ func (a *Application) Run() error {
178183
)
179184
}
180185

186+
// Create a empty FlutterEngine.
181187
a.engine = embedder.NewFlutterEngine()
182188

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+
183194
// Create a messenger and init plugins
184195
messenger := newMessenger(a.engine)
196+
// Attach PlatformMessage callback function onto the engine
197+
a.engine.PlatfromMessage = messenger.handlePlatformMessage
198+
185199
// Create a TextureRegistry
186200
texturer := newTextureRegistry(a.engine, a.window)
201+
// Attach TextureRegistry callback function onto the engine
202+
a.engine.GLExternalTextureFrameCallback = texturer.handleExternalTexture
187203

188204
// Create a new eventloop
189205
eventLoop := newEventLoop(
190206
glfw.PostEmptyEvent, // Wakeup GLFW
191207
a.engine.RunTask, // Flush tasks
192208
)
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
209212

210213
// Attach GL callback functions onto the engine
211214
a.engine.GLMakeCurrent = func() bool {
@@ -233,14 +236,6 @@ func (a *Application) Run() error {
233236
a.engine.GLProcResolver = func(procName string) unsafe.Pointer {
234237
return glfw.GetProcAddress(procName)
235238
}
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
244239

245240
// Not very nice, but we can only really fix this when there's a pluggable
246241
// renderer.
@@ -256,18 +251,9 @@ func (a *Application) Run() error {
256251
a.window.SetUserPointer(unsafe.Pointer(&flutterEnginePointer))
257252

258253
// 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)
271257
os.Exit(1)
272258
}
273259

@@ -279,9 +265,9 @@ func (a *Application) Run() error {
279265
base, _ := languageTag.Base()
280266
region, _ := languageTag.Region()
281267
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)
285271
}
286272

287273
// Register plugins
@@ -312,9 +298,8 @@ func (a *Application) Run() error {
312298
initialRoute := os.Getenv("GOFLUTTER_ROUTE")
313299
if initialRoute != "" {
314300
defaultPlatformPlugin.addFrameworkReadyCallback(func() {
315-
plugin.
316-
NewMethodChannel(messenger, "flutter/navigation", plugin.JSONMethodCodec{}).
317-
InvokeMethod("pushRoute", initialRoute)
301+
defaultNavigationPlugin.
302+
channel.InvokeMethod("pushRoute", initialRoute)
318303
})
319304
}
320305

@@ -372,7 +357,6 @@ func (a *Application) Run() error {
372357

373358
// Execute tasks that MUST be run in the engine thread (!blocks rendering!)
374359
glfwDebouceTasker.ExecuteTasks()
375-
defaultPlatformPlugin.glfwTasker.ExecuteTasks()
376360
messenger.engineTasker.ExecuteTasks()
377361
texturer.engineTasker.ExecuteTasks()
378362
}

0 commit comments

Comments
 (0)