Skip to content

Commit 4c16bc2

Browse files
committed
Implement system UI Clipboard
1 parent 6e1b3f4 commit 4c16bc2

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

gutter.go

Lines changed: 1 addition & 0 deletions
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

system_plugins.go

Lines changed: 41 additions & 0 deletions
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
@@ -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
/////////////////

0 commit comments

Comments
 (0)