mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
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 <mail@eliasnaur.com>
This commit is contained in:
+28
-4
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user