diff --git a/MTMR.xcodeproj/project.pbxproj b/MTMR.xcodeproj/project.pbxproj index 5041c9a..4aa24bc 100644 --- a/MTMR.xcodeproj/project.pbxproj +++ b/MTMR.xcodeproj/project.pbxproj @@ -20,6 +20,7 @@ 6042B6AA2083E27000C525C8 /* DeprecatedCarbonAPI.c in Sources */ = {isa = PBXBuildFile; fileRef = 6042B6A92083E27000C525C8 /* DeprecatedCarbonAPI.c */; }; 607EEA4B2087835F009DA5F0 /* WeatherBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607EEA4A2087835F009DA5F0 /* WeatherBarItem.swift */; }; 607EEA4D2087A8DA009DA5F0 /* CurrencyBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607EEA4C2087A8DA009DA5F0 /* CurrencyBarItem.swift */; }; + 60F7D454208CC31400ABF5D2 /* InputSourceBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 60F7D453208CC31400ABF5D2 /* InputSourceBarItem.swift */; }; B0008E552080286C003AD4DD /* SupportHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = B0008E542080286C003AD4DD /* SupportHelpers.swift */; }; B04B7BB72087398C00C835D0 /* BatteryBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = B04B7BB62087398C00C835D0 /* BatteryBarItem.swift */; }; B05600D32083E9BB00EB218D /* CustomSlider.swift in Sources */ = {isa = PBXBuildFile; fileRef = B05600D22083E9BB00EB218D /* CustomSlider.swift */; }; @@ -71,6 +72,7 @@ 6042B6A92083E27000C525C8 /* DeprecatedCarbonAPI.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = DeprecatedCarbonAPI.c; sourceTree = ""; }; 607EEA4A2087835F009DA5F0 /* WeatherBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeatherBarItem.swift; sourceTree = ""; }; 607EEA4C2087A8DA009DA5F0 /* CurrencyBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrencyBarItem.swift; sourceTree = ""; }; + 60F7D453208CC31400ABF5D2 /* InputSourceBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputSourceBarItem.swift; sourceTree = ""; }; B0008E542080286C003AD4DD /* SupportHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SupportHelpers.swift; sourceTree = ""; }; B04B7BB62087398C00C835D0 /* BatteryBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BatteryBarItem.swift; sourceTree = ""; }; B05600D22083E9BB00EB218D /* CustomSlider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomSlider.swift; sourceTree = ""; }; @@ -228,6 +230,7 @@ 607EEA4A2087835F009DA5F0 /* WeatherBarItem.swift */, 607EEA4C2087A8DA009DA5F0 /* CurrencyBarItem.swift */, 6042B6A62083E03A00C525C8 /* AppScrubberTouchBarItem.swift */, + 60F7D453208CC31400ABF5D2 /* InputSourceBarItem.swift */, ); path = Widgets; sourceTree = ""; @@ -360,6 +363,7 @@ 607EEA4B2087835F009DA5F0 /* WeatherBarItem.swift in Sources */, 6042B6AA2083E27000C525C8 /* DeprecatedCarbonAPI.c in Sources */, B09EB1E4207C082000D5C1E0 /* HapticFeedback.swift in Sources */, + 60F7D454208CC31400ABF5D2 /* InputSourceBarItem.swift in Sources */, 36C2ECDB207C3FE7003CDA33 /* ItemsParsing.swift in Sources */, B0A7E9AA205D6AA400EEF070 /* KeyPress.swift in Sources */, 36C2ECD7207B6DAE003CDA33 /* TimeTouchBarItem.swift in Sources */, diff --git a/MTMR/InputSourceBarItem.swift b/MTMR/InputSourceBarItem.swift new file mode 100644 index 0000000..fbce9db --- /dev/null +++ b/MTMR/InputSourceBarItem.swift @@ -0,0 +1,129 @@ +// +// InputSourceBarItem.swift +// MTMR +// +// Created by Daniel Apatin on 22.04.2018. +// Copyright © 2018 Anton Palgunov. All rights reserved. +// + +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] = [] + + let currentSource = TISCopyCurrentKeyboardInputSource().takeUnretainedValue() + let inputSourceNSArray = TISCreateInputSourceList(nil, false).takeRetainedValue() as NSArray + let elements = inputSourceNSArray as! [TISInputSource] + + inputSources = elements.filter({ + $0.category == TISInputSource.Category.keyboardInputSource && $0.isSelectable + }) + + for item in inputSources { + if (item.id != currentSource.id) { + TISSelectInputSource(item) + break + } + } + } + + @objc public func observeIputSourceChangedNotification(){ + let callback: CFNotificationCallback = { center, observer, name, object, info in + let mySelf = Unmanaged.fromOpaque(observer!).takeUnretainedValue() + mySelf.textInputSourceDidChange() + } + + CFNotificationCenterAddObserver(notificationCenter, + UnsafeRawPointer(Unmanaged.passUnretained(self).toOpaque()), + callback, + kTISNotifySelectedKeyboardInputSourceChanged, + nil, + .deliverImmediately) + } +} + +extension TISInputSource { + enum Category { + static var keyboardInputSource: String { + return kTISCategoryKeyboardInputSource as String + } + } + + private func getProperty(_ key: CFString) -> 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/ItemsParsing.swift b/MTMR/ItemsParsing.swift index dedc5af..68d8600 100644 --- a/MTMR/ItemsParsing.swift +++ b/MTMR/ItemsParsing.swift @@ -100,6 +100,9 @@ class SupportedTypesHolder { "dock": { decoder in return (item: .dock(), action: .none, longAction: .none, parameters: [:]) }, + "inputsource": { decoder in + return (item: .inputsource(), action: .none, longAction: .none, parameters: [:]) + }, "volume": { decoder in enum CodingKeys: String, CodingKey { case image } let container = try decoder.container(keyedBy: CodingKeys.self) @@ -152,6 +155,7 @@ enum ItemType: Decodable { case brightness(refreshInterval: Double) case weather(interval: Double, units: String, api_key: String, icon_type: String) case currency(interval: Double, from: String, to: String) + case inputsource() private enum CodingKeys: String, CodingKey { case type @@ -179,6 +183,7 @@ enum ItemType: Decodable { case brightness case weather case currency + case inputsource } init(from decoder: Decoder) throws { @@ -215,6 +220,8 @@ enum ItemType: Decodable { let from = try container.decodeIfPresent(String.self, forKey: .from) ?? "RUB" let to = try container.decodeIfPresent(String.self, forKey: .to) ?? "USD" self = .currency(interval: interval, from: from, to: to) + case .inputsource: + self = .inputsource() } } } diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index b4f7f4f..20c22d5 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -35,6 +35,8 @@ extension ItemType { return "com.toxblh.mtmr.weather" case .currency(interval: _, from: _, to: _): return "com.toxblh.mtmr.currency" + case .inputsource(): + return "com.toxblh.mtmr.inputsource." } } @@ -190,6 +192,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate { barItem = WeatherBarItem(identifier: identifier, interval: interval, units: units, api_key: api_key, icon_type: icon_type, onTap: action, onLongTap: longAction) 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, onTap: action, onLongTap: longAction) } if case .width(let value)? = item.additionalParameters[.width], let widthBarItem = barItem as? CanSetWidth {