Adds icons for keyboard layouts in preference.

This commit is contained in:
zonble 2022-01-14 19:37:48 +08:00
parent 9faed2153f
commit 83354f7c48
1 changed files with 37 additions and 14 deletions

View File

@ -52,45 +52,68 @@ import Carbon
let basisKeyboardLayoutID = Preferences.basisKeyboardLayout let basisKeyboardLayoutID = Preferences.basisKeyboardLayout
for source in list { for source in list {
if let categoryPtr = TISGetInputSourceProperty(source, kTISPropertyInputSourceCategory) {
let category = Unmanaged<CFString>.fromOpaque(categoryPtr).takeUnretainedValue() func getString(_ key: CFString) -> String? {
if category != kTISCategoryKeyboardInputSource { if let ptr = TISGetInputSourceProperty(source, key) {
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 asciiCapablePtr = TISGetInputSourceProperty(source, kTISPropertyInputSourceIsASCIICapable) { if let asciiCapable = getBool(kTISPropertyInputSourceIsASCIICapable) {
let asciiCapable = Unmanaged<CFBoolean>.fromOpaque(asciiCapablePtr).takeUnretainedValue() if !asciiCapable {
if asciiCapable != kCFBooleanTrue {
continue continue
} }
} else { } else {
continue continue
} }
if let sourceTypePtr = TISGetInputSourceProperty(source, kTISPropertyInputSourceType) { if let sourceType = getString(kTISPropertyInputSourceType) {
let sourceType = Unmanaged<CFString>.fromOpaque(sourceTypePtr).takeUnretainedValue() if sourceType != String(kTISTypeKeyboardLayout) {
if sourceType != kTISTypeKeyboardLayout {
continue continue
} }
} else { } else {
continue continue
} }
guard let sourceIDPtr = TISGetInputSourceProperty(source, kTISPropertyInputSourceID), guard let sourceID = getString(kTISPropertyInputSourceID),
let localizedNamePtr = TISGetInputSourceProperty(source, kTISPropertyLocalizedName) else { let localizedName = getString(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
if let iconPtr = TISGetInputSourceProperty(source, kTISPropertyIconRef) {
let icon = IconRef(iconPtr)
let image = NSImage(iconRef: icon)
func resize( _ image: NSImage) -> NSImage {
let newImage = NSImage(size: NSSize(width: 16, height: 16))
newImage.lockFocus()
image.draw(in: NSRect(x: 0, y: 0, width: 16, height: 16))
newImage.unlockFocus()
return newImage
}
menuItem.image = resize(image)
}
if sourceID == "com.apple.keylayout.US" { if sourceID == "com.apple.keylayout.US" {
usKeyboardLayoutItem = menuItem usKeyboardLayoutItem = menuItem
} }