Skip to content

Commit e7ec141

Browse files
authored
fix zero width space preedit at the end spreads whole text in SwiftUI TextField (#276)
1 parent c91077d commit e7ec141

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

macosfrontend/macosfrontend.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,17 @@ public func commitAndSetPreeditSync(
7676
// replace selected text will let Esc bypass IM. When using Shift+click to select, if
7777
// interval is too little, IM switch happens, but dummyPreedit is false in that case.
7878
if preedit.isEmpty && dummyPreedit {
79-
setPreedit(client, zeroWidthSpace, 0)
79+
let length = client.length()
80+
let selectedRange = client.selectedRange()
81+
// For SwiftUI TextField, there is a bug that if caret is at the end of text, zero-width space preedit
82+
// spreads from the start to the end, making the whole text underlined. Fortunately, SwiftUI's length
83+
// and selectedRange are reliable, so we use a normal space in this case.
84+
if length > 0 && length - currentPreedit.count == selectedRange.location + selectedRange.length
85+
{
86+
setPreedit(client, " ", 0)
87+
} else {
88+
setPreedit(client, zeroWidthSpace, 0)
89+
}
8090
} else {
8191
setPreedit(client, preedit, caretPos)
8292
}

0 commit comments

Comments
 (0)