forked from go-flutter-desktop/go-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
platform.go
124 lines (105 loc) · 4.1 KB
/
platform.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package flutter
import (
"encoding/json"
"github.com/go-gl/glfw/v3.3/glfw"
"github.com/pkg/errors"
"github.com/go-flutter-desktop/go-flutter/internal/tasker"
"github.com/go-flutter-desktop/go-flutter/plugin"
)
// platformPlugin implements flutter.Plugin and handles method calls to the
// flutter/platform channel.
type platformPlugin struct {
popBehavior popBehavior
messenger plugin.BinaryMessenger
glfwTasker *tasker.Tasker
window *glfw.Window
channel *plugin.MethodChannel
// flutterInitialized is used as callbacks to know when the flutter framework
// is running and ready to process upstream plugin calls.
// (It usually takes ~10 rendering frame).
// flutterInitialized is trigger when the plugin "flutter/platform" received
// a message from "SystemChrome.setApplicationSwitcherDescription".
flutterInitialized []func()
}
// hardcoded because there is no swappable renderer interface.
var defaultPlatformPlugin = &platformPlugin{
popBehavior: PopBehaviorNone,
}
var _ Plugin = &platformPlugin{} // compile-time type check
var _ PluginGLFW = &platformPlugin{} // compile-time type check
func (p *platformPlugin) InitPlugin(messenger plugin.BinaryMessenger) error {
p.messenger = messenger
p.glfwTasker = tasker.New()
return nil
}
func (p *platformPlugin) InitPluginGLFW(window *glfw.Window) (err error) {
p.window = window
p.channel = plugin.NewMethodChannel(p.messenger, "flutter/platform", plugin.JSONMethodCodec{})
p.channel.HandleFunc("Clipboard.setData", p.handleClipboardSetData)
p.channel.HandleFunc("Clipboard.getData", p.handleClipboardGetData)
p.channel.HandleFunc("SystemChrome.setApplicationSwitcherDescription", p.handleWindowSetTitle)
p.channel.HandleFunc("SystemNavigator.pop", p.handleSystemNavigatorPop)
// Ignored: Desktop's don't have system overlays
p.channel.HandleFuncSync("SystemChrome.setSystemUIOverlayStyle", func(_ interface{}) (interface{}, error) { return nil, nil })
// Ignored: Desktop's don't have haptic feedback
p.channel.HandleFuncSync("HapticFeedback.vibrate", func(_ interface{}) (interface{}, error) { return nil, nil })
// Ignored: Desktop's don't play sound on every clicks
p.channel.HandleFuncSync("SystemSound.play", func(_ interface{}) (interface{}, error) { return nil, nil })
return nil
}
func (p *platformPlugin) handleClipboardSetData(arguments interface{}) (reply interface{}, err error) {
newClipboard := struct {
Text string `json:"text"`
}{}
err = json.Unmarshal(arguments.(json.RawMessage), &newClipboard)
if err != nil {
return nil, errors.Wrap(err, "failed to decode json arguments for handleClipboardSetData")
}
p.glfwTasker.Do(func() {
p.window.SetClipboardString(newClipboard.Text)
})
return nil, nil
}
func (p *platformPlugin) handleClipboardGetData(arguments interface{}) (reply interface{}, err error) {
requestedMime := ""
err = json.Unmarshal(arguments.(json.RawMessage), &requestedMime)
if err != nil {
return nil, errors.Wrap(err, "failed to decode json arguments for handleClipboardGetData")
}
if requestedMime != "text/plain" {
return nil, errors.New("obtaining mime type " + requestedMime + " from clipboard is not yet supported in go-flutter")
}
var clipText string
p.glfwTasker.Do(func() {
clipText = p.window.GetClipboardString()
})
reply = struct {
Text string `json:"text"`
}{
Text: clipText,
}
return reply, nil
}
func (p *platformPlugin) handleWindowSetTitle(arguments interface{}) (reply interface{}, err error) {
appSwitcherDescription := struct {
Label string `json:"label"`
PrimaryColor int64 `json:"primaryColor"`
}{}
err = json.Unmarshal(arguments.(json.RawMessage), &appSwitcherDescription)
if err != nil {
return nil, errors.Wrap(err, "failed to decode arguments")
}
p.glfwTasker.Do(func() {
p.window.SetTitle(appSwitcherDescription.Label)
})
// triggers flutter framework initialized callbacks
for _, f := range p.flutterInitialized {
f()
}
return nil, nil
}
// addFrameworkReadyCallback adds a callback which if trigger when the flutter
// framework is ready.
func (p *platformPlugin) addFrameworkReadyCallback(f func()) {
p.flutterInitialized = append(p.flutterInitialized, f)
}