diff --git a/gutter.go b/gutter.go index f978dab0..f237c17d 100644 --- a/gutter.go +++ b/gutter.go @@ -21,6 +21,7 @@ func Run(options ...Option) (err error) { // The Windows Title Handler and the TextInput handler come by default options = append(options, addHandlerWindowTitle()) options = append(options, addHandlerTextInput()) + options = append(options, addHandlerClipboard()) c = c.merge(options...) diff --git a/system_plugins.go b/system_plugins.go index b88a3520..cef86d52 100644 --- a/system_plugins.go +++ b/system_plugins.go @@ -20,6 +20,9 @@ const ( platformChannel = "flutter/platform" // Args -> struct ArgsAppSwitcherDescription setDescriptionMethod = "SystemChrome.setApplicationSwitcherDescription" + + clipboardSetData = "Clipboard.setData" + clipboardGetData = "Clipboard.getData" ) // ArgsAppSwitcherDescription Args content @@ -50,6 +53,44 @@ func addHandlerWindowTitle() Option { } +func addHandlerClipboard() Option { + handler := func(platMessage flutter.PlatformMessage, + flutterEngine *flutter.EngineOpenGL, + window *glfw.Window) bool { + + message := platMessage.Message + switch message.Method { + case clipboardSetData: + newClipboard := struct { + Text string `json:"text"` + }{} + json.Unmarshal(message.Args, &newClipboard) + window.SetClipboardString(newClipboard.Text) + case clipboardGetData: + requestedMime := "" + json.Unmarshal(message.Args, &requestedMime) + if requestedMime == "text/plain" { + clipText, _ := window.GetClipboardString() + + retBytes, _ := json.Marshal([]struct { + Text string `json:"text"` + }{{clipText}}) + + flutterEngine.SendPlatformMessageResponse(platMessage, retBytes) + return true + } else { + // log.Printf("Don't know how to acquire type #v from the clipboard", requestedMime) + } + + default: + // log.Printf("unhandled platform method: %#v\n", platMessage.Message) + } + return false + + } + return OptionAddPluginReceiver(handler, platformChannel) +} + ///////////////// // TextInput // /////////////////