Skip to content

Commit f13998e

Browse files
pas2kPierre Champion
authored and
Pierre Champion
committed
Avoid unnecessary PlatformMessage copies in plugin callback loop (#25)
* Implement system UI Clipboard * Avoid unnecessary PlatformMessage copies in plugin callback loop
1 parent 6e1b3f4 commit f13998e

File tree

6 files changed

+59
-18
lines changed

6 files changed

+59
-18
lines changed

example/simpleDemo/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func setIcon(window *glfw.Window) error {
5757

5858
// Plugin that read the stdin and send the number to the dart side
5959
func ownPlugin(
60-
platMessage flutter.PlatformMessage,
60+
platMessage *flutter.PlatformMessage,
6161
flutterEngine *flutter.EngineOpenGL,
6262
window *glfw.Window,
6363
) bool {

flutter/flutter.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type EngineOpenGL struct {
4848
FMakeResourceCurrent func(v unsafe.Pointer) bool
4949

5050
// platform message callback.
51-
FPlatfromMessage func(message PlatformMessage, window unsafe.Pointer) bool
51+
FPlatfromMessage func(message *PlatformMessage, window unsafe.Pointer) bool
5252

5353
// Engine arguments
5454
PixelRatio float64
@@ -163,9 +163,9 @@ type Message struct {
163163
}
164164

165165
// SendPlatformMessage is used to send a PlatformMessage to the Flutter engine.
166-
func (flu *EngineOpenGL) SendPlatformMessage(Message PlatformMessage) Result {
166+
func (flu *EngineOpenGL) SendPlatformMessage(Message *PlatformMessage) Result {
167167

168-
marshalled, err := json.Marshal(Message.Message)
168+
marshalled, err := json.Marshal(&Message.Message)
169169
if err != nil {
170170
panic("Cound not send a message to the flutter engine: Error while creating the JSON")
171171
}
@@ -189,7 +189,7 @@ func (flu *EngineOpenGL) SendPlatformMessage(Message PlatformMessage) Result {
189189

190190
// SendPlatformMessageResponse is used to send a message to the Flutter side using the correct ResponseHandle!
191191
func (flu *EngineOpenGL) SendPlatformMessageResponse(
192-
responseTo PlatformMessage,
192+
responseTo *PlatformMessage,
193193
data []byte,
194194
) Result {
195195

flutter/flutter_proxy_glfw.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ func proxy_on_platform_message(message *C.FlutterPlatformMessage, userPointer un
2222
if message.message != nil {
2323
str := C.GoStringN(C.c_str(message.message), C.int(message.message_size))
2424

25-
FlutterPlatformMessage := PlatformMessage{}
26-
2725
messageContent := Message{}
2826
json.Unmarshal([]byte(str), &messageContent)
2927

30-
FlutterPlatformMessage.Message = messageContent
31-
FlutterPlatformMessage.Channel = C.GoString(message.channel)
32-
FlutterPlatformMessage.ResponseHandle = message.response_handle
33-
28+
FlutterPlatformMessage := &PlatformMessage{
29+
Message: messageContent,
30+
Channel: C.GoString(message.channel),
31+
ResponseHandle: message.response_handle,
32+
}
3433
return C.bool(flutterEngines[0].FPlatfromMessage(FlutterPlatformMessage, userPointer))
3534
}
3635
return C.bool(false)

gutter.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func Run(options ...Option) (err error) {
2121
// The Windows Title Handler and the TextInput handler come by default
2222
options = append(options, addHandlerWindowTitle())
2323
options = append(options, addHandlerTextInput())
24+
options = append(options, addHandlerClipboard())
2425

2526
c = c.merge(options...)
2627

@@ -203,7 +204,7 @@ func runFlutter(window *glfw.Window, c config) *flutter.EngineOpenGL {
203204
}
204205

205206
// PlatformMessage
206-
flutterOGL.FPlatfromMessage = func(platMessage flutter.PlatformMessage, window unsafe.Pointer) bool {
207+
flutterOGL.FPlatfromMessage = func(platMessage *flutter.PlatformMessage, window unsafe.Pointer) bool {
207208
windows := glfw.GoWindow(window)
208209

209210
hasDispatched := false
@@ -262,7 +263,7 @@ func updateEditingState(window *glfw.Window) {
262263
Method: textUpdateStateMethod,
263264
}
264265

265-
var mess = flutter.PlatformMessage{
266+
var mess = &flutter.PlatformMessage{
266267
Channel: textInputChannel,
267268
Message: message,
268269
}

plugin_receiver.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
// PluginReceivers do stuff when receiving Message from the Engine,
99
// send result with `flutterEngine.SendPlatformMessageResponse`
1010
type PluginReceivers func(
11-
message flutter.PlatformMessage,
11+
message *flutter.PlatformMessage,
1212
flutterEngine *flutter.EngineOpenGL,
1313
window *glfw.Window,
1414
) bool

system_plugins.go

+45-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ const (
2020
platformChannel = "flutter/platform"
2121
// Args -> struct ArgsAppSwitcherDescription
2222
setDescriptionMethod = "SystemChrome.setApplicationSwitcherDescription"
23+
24+
clipboardSetData = "Clipboard.setData"
25+
clipboardGetData = "Clipboard.getData"
2326
)
2427

2528
// ArgsAppSwitcherDescription Args content
@@ -31,11 +34,11 @@ type ArgsAppSwitcherDescription struct {
3134
func addHandlerWindowTitle() Option {
3235

3336
var handler PluginReceivers = func(
34-
platMessage flutter.PlatformMessage,
37+
platMessage *flutter.PlatformMessage,
3538
flutterEngine *flutter.EngineOpenGL,
3639
window *glfw.Window,
3740
) bool {
38-
message := platMessage.Message
41+
message := &platMessage.Message
3942

4043
if message.Method == setDescriptionMethod {
4144
msgBody := ArgsAppSwitcherDescription{}
@@ -50,6 +53,44 @@ func addHandlerWindowTitle() Option {
5053

5154
}
5255

56+
func addHandlerClipboard() Option {
57+
handler := func(platMessage *flutter.PlatformMessage,
58+
flutterEngine *flutter.EngineOpenGL,
59+
window *glfw.Window) bool {
60+
61+
message := &platMessage.Message
62+
switch message.Method {
63+
case clipboardSetData:
64+
newClipboard := struct {
65+
Text string `json:"text"`
66+
}{}
67+
json.Unmarshal(message.Args, &newClipboard)
68+
window.SetClipboardString(newClipboard.Text)
69+
case clipboardGetData:
70+
requestedMime := ""
71+
json.Unmarshal(message.Args, &requestedMime)
72+
if requestedMime == "text/plain" {
73+
clipText, _ := window.GetClipboardString()
74+
75+
retBytes, _ := json.Marshal([]struct {
76+
Text string `json:"text"`
77+
}{{clipText}})
78+
79+
flutterEngine.SendPlatformMessageResponse(platMessage, retBytes)
80+
return true
81+
} else {
82+
// log.Printf("Don't know how to acquire type #v from the clipboard", requestedMime)
83+
}
84+
85+
default:
86+
// log.Printf("unhandled platform method: %#v\n", platMessage.Message)
87+
}
88+
return false
89+
90+
}
91+
return OptionAddPluginReceiver(handler, platformChannel)
92+
}
93+
5394
/////////////////
5495
// TextInput //
5596
/////////////////
@@ -83,12 +124,12 @@ type argsEditingState struct {
83124
func addHandlerTextInput() Option {
84125

85126
var handler PluginReceivers = func(
86-
platMessage flutter.PlatformMessage,
127+
platMessage *flutter.PlatformMessage,
87128
flutterEngine *flutter.EngineOpenGL,
88129
window *glfw.Window,
89130
) bool {
90131

91-
message := platMessage.Message
132+
message := &platMessage.Message
92133

93134
switch message.Method {
94135
case textInputClientClear:

0 commit comments

Comments
 (0)