diff --git a/MTMR/InputSourceBarItem.swift b/MTMR/Widgets/InputSourceBarItem.swift similarity index 97% rename from MTMR/InputSourceBarItem.swift rename to MTMR/Widgets/InputSourceBarItem.swift index fbce9db..d131111 100644 --- a/MTMR/InputSourceBarItem.swift +++ b/MTMR/Widgets/InputSourceBarItem.swift @@ -11,43 +11,43 @@ import Cocoa class InputSourceBarItem: CustomButtonTouchBarItem { 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) { fatalError("init(coder:) has not been implemented") } - + @objc public func textInputSourceDidChange() { let currentSource = TISCopyCurrentKeyboardInputSource().takeUnretainedValue() var iconImage: NSImage? = nil - + 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 } } - + @objc private func switchInputSource() { var inputSources: [TISInputSource] = [] @@ -58,7 +58,7 @@ class InputSourceBarItem: CustomButtonTouchBarItem { inputSources = elements.filter({ $0.category == TISInputSource.Category.keyboardInputSource && $0.isSelectable }) - + for item in inputSources { if (item.id != currentSource.id) { TISSelectInputSource(item) @@ -72,7 +72,7 @@ class InputSourceBarItem: CustomButtonTouchBarItem { let mySelf = Unmanaged.fromOpaque(observer!).takeUnretainedValue() mySelf.textInputSourceDidChange() } - + CFNotificationCenterAddObserver(notificationCenter, UnsafeRawPointer(Unmanaged.passUnretained(self).toOpaque()), callback, @@ -88,7 +88,7 @@ extension TISInputSource { return kTISCategoryKeyboardInputSource as String } } - + private func getProperty(_ key: CFString) -> AnyObject? { let cfType = TISGetInputSourceProperty(self, key) if (cfType != nil) { @@ -97,31 +97,31 @@ extension TISInputSource { 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? }