From 5fc9312f46a131d4a5052f0d7535492cba68e460 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Mon, 8 Aug 2022 15:32:20 +0200 Subject: [PATCH] widget: report SubmitEvents for IME newlines in submit mode Before this change, an IME text edit would always have its newlines replaced with spaces. However, for Editors where Submit is enabled we want newlines to result in SubmitEvents. Signed-off-by: Elias Naur --- widget/editor.go | 32 ++++++++++++++++++++++++++++---- widget/editor_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 4 deletions(-) diff --git a/widget/editor.go b/widget/editor.go index d130e86f..c7eb9306 100644 --- a/widget/editor.go +++ b/widget/editor.go @@ -375,9 +375,30 @@ func (e *Editor) processKey(gtx layout.Context) { case key.EditEvent: e.caret.scroll = true e.scroller.Stop() - moves := e.replace(ke.Range.Start, ke.Range.End, ke.Text, true) + s := ke.Text + moves := 0 + submit := false + switch { + case e.Submit: + if i := strings.IndexByte(s, '\n'); i != -1 { + submit = true + moves += len(s) - i + s = s[:i] + } + case e.SingleLine: + s = strings.ReplaceAll(s, "\n", " ") + } + moves += e.replace(ke.Range.Start, ke.Range.End, s, true) adjust += utf8.RuneCountInString(ke.Text) - moves e.caret.xoff = 0 + if submit { + if e.rr.Changed() { + e.events = append(e.events, ChangeEvent{}) + } + e.events = append(e.events, SubmitEvent{ + Text: e.Text(), + }) + } // Complete a paste event, initiated by Shortcut-V in Editor.command(). case clipboard.Event: e.caret.scroll = true @@ -874,6 +895,9 @@ func (e *Editor) SetText(s string) { e.rr = editBuffer{} e.caret.start = 0 e.caret.end = 0 + if e.SingleLine { + s = strings.ReplaceAll(s, "\n", " ") + } e.replace(e.caret.start, e.caret.end, s, true) e.caret.xoff = 0 } @@ -1171,6 +1195,9 @@ func (e *Editor) Insert(s string) { // there is a selection, append overwrites it. // xxx|yyy + append zzz => xxxzzz|yyy func (e *Editor) append(s string) { + if e.SingleLine { + s = strings.ReplaceAll(s, "\n", " ") + } moves := e.replace(e.caret.start, e.caret.end, s, true) e.caret.xoff = 0 start := e.caret.start @@ -1229,9 +1256,6 @@ func (e *Editor) redo() { // addHistory controls whether this modification is recorded in the undo // history. func (e *Editor) replace(start, end int, s string, addHistory bool) int { - if e.SingleLine { - s = strings.ReplaceAll(s, "\n", " ") - } if start > end { start, end = end, start } diff --git a/widget/editor_test.go b/widget/editor_test.go index 8926a690..4850536b 100644 --- a/widget/editor_test.go +++ b/widget/editor_test.go @@ -1067,6 +1067,35 @@ func TestEditor_Filter(t *testing.T) { } } +func TestEditor_Submit(t *testing.T) { + e := new(Editor) + e.Submit = true + + gtx := layout.Context{ + Ops: new(op.Ops), + Constraints: layout.Exact(image.Pt(100, 100)), + Queue: newQueue( + key.EditEvent{Range: key.Range{Start: 0, End: 0}, Text: "ab1\n"}, + ), + } + cache := text.NewCache(gofont.Collection()) + fontSize := unit.Sp(10) + font := text.Font{} + e.Layout(gtx, cache, font, fontSize, nil) + + if got, want := e.Text(), "ab1"; got != want { + t.Errorf("editor failed to filter newline") + } + got := e.Events() + want := []EditorEvent{ + ChangeEvent{}, + SubmitEvent{Text: e.Text()}, + } + if !reflect.DeepEqual(want, got) { + t.Errorf("editor failed to register submit") + } +} + func textWidth(e *Editor, lineNum, colStart, colEnd int) float32 { var w fixed.Int26_6 glyphs := e.lines[lineNum].Layout.Glyphs