Skip to content

Commit 5a56184

Browse files
committed
Feature/callback messages (#220)
1 parent f7b8240 commit 5a56184

16 files changed

+309
-141
lines changed

application.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func (a *Application) Run() error {
104104
glfw.WindowHint(glfw.ContextVersionMinor, 1)
105105
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
106106
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
107-
107+
108108
if a.config.windowInitialLocations.xpos != 0 {
109109
// To create the window at a specific position, make it initially invisible
110110
// using the Visible window hint, set its position and then show it.

embedder/embedder.go

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ package embedder
33
// #include "embedder.h"
44
// FlutterEngineResult runFlutter(void *user_data, FlutterEngine *engine, FlutterProjectArgs * Args,
55
// const char *const * vmArgs, int nVmAgrs);
6+
// FlutterEngineResult
7+
// createMessageResponseHandle(FlutterEngine engine, void *user_data,
8+
// FlutterPlatformMessageResponseHandle **reply);
69
// char** makeCharArray(int size);
710
// void setArrayString(char **a, char *s, int n);
811
// const int32_t kFlutterSemanticsNodeIdBatchEnd = -1;
912
// const int32_t kFlutterSemanticsCustomActionIdBatchEnd = -1;
1013
import "C"
1114
import (
15+
"errors"
1216
"fmt"
17+
"runtime"
1318
"runtime/debug"
1419
"sync"
1520
"unsafe"
@@ -256,8 +261,11 @@ type PlatformMessage struct {
256261
Channel string
257262
Message []byte
258263

259-
// ResponseHandle is only set when receiving a platform message.
260-
// https://github.com/flutter/flutter/issues/18852
264+
// ResponseHandle is set on some recieved platform message. All
265+
// PlatformMessage recieved with this attribute must send a response with
266+
// `SendPlatformMessageResponse`.
267+
// ResponseHandle can also be created from the embedder side when a
268+
// platform(golang) message needs native callback.
261269
ResponseHandle PlatformMessageResponseHandle
262270
}
263271

@@ -357,8 +365,42 @@ func (flu *FlutterEngine) MarkExternalTextureFrameAvailable(textureID int64) Res
357365
return (Result)(res)
358366
}
359367

360-
// FlutterEngineGetCurrentTime gets the current time in nanoseconds from the
361-
// clock used by the flutter engine.
368+
// DataCallback is a function called when a PlatformMessage response send back
369+
// to the embedder.
370+
type DataCallback func(binaryReply []byte)
371+
372+
// CreatePlatformMessageResponseHandle creates a platform message response
373+
// handle that allows the embedder to set a native callback for a response to a
374+
// message.
375+
// Must be collected via `ReleasePlatformMessageResponseHandle` after the call
376+
// to `SendPlatformMessage`.
377+
func (flu *FlutterEngine) CreatePlatformMessageResponseHandle(callback DataCallback) (PlatformMessageResponseHandle, error) {
378+
var responseHandle *C.FlutterPlatformMessageResponseHandle
379+
380+
callbackPointer := uintptr(unsafe.Pointer(&callback))
381+
defer func() {
382+
runtime.KeepAlive(callbackPointer)
383+
}()
384+
385+
res := C.createMessageResponseHandle(flu.Engine, unsafe.Pointer(&callbackPointer), &responseHandle)
386+
if (Result)(res) != ResultSuccess {
387+
return 0, errors.New("failed to create a response handle")
388+
}
389+
return PlatformMessageResponseHandle(unsafe.Pointer(responseHandle)), nil
390+
}
391+
392+
// ReleasePlatformMessageResponseHandle collects a platform message response
393+
// handle.
394+
func (flu *FlutterEngine) ReleasePlatformMessageResponseHandle(responseHandle PlatformMessageResponseHandle) {
395+
cResponseHandle := (*C.FlutterPlatformMessageResponseHandle)(unsafe.Pointer(responseHandle))
396+
res := C.FlutterPlatformMessageReleaseResponseHandle(flu.Engine, cResponseHandle)
397+
if (Result)(res) != ResultSuccess {
398+
fmt.Printf("go-flutter: failed to collect platform response message handle")
399+
}
400+
}
401+
402+
// FlutterEngineGetCurrentTime gets the current time in nanoseconds from the clock used by the flutter
403+
// engine.
362404
func FlutterEngineGetCurrentTime() uint64 {
363405
return uint64(C.FlutterEngineGetCurrentTime())
364406
}

0 commit comments

Comments
 (0)