Skip to content

Commit f3f4361

Browse files
author
Thibault Brocherieux
committed
Support different layout
1 parent 1a6f127 commit f3f4361

File tree

3 files changed

+89
-53
lines changed

3 files changed

+89
-53
lines changed

example/simpleDemo/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import (
1313
"strings"
1414
"time"
1515

16-
gutter "github.com/Drakirus/go-flutter-desktop-embedder"
16+
gutter "../.."
17+
//gutter "github.com/Drakirus/go-flutter-desktop-embedder"
1718
"github.com/Drakirus/go-flutter-desktop-embedder/flutter"
1819

1920
"github.com/go-gl/glfw/v3.2/glfw"
@@ -39,6 +40,7 @@ func main() {
3940
gutter.OptionPixelRatio(1.2),
4041
gutter.OptionVMArguments([]string{"--dart-non-checked-mode", "--observatory-port=50300"}),
4142
gutter.OptionAddPluginReceiver(ownPlugin, "plugin_demo"),
43+
gutter.OptionKeyboardLayout("FR"),
4244
}
4345

4446
if err = gutter.Run(options...); err != nil {

gutter.go

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -93,73 +93,74 @@ func glfwMouseButtonCallback(window *glfw.Window, key glfw.MouseButton, action g
9393

9494
var state = textModel{}
9595

96-
func glfwKeyCallback(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
96+
func glfwKey(keyboardLayout keyboardShortcuts) func(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
9797

98-
if key == glfw.KeyEscape && action == glfw.Press {
99-
w.SetShouldClose(true)
100-
}
98+
return func(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
99+
if key == glfw.KeyEscape && action == glfw.Press {
100+
w.SetShouldClose(true)
101+
}
101102

102-
if action == glfw.Repeat || action == glfw.Press {
103-
if state.clientID != 0 {
103+
if action == glfw.Repeat || action == glfw.Press {
104+
if state.clientID != 0 {
104105

105-
switch key {
106-
case glfw.KeyEnter:
107-
if mods == glfw.ModControl {
108-
performAction(w, "done")
109-
} else {
110-
state.addChar([]rune{'\n'})
111-
performAction(w, "newline")
112-
}
106+
switch key {
107+
case glfw.KeyEnter:
108+
if mods == glfw.ModControl {
109+
performAction(w, "done")
110+
} else {
111+
state.addChar([]rune{'\n'})
112+
performAction(w, "newline")
113+
}
113114

114-
case glfw.KeyHome:
115-
state.MoveCursorHome(int(mods))
115+
case glfw.KeyHome:
116+
state.MoveCursorHome(int(mods))
116117

117-
case glfw.KeyEnd:
118-
state.MoveCursorEnd(int(mods))
118+
case glfw.KeyEnd:
119+
state.MoveCursorEnd(int(mods))
119120

120-
case glfw.KeyLeft:
121-
state.MoveCursorLeft(int(mods))
121+
case glfw.KeyLeft:
122+
state.MoveCursorLeft(int(mods))
122123

123-
case glfw.KeyRight:
124-
state.MoveCursorRight(int(mods))
124+
case glfw.KeyRight:
125+
state.MoveCursorRight(int(mods))
125126

126-
case glfw.KeyDelete:
127-
state.Delete(int(mods))
127+
case glfw.KeyDelete:
128+
state.Delete(int(mods))
128129

129-
case glfw.KeyBackspace:
130-
state.Backspace(int(mods))
130+
case glfw.KeyBackspace:
131+
state.Backspace(int(mods))
131132

132-
case glfw.KeyA:
133-
if mods == glfw.ModControl {
134-
state.SelectAll()
135-
}
133+
case keyboardLayout.selectAll:
134+
if mods == glfw.ModControl {
135+
state.SelectAll()
136+
}
136137

137-
case glfw.KeyC:
138-
if mods == glfw.ModControl && state.isSelected() {
139-
_, _, selectedContent := state.GetSelectedText()
140-
w.SetClipboardString(selectedContent)
141-
}
138+
case keyboardLayout.copy:
139+
if mods == glfw.ModControl && state.isSelected() {
140+
_, _, selectedContent := state.GetSelectedText()
141+
w.SetClipboardString(selectedContent)
142+
}
142143

143-
case glfw.KeyX:
144-
if mods == glfw.ModControl && state.isSelected() {
145-
_, _, selectedContent := state.GetSelectedText()
146-
w.SetClipboardString(selectedContent)
147-
state.RemoveSelectedText()
148-
}
144+
case keyboardLayout.cut:
145+
if mods == glfw.ModControl && state.isSelected() {
146+
_, _, selectedContent := state.GetSelectedText()
147+
w.SetClipboardString(selectedContent)
148+
state.RemoveSelectedText()
149+
}
149150

150-
case glfw.KeyV:
151-
if mods == glfw.ModControl {
152-
var clpString, err = w.GetClipboardString()
153-
if err != nil {
154-
log.Printf("unable to get the clipboard content: %v\n", err)
155-
} else {
156-
state.addChar([]rune(clpString))
151+
case keyboardLayout.paste:
152+
if mods == glfw.ModControl {
153+
var clpString, err = w.GetClipboardString()
154+
if err != nil {
155+
log.Printf("unable to get the clipboard content: %v\n", err)
156+
} else {
157+
state.addChar([]rune(clpString))
158+
}
157159
}
158160
}
159161
}
160162
}
161163
}
162-
163164
}
164165

165166
func glfwWindowSizeCallback(window *glfw.Window, width int, height int) {
@@ -243,6 +244,8 @@ func runFlutter(window *glfw.Window, c config) *flutter.EngineOpenGL {
243244
width, height := window.GetFramebufferSize()
244245
glfwWindowSizeCallback(window, width, height)
245246

247+
glfwKeyCallback := glfwKey(c.KeyboardLayout)
248+
246249
window.SetKeyCallback(glfwKeyCallback)
247250
window.SetFramebufferSizeCallback(glfwWindowSizeCallback)
248251
window.SetMouseButtonCallback(glfwMouseButtonCallback)
@@ -286,11 +289,11 @@ func performAction(window *glfw.Window, action string) {
286289
"TextInputAction." + action,
287290
})
288291
message := flutter.Message{
289-
Args: actionArgs,
290-
Method:"TextInputClient.performAction",
292+
Args: actionArgs,
293+
Method: "TextInputClient.performAction",
291294
}
292295
var mess = &flutter.PlatformMessage{
293-
Channel:textInputChannel,
296+
Channel: textInputChannel,
294297
Message: message,
295298
}
296299
flutterOGL := flutter.SelectEngine(0)

option.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,29 @@ func OptionAddPluginReceiver(handler PluginReceivers, channelName string) Option
8686
}
8787
}
8888

89+
// OptionKeyboardLayout allow application to support keyboard that have a different layout
90+
// when the FlutterEngine send a PlatformMessage to the Embedder
91+
func OptionKeyboardLayout(layout string) Option {
92+
return func(c *config) {
93+
switch layout {
94+
case "US":
95+
c.KeyboardLayout = keyboardShortcuts{
96+
cut: glfw.KeyX,
97+
copy: glfw.KeyC,
98+
paste: glfw.KeyV,
99+
selectAll: glfw.KeyA,
100+
}
101+
case "FR":
102+
c.KeyboardLayout = keyboardShortcuts{
103+
cut: glfw.KeyX,
104+
copy: glfw.KeyC,
105+
paste: glfw.KeyV,
106+
selectAll: glfw.KeyQ,
107+
}
108+
}
109+
}
110+
}
111+
89112
type config struct {
90113
WindowDimension struct {
91114
x int
@@ -97,6 +120,14 @@ type config struct {
97120
PixelRatio float64
98121
VMArguments []string
99122
PlatformMessageReceivers map[string][]PluginReceivers // The Key is the Channel name.
123+
KeyboardLayout keyboardShortcuts
124+
}
125+
126+
type keyboardShortcuts struct {
127+
cut glfw.Key
128+
copy glfw.Key
129+
paste glfw.Key
130+
selectAll glfw.Key
100131
}
101132

102133
func (t config) merge(options ...Option) config {

0 commit comments

Comments
 (0)