Skip to content

Commit 7c8601f

Browse files
authored
Option to pass escape presses to the app instead of popping current route (#632)
1 parent 997345f commit 7c8601f

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

application.go

+1
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ func (a *Application) Run() error {
322322
})
323323

324324
// Attach glfw window callbacks for text input
325+
defaultTextinputPlugin.backOnEscape = a.config.backOnEscape
325326
a.window.SetKeyCallback(
326327
func(window *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
327328
defaultTextinputPlugin.glfwKeyCallback(window, key, scancode, action, mods)

option.go

+14
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ type config struct {
2323
windowAlwaysOnTop bool
2424
windowTransparent bool
2525

26+
backOnEscape bool
27+
2628
forcePixelRatio float64
2729
scrollAmount float64
2830

@@ -64,6 +66,8 @@ func newApplicationConfig() config {
6466
windowTransparent: false,
6567
scrollAmount: 100.0,
6668

69+
backOnEscape: true,
70+
6771
// Sane configuration values for the engine.
6872
flutterAssetsPath: filepath.Join(filepath.Dir(execPath), "flutter_assets"),
6973
icuDataPath: filepath.Join(filepath.Dir(execPath), "icudtl.dat"),
@@ -184,6 +188,16 @@ func WindowDimensionLimits(minWidth, minHeight, maxWidth, maxHeight int) Option
184188
}
185189
}
186190

191+
// BackOnEscape controls the mapping of the escape key.
192+
//
193+
// If true, pops the current route when escape is pressed.
194+
// If false, escape is delivered to the application.
195+
func BackOnEscape(backOnEscape bool) Option {
196+
return func(c *config) {
197+
c.backOnEscape = backOnEscape
198+
}
199+
}
200+
187201
// WindowIcon sets an icon provider func, which is called during window
188202
// initialization. For tips on the kind of images to provide, see
189203
// https://godoc.org/github.com/go-gl/glfw/v3.3/glfw#Window.SetIcon

text-input.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ type textinputPlugin struct {
2424
clientConf argSetClientConf
2525
ed argsEditingState
2626

27+
backOnEscape bool
28+
2729
virtualKeyboardShow func()
2830
virtualKeyboardHide func()
2931
}
@@ -157,8 +159,7 @@ func (p *textinputPlugin) glfwCharCallback(w *glfw.Window, char rune) {
157159
}
158160

159161
func (p *textinputPlugin) glfwKeyCallback(window *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
160-
161-
if key == glfw.KeyEscape && action == glfw.Press {
162+
if p.backOnEscape && key == glfw.KeyEscape && action == glfw.Press {
162163
err := defaultNavigationPlugin.channel.InvokeMethod("popRoute", nil)
163164
if err != nil {
164165
fmt.Printf("go-flutter: failed to pop route after escape key press: %v\n", err)

0 commit comments

Comments
 (0)