Make the Shift tap a persistent English-mode toggle

A Shift tap with nothing composed now switches English mode on until the
next tap, instead of forcing only the next word: space, Enter and other keys
no longer drop back to automatic detection. In English mode every key goes
straight to the app, so there is no composition or underline. A Shift tap
while typing still flips the buffer like Tab. Windows shows "英文" /
"中英自動" when toggling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-11 11:44:25 +08:00
co-authored by Claude Opus 5.5
parent fa0b78e5a4
commit 7e173a1da8
8 changed files with 72 additions and 160 deletions
+16 -45
View File
@@ -35,12 +35,12 @@ public final class CompositionEngine {
public let converter: Converter
public private(set) var keys: [Character] = []
/// Set by Tab / Shift tap: overrides the classifier for the whole buffer.
/// Set by Tab / a Shift tap while typing: overrides the classifier for the whole
/// buffer. It is a correction, learned on commit.
public private(set) var forcedMode: InputMode?
/// True when forcedMode corrects the classifier (learned on commit); false when the
/// user forced English before typing (Shift tap on an empty buffer), which is not a
/// correction and is not learned.
private var forcedIsCorrection = false
/// English mode, toggled by a Shift tap when nothing is being typed. Keys go straight
/// to the app (no composition, no underline) until Shift is tapped again.
public private(set) var isEnglishMode = false
/// User-picked phrases: Zhuyin segment start (key index) → syllable range → text.
private var fixed: [Int: [Range<Int>: String]] = [:]
public private(set) var candidates: [Candidate] = []
@@ -165,28 +165,25 @@ public final class CompositionEngine {
cursor = nil
keys.removeLast()
fixed = fixed.filter { $0.key < keys.count }
if keys.isEmpty {
// A Shift-tap "next word is English" stays armed; a correction is dropped.
if forcedIsCorrection { forcedMode = nil; forcedIsCorrection = false }
fixed = [:]
}
if keys.isEmpty { forcedMode = nil; fixed = [:] }
return .consumed
case .enter:
guard !keys.isEmpty else { endDirectEnglish(); return .passThrough }
guard !keys.isEmpty else { return .passThrough }
return KeyResult(handled: true, commit: commitCurrent())
case .escape:
guard !keys.isEmpty else { endDirectEnglish(); return .passThrough }
guard !keys.isEmpty else { return .passThrough }
reset()
return .consumed
case .tab:
guard !keys.isEmpty else { endDirectEnglish(); return .passThrough }
guard !keys.isEmpty else { return .passThrough }
flip()
return .consumed
case .shiftTap:
if keys.isEmpty {
// Next word is English, typed as-is (lowercase stays lowercase). Tap again to cancel.
forcedMode = forcedMode == .literal ? nil : .literal
forcedIsCorrection = false
// Toggle English mode; it stays on until the next Shift tap.
isEnglishMode.toggle()
previousMode = isEnglishMode ? .literal : nil
previousCommitTime = now()
} else {
flip()
}
@@ -211,37 +208,12 @@ public final class CompositionEngine {
/// Forces the whole buffer into the other mode (a correction that is learned).
private func flip() {
forcedMode = (mode == .zhuyin) ? .literal : .zhuyin
forcedIsCorrection = true
}
/// 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).
public var isEnglishArmed: Bool { keys.isEmpty && forcedMode == .literal && !forcedIsCorrection }
/// Leaves forced-English mode once the word is done.
private func endDirectEnglish() {
guard isEnglishArmed else { return }
forcedMode = nil
previousMode = .literal
previousCommitTime = now()
}
private static let englishWordPunctuation: Set<Character> = ["'", ".", ",", "-", "/", ";"]
private func handleCharacter(_ c: Character) -> KeyResult {
cursor = nil // typing always continues at the end
if isEnglishArmed {
// Forced English: insert directly. Space ends the word; other symbols end it too.
if c == " " {
endDirectEnglish()
return KeyResult(handled: true, commit: " ")
}
if c.isASCII && (c.isLetter || c.isNumber || CompositionEngine.englishWordPunctuation.contains(c)) {
return KeyResult(handled: true, commit: String(c))
}
endDirectEnglish()
return .passThrough
}
// English mode: every key goes to the app as typed.
if isEnglishMode && keys.isEmpty { return .passThrough }
if c == " " {
guard !keys.isEmpty else { return .passThrough }
let segs = segments(of: keys, finishing: true)
@@ -338,7 +310,7 @@ public final class CompositionEngine {
private func commitCurrent(_ given: [Segment]? = nil) -> String {
let segs = given ?? segments
let text = segs.map { render($0, final: true) }.joined()
if let forced = forcedMode, forcedIsCorrection {
if let forced = forcedMode {
classifier.learned.record(String(keys), as: forced)
}
previousMode = segs.last?.mode
@@ -356,7 +328,6 @@ public final class CompositionEngine {
keys = []
cursor = nil
forcedMode = nil
forcedIsCorrection = false
fixed = [:]
candidates = []
}