Skip to content

Commit

Permalink
Addressing comments & fix pointer warning
Browse files Browse the repository at this point in the history
  • Loading branch information
pchampio committed Aug 20, 2019
1 parent 9d9d2cb commit 2c365ea
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 34 deletions.
51 changes: 23 additions & 28 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,21 +157,6 @@ func (a *Application) Run() error {

// Create a messenger and init plugins
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))
}
}
}

// Create a TextureRegistry
texturer := newTextureRegistry(a.engine, a.window)

Expand Down Expand Up @@ -263,17 +248,21 @@ func (a *Application) Run() error {
os.Exit(1)
}

// Setup a new windowManager to handle windows pixel ratio's and pointer
// devices.
windowManager := newWindowManager(a.config.forcePixelRatio)
// force first refresh
windowManager.glfwRefreshCallback(a.window)
// Attach glfw window callbacks for refresh and position changes
a.window.SetRefreshCallback(windowManager.glfwRefreshCallback)
a.window.SetPosCallback(windowManager.glfwPosCallback)

// TODO: Can this only be done here? Why not in the plugin/glfwPlugin init loop above?
// Register plugins
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))
}
}

// Extra init call for plugins that satisfy the PluginTexture interface.
if texturePlugin, ok := p.(PluginTexture); ok {
err = texturePlugin.InitPluginTexture(texturer)
Expand All @@ -283,6 +272,15 @@ func (a *Application) Run() error {
}
}

// Setup a new windowManager to handle windows pixel ratio's and pointer
// devices.
windowManager := newWindowManager(a.config.forcePixelRatio)
// force first refresh
windowManager.glfwRefreshCallback(a.window)
// Attach glfw window callbacks for refresh and position changes
a.window.SetRefreshCallback(windowManager.glfwRefreshCallback)
a.window.SetPosCallback(windowManager.glfwPosCallback)

// Attach glfw window callbacks for text input
a.window.SetKeyCallback(
func(window *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
Expand Down Expand Up @@ -313,9 +311,6 @@ func (a *Application) Run() error {
messenger.engineTasker.ExecuteTasks()
}

// TODO: What if the window indicates to stop, but there are tasks left on
// the queue?

fmt.Println("go-flutter: closing application")

return nil
Expand Down
12 changes: 8 additions & 4 deletions event-loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type EventLoop struct {
onExpiredTask func(*embedder.FlutterTask) embedder.Result

// timeout for non-Rendering events that needs to be processed in a polling manner
maxWaitDuration time.Duration
platformMessageRefreshRate time.Duration
}

func newEventLoop(postEmptyEvent func(), onExpiredTask func(*embedder.FlutterTask) embedder.Result) *EventLoop {
Expand All @@ -37,7 +37,11 @@ func newEventLoop(postEmptyEvent func(), onExpiredTask func(*embedder.FlutterTas
// platform messages) and not too low (heavy CPU consumption).
// This value isn't related to FPS, as rendering events are process in a
// waiting manner.
maxWaitDuration: time.Duration(25) * time.Millisecond,
// Platform message are fetched from the engine every time the rendering
// event loop process rendering event (e.g.: moving the cursor on the
// window), when no rendering event occur (e.g., window minimized) platform
// message are fetch every 25ms.
platformMessageRefreshRate: time.Duration(25) * time.Millisecond,
}
}

Expand Down Expand Up @@ -110,10 +114,10 @@ func (t *EventLoop) WaitForEvents(rendererWaitEvents func(float64)) {
// along, the rendererWaitEvents will be resolved early because PostTask
// posts an empty event.
if t.priorityqueue.Len() == 0 {
rendererWaitEvents(t.maxWaitDuration.Seconds())
rendererWaitEvents(t.platformMessageRefreshRate.Seconds())
} else {
if top.FireTime.After(now) {
durationWait := math.Min(top.FireTime.Sub(now).Seconds(), t.maxWaitDuration.Seconds())
durationWait := math.Min(top.FireTime.Sub(now).Seconds(), t.platformMessageRefreshRate.Seconds())
rendererWaitEvents(durationWait)
}
}
Expand Down
11 changes: 9 additions & 2 deletions glfw.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ func (m *windowManager) sendPointerEvent(window *glfw.Window, phase embedder.Poi
return
}

// TODO(GeertJohan): sometimes the x and/or y given by glfw is negative or over window size, could this cause an issue?
// spew.Dump(event)
event := embedder.PointerEvent{
Phase: phase,
X: x * m.pixelsPerScreenCoordinate,
Expand All @@ -66,6 +64,15 @@ func (m *windowManager) sendPointerEvent(window *glfw.Window, phase embedder.Poi
flutterEnginePointer := *(*uintptr)(window.GetUserPointer())
flutterEngine := (*embedder.FlutterEngine)(unsafe.Pointer(flutterEnginePointer))

// Always send a pointer event with PhaseMove before an eventual PhaseRemove.
// If x/y on the last move doesn't equal x/y on the PhaseRemove, the remove
// is canceled in Flutter.
if phase == embedder.PointerPhaseRemove {
event.Phase = embedder.PointerPhaseHover
flutterEngine.SendPointerEvent(event)
event.Phase = embedder.PointerPhaseRemove
}

flutterEngine.SendPointerEvent(event)

if phase == embedder.PointerPhaseAdd {
Expand Down

0 comments on commit 2c365ea

Please sign in to comment.