Show English mode in the input method icon

macOS: the input method now has two input modes in Info.plist, 中英自動 ("注")
and 英文 ("A"), each with its own menu-bar icon. A Shift tap with nothing typed
switches the mode (TISSelectInputSource), and choosing a mode in the menu bar
switches the engine (setValue:forTag:). --register enables both modes.
Windows: the taskbar IME icon switches between icon.ico and icon-en.ico.
Linux: the IBus mode property also switches its icon.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-13 16:56:08 +08:00
co-authored by Claude Opus 5.5
parent 0155ddd16e
commit 6443f771a0
19 changed files with 184 additions and 9 deletions
+16
View File
@@ -1,3 +1,4 @@
import Carbon
import Cocoa
import InputMethodKit
import AutoZhuyinCore
@@ -74,8 +75,11 @@ final class InputController: IMKInputController {
otherModifiers: !flags.intersection([.command, .control, .option, .function]).isEmpty,
time: event.timestamp)
if tapped {
let idle = engine.keys.isEmpty
_ = engine.handle(.shiftTap)
updateComposition(client)
// English mode on/off: switch input mode so the menu-bar icon shows it.
if idle { InputModes.select(english: engine.isEnglishMode) }
}
return false
}
@@ -152,6 +156,18 @@ final class InputController: IMKInputController {
}
}
/// The input mode changed (menu bar, keyboard shortcut, or our own Shift tap).
override func setValue(_ value: Any!, forTag tag: Int, client sender: Any!) {
if tag == kTextServiceInputModePropertyTag, let id = value as? String {
let english = InputModes.isEnglish(id)
if english != engine.isEnglishMode {
commitComposition(sender)
engine.setEnglishMode(english)
}
}
super.setValue(value, forTag: tag, client: sender)
}
override func commitComposition(_ sender: Any!) {
guard let client = sender as? IMKTextInput else { return }
commit(engine.commitAll(), to: client)
+32
View File
@@ -0,0 +1,32 @@
import Carbon
import Foundation
/// The two input modes declared in Info.plist (ComponentInputModeDict). Each has its own
/// menu-bar icon, so switching modes is how the icon shows "中英自動" vs "英文".
enum InputModes {
static let auto = "com.autozhuyin.inputmethod.AutoZhuyin.Auto"
static let english = "com.autozhuyin.inputmethod.AutoZhuyin.English"
static func isEnglish(_ modeID: String) -> Bool { modeID == english }
private static func source(_ id: String) -> TISInputSource? {
let filter = [kTISPropertyInputSourceID as String: id] as CFDictionary
guard let list = TISCreateInputSourceList(filter, true)?.takeRetainedValue() as? [TISInputSource] else { return nil }
return list.first
}
/// Makes the given mode current (enabling it first if needed). Returns false on failure.
@discardableResult
static func select(english: Bool) -> Bool {
guard let src = source(english ? InputModes.english : InputModes.auto) else { return false }
TISEnableInputSource(src)
return TISSelectInputSource(src) == noErr
}
/// Called by `--register`: enable both modes so they are in the input menu.
static func enableAll() {
for id in [auto, english] {
if let src = source(id) { TISEnableInputSource(src) }
}
}
}
+1
View File
@@ -6,6 +6,7 @@ import InputMethodKit
// in System Settings without logging out (used by the installer's postinstall script).
if CommandLine.arguments.contains("--register") {
let status = TISRegisterInputSource(Bundle.main.bundleURL as CFURL)
if status == noErr { InputModes.enableAll() } // "中英自動" and "英文" (menu-bar icons)
print(status == noErr ? "Registered \(Bundle.main.bundlePath)" : "TISRegisterInputSource failed: \(status)")
exit(status == noErr ? 0 : 1)
}
@@ -417,6 +417,16 @@ public final class CompositionEngine {
}
}
/// Turns English mode on or off directly (e.g. the input mode was switched in the menu
/// bar). Commit any composition first.
public func setEnglishMode(_ on: Bool) {
guard on != isEnglishMode else { return }
reset()
isEnglishMode = on
previousMode = on ? .literal : nil
previousCommitTime = now()
}
/// Forces the whole buffer into the other mode (a correction that is learned).
private func flip() {
forcedMode = (mode == .zhuyin) ? .literal : .zhuyin