-
Notifications
You must be signed in to change notification settings - Fork 0
V1.1.7 #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nissy
wants to merge
5
commits into
main
Choose a base branch
from
v1.1.7
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
V1.1.7 #17
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,227 @@ | ||
| import Foundation | ||
| import Carbon | ||
| import Cocoa | ||
|
|
||
| extension ImeController { | ||
| func initializeCache() { | ||
| if Thread.isMainThread { | ||
| buildCacheSync() | ||
| } else { | ||
| let semaphore = DispatchSemaphore(value: 0) | ||
| DispatchQueue.main.async { [weak self] in | ||
| self?.buildCacheSync() | ||
| semaphore.signal() | ||
| } | ||
| _ = semaphore.wait(timeout: .now() + 2.0) | ||
| } | ||
| } | ||
|
|
||
| func buildCacheSync() { | ||
| guard let cfInputSources = TISCreateInputSourceList(nil, false) else { | ||
| Logger.error("TISCreateInputSourceList returned nil", category: .ime) | ||
| return | ||
| } | ||
|
|
||
| let inputSources = cfInputSources.takeRetainedValue() as? [TISInputSource] ?? [] | ||
|
|
||
| if inputSources.isEmpty { | ||
| Logger.warning("No input sources found", category: .ime) | ||
| return | ||
| } | ||
|
|
||
| var newCache: [String: TISInputSource] = [:] | ||
|
|
||
| for inputSource in inputSources { | ||
| if let sourceID = TISGetInputSourceProperty(inputSource, kTISPropertyInputSourceID) { | ||
| let id = Unmanaged<CFString>.fromOpaque(sourceID).takeUnretainedValue() as String | ||
| newCache[id] = inputSource | ||
| } | ||
| } | ||
|
|
||
| cacheQueue.sync { | ||
| inputSourceCache = newCache | ||
| } | ||
| Logger.debug("IME cache initialized with \(newCache.count) input sources", category: .ime) | ||
| } | ||
|
|
||
| func refreshInputSourceCache() { | ||
| DispatchQueue.main.async { [weak self] in | ||
| self?.buildCacheSync() | ||
| } | ||
| } | ||
|
|
||
| func refreshCacheSync() { | ||
| if Thread.isMainThread { | ||
| buildCacheSync() | ||
| return | ||
| } | ||
|
|
||
| let semaphore = DispatchSemaphore(value: 0) | ||
| DispatchQueue.main.async { [weak self] in | ||
| self?.buildCacheSync() | ||
| semaphore.signal() | ||
| } | ||
| _ = semaphore.wait(timeout: .now() + 1.0) | ||
| } | ||
|
|
||
| // MARK: - IME Change Monitoring | ||
|
|
||
| func startMonitoringIMEChanges() { | ||
| DistributedNotificationCenter.default().addObserver( | ||
| self, | ||
| selector: #selector(inputSourcesChanged), | ||
| name: NSNotification.Name("com.apple.Carbon.TISNotifyEnabledKeyboardInputSourcesChanged"), | ||
| object: nil | ||
| ) | ||
|
|
||
| DistributedNotificationCenter.default().addObserver( | ||
| self, | ||
| selector: #selector(inputSourcesChanged), | ||
| name: NSNotification.Name("com.apple.Carbon.TISNotifySelectedKeyboardInputSourceChanged"), | ||
| object: nil | ||
| ) | ||
|
|
||
| let notificationCenter = NSWorkspace.shared.notificationCenter | ||
|
|
||
| notificationCenter.addObserver( | ||
| self, | ||
| selector: #selector(systemWillSleep), | ||
| name: NSWorkspace.willSleepNotification, | ||
| object: nil | ||
| ) | ||
|
|
||
| notificationCenter.addObserver( | ||
| self, | ||
| selector: #selector(systemDidWake), | ||
| name: NSWorkspace.didWakeNotification, | ||
| object: nil | ||
| ) | ||
| } | ||
|
|
||
| @objc func inputSourcesChanged(_ notification: Notification) { | ||
| Logger.debug("Input sources changed, refreshing cache", category: .ime) | ||
| syncLastNotifiedIMEWithCurrentInputSource() | ||
| refreshInputSourceCache() | ||
| } | ||
|
|
||
| @objc func systemWillSleep(_ notification: Notification) { | ||
| Logger.info("System will sleep - preparing IME cache", category: .ime) | ||
| } | ||
|
|
||
| @objc func systemDidWake(_ notification: Notification) { | ||
| Logger.info("System did wake - refreshing IME cache", category: .ime) | ||
|
|
||
| DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { [weak self] in | ||
| self?.refreshInputSourceCache() | ||
| Logger.debug("IME cache refreshed after system wake", category: .ime) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Application Focus Monitoring | ||
|
|
||
| func startMonitoringApplicationFocus() { | ||
| NSWorkspace.shared.notificationCenter.addObserver( | ||
| self, | ||
| selector: #selector(applicationDidActivate), | ||
| name: NSWorkspace.didActivateApplicationNotification, | ||
| object: nil | ||
| ) | ||
| } | ||
|
|
||
| @objc func applicationDidActivate(_ notification: Notification) { | ||
| guard let app = notification.userInfo?[NSWorkspace.applicationUserInfoKey] as? NSRunningApplication else { | ||
| return | ||
| } | ||
|
|
||
| let appName = app.localizedName ?? "Unknown" | ||
| Logger.debug("Application activated: \(appName)", category: .ime) | ||
|
|
||
| DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) { [weak self] in | ||
| self?.verifyIMEStateAfterAppSwitch() | ||
| } | ||
| } | ||
|
|
||
| func verifyIMEStateAfterAppSwitch() { | ||
| let actualIME = getCurrentInputSource() | ||
| let expectedIME = lastSwitchedIMEQueue.sync { lastSwitchedIME } | ||
|
|
||
| if let expected = expectedIME, actualIME != expected { | ||
| Logger.warning( | ||
| "IME state mismatch after app switch: expected=\(expected), actual=\(actualIME)", | ||
| category: .ime | ||
| ) | ||
|
|
||
| refreshInputSourceCache() | ||
|
|
||
| Logger.debug("Reapplying IME after focus change: \(expected)", category: .ime) | ||
| switchToSpecificIME(expected, fromUser: false) | ||
| } else { | ||
| Logger.debug("IME state verified after app switch: \(actualIME)", category: .ime) | ||
| } | ||
| } | ||
|
|
||
| func setLastSwitchedIME(_ imeId: String) { | ||
| lastSwitchedIMEQueue.sync { | ||
| lastSwitchedIME = imeId | ||
| } | ||
| } | ||
|
|
||
| func getLastSwitchedIME() -> String? { | ||
| return lastSwitchedIMEQueue.sync { lastSwitchedIME } | ||
| } | ||
| } | ||
|
|
||
| extension ImeController { | ||
| func postIMESwitchNotification(_ imeId: String, isRetry: Bool = false) { | ||
| var shouldNotify = false | ||
| let currentIME = isRetry ? getCurrentInputSource() : "" | ||
|
|
||
| notificationQueue.sync { | ||
| if lastNotifiedIME != imeId { | ||
| lastNotifiedIME = imeId | ||
| shouldNotify = true | ||
| } else if isRetry && currentIME == imeId { | ||
| shouldNotify = true | ||
| } | ||
| } | ||
|
|
||
| guard shouldNotify else { | ||
| Logger.debug("Skipping duplicate notification for: \(imeId)", category: .ime) | ||
| return | ||
| } | ||
|
|
||
| DispatchQueue.main.async { | ||
| NotificationCenter.default.post( | ||
| name: NSNotification.Name("ModSwitchIME.didSwitchIME"), | ||
| object: nil, | ||
| userInfo: ["imeId": imeId] | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| func postUIRefreshNotification() { | ||
| syncLastNotifiedIMEWithCurrentInputSource() | ||
| DispatchQueue.main.async { | ||
| NotificationCenter.default.post( | ||
| name: NSNotification.Name("ModSwitchIME.shouldRefreshUI"), | ||
| object: nil | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| func syncLastNotifiedIMEWithCurrentInputSource() { | ||
| let currentIME = getCurrentInputSource() | ||
| notificationQueue.sync { | ||
| lastNotifiedIME = currentIME | ||
| } | ||
| } | ||
|
|
||
| #if DEBUG | ||
| func debugGetLastSwitchedIME() -> String? { getLastSwitchedIME() } | ||
| func debugGetLastNotifiedIME() -> String { notificationQueue.sync { lastNotifiedIME } } | ||
| func debugSetLastNotifiedIME(_ imeId: String) { | ||
| notificationQueue.sync { lastNotifiedIME = imeId } | ||
| } | ||
| func debugSyncLastNotifiedIMEWithCurrentInputSource() { syncLastNotifiedIMEWithCurrentInputSource() } | ||
| #endif | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The delay
0.2used for verifying IME state after an app switch is a magic number. Consider defining it as a constant likeappSwitchVerificationDelay.