1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-11 17:38:38 +00:00

* file moved to new location

This commit is contained in:
ad 2018-04-22 19:28:23 +03:00
parent f6e5b3ca09
commit 0f41638d0f

View File

@ -11,43 +11,43 @@ import Cocoa
class InputSourceBarItem: CustomButtonTouchBarItem { class InputSourceBarItem: CustomButtonTouchBarItem {
fileprivate var notificationCenter: CFNotificationCenter fileprivate var notificationCenter: CFNotificationCenter
init(identifier: NSTouchBarItem.Identifier, onTap: @escaping () -> (), onLongTap: @escaping () -> ()) { init(identifier: NSTouchBarItem.Identifier, onTap: @escaping () -> (), onLongTap: @escaping () -> ()) {
notificationCenter = CFNotificationCenterGetDistributedCenter(); notificationCenter = CFNotificationCenterGetDistributedCenter();
super.init(identifier: identifier, title: "", onTap: onTap, onLongTap: onLongTap) super.init(identifier: identifier, title: "", onTap: onTap, onLongTap: onLongTap)
observeIputSourceChangedNotification(); observeIputSourceChangedNotification();
textInputSourceDidChange() textInputSourceDidChange()
self.button.action = #selector(switchInputSource) self.button.action = #selector(switchInputSource)
} }
required init?(coder: NSCoder) { required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented") fatalError("init(coder:) has not been implemented")
} }
@objc public func textInputSourceDidChange() { @objc public func textInputSourceDidChange() {
let currentSource = TISCopyCurrentKeyboardInputSource().takeUnretainedValue() let currentSource = TISCopyCurrentKeyboardInputSource().takeUnretainedValue()
var iconImage: NSImage? = nil var iconImage: NSImage? = nil
if let imageURL = currentSource.iconImageURL { if let imageURL = currentSource.iconImageURL {
if let image = NSImage(contentsOf: imageURL) { if let image = NSImage(contentsOf: imageURL) {
iconImage = image iconImage = image
} }
} }
if iconImage == nil, let iconRef = currentSource.iconRef { if iconImage == nil, let iconRef = currentSource.iconRef {
iconImage = NSImage(iconRef: iconRef) iconImage = NSImage(iconRef: iconRef)
} }
if (iconImage != nil) { if (iconImage != nil) {
self.button.image = iconImage self.button.image = iconImage
} else { } else {
self.button.title = currentSource.name self.button.title = currentSource.name
} }
} }
@objc private func switchInputSource() { @objc private func switchInputSource() {
var inputSources: [TISInputSource] = [] var inputSources: [TISInputSource] = []
@ -58,7 +58,7 @@ class InputSourceBarItem: CustomButtonTouchBarItem {
inputSources = elements.filter({ inputSources = elements.filter({
$0.category == TISInputSource.Category.keyboardInputSource && $0.isSelectable $0.category == TISInputSource.Category.keyboardInputSource && $0.isSelectable
}) })
for item in inputSources { for item in inputSources {
if (item.id != currentSource.id) { if (item.id != currentSource.id) {
TISSelectInputSource(item) TISSelectInputSource(item)
@ -72,7 +72,7 @@ class InputSourceBarItem: CustomButtonTouchBarItem {
let mySelf = Unmanaged<InputSourceBarItem>.fromOpaque(observer!).takeUnretainedValue() let mySelf = Unmanaged<InputSourceBarItem>.fromOpaque(observer!).takeUnretainedValue()
mySelf.textInputSourceDidChange() mySelf.textInputSourceDidChange()
} }
CFNotificationCenterAddObserver(notificationCenter, CFNotificationCenterAddObserver(notificationCenter,
UnsafeRawPointer(Unmanaged.passUnretained(self).toOpaque()), UnsafeRawPointer(Unmanaged.passUnretained(self).toOpaque()),
callback, callback,
@ -88,7 +88,7 @@ extension TISInputSource {
return kTISCategoryKeyboardInputSource as String return kTISCategoryKeyboardInputSource as String
} }
} }
private func getProperty(_ key: CFString) -> AnyObject? { private func getProperty(_ key: CFString) -> AnyObject? {
let cfType = TISGetInputSourceProperty(self, key) let cfType = TISGetInputSourceProperty(self, key)
if (cfType != nil) { if (cfType != nil) {
@ -97,31 +97,31 @@ extension TISInputSource {
return nil return nil
} }
} }
var id: String { var id: String {
return getProperty(kTISPropertyInputSourceID) as! String return getProperty(kTISPropertyInputSourceID) as! String
} }
var name: String { var name: String {
return getProperty(kTISPropertyLocalizedName) as! String return getProperty(kTISPropertyLocalizedName) as! String
} }
var category: String { var category: String {
return getProperty(kTISPropertyInputSourceCategory) as! String return getProperty(kTISPropertyInputSourceCategory) as! String
} }
var isSelectable: Bool { var isSelectable: Bool {
return getProperty(kTISPropertyInputSourceIsSelectCapable) as! Bool return getProperty(kTISPropertyInputSourceIsSelectCapable) as! Bool
} }
var sourceLanguages: [String] { var sourceLanguages: [String] {
return getProperty(kTISPropertyInputSourceLanguages) as! [String] return getProperty(kTISPropertyInputSourceLanguages) as! [String]
} }
var iconImageURL: URL? { var iconImageURL: URL? {
return getProperty(kTISPropertyIconImageURL) as! URL? return getProperty(kTISPropertyIconImageURL) as! URL?
} }
var iconRef: IconRef? { var iconRef: IconRef? {
return OpaquePointer(TISGetInputSourceProperty(self, kTISPropertyIconRef)) as IconRef? return OpaquePointer(TISGetInputSourceProperty(self, kTISPropertyIconRef)) as IconRef?
} }