From 912de88b90207c516b115a90fd1b7353e36d7455 Mon Sep 17 00:00:00 2001 From: Jakub Sobon Date: Sun, 27 Dec 2020 02:19:38 -0500 Subject: [PATCH] Textinput can request keyboard exclusively. --- CHANGELOG.md | 5 +++++ widgets/textinput/options.go | 15 ++++++++++++--- widgets/textinput/textinput.go | 5 +++-- widgets/textinput/textinput_test.go | 13 +++++++++++++ 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index daacc77..aedbf0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - the `button` widget allows specifying separate fill color values for each of its main states (up, focused and up, down). +#### Updates to the `textinput` widget + +- the `textinput` widget can now be configured to request keyboard events + exclusively when focused. + ## [0.13.0] - 17-Nov-2020 ### Added diff --git a/widgets/textinput/options.go b/widgets/textinput/options.go index 915fe9a..f708c2a 100644 --- a/widgets/textinput/options.go +++ b/widgets/textinput/options.go @@ -59,9 +59,10 @@ type options struct { placeHolder string hideTextWith rune - filter FilterFn - onSubmit SubmitFn - clearOnSubmit bool + filter FilterFn + onSubmit SubmitFn + clearOnSubmit bool + exclusiveKeyboardOnFocus bool } // validate validates the provided options. @@ -263,3 +264,11 @@ func ClearOnSubmit() Option { opts.clearOnSubmit = true }) } + +// ExclusiveKeyboardOnFocus when set ensures that when this widget is focused, +// no other widget receives any keyboard events. +func ExclusiveKeyboardOnFocus() Option { + return option(func(opts *options) { + opts.exclusiveKeyboardOnFocus = true + }) +} diff --git a/widgets/textinput/textinput.go b/widgets/textinput/textinput.go index a13c860..bfb0399 100644 --- a/widgets/textinput/textinput.go +++ b/widgets/textinput/textinput.go @@ -326,8 +326,9 @@ func (ti *TextInput) Options() widgetapi.Options { maxWidth, needHeight, }, - WantKeyboard: widgetapi.KeyScopeFocused, - WantMouse: widgetapi.MouseScopeWidget, + WantKeyboard: widgetapi.KeyScopeFocused, + WantMouse: widgetapi.MouseScopeWidget, + ExclusiveKeyboardOnFocus: ti.opts.exclusiveKeyboardOnFocus, } } diff --git a/widgets/textinput/textinput_test.go b/widgets/textinput/textinput_test.go index 852ab7c..ec73f0e 100644 --- a/widgets/textinput/textinput_test.go +++ b/widgets/textinput/textinput_test.go @@ -1704,6 +1704,19 @@ func TestOptions(t *testing.T) { WantMouse: widgetapi.MouseScopeWidget, }, }, + { + desc: "requests ExclusiveKeyboardOnFocus", + opts: []Option{ + ExclusiveKeyboardOnFocus(), + }, + want: widgetapi.Options{ + MinimumSize: image.Point{4, 1}, + MaximumSize: image.Point{0, 1}, + WantKeyboard: widgetapi.KeyScopeFocused, + WantMouse: widgetapi.MouseScopeWidget, + ExclusiveKeyboardOnFocus: true, + }, + }, } for _, tc := range tests {