diff --git a/README.md b/README.md index 61fdbe3..008ff16 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ - **中英自動判斷**:每按一個鍵就重新判斷,組字區即時顯示結果(Mac 版英文以藍字顯示) - **中英混打不用空白**:`ji3hello` → 我hello、`hellosu3cl3` → hello你好,同一段內會自動切分 - **判斷錯了按 Tab**:在中/英之間翻轉,輸入法會記住這組按鍵,下次自動判斷對 -- **單按 Shift 切換英文模式**:英文模式下按鍵直接送出、保持小寫、不顯示底線,再單按一次 Shift 回到中英自動判斷;英文模式時輸入法圖示會從「注」變成「A」 +- **單按 Shift 切換英文模式**:英文模式下按鍵直接送出、保持小寫、不顯示底線,再單按一次 Shift 回到中英自動判斷;切換時游標旁會短暫顯示「A」或「中」(Windows、Linux 的輸入法圖示也會變成「A」) - **注音順序打錯也能修正**:`ㄧㄒㄠˇ`、`ㄒㄠㄧˇ` 都會變成 `ㄒㄧㄠˇ`(小) - **可省略聲調**:`jiap7`(ㄨㄛ ㄇㄣ˙)一樣能打出「我們」 - **候選字**:用 ← → 把游標移到要改的字後面,按 ↓ 選字;選過的會固定下來 @@ -34,7 +34,7 @@ 2. 系統設定 → 鍵盤 → 文字輸入 → 編輯… → + → 繁體中文 → **自動注音**。 3. 清單裡沒出現的話,登出再登入一次。 -從 0.12 以前的版本更新:輸入法現在分成「中英自動」和「英文」兩個模式(各有圖示),安裝時會自動加入。如果輸入法清單裡還留著舊的「自動注音」項目,把它移除即可。 +從 0.13.x 更新:0.13 把輸入法拆成「中英自動」和「英文」兩個項目,0.14 起改回單一個「自動注音」。更新後如果輸入法清單裡沒有「自動注音」,到「系統設定 → 鍵盤 → 輸入方式」重新加入即可。 PKG 會裝到 `/Library/Input Methods/`,系統上所有使用者都能用。 @@ -69,7 +69,7 @@ SmartScreen 出現警告時,按「其他資訊 → 仍要執行」。解除安 | ← → | 在組字區移動游標(最左停在第一個中文字後面)。游標在中間時:直接打注音會**插入**新字,Backspace 刪掉游標前的字,↓ 選游標前的字 | | ↓ | 開啟游標前那個字或詞的候選字(游標預設在最後),數字鍵 1–9 選字,Esc 關閉 | | Esc | 取消組字 | -| **單按 Shift** | 沒在打字時:切換**英文模式**,按鍵直接送出、保持小寫、沒有底線,一直維持到再單按一次 Shift 才回到中英自動判斷,圖示會變成「A」(Mac 的選單列、Windows 的工作列、Linux 的 IBus 狀態)。Mac 也可以直接在選單列選「英文」模式。打到一半時:同 Tab,整段翻轉 | +| **單按 Shift** | 沒在打字時:切換**英文模式**,按鍵直接送出、保持小寫、沒有底線,一直維持到再單按一次 Shift 才回到中英自動判斷,Mac 會在游標旁短暫顯示「A」/「中」,Windows 的工作列和 Linux 的 IBus 狀態圖示會變成「A」;Mac 也可以從輸入法選單的「英文模式」切換。打到一半時:同 Tab,整段翻轉 | | Shift + 字母 | 打大寫字母(也會強制英文) | | `< > ? ! : ( ) [ ]` | 中文語境下自動轉成全形 `,。?!:()「」` | | `, . / ; -` | 英文單字後是標點;在注音中是 ㄝ ㄡ ㄥ ㄤ ㄦ(例如 `2;3` = 檔) | diff --git a/Resources/Base.lproj/InfoPlist.strings b/Resources/Base.lproj/InfoPlist.strings index 0b5bbd4..e46a5bd 100644 --- a/Resources/Base.lproj/InfoPlist.strings +++ b/Resources/Base.lproj/InfoPlist.strings @@ -2,5 +2,3 @@ CFBundleName = "自動注音"; CFBundleDisplayName = "自動注音"; "com.autozhuyin.inputmethod.AutoZhuyin" = "自動注音"; -"com.autozhuyin.inputmethod.AutoZhuyin.Auto" = "自動注音(中英自動)"; -"com.autozhuyin.inputmethod.AutoZhuyin.English" = "自動注音(英文)"; diff --git a/Resources/Info.plist b/Resources/Info.plist index 6c53b1d..651838f 100644 --- a/Resources/Info.plist +++ b/Resources/Info.plist @@ -18,67 +18,6 @@ 0.13.2 CFBundleVersion 23 - ComponentInputModeDict - - tsInputModeListKey - - com.autozhuyin.inputmethod.AutoZhuyin.Auto - - TISInputSourceID - com.autozhuyin.inputmethod.AutoZhuyin.Auto - TISIntendedLanguage - zh-Hant - tsInputModeAlternateMenuIconFileKey - icon.tiff - tsInputModeCharacterRepertoireKey - - Hant - - tsInputModeDefaultStateKey - - tsInputModeIsVisibleKey - - tsInputModeMenuIconFileKey - icon.tiff - tsInputModePaletteIconFileKey - icon.tiff - tsInputModePrimaryInScriptKey - - tsInputModeScriptKey - smTradChinese - - com.autozhuyin.inputmethod.AutoZhuyin.English - - TISInputSourceID - com.autozhuyin.inputmethod.AutoZhuyin.English - TISIntendedLanguage - zh-Hant - tsInputModeAlternateMenuIconFileKey - icon-en.tiff - tsInputModeCharacterRepertoireKey - - Hant - - tsInputModeDefaultStateKey - - tsInputModeIsVisibleKey - - tsInputModeMenuIconFileKey - icon-en.tiff - tsInputModePaletteIconFileKey - icon-en.tiff - tsInputModePrimaryInScriptKey - - tsInputModeScriptKey - smTradChinese - - - tsVisibleInputModeOrderedArrayKey - - com.autozhuyin.inputmethod.AutoZhuyin.Auto - com.autozhuyin.inputmethod.AutoZhuyin.English - - InputMethodConnectionName com.autozhuyin.inputmethod.AutoZhuyin_Connection InputMethodServerControllerClass diff --git a/Resources/en.lproj/InfoPlist.strings b/Resources/en.lproj/InfoPlist.strings index 6d26fc8..60997c6 100644 --- a/Resources/en.lproj/InfoPlist.strings +++ b/Resources/en.lproj/InfoPlist.strings @@ -2,5 +2,3 @@ CFBundleName = "AutoZhuyin"; CFBundleDisplayName = "AutoZhuyin"; "com.autozhuyin.inputmethod.AutoZhuyin" = "AutoZhuyin"; -"com.autozhuyin.inputmethod.AutoZhuyin.Auto" = "AutoZhuyin (Auto)"; -"com.autozhuyin.inputmethod.AutoZhuyin.English" = "AutoZhuyin (English)"; diff --git a/Resources/zh-Hant.lproj/InfoPlist.strings b/Resources/zh-Hant.lproj/InfoPlist.strings index 0b5bbd4..e46a5bd 100644 --- a/Resources/zh-Hant.lproj/InfoPlist.strings +++ b/Resources/zh-Hant.lproj/InfoPlist.strings @@ -2,5 +2,3 @@ CFBundleName = "自動注音"; CFBundleDisplayName = "自動注音"; "com.autozhuyin.inputmethod.AutoZhuyin" = "自動注音"; -"com.autozhuyin.inputmethod.AutoZhuyin.Auto" = "自動注音(中英自動)"; -"com.autozhuyin.inputmethod.AutoZhuyin.English" = "自動注音(英文)"; diff --git a/Sources/AutoZhuyin/InputController.swift b/Sources/AutoZhuyin/InputController.swift index 03eeb99..bf808a6 100644 --- a/Sources/AutoZhuyin/InputController.swift +++ b/Sources/AutoZhuyin/InputController.swift @@ -78,8 +78,8 @@ final class InputController: IMKInputController { 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) } + // English mode on/off: show 中 / A by the cursor. + if idle { ModeIndicator.show(english: engine.isEnglishMode, client: client) } } return false } @@ -156,18 +156,6 @@ 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) @@ -202,6 +190,9 @@ final class InputController: IMKInputController { override func menu() -> NSMenu! { let menu = NSMenu() + let english = menu.addItem(withTitle: "英文模式(單按 Shift)", action: #selector(toggleEnglish(_:)), keyEquivalent: "") + english.state = engine.isEnglishMode ? .on : .off + menu.addItem(.separator()) let modes: [(Reranker.Mode, String, Selector)] = [ (.off, "AI 選字:關閉", #selector(aiOff(_:))), (.saver, "AI 選字:省電", #selector(aiSaver(_:))), @@ -221,6 +212,11 @@ final class InputController: IMKInputController { return menu } + @objc private func toggleEnglish(_ sender: Any?) { + if let client = client() { commitComposition(client) } + engine.setEnglishMode(!engine.isEnglishMode) + } + @objc private func aiOff(_ sender: Any?) { AI.setMode(.off) } @objc private func aiSaver(_ sender: Any?) { AI.setMode(.saver) } @objc private func aiStandard(_ sender: Any?) { AI.setMode(.standard) } diff --git a/Sources/AutoZhuyin/InputModes.swift b/Sources/AutoZhuyin/InputModes.swift deleted file mode 100644 index a111be6..0000000 --- a/Sources/AutoZhuyin/InputModes.swift +++ /dev/null @@ -1,39 +0,0 @@ -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 - } - - private static func isEnabled(_ src: TISInputSource) -> Bool { - guard let p = TISGetInputSourceProperty(src, kTISPropertyInputSourceIsEnabled) else { return false } - return CFBooleanGetValue(Unmanaged.fromOpaque(p).takeUnretainedValue()) - } - - /// Makes the given mode current. Returns false on failure. - /// Only enables the mode if it is not enabled yet: TISEnableInputSource opens System - /// Settings → Keyboard, even for a source that is already enabled. - @discardableResult - static func select(english: Bool) -> Bool { - guard let src = source(english ? InputModes.english : InputModes.auto) else { return false } - if !isEnabled(src) { TISEnableInputSource(src) } - return TISSelectInputSource(src) == noErr - } - - /// Called by `--register`: enable both modes (if not yet) so they are in the input menu. - static func enableAll() { - for id in [auto, english] { - if let src = source(id), !isEnabled(src) { TISEnableInputSource(src) } - } - } -} diff --git a/Sources/AutoZhuyin/ModeIndicator.swift b/Sources/AutoZhuyin/ModeIndicator.swift new file mode 100644 index 0000000..8738a3f --- /dev/null +++ b/Sources/AutoZhuyin/ModeIndicator.swift @@ -0,0 +1,67 @@ +import Cocoa +import InputMethodKit + +/// A small "中" / "A" badge shown under the text cursor for a moment when English mode is +/// switched on or off (the menu-bar icon of an input method can't change on its own). +enum ModeIndicator { + private static var panel: NSPanel? + private static var label: NSTextField? + private static var hideWork: DispatchWorkItem? + + static func show(english: Bool, client: IMKTextInput) { + let (panel, label) = makePanel() + label.stringValue = english ? "A" : "中" + + var lineRect = NSRect.zero + _ = client.attributes(forCharacterIndex: 0, lineHeightRectangle: &lineRect) + let size = panel.frame.size + var origin = NSPoint(x: lineRect.minX, y: lineRect.minY - size.height - 4) + if lineRect == .zero, let screen = NSScreen.main { // the app gave no caret position + origin = NSPoint(x: screen.visibleFrame.midX - size.width / 2, y: screen.visibleFrame.midY) + } + panel.setFrameOrigin(origin) + panel.alphaValue = 1 + panel.orderFrontRegardless() + + hideWork?.cancel() + let work = DispatchWorkItem { + NSAnimationContext.runAnimationGroup({ ctx in + ctx.duration = 0.25 + panel.animator().alphaValue = 0 + }, completionHandler: { panel.orderOut(nil) }) + } + hideWork = work + DispatchQueue.main.asyncAfter(deadline: .now() + 0.8, execute: work) + } + + private static func makePanel() -> (NSPanel, NSTextField) { + if let panel, let label { return (panel, label) } + let side: CGFloat = 28 + let p = NSPanel(contentRect: NSRect(x: 0, y: 0, width: side, height: side), + styleMask: [.borderless, .nonactivatingPanel], backing: .buffered, defer: true) + p.level = .popUpMenu + p.isOpaque = false + p.backgroundColor = .clear + p.hasShadow = true + p.ignoresMouseEvents = true + p.collectionBehavior = [.canJoinAllSpaces, .transient, .ignoresCycle] + + let background = NSVisualEffectView(frame: p.contentView!.bounds) + background.material = .hudWindow + background.state = .active + background.wantsLayer = true + background.layer?.cornerRadius = 6 + background.layer?.masksToBounds = true + background.autoresizingMask = [.width, .height] + + let l = NSTextField(labelWithString: "") + l.font = .systemFont(ofSize: 15, weight: .semibold) + l.alignment = .center + l.frame = NSRect(x: 0, y: (side - 20) / 2, width: side, height: 20) + background.addSubview(l) + p.contentView = background + panel = p + label = l + return (p, l) + } +} diff --git a/Sources/AutoZhuyin/main.swift b/Sources/AutoZhuyin/main.swift index a86d481..3d16cd3 100644 --- a/Sources/AutoZhuyin/main.swift +++ b/Sources/AutoZhuyin/main.swift @@ -6,7 +6,6 @@ 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) } diff --git a/scripts/build.sh b/scripts/build.sh index f6d6b37..5a1315b 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -33,9 +33,6 @@ if [ ! -f build/icon.tiff ]; then xcrun swift scripts/make-icon.swift build/icon.tiff fi cp build/icon.tiff "$APP/Contents/Resources/icon.tiff" -# English-mode icon ("A"), shown in the menu bar while English mode is on. -[ -f build/icon-en.tiff ] || xcrun swift scripts/make-icon.swift build/icon-en.tiff A -cp build/icon-en.tiff "$APP/Contents/Resources/icon-en.tiff" codesign --force --sign - "$APP" echo "Built $APP"