Skip to content

Commit

Permalink
Option to pass escape presses to the app instead of popping current r…
Browse files Browse the repository at this point in the history
…oute (#632)
  • Loading branch information
jslater89 authored Nov 29, 2021
1 parent 997345f commit 7c8601f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ func (a *Application) Run() error {
})

// Attach glfw window callbacks for text input
defaultTextinputPlugin.backOnEscape = a.config.backOnEscape
a.window.SetKeyCallback(
func(window *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
defaultTextinputPlugin.glfwKeyCallback(window, key, scancode, action, mods)
Expand Down
14 changes: 14 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type config struct {
windowAlwaysOnTop bool
windowTransparent bool

backOnEscape bool

forcePixelRatio float64
scrollAmount float64

Expand Down Expand Up @@ -64,6 +66,8 @@ func newApplicationConfig() config {
windowTransparent: false,
scrollAmount: 100.0,

backOnEscape: true,

// Sane configuration values for the engine.
flutterAssetsPath: filepath.Join(filepath.Dir(execPath), "flutter_assets"),
icuDataPath: filepath.Join(filepath.Dir(execPath), "icudtl.dat"),
Expand Down Expand Up @@ -184,6 +188,16 @@ func WindowDimensionLimits(minWidth, minHeight, maxWidth, maxHeight int) Option
}
}

// BackOnEscape controls the mapping of the escape key.
//
// If true, pops the current route when escape is pressed.
// If false, escape is delivered to the application.
func BackOnEscape(backOnEscape bool) Option {
return func(c *config) {
c.backOnEscape = backOnEscape
}
}

// WindowIcon sets an icon provider func, which is called during window
// initialization. For tips on the kind of images to provide, see
// https://godoc.org/github.com/go-gl/glfw/v3.3/glfw#Window.SetIcon
Expand Down
5 changes: 3 additions & 2 deletions text-input.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ type textinputPlugin struct {
clientConf argSetClientConf
ed argsEditingState

backOnEscape bool

virtualKeyboardShow func()
virtualKeyboardHide func()
}
Expand Down Expand Up @@ -157,8 +159,7 @@ func (p *textinputPlugin) glfwCharCallback(w *glfw.Window, char rune) {
}

func (p *textinputPlugin) glfwKeyCallback(window *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {

if key == glfw.KeyEscape && action == glfw.Press {
if p.backOnEscape && 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)
Expand Down

0 comments on commit 7c8601f

Please sign in to comment.