Skip to content

Commit 72a72a2

Browse files
committed
app: [macOS] don't discard IME session for consistent snippets
An IME session must be discarded when its text content no longer matches the underlying text component content. However, the check for matching was too pessimistic; the IME session would be discarded if the new snippet from the text component was not equal to the snippet reported to the IME. This change implements a refined check that only discards a session if the content of the overlap between the new and old snippets don't match. Fixes an IME issue reported by Zhang Zj. Signed-off-by: Elias Naur <[email protected]>
1 parent 14a9fbc commit 72a72a2

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

app/ime.go

+26
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package app
55
import (
66
"unicode"
77
"unicode/utf16"
8+
"unicode/utf8"
89

910
"gioui.org/io/input"
1011
"gioui.org/io/key"
@@ -117,3 +118,28 @@ func (e *editorState) RunesIndex(chars int) int {
117118
// Assume runes after snippets are one UTF-16 character each.
118119
return runes + chars
119120
}
121+
122+
// areSnippetsConsistent reports whether the content of the old snippet is
123+
// consistent with the content of the new.
124+
func areSnippetsConsistent(old, new key.Snippet) bool {
125+
// Compute the overlapping range.
126+
r := old.Range
127+
r.Start = max(r.Start, new.Start)
128+
r.End = max(r.End, r.Start)
129+
r.End = min(r.End, new.End)
130+
return snippetSubstring(old, r) == snippetSubstring(new, r)
131+
}
132+
133+
func snippetSubstring(s key.Snippet, r key.Range) string {
134+
for r.Start > s.Start && r.Start < s.End {
135+
_, n := utf8.DecodeRuneInString(s.Text)
136+
s.Text = s.Text[n:]
137+
s.Start++
138+
}
139+
for r.End < s.End && r.End > s.Start {
140+
_, n := utf8.DecodeLastRuneInString(s.Text)
141+
s.Text = s.Text[:len(s.Text)-n]
142+
s.End--
143+
}
144+
return s.Text
145+
}

app/os_macos.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ func (w *window) SetCursor(cursor pointer.Cursor) {
534534
}
535535

536536
func (w *window) EditorStateChanged(old, new editorState) {
537-
if old.Selection.Range != new.Selection.Range || old.Snippet != new.Snippet {
537+
if old.Selection.Range != new.Selection.Range || !areSnippetsConsistent(old.Snippet, new.Snippet) {
538538
C.discardMarkedText(w.view)
539539
w.w.SetComposingRegion(key.Range{Start: -1, End: -1})
540540
}

0 commit comments

Comments
 (0)