ctlPrefWindow // Re-enable previous keyboard layouts menu procesing.

- This is to attempt fixing an issue that the PrefWindow may hang for few secongs when being switched back from other windows / apps.
This commit is contained in:
ShikiSuen 2022-03-07 21:05:34 +08:00
parent 5e6a523be2
commit 81cdb6d1b1
1 changed files with 15 additions and 24 deletions

View File

@ -90,52 +90,43 @@ extension RangeReplaceableCollection where Element: Hashable {
basisKeyboardLayoutButton.menu?.addItem(menuItem_vChewingDachen) basisKeyboardLayoutButton.menu?.addItem(menuItem_vChewingDachen)
let basisKeyboardLayoutID = Preferences.basisKeyboardLayout let basisKeyboardLayoutID = Preferences.basisKeyboardLayout
for source in list { for source in list {
if let categoryPtr = TISGetInputSourceProperty(source, kTISPropertyInputSourceCategory) {
func getString(_ key: CFString) -> String? { let category = Unmanaged<CFString>.fromOpaque(categoryPtr).takeUnretainedValue()
if let ptr = TISGetInputSourceProperty(source, key) { if category != kTISCategoryKeyboardInputSource {
return String(Unmanaged<CFString>.fromOpaque(ptr).takeUnretainedValue())
}
return nil
}
func getBool(_ key: CFString) -> Bool? {
if let ptr = TISGetInputSourceProperty(source, key) {
return Unmanaged<CFBoolean>.fromOpaque(ptr).takeUnretainedValue() == kCFBooleanTrue
}
return nil
}
if let category = getString(kTISPropertyInputSourceCategory) {
if category != String(kTISCategoryKeyboardInputSource) {
continue continue
} }
} else { } else {
continue continue
} }
if let asciiCapable = getBool(kTISPropertyInputSourceIsASCIICapable) { if let asciiCapablePtr = TISGetInputSourceProperty(source, kTISPropertyInputSourceIsASCIICapable) {
if !asciiCapable { let asciiCapable = Unmanaged<CFBoolean>.fromOpaque(asciiCapablePtr).takeUnretainedValue()
if asciiCapable != kCFBooleanTrue {
continue continue
} }
} else { } else {
continue continue
} }
if let sourceType = getString(kTISPropertyInputSourceType) { if let sourceTypePtr = TISGetInputSourceProperty(source, kTISPropertyInputSourceType) {
if sourceType != String(kTISTypeKeyboardLayout) { let sourceType = Unmanaged<CFString>.fromOpaque(sourceTypePtr).takeUnretainedValue()
if sourceType != kTISTypeKeyboardLayout {
continue continue
} }
} else { } else {
continue continue
} }
guard let sourceID = getString(kTISPropertyInputSourceID), guard let sourceIDPtr = TISGetInputSourceProperty(source, kTISPropertyInputSourceID),
let localizedName = getString(kTISPropertyLocalizedName) else { let localizedNamePtr = TISGetInputSourceProperty(source, kTISPropertyLocalizedName) else {
continue continue
} }
let sourceID = String(Unmanaged<CFString>.fromOpaque(sourceIDPtr).takeUnretainedValue())
let localizedName = String(Unmanaged<CFString>.fromOpaque(localizedNamePtr).takeUnretainedValue())
let menuItem = NSMenuItem() let menuItem = NSMenuItem()
menuItem.title = localizedName menuItem.title = localizedName
menuItem.representedObject = sourceID menuItem.representedObject = sourceID