Skip to content

Commit 2c365ea

Browse files
committed
Addressing comments & fix pointer warning
1 parent 9d9d2cb commit 2c365ea

File tree

3 files changed

+40
-34
lines changed

3 files changed

+40
-34
lines changed

application.go

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -157,21 +157,6 @@ func (a *Application) Run() error {
157157

158158
// Create a messenger and init plugins
159159
messenger := newMessenger(a.engine)
160-
for _, p := range a.config.plugins {
161-
err = p.InitPlugin(messenger)
162-
if err != nil {
163-
return errors.Wrap(err, "failed to initialize plugin "+fmt.Sprintf("%T", p))
164-
}
165-
166-
// Extra init call for plugins that satisfy the PluginGLFW interface.
167-
if glfwPlugin, ok := p.(PluginGLFW); ok {
168-
err = glfwPlugin.InitPluginGLFW(a.window)
169-
if err != nil {
170-
return errors.Wrap(err, "failed to initialize glfw plugin"+fmt.Sprintf("%T", p))
171-
}
172-
}
173-
}
174-
175160
// Create a TextureRegistry
176161
texturer := newTextureRegistry(a.engine, a.window)
177162

@@ -263,17 +248,21 @@ func (a *Application) Run() error {
263248
os.Exit(1)
264249
}
265250

266-
// Setup a new windowManager to handle windows pixel ratio's and pointer
267-
// devices.
268-
windowManager := newWindowManager(a.config.forcePixelRatio)
269-
// force first refresh
270-
windowManager.glfwRefreshCallback(a.window)
271-
// Attach glfw window callbacks for refresh and position changes
272-
a.window.SetRefreshCallback(windowManager.glfwRefreshCallback)
273-
a.window.SetPosCallback(windowManager.glfwPosCallback)
274-
275-
// TODO: Can this only be done here? Why not in the plugin/glfwPlugin init loop above?
251+
// Register plugins
276252
for _, p := range a.config.plugins {
253+
err = p.InitPlugin(messenger)
254+
if err != nil {
255+
return errors.Wrap(err, "failed to initialize plugin "+fmt.Sprintf("%T", p))
256+
}
257+
258+
// Extra init call for plugins that satisfy the PluginGLFW interface.
259+
if glfwPlugin, ok := p.(PluginGLFW); ok {
260+
err = glfwPlugin.InitPluginGLFW(a.window)
261+
if err != nil {
262+
return errors.Wrap(err, "failed to initialize glfw plugin"+fmt.Sprintf("%T", p))
263+
}
264+
}
265+
277266
// Extra init call for plugins that satisfy the PluginTexture interface.
278267
if texturePlugin, ok := p.(PluginTexture); ok {
279268
err = texturePlugin.InitPluginTexture(texturer)
@@ -283,6 +272,15 @@ func (a *Application) Run() error {
283272
}
284273
}
285274

275+
// Setup a new windowManager to handle windows pixel ratio's and pointer
276+
// devices.
277+
windowManager := newWindowManager(a.config.forcePixelRatio)
278+
// force first refresh
279+
windowManager.glfwRefreshCallback(a.window)
280+
// Attach glfw window callbacks for refresh and position changes
281+
a.window.SetRefreshCallback(windowManager.glfwRefreshCallback)
282+
a.window.SetPosCallback(windowManager.glfwPosCallback)
283+
286284
// Attach glfw window callbacks for text input
287285
a.window.SetKeyCallback(
288286
func(window *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
@@ -313,9 +311,6 @@ func (a *Application) Run() error {
313311
messenger.engineTasker.ExecuteTasks()
314312
}
315313

316-
// TODO: What if the window indicates to stop, but there are tasks left on
317-
// the queue?
318-
319314
fmt.Println("go-flutter: closing application")
320315

321316
return nil

event-loop.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type EventLoop struct {
2222
onExpiredTask func(*embedder.FlutterTask) embedder.Result
2323

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

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

@@ -110,10 +114,10 @@ func (t *EventLoop) WaitForEvents(rendererWaitEvents func(float64)) {
110114
// along, the rendererWaitEvents will be resolved early because PostTask
111115
// posts an empty event.
112116
if t.priorityqueue.Len() == 0 {
113-
rendererWaitEvents(t.maxWaitDuration.Seconds())
117+
rendererWaitEvents(t.platformMessageRefreshRate.Seconds())
114118
} else {
115119
if top.FireTime.After(now) {
116-
durationWait := math.Min(top.FireTime.Sub(now).Seconds(), t.maxWaitDuration.Seconds())
120+
durationWait := math.Min(top.FireTime.Sub(now).Seconds(), t.platformMessageRefreshRate.Seconds())
117121
rendererWaitEvents(durationWait)
118122
}
119123
}

glfw.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ func (m *windowManager) sendPointerEvent(window *glfw.Window, phase embedder.Poi
5353
return
5454
}
5555

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

67+
// Always send a pointer event with PhaseMove before an eventual PhaseRemove.
68+
// If x/y on the last move doesn't equal x/y on the PhaseRemove, the remove
69+
// is canceled in Flutter.
70+
if phase == embedder.PointerPhaseRemove {
71+
event.Phase = embedder.PointerPhaseHover
72+
flutterEngine.SendPointerEvent(event)
73+
event.Phase = embedder.PointerPhaseRemove
74+
}
75+
6976
flutterEngine.SendPointerEvent(event)
7077

7178
if phase == embedder.PointerPhaseAdd {

0 commit comments

Comments
 (0)