Type forced English directly, without an underlined composition

After a Shift tap, letters, digits and word punctuation go straight to the
app instead of into marked text, so nothing is underlined. Space, Enter, Tab,
Escape or another symbol ends the forced word and automatic detection
resumes; backspace is handled by the app.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-11 11:35:59 +08:00
co-authored by Claude Opus 5.5
parent 82f20a6551
commit e928d08fdc
6 changed files with 101 additions and 12 deletions
+25 -4
View File
@@ -152,8 +152,15 @@ class CompositionEngine:
@property
def english_armed(self):
"""True while the user has forced English before typing anything (Shift tap)."""
return not self.keys and self.forced_mode == LITERAL
"""True while the user has forced English for the next word (Shift tap). In this
mode keys go straight to the app with no composition (no underline)."""
return not self.keys and self.forced_mode == LITERAL and not self._forced_is_correction
def _end_direct_english(self):
if self.english_armed:
self.forced_mode = None
self._previous_mode = LITERAL
self._previous_time = self.now()
def _flip(self):
self.forced_mode = LITERAL if self.mode == ZHUYIN else ZHUYIN
@@ -161,7 +168,7 @@ class CompositionEngine:
def wants(self, key):
"""Whether handle(key) would consume the key, without changing state."""
if self.candidates or key == SHIFT_TAP:
if self.candidates or key == SHIFT_TAP or self.english_armed:
return True
if key in SPECIAL_KEYS:
return bool(self.keys)
@@ -190,14 +197,19 @@ class CompositionEngine:
self.fixed = {}
return CONSUMED
if key == ENTER:
return (True, self._commit()) if self.keys else PASS
if not self.keys:
self._end_direct_english()
return PASS
return True, self._commit()
if key == ESCAPE:
if not self.keys:
self._end_direct_english()
return PASS
self.reset()
return CONSUMED
if key == TAB:
if not self.keys:
self._end_direct_english()
return PASS
self._flip()
return CONSUMED
@@ -231,6 +243,15 @@ class CompositionEngine:
def _handle_character(self, c):
self.cursor = None # typing always continues at the end
if self.english_armed:
# Forced English: insert directly. Space ends the word; other symbols end it too.
if c == " ":
self._end_direct_english()
return True, " "
if c.isascii() and (c.isalnum() or c in "'.,-/;"):
return True, c
self._end_direct_english()
return PASS
if c == " ":
if not self.keys:
return PASS
+2 -1
View File
@@ -29,7 +29,8 @@
← → 在組字區移動游標,選要改哪個字
↓ 開啟游標前那個字或詞的候選字,按數字鍵 1–9 選字
Esc 取消組字
單按 Shift 下一個字強制英文(保持小寫),送出後恢復自動判斷;
單按 Shift 下一個字強制英文(保持小寫、直接送出、沒有底線),
按空白或 Enter 後恢復自動判斷;
打到一半時按,同 Tab 整段翻轉。會出現「英文」小提示
Shift+字母 打大寫字母(也會強制英文)
< > ? ! : 中文語境下自動變成全形 ,。?!:
+20
View File
@@ -267,5 +267,25 @@ class CursorTests(unittest.TestCase):
self.assertTrue(e.showing_candidates)
class DirectEnglishTests(unittest.TestCase):
def test_forced_english_has_no_composition(self):
e = make_engine()
e.handle(SHIFT_TAP)
self.assertEqual(e.handle("r"), (True, "r"))
self.assertEqual(e.marked_text, "")
self.assertEqual(e.handle("'"), (True, "'"))
self.assertEqual(e.handle(BACKSPACE), (False, None))
self.assertTrue(e.english_armed)
self.assertEqual(e.handle(" "), (True, " "))
self.assertFalse(e.english_armed)
def test_enter_ends_forced_english(self):
e = make_engine()
e.handle(SHIFT_TAP)
e.handle("a")
self.assertEqual(e.handle(ENTER), (False, None))
self.assertFalse(e.english_armed)
if __name__ == "__main__":
unittest.main()