forked from go-flutter-desktop/go-flutter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtextinput.go
215 lines (178 loc) · 6.11 KB
/
textinput.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package flutter
import (
"encoding/json"
"fmt"
"runtime"
"unicode"
"github.com/go-flutter-desktop/go-flutter/plugin"
"github.com/go-gl/glfw/v3.3/glfw"
"github.com/pkg/errors"
)
const textinputChannelName = "flutter/textinput"
// textinputPlugin implements flutter.Plugin and handles method calls to the
// flutter/textinput channel.
type textinputPlugin struct {
messenger plugin.BinaryMessenger
window *glfw.Window
channel *plugin.MethodChannel
keyboardLayout KeyboardShortcuts
clientID float64
clientConf argSetClientConf
word []rune
selectionBase int
selectionExtent int
virtualKeyboardShow func()
virtualKeyboardHide func()
}
// keyboardShortcutsGLFW handle glfw.ModifierKey from glfwKeyCallback.
type keyboardShortcutsGLFW struct {
mod glfw.ModifierKey
}
// all hardcoded because theres not pluggable renderer system.
var defaultTextinputPlugin = &textinputPlugin{}
var _ Plugin = &textinputPlugin{} // compile-time type check
var _ PluginGLFW = &textinputPlugin{} // compile-time type check
func (p *textinputPlugin) InitPlugin(messenger plugin.BinaryMessenger) error {
p.messenger = messenger
return nil
}
func (p *textinputPlugin) InitPluginGLFW(window *glfw.Window) error {
p.window = window
p.channel = plugin.NewMethodChannel(p.messenger, textinputChannelName, plugin.JSONMethodCodec{})
p.channel.HandleFuncSync("TextInput.setClient", p.handleSetClient)
p.channel.HandleFuncSync("TextInput.clearClient", p.handleClearClient)
p.channel.HandleFuncSync("TextInput.setEditingState", p.handleSetEditingState)
p.channel.HandleFunc("TextInput.show", func(_ interface{}) (interface{}, error) {
if p.virtualKeyboardShow != nil {
p.virtualKeyboardShow()
}
return nil, nil
})
p.channel.HandleFunc("TextInput.hide", func(_ interface{}) (interface{}, error) {
if p.virtualKeyboardHide != nil {
p.virtualKeyboardHide()
}
return nil, nil
})
return nil
}
func (p *textinputPlugin) handleSetClient(arguments interface{}) (reply interface{}, err error) {
args := []json.RawMessage{}
err = json.Unmarshal(arguments.(json.RawMessage), &args)
if err != nil {
return nil, errors.Wrap(err, "failed to decode json arguments for handleSetClient")
}
err = json.Unmarshal(args[0], &p.clientID)
if err != nil {
return nil, errors.Wrap(err, "failed to decode clientID for handleSetClient")
}
err = json.Unmarshal(args[1], &p.clientConf)
if err != nil {
return nil, errors.Wrap(err, "failed to decode clientConf for handleSetClient")
}
return nil, nil
}
func (p *textinputPlugin) handleClearClient(arguments interface{}) (reply interface{}, err error) {
p.clientID = 0
return nil, nil
}
func (p *textinputPlugin) handleSetEditingState(arguments interface{}) (reply interface{}, err error) {
if p.clientID == 0 {
return nil, errors.New("cannot set editing state when no client is selected")
}
editingState := argsEditingState{}
err = json.Unmarshal(arguments.(json.RawMessage), &editingState)
if err != nil {
return nil, errors.Wrap(err, "failed to decode json arguments for handleSetEditingState")
}
if editingState.SelectionBase < 0 || editingState.SelectionExtent < 0 {
errorMsg := fmt.Sprintf("invalid text selection: selectionBase:%v, selectionExtent:%v. Refer to go-flutter-desktop/go-flutter#221\n",
editingState.SelectionBase, editingState.SelectionExtent)
p.word = []rune(editingState.Text)
wordLen := len(p.word)
p.selectionBase = wordLen
p.selectionExtent = wordLen
p.updateEditingState()
fmt.Printf("go-flutter: recover from wrong editingState: %s", errorMsg)
return nil, nil
}
p.word = []rune(editingState.Text)
p.selectionBase = editingState.SelectionBase
p.selectionExtent = editingState.SelectionExtent
return nil, nil
}
func (p *textinputPlugin) glfwCharCallback(w *glfw.Window, char rune) {
if p.clientID == 0 {
return
}
// Opinionated: If a flutter dev uses TextCapitalization.characters
// in a TextField, that means he wants only to receive
// uppercase characters.
// TODO(Drakirus): Handle language-specific case mappings such as Turkish.
if p.clientConf.TextCapitalization == "TextCapitalization.characters" {
char = unicode.ToUpper(char)
}
p.addChar([]rune{char})
}
func (p *textinputPlugin) glfwKeyCallback(window *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
keyboardShortcutBind := keyboardShortcutsGLFW{mod: mods}
if key == glfw.KeyEscape && action == glfw.Press {
err := defaultNavigationPlugin.channel.InvokeMethod("popRoute", nil)
if err != nil {
fmt.Printf("go-flutter: failed to pop route after escape key press: %v\n", err)
}
return
}
if action == glfw.Repeat || action == glfw.Press {
if p.clientID == 0 {
return
}
switch key {
case glfw.KeyEnter:
if keyboardShortcutBind.isModifier() {
// Indicates that they are done typing in the TextInput
p.performAction("TextInputAction.done")
return
} else if p.clientConf.InputType.Name == "TextInputType.multiline" {
p.addChar([]rune{'\n'})
}
p.performTextInputAction()
case glfw.KeyHome:
p.MoveCursorHome(keyboardShortcutBind)
case glfw.KeyEnd:
p.MoveCursorEnd(keyboardShortcutBind)
case glfw.KeyLeft:
p.MoveCursorLeft(keyboardShortcutBind)
case glfw.KeyRight:
p.MoveCursorRight(keyboardShortcutBind)
case glfw.KeyDelete:
p.Delete(keyboardShortcutBind)
case glfw.KeyBackspace:
p.Backspace(keyboardShortcutBind)
case p.keyboardLayout.SelectAll:
if keyboardShortcutBind.isModifier() {
p.selectAll()
}
case p.keyboardLayout.Copy:
if keyboardShortcutBind.isModifier() && p.isSelected() {
_, _, selectedContent := p.getSelectedText()
window.SetClipboardString(selectedContent)
}
case p.keyboardLayout.Cut:
if keyboardShortcutBind.isModifier() && p.isSelected() {
_, _, selectedContent := p.getSelectedText()
window.SetClipboardString(selectedContent)
p.removeSelectedText()
}
case p.keyboardLayout.Paste:
if runtime.GOOS != "darwin" {
break
}
if keyboardShortcutBind.isModifier() {
clpString := window.GetClipboardString()
p.addChar([]rune(clpString))
}
}
p.updateEditingState()
}
}