diff --git a/MTMR/InputSourceBarItem.swift b/MTMR/InputSourceBarItem.swift index a17f972..fbce9db 100644 --- a/MTMR/InputSourceBarItem.swift +++ b/MTMR/InputSourceBarItem.swift @@ -8,22 +8,18 @@ import Cocoa -class InputSourceBarItem: NSCustomTouchBarItem { - private(set) var button: NSButton! - fileprivate var notificationCenter: CFNotificationCenter - var lastLang: String! - - override init(identifier: NSTouchBarItem.Identifier) { - notificationCenter = CFNotificationCenterGetDistributedCenter(); - super.init(identifier: identifier) - - button = NSButton(title: " ", target: self, action: nil) +class InputSourceBarItem: CustomButtonTouchBarItem { - self.view = button + fileprivate var notificationCenter: CFNotificationCenter + + init(identifier: NSTouchBarItem.Identifier, onTap: @escaping () -> (), onLongTap: @escaping () -> ()) { + notificationCenter = CFNotificationCenterGetDistributedCenter(); + super.init(identifier: identifier, title: "⏳", onTap: onTap, onLongTap: onLongTap) observeIputSourceChangedNotification(); - textInputSourceDidChange() + + self.button.action = #selector(switchInputSource) } required init?(coder: NSCoder) { @@ -32,31 +28,44 @@ class InputSourceBarItem: NSCustomTouchBarItem { @objc public func textInputSourceDidChange() { let currentSource = TISCopyCurrentKeyboardInputSource().takeUnretainedValue() - let ptrID = TISGetInputSourceProperty(currentSource as TISInputSource, kTISPropertyInputSourceID) - let ID = unsafeBitCast(ptrID, to: CFString.self) + + var iconImage: NSImage? = nil - switch String(ID) { - case "com.apple.keylayout.RussianWin": - self.button.title = "RU" - break - case "com.apple.keylayout.US": - self.button.title = "US" - break - default: - self.button.title = String(ID) + if let imageURL = currentSource.iconImageURL { + if let image = NSImage(contentsOf: imageURL) { + iconImage = image + } + } + + if iconImage == nil, let iconRef = currentSource.iconRef { + iconImage = NSImage(iconRef: iconRef) + } + + if (iconImage != nil) { + self.button.image = iconImage + } else { + self.button.title = currentSource.name } -// print(ID) } -// private func getInputSource() -> String { -// let keyboard = TISCopyCurrentKeyboardInputSource().takeRetainedValue() -// let keyboardString = String(describing: keyboard) -// let range = keyboardString.range(of: "KB Layout: ", options: .literal, range: keyboardString.startIndex.. AnyObject? { + let cfType = TISGetInputSourceProperty(self, key) + if (cfType != nil) { + return Unmanaged.fromOpaque(cfType!).takeUnretainedValue() + } else { + return nil + } + } + + var id: String { + return getProperty(kTISPropertyInputSourceID) as! String + } + + var name: String { + return getProperty(kTISPropertyLocalizedName) as! String + } + + var category: String { + return getProperty(kTISPropertyInputSourceCategory) as! String + } + + var isSelectable: Bool { + return getProperty(kTISPropertyInputSourceIsSelectCapable) as! Bool + } + + var sourceLanguages: [String] { + return getProperty(kTISPropertyInputSourceLanguages) as! [String] + } + + var iconImageURL: URL? { + return getProperty(kTISPropertyIconImageURL) as! URL? + } + + var iconRef: IconRef? { + return OpaquePointer(TISGetInputSourceProperty(self, kTISPropertyIconRef)) as IconRef? + } +} diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index df188c3..20c22d5 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -193,7 +193,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate { case .currency(interval: let interval, from: let from, to: let to): barItem = CurrencyBarItem(identifier: identifier, interval: interval, from: from, to: to, onTap: action, onLongTap: longAction) case .inputsource(): - barItem = InputSourceBarItem(identifier: identifier) + barItem = InputSourceBarItem(identifier: identifier, onTap: action, onLongTap: longAction) } if case .width(let value)? = item.additionalParameters[.width], let widthBarItem = barItem as? CanSetWidth {