mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 17:38:38 +00:00
+ switch input source by tap
This commit is contained in:
parent
d632bd3ee7
commit
04b821510b
@ -8,22 +8,18 @@
|
|||||||
|
|
||||||
import Cocoa
|
import Cocoa
|
||||||
|
|
||||||
class InputSourceBarItem: NSCustomTouchBarItem {
|
class InputSourceBarItem: CustomButtonTouchBarItem {
|
||||||
private(set) var button: NSButton!
|
|
||||||
fileprivate var notificationCenter: CFNotificationCenter
|
fileprivate var notificationCenter: CFNotificationCenter
|
||||||
var lastLang: String!
|
|
||||||
|
|
||||||
override init(identifier: NSTouchBarItem.Identifier) {
|
init(identifier: NSTouchBarItem.Identifier, onTap: @escaping () -> (), onLongTap: @escaping () -> ()) {
|
||||||
notificationCenter = CFNotificationCenterGetDistributedCenter();
|
notificationCenter = CFNotificationCenterGetDistributedCenter();
|
||||||
super.init(identifier: identifier)
|
super.init(identifier: identifier, title: "⏳", onTap: onTap, onLongTap: onLongTap)
|
||||||
|
|
||||||
button = NSButton(title: " ", target: self, action: nil)
|
|
||||||
|
|
||||||
self.view = button
|
|
||||||
|
|
||||||
observeIputSourceChangedNotification();
|
observeIputSourceChangedNotification();
|
||||||
|
|
||||||
textInputSourceDidChange()
|
textInputSourceDidChange()
|
||||||
|
|
||||||
|
self.button.action = #selector(switchInputSource)
|
||||||
}
|
}
|
||||||
|
|
||||||
required init?(coder: NSCoder) {
|
required init?(coder: NSCoder) {
|
||||||
@ -32,31 +28,44 @@ class InputSourceBarItem: NSCustomTouchBarItem {
|
|||||||
|
|
||||||
@objc public func textInputSourceDidChange() {
|
@objc public func textInputSourceDidChange() {
|
||||||
let currentSource = TISCopyCurrentKeyboardInputSource().takeUnretainedValue()
|
let currentSource = TISCopyCurrentKeyboardInputSource().takeUnretainedValue()
|
||||||
let ptrID = TISGetInputSourceProperty(currentSource as TISInputSource, kTISPropertyInputSourceID)
|
|
||||||
let ID = unsafeBitCast(ptrID, to: CFString.self)
|
|
||||||
|
|
||||||
switch String(ID) {
|
var iconImage: NSImage? = nil
|
||||||
case "com.apple.keylayout.RussianWin":
|
|
||||||
self.button.title = "RU"
|
if let imageURL = currentSource.iconImageURL {
|
||||||
break
|
if let image = NSImage(contentsOf: imageURL) {
|
||||||
case "com.apple.keylayout.US":
|
iconImage = image
|
||||||
self.button.title = "US"
|
}
|
||||||
break
|
}
|
||||||
default:
|
|
||||||
self.button.title = String(ID)
|
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 {
|
@objc private func switchInputSource() {
|
||||||
// let keyboard = TISCopyCurrentKeyboardInputSource().takeRetainedValue()
|
var inputSources: [TISInputSource] = []
|
||||||
// let keyboardString = String(describing: keyboard)
|
|
||||||
// let range = keyboardString.range(of: "KB Layout: ", options: .literal, range: keyboardString.startIndex..<keyboardString.endIndex)!
|
let currentSource = TISCopyCurrentKeyboardInputSource().takeUnretainedValue()
|
||||||
// let startingKeyboard = range.upperBound
|
let inputSourceNSArray = TISCreateInputSourceList(nil, false).takeRetainedValue() as NSArray
|
||||||
// let theKeyboardLayout = keyboardString[startingKeyboard ..< keyboardString.endIndex]
|
let elements = inputSourceNSArray as! [TISInputSource]
|
||||||
// print("theKeyboardLayout ", theKeyboardLayout)
|
|
||||||
// return String(theKeyboardLayout)
|
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(){
|
@objc public func observeIputSourceChangedNotification(){
|
||||||
let callback: CFNotificationCallback = { center, observer, name, object, info in
|
let callback: CFNotificationCallback = { center, observer, name, object, info in
|
||||||
@ -73,4 +82,48 @@ class InputSourceBarItem: NSCustomTouchBarItem {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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<AnyObject>.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?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@ -193,7 +193,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
case .currency(interval: let interval, from: let from, to: let to):
|
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)
|
barItem = CurrencyBarItem(identifier: identifier, interval: interval, from: from, to: to, onTap: action, onLongTap: longAction)
|
||||||
case .inputsource():
|
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 {
|
if case .width(let value)? = item.additionalParameters[.width], let widthBarItem = barItem as? CanSetWidth {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user