mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 09:28:38 +00:00
+ show input source
This commit is contained in:
parent
a513c0083a
commit
d632bd3ee7
@ -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 = "<group>"; };
|
||||
607EEA4A2087835F009DA5F0 /* WeatherBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeatherBarItem.swift; sourceTree = "<group>"; };
|
||||
607EEA4C2087A8DA009DA5F0 /* CurrencyBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrencyBarItem.swift; sourceTree = "<group>"; };
|
||||
60F7D453208CC31400ABF5D2 /* InputSourceBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InputSourceBarItem.swift; sourceTree = "<group>"; };
|
||||
B0008E542080286C003AD4DD /* SupportHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SupportHelpers.swift; sourceTree = "<group>"; };
|
||||
B04B7BB62087398C00C835D0 /* BatteryBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BatteryBarItem.swift; sourceTree = "<group>"; };
|
||||
B05600D22083E9BB00EB218D /* CustomSlider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomSlider.swift; sourceTree = "<group>"; };
|
||||
@ -181,6 +183,7 @@
|
||||
B04B7BB62087398C00C835D0 /* BatteryBarItem.swift */,
|
||||
607EEA4A2087835F009DA5F0 /* WeatherBarItem.swift */,
|
||||
607EEA4C2087A8DA009DA5F0 /* CurrencyBarItem.swift */,
|
||||
60F7D453208CC31400ABF5D2 /* InputSourceBarItem.swift */,
|
||||
);
|
||||
path = MTMR;
|
||||
sourceTree = "<group>";
|
||||
@ -352,6 +355,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 */,
|
||||
|
||||
76
MTMR/InputSourceBarItem.swift
Normal file
76
MTMR/InputSourceBarItem.swift
Normal file
@ -0,0 +1,76 @@
|
||||
//
|
||||
// InputSourceBarItem.swift
|
||||
// MTMR
|
||||
//
|
||||
// Created by Daniel Apatin on 22.04.2018.
|
||||
// Copyright © 2018 Anton Palgunov. All rights reserved.
|
||||
//
|
||||
|
||||
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)
|
||||
|
||||
self.view = button
|
||||
|
||||
observeIputSourceChangedNotification();
|
||||
|
||||
textInputSourceDidChange()
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
@objc public func textInputSourceDidChange() {
|
||||
let currentSource = TISCopyCurrentKeyboardInputSource().takeUnretainedValue()
|
||||
let ptrID = TISGetInputSourceProperty(currentSource as TISInputSource, kTISPropertyInputSourceID)
|
||||
let ID = unsafeBitCast(ptrID, to: CFString.self)
|
||||
|
||||
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)
|
||||
}
|
||||
// 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..<keyboardString.endIndex)!
|
||||
// let startingKeyboard = range.upperBound
|
||||
// let theKeyboardLayout = keyboardString[startingKeyboard ..< keyboardString.endIndex]
|
||||
// print("theKeyboardLayout ", theKeyboardLayout)
|
||||
// return String(theKeyboardLayout)
|
||||
// }
|
||||
|
||||
@objc public func observeIputSourceChangedNotification(){
|
||||
let callback: CFNotificationCallback = { center, observer, name, object, info in
|
||||
let mySelf = Unmanaged<InputSourceBarItem>.fromOpaque(observer!).takeUnretainedValue()
|
||||
mySelf.textInputSourceDidChange()
|
||||
}
|
||||
|
||||
CFNotificationCenterAddObserver(notificationCenter,
|
||||
UnsafeRawPointer(Unmanaged.passUnretained(self).toOpaque()),
|
||||
callback,
|
||||
kTISNotifySelectedKeyboardInputSourceChanged,
|
||||
nil,
|
||||
.deliverImmediately)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
if case .width(let value)? = item.additionalParameters[.width], let widthBarItem = barItem as? CanSetWidth {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user