forked from go-flutter-desktop/go-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
system_plugins.go
154 lines (127 loc) · 4.02 KB
/
system_plugins.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package flutter
import (
"encoding/json"
"github.com/go-flutter-desktop/go-flutter/embedder"
"github.com/go-gl/glfw/v3.2/glfw"
)
// Talks to the dart side
// https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/services/system_channels.dart
////////////////////
// Window Title //
////////////////////
// const for `addHandlerWindowTitle`
const (
// Channel
platformChannel = "flutter/platform"
// Args -> struct ArgsAppSwitcherDescription
setDescriptionMethod = "SystemChrome.setApplicationSwitcherDescription"
clipboardSetData = "Clipboard.setData"
clipboardGetData = "Clipboard.getData"
)
// ArgsAppSwitcherDescription Args content
type ArgsAppSwitcherDescription struct {
Label string `json:"label"`
PrimaryColor int64 `json:"primaryColor"`
}
func addHandlerWindowTitle() Option {
var handler PluginReceivers = func(
platMessage *embedder.PlatformMessage,
flutterEngine *embedder.FlutterEngine,
window *glfw.Window,
) bool {
message := &platMessage.Message
if message.Method == setDescriptionMethod {
msgBody := ArgsAppSwitcherDescription{}
json.Unmarshal(message.Args, &msgBody)
window.SetTitle(msgBody.Label)
return true
}
return false
}
return OptionAddPluginReceiver(handler, platformChannel)
}
func addHandlerClipboard() Option {
handler := func(platMessage *embedder.PlatformMessage,
flutterEngine *embedder.FlutterEngine,
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 //
/////////////////
// const for `addHandlerTextInput`
const (
// channel
textInputChannel = "flutter/textinput"
// Args -> struct argsEditingState
textUpdateStateMethod = "TextInputClient.updateEditingState"
// text
textInputClientSet = "TextInput.setClient"
textInputClientClear = "TextInput.clearClient"
textInputSetEditState = "TextInput.setEditingState"
)
// argsEditingState Args content
// To update the embedder text use `flutter.SendPlatformMessage` whenever a keys is pressed
type argsEditingState struct {
Text string `json:"text"`
SelectionBase int `json:"selectionBase"`
SelectionExtent int `json:"selectionExtent"`
SelectionAffinity string `json:"selectionAffinity"`
SelectionIsDirectional bool `json:"selectionIsDirectional"`
ComposingBase int `json:"composingBase"`
ComposingExtent int `json:"composingExtent"`
}
func addHandlerTextInput() Option {
var handler PluginReceivers = func(
platMessage *embedder.PlatformMessage,
flutterEngine *embedder.FlutterEngine,
window *glfw.Window,
) bool {
message := &platMessage.Message
switch message.Method {
case textInputClientClear:
state.clientID = 0
case textInputClientSet:
var body []interface{}
json.Unmarshal(message.Args, &body)
state.clientID = body[0].(float64)
case textInputSetEditState:
if state.clientID != 0 {
editingState := argsEditingState{}
json.Unmarshal(message.Args, &editingState)
state.word = []rune(editingState.Text)
state.selectionBase = editingState.SelectionBase
state.selectionExtent = editingState.SelectionExtent
}
default:
}
return true
}
return OptionAddPluginReceiver(handler, textInputChannel)
}