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