diff options
Diffstat (limited to 'xA/xA.go')
-rw-r--r-- | xA/xA.go | 65 |
1 files changed, 62 insertions, 3 deletions
@@ -24,6 +24,7 @@ import ( "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/container" "fyne.io/fyne/v2/dialog" + "fyne.io/fyne/v2/driver/desktop" "fyne.io/fyne/v2/theme" "fyne.io/fyne/v2/widget" ) @@ -219,7 +220,7 @@ var ( wRichScroll *container.Scroll wPrompt *widget.Label wStatus *widget.Label - wEntry *widget.Entry + wEntry *customEntry ) // ----------------------------------------------------------------------------- @@ -974,6 +975,64 @@ func inputSubmit(text string) bool { // --- General UI -------------------------------------------------------------- +type customEntry struct { + widget.Entry + selectKeyDown bool +} + +func newCustomEntry() *customEntry { + e := &customEntry{} + e.MultiLine = true + e.Wrapping = fyne.TextWrap(fyne.TextTruncateClip) + e.ExtendBaseWidget(e) + return e +} + +func (e *customEntry) FocusLost() { + e.selectKeyDown = false + e.Entry.FocusLost() +} + +func (e *customEntry) KeyDown(key *fyne.KeyEvent) { + if key.Name == desktop.KeyShiftLeft || key.Name == desktop.KeyShiftRight { + e.selectKeyDown = true + } + e.Entry.KeyDown(key) +} + +func (e *customEntry) KeyUp(key *fyne.KeyEvent) { + if key.Name == desktop.KeyShiftLeft || key.Name == desktop.KeyShiftRight { + e.selectKeyDown = false + } + e.Entry.KeyUp(key) +} + +func (e *customEntry) TypedKey(key *fyne.KeyEvent) { + if e.Disabled() { + return + } + + // Invert the Shift key behaviour here. + // Notice that this will never work on mobile. + shift := &fyne.KeyEvent{Name: desktop.KeyShiftLeft} + switch key.Name { + case fyne.KeyReturn, fyne.KeyEnter: + if e.selectKeyDown { + e.Entry.KeyUp(shift) + e.Entry.TypedKey(key) + e.Entry.KeyDown(shift) + } else if e.OnSubmitted != nil { + e.OnSubmitted(e.Text) + } + case fyne.KeyTab: + // TODO(p) + default: + e.Entry.TypedKey(key) + } +} + +// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + func main() { flag.Usage = func() { fmt.Fprintf(flag.CommandLine.Output(), @@ -1042,12 +1101,12 @@ func main() { "", fyne.TextAlignLeading, fyne.TextStyle{Bold: true}) wStatus = widget.NewLabelWithStyle( "", fyne.TextAlignTrailing, fyne.TextStyle{}) - wEntry = widget.NewMultiLineEntry() + + wEntry = newCustomEntry() // TODO(p): Rather respond to all keypresses/similar activity. wEntry.OnChanged = func(text string) { relaySend(RelayCommandData{Variant: &RelayCommandDataActive{}}, nil) } - // TODO(p): Make this submit on Enter rather than Shift+Enter. wEntry.OnSubmitted = func(text string) { inputSubmit(text) } |