From f323a295522708a9909f86de3d0acf5f03de5a90 Mon Sep 17 00:00:00 2001 From: John Kao Date: Fri, 13 Nov 2026 17:07:49 +0800 Subject: [PATCH] Don't open Keyboard settings on every Shift tap InputModes.select called TISEnableInputSource before every switch, and macOS opens System Settings > Keyboard whenever a source is enabled, even one that is already enabled. Enable only when the mode is not enabled yet (also in --register). Co-Authored-By: Claude Opus 5.5 --- Sources/AutoZhuyin/InputModes.swift | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Sources/AutoZhuyin/InputModes.swift b/Sources/AutoZhuyin/InputModes.swift index 8c049d5..a111be6 100644 --- a/Sources/AutoZhuyin/InputModes.swift +++ b/Sources/AutoZhuyin/InputModes.swift @@ -15,18 +15,25 @@ enum InputModes { return list.first } - /// Makes the given mode current (enabling it first if needed). Returns false on failure. + 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 } - TISEnableInputSource(src) + if !isEnabled(src) { TISEnableInputSource(src) } return TISSelectInputSource(src) == noErr } - /// Called by `--register`: enable both modes so they are in the input menu. + /// 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) { TISEnableInputSource(src) } + if let src = source(id), !isEnabled(src) { TISEnableInputSource(src) } } } }