Skip to content

Clean up #435

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 25, 2020
Merged
2 changes: 0 additions & 2 deletions accessibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import "github.com/go-flutter-desktop/go-flutter/plugin"

type accessibilityPlugin struct{}

var _ Plugin = &accessibilityPlugin{} // compile-time type check

// hardcoded because there is no swappable renderer interface.
var defaultAccessibilityPlugin = &accessibilityPlugin{}

Expand Down
78 changes: 31 additions & 47 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package flutter
import (
"fmt"
"os"
"path/filepath"
"runtime"
"time"
"unsafe"
Expand All @@ -15,10 +14,8 @@ import (

"github.com/go-flutter-desktop/go-flutter/embedder"
"github.com/go-flutter-desktop/go-flutter/internal/debounce"
"github.com/go-flutter-desktop/go-flutter/internal/execpath"
"github.com/go-flutter-desktop/go-flutter/internal/opengl"
"github.com/go-flutter-desktop/go-flutter/internal/tasker"
"github.com/go-flutter-desktop/go-flutter/plugin"
)

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

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

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

opengl.GLFWWindowHint()

{
// TODO(drakirus): Delete this when https://github.com/go-gl/glfw/issues/272 is resolved.
// Post an empty event from the main thread before it can happen in a non-main thread,
// to work around https://github.com/glfw/glfw/issues/1649.
glfw.PostEmptyEvent()
}

if a.config.windowInitialLocation.xpos != 0 {
// To create the window at a specific position, make it initially invisible
// using the Visible window hint, set its position and then show it.
Expand Down Expand Up @@ -176,34 +181,32 @@ func (a *Application) Run() error {
)
}

// Create a empty FlutterEngine.
a.engine = embedder.NewFlutterEngine()

// Set configuration values to engine.
a.engine.AssetsPath = a.config.flutterAssetsPath
a.engine.IcuDataPath = a.config.icuDataPath
a.engine.ElfSnapshotPath = a.config.elfSnapshotpath

// Create a messenger and init plugins
messenger := newMessenger(a.engine)
// Attach PlatformMessage callback function onto the engine
a.engine.PlatfromMessage = messenger.handlePlatformMessage

// Create a TextureRegistry
texturer := newTextureRegistry(a.engine, a.window)
// Attach TextureRegistry callback function onto the engine
a.engine.GLExternalTextureFrameCallback = texturer.handleExternalTexture

// Create a new eventloop
eventLoop := newEventLoop(
glfw.PostEmptyEvent, // Wakeup GLFW
a.engine.RunTask, // Flush tasks
)

execPath, err := execpath.ExecPath()
if err != nil {
return errors.Wrap(err, "failed to resolve path for executable")
}
// Set configuration values to engine, with fallbacks to sane defaults.
if a.config.flutterAssetsPath != "" {
a.engine.AssetsPath = a.config.flutterAssetsPath
} else {
a.engine.AssetsPath = filepath.Join(filepath.Dir(execPath), "flutter_assets")
}
if a.config.icuDataPath != "" {
a.engine.IcuDataPath = a.config.icuDataPath
} else {
a.engine.IcuDataPath = filepath.Join(filepath.Dir(execPath), "icudtl.dat")
}
// Attach TaskRunner callback functions onto the engine
a.engine.TaskRunnerRunOnCurrentThread = eventLoop.RunOnCurrentThread
a.engine.TaskRunnerPostTask = eventLoop.PostTask

// Attach GL callback functions onto the engine
a.engine.GLMakeCurrent = func() bool {
Expand Down Expand Up @@ -231,14 +234,6 @@ func (a *Application) Run() error {
a.engine.GLProcResolver = func(procName string) unsafe.Pointer {
return glfw.GetProcAddress(procName)
}
a.engine.GLExternalTextureFrameCallback = texturer.handleExternalTexture

// Attach TaskRunner callback functions onto the engine
a.engine.TaskRunnerRunOnCurrentThread = eventLoop.RunOnCurrentThread
a.engine.TaskRunnerPostTask = eventLoop.PostTask

// Attach PlatformMessage callback functions onto the engine
a.engine.PlatfromMessage = messenger.handlePlatformMessage

// Not very nice, but we can only really fix this when there's a pluggable
// renderer.
Expand All @@ -254,18 +249,9 @@ func (a *Application) Run() error {
a.window.SetUserPointer(unsafe.Pointer(&flutterEnginePointer))

// Start the engine
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)
case embedder.ResultInternalInconsistency:
fmt.Printf("go-flutter: engine.Run() returned result code %d (internal inconsistency)\n", result)
default:
fmt.Printf("go-flutter: engine.Run() returned result code %d (unknown result code)\n", result)
}
err = a.engine.Run(unsafe.Pointer(&flutterEnginePointer), a.config.vmArguments)
if err != nil {
fmt.Printf("go-flutter: %v\n", err)
os.Exit(1)
}

Expand All @@ -277,9 +263,9 @@ func (a *Application) Run() error {
base, _ := languageTag.Base()
region, _ := languageTag.Region()
scriptCode, _ := languageTag.Script()
result = a.engine.UpdateSystemLocale(base.String(), region.String(), scriptCode.String())
if result != embedder.ResultSuccess {
fmt.Printf("go-flutter: engine.UpdateSystemLocale() returned result code %d\n", result)
err = a.engine.UpdateSystemLocale(base.String(), region.String(), scriptCode.String())
if err != nil {
fmt.Printf("go-flutter: %v\n", err)
}

// Register plugins
Expand Down Expand Up @@ -310,9 +296,8 @@ func (a *Application) Run() error {
initialRoute := os.Getenv("GOFLUTTER_ROUTE")
if initialRoute != "" {
defaultPlatformPlugin.addFrameworkReadyCallback(func() {
plugin.
NewMethodChannel(messenger, "flutter/navigation", plugin.JSONMethodCodec{}).
InvokeMethod("pushRoute", initialRoute)
defaultNavigationPlugin.
channel.InvokeMethod("pushRoute", initialRoute)
})
}

Expand Down Expand Up @@ -370,7 +355,6 @@ func (a *Application) Run() error {

// Execute tasks that MUST be run in the engine thread (!blocks rendering!)
glfwDebouceTasker.ExecuteTasks()
defaultPlatformPlugin.glfwTasker.ExecuteTasks()
messenger.engineTasker.ExecuteTasks()
texturer.engineTasker.ExecuteTasks()
}
Expand Down
Loading