mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 17:38:38 +00:00
Merge branch 'master' into musicWidget
# Conflicts: # MTMR/CustomButtonTouchBarItem.swift
This commit is contained in:
commit
b29c74ccca
@ -41,6 +41,12 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
|||||||
TouchBarController.shared.createAndUpdatePreset()
|
TouchBarController.shared.createAndUpdatePreset()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@objc func toggleControlStrip(_ sender: Any?) {
|
||||||
|
TouchBarController.shared.controlStripState = !TouchBarController.shared.controlStripState
|
||||||
|
createMenu()
|
||||||
|
TouchBarController.shared.resetControlStrip()
|
||||||
|
}
|
||||||
|
|
||||||
@objc func openPreset(_ sender: Any?) {
|
@objc func openPreset(_ sender: Any?) {
|
||||||
let dialog = NSOpenPanel();
|
let dialog = NSOpenPanel();
|
||||||
|
|
||||||
@ -71,6 +77,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
|
|||||||
menu.addItem(withTitle: "Preferences", action: #selector(openPrefereces(_:)), keyEquivalent: ",")
|
menu.addItem(withTitle: "Preferences", action: #selector(openPrefereces(_:)), keyEquivalent: ",")
|
||||||
menu.addItem(withTitle: "Reload Preset", action: #selector(updatePreset(_:)), keyEquivalent: "r")
|
menu.addItem(withTitle: "Reload Preset", action: #selector(updatePreset(_:)), keyEquivalent: "r")
|
||||||
menu.addItem(withTitle: "Open Preset", action: #selector(openPreset(_:)), keyEquivalent: "O")
|
menu.addItem(withTitle: "Open Preset", action: #selector(openPreset(_:)), keyEquivalent: "O")
|
||||||
|
menu.addItem(withTitle: TouchBarController.shared.controlStripState ? "Hide Control Strip" : "Show Control Strip" , action: #selector(toggleControlStrip(_:)), keyEquivalent: "T")
|
||||||
menu.addItem(NSMenuItem.separator())
|
menu.addItem(NSMenuItem.separator())
|
||||||
menu.addItem(withTitle: "Quit", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q")
|
menu.addItem(withTitle: "Quit", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q")
|
||||||
statusItem.menu = menu
|
statusItem.menu = menu
|
||||||
|
|||||||
@ -10,7 +10,7 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem {
|
|||||||
super.init(identifier: identifier, title: "⏳", onTap: onTap, onLongTap: onLongTap)
|
super.init(identifier: identifier, title: "⏳", onTap: onTap, onLongTap: onLongTap)
|
||||||
self.forceHideConstraint = self.view.widthAnchor.constraint(equalToConstant: 0)
|
self.forceHideConstraint = self.view.widthAnchor.constraint(equalToConstant: 0)
|
||||||
guard let script = source.appleScript else {
|
guard let script = source.appleScript else {
|
||||||
button.title = "no script"
|
self.title = "no script"
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
self.script = script
|
self.script = script
|
||||||
@ -22,7 +22,7 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem {
|
|||||||
print(error?.description ?? "unknown error")
|
print(error?.description ?? "unknown error")
|
||||||
#endif
|
#endif
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.button.title = "error"
|
self.title = "error"
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -40,7 +40,7 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem {
|
|||||||
#endif
|
#endif
|
||||||
let scriptResult = self.execute()
|
let scriptResult = self.execute()
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
self.button.title = scriptResult
|
self.title = scriptResult
|
||||||
self.forceHideConstraint.isActive = scriptResult == ""
|
self.forceHideConstraint.isActive = scriptResult == ""
|
||||||
}
|
}
|
||||||
DispatchQueue.main.asyncAfter(deadline: .now() + self.interval) { [weak self] in
|
DispatchQueue.main.asyncAfter(deadline: .now() + self.interval) { [weak self] in
|
||||||
|
|||||||
@ -10,8 +10,6 @@
|
|||||||
|
|
||||||
@interface MediaKeys : NSObject
|
@interface MediaKeys : NSObject
|
||||||
|
|
||||||
+ (void)decreaseVolume;
|
+ (void)HIDPostAuxKey:(UInt8)keyCode;
|
||||||
+ (void)increaseVolume;
|
|
||||||
+ (void)muteVolume;
|
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
@ -31,7 +31,7 @@ static io_connect_t get_event_driver(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void HIDPostAuxKey( const UInt8 auxKeyCode )
|
static void HIDReleaseAuxKey( const UInt8 auxKeyCode )
|
||||||
{
|
{
|
||||||
NXEventData event;
|
NXEventData event;
|
||||||
kern_return_t kr;
|
kern_return_t kr;
|
||||||
@ -52,16 +52,8 @@ static void HIDPostAuxKey( const UInt8 auxKeyCode )
|
|||||||
kr = IOHIDPostEvent( get_event_driver(), NX_SYSDEFINED, loc, &event, kNXEventDataVersion, 0, FALSE );
|
kr = IOHIDPostEvent( get_event_driver(), NX_SYSDEFINED, loc, &event, kNXEventDataVersion, 0, FALSE );
|
||||||
}
|
}
|
||||||
|
|
||||||
+ (void)decreaseVolume {
|
+ (void)HIDPostAuxKey: (UInt8)keyCode {
|
||||||
HIDPostAuxKey(NX_KEYTYPE_SOUND_DOWN);
|
HIDReleaseAuxKey(keyCode);
|
||||||
}
|
|
||||||
|
|
||||||
+ (void)increaseVolume {
|
|
||||||
HIDPostAuxKey(NX_KEYTYPE_SOUND_UP);
|
|
||||||
}
|
|
||||||
|
|
||||||
+ (void)muteVolume {
|
|
||||||
HIDPostAuxKey(NX_KEYTYPE_MUTE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
@ -9,9 +9,9 @@
|
|||||||
import Cocoa
|
import Cocoa
|
||||||
|
|
||||||
class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegate {
|
class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegate {
|
||||||
public var tapClosure: (() -> ())?
|
var tapClosure: (() -> ())?
|
||||||
public var longTapClosure: (() -> ())?
|
var longTapClosure: (() -> ())?
|
||||||
private(set) var button: NSButton!
|
private(set) var button: NSButton! //todo hide completely
|
||||||
|
|
||||||
private var singleClick: NSClickGestureRecognizer!
|
private var singleClick: NSClickGestureRecognizer!
|
||||||
private var longClick: NSPressGestureRecognizer!
|
private var longClick: NSPressGestureRecognizer!
|
||||||
@ -19,9 +19,10 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat
|
|||||||
init(identifier: NSTouchBarItem.Identifier, title: String, onTap callback: @escaping () -> (), onLongTap callbackLong: @escaping () -> (), bezelColor: NSColor? = .clear) {
|
init(identifier: NSTouchBarItem.Identifier, title: String, onTap callback: @escaping () -> (), onLongTap callbackLong: @escaping () -> (), bezelColor: NSColor? = .clear) {
|
||||||
self.tapClosure = callback
|
self.tapClosure = callback
|
||||||
self.longTapClosure = callbackLong
|
self.longTapClosure = callbackLong
|
||||||
|
self.attributedTitle = title.defaultTouchbarAttributedString
|
||||||
|
|
||||||
super.init(identifier: identifier)
|
super.init(identifier: identifier)
|
||||||
installButton(titled: title, bordered: true, backgroundColor: nil)
|
button = CustomHeightButton(title: title, target: nil, action: nil)
|
||||||
|
|
||||||
longClick = NSPressGestureRecognizer(target: self, action: #selector(handleGestureLong))
|
longClick = NSPressGestureRecognizer(target: self, action: #selector(handleGestureLong))
|
||||||
longClick.allowedTouchTypes = .direct
|
longClick.allowedTouchTypes = .direct
|
||||||
@ -31,8 +32,8 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat
|
|||||||
singleClick.allowedTouchTypes = .direct
|
singleClick.allowedTouchTypes = .direct
|
||||||
singleClick.delegate = self
|
singleClick.delegate = self
|
||||||
|
|
||||||
self.view.addGestureRecognizer(longClick)
|
reinstallButton()
|
||||||
self.view.addGestureRecognizer(singleClick)
|
button.attributedTitle = attributedTitle
|
||||||
}
|
}
|
||||||
|
|
||||||
required init?(coder: NSCoder) {
|
required init?(coder: NSCoder) {
|
||||||
@ -41,30 +42,50 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat
|
|||||||
|
|
||||||
var isBordered: Bool = true {
|
var isBordered: Bool = true {
|
||||||
didSet {
|
didSet {
|
||||||
installButton(titled: self.button.title, bordered: isBordered, backgroundColor: backgroundColor)
|
reinstallButton()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var backgroundColor: NSColor? {
|
var backgroundColor: NSColor? {
|
||||||
didSet {
|
didSet {
|
||||||
installButton(titled: self.button.title, bordered: isBordered, backgroundColor: backgroundColor)
|
reinstallButton()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func installButton(titled title: String, bordered: Bool, backgroundColor: NSColor?) {
|
var title: String {
|
||||||
button = CustomHeightButton(title: title, target: nil, action: nil)
|
get {
|
||||||
let cell = CustomButtonCell()
|
return self.attributedTitle.string
|
||||||
|
}
|
||||||
|
set {
|
||||||
|
self.attributedTitle = newValue.defaultTouchbarAttributedString
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var attributedTitle: NSAttributedString {
|
||||||
|
didSet {
|
||||||
|
self.button?.attributedTitle = attributedTitle
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func reinstallButton() {
|
||||||
|
let title = button.attributedTitle
|
||||||
|
let image = button.image
|
||||||
|
let cell = CustomButtonCell(parentItem: self)
|
||||||
button.cell = cell
|
button.cell = cell
|
||||||
if let color = backgroundColor {
|
if let color = backgroundColor {
|
||||||
cell.isBordered = true
|
cell.isBordered = true
|
||||||
button.bezelColor = color
|
button.bezelColor = color
|
||||||
cell.backgroundColor = color
|
cell.backgroundColor = color
|
||||||
} else {
|
} else {
|
||||||
button.isBordered = bordered
|
button.isBordered = isBordered
|
||||||
button.bezelStyle = bordered ? .rounded : .inline
|
button.bezelStyle = isBordered ? .rounded : .inline
|
||||||
}
|
}
|
||||||
button.title = title
|
button.attributedTitle = title
|
||||||
|
button.image = image
|
||||||
self.view = button
|
self.view = button
|
||||||
|
|
||||||
|
self.view.addGestureRecognizer(longClick)
|
||||||
|
self.view.addGestureRecognizer(singleClick)
|
||||||
}
|
}
|
||||||
|
|
||||||
func gestureRecognizer(_ gestureRecognizer: NSGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: NSGestureRecognizer) -> Bool {
|
func gestureRecognizer(_ gestureRecognizer: NSGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: NSGestureRecognizer) -> Bool {
|
||||||
@ -117,15 +138,21 @@ class CustomHeightButton : NSButton {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class CustomButtonCell: NSButtonCell {
|
class CustomButtonCell: NSButtonCell {
|
||||||
|
weak var parentItem: CustomButtonTouchBarItem?
|
||||||
|
|
||||||
init() {
|
init(parentItem: CustomButtonTouchBarItem) {
|
||||||
super.init(textCell: "")
|
super.init(textCell: "")
|
||||||
|
self.parentItem = parentItem
|
||||||
}
|
}
|
||||||
|
|
||||||
override func highlight(_ flag: Bool, withFrame cellFrame: NSRect, in controlView: NSView) {
|
override func highlight(_ flag: Bool, withFrame cellFrame: NSRect, in controlView: NSView) {
|
||||||
super.highlight(flag, withFrame: cellFrame, in: controlView)
|
super.highlight(flag, withFrame: cellFrame, in: controlView)
|
||||||
if !self.isBordered {
|
if !self.isBordered {
|
||||||
self.setTitle(self.title, withColor: flag ? .lightGray : .white)
|
if flag {
|
||||||
|
self.setAttributedTitle(self.attributedTitle, withColor: .lightGray)
|
||||||
|
} else if let parentItem = self.parentItem {
|
||||||
|
self.attributedTitle = parentItem.attributedTitle
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -133,22 +160,19 @@ class CustomButtonCell: NSButtonCell {
|
|||||||
fatalError("init(coder:) has not been implemented")
|
fatalError("init(coder:) has not been implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
override var title: String! {
|
func setAttributedTitle(_ title: NSAttributedString, withColor color: NSColor) {
|
||||||
get {
|
let attrTitle = NSMutableAttributedString(attributedString: title)
|
||||||
return self.attributedTitle.string
|
attrTitle.addAttributes([.foregroundColor: color], range: NSRange(location: 0, length: attrTitle.length))
|
||||||
}
|
|
||||||
|
|
||||||
set (newTitle) {
|
|
||||||
setTitle(newTitle, withColor: .white)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func setTitle(_ title: String, withColor color: NSColor) {
|
|
||||||
let attrTitle = NSMutableAttributedString(string: title as String, attributes: [.foregroundColor: color, .font: NSFont.systemFont(ofSize: 15, weight: .regular), .baselineOffset: 1])
|
|
||||||
attrTitle.setAlignment(.center, range: NSRange(location: 0, length: title.count))
|
|
||||||
|
|
||||||
self.attributedTitle = attrTitle
|
self.attributedTitle = attrTitle
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension String {
|
||||||
|
var defaultTouchbarAttributedString: NSAttributedString {
|
||||||
|
let attrTitle = NSMutableAttributedString(string: self, attributes: [.foregroundColor: NSColor.white, .font: NSFont.systemFont(ofSize: 15, weight: .regular), .baselineOffset: 1])
|
||||||
|
attrTitle.setAlignment(.center, range: NSRange(location: 0, length: self.count))
|
||||||
|
return attrTitle
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@ -47,11 +47,11 @@ class SupportedTypesHolder {
|
|||||||
"delete": { _ in return (item: .staticButton(title: "del"), action: .keyPress(keycode: 117), longAction: .none, parameters: [:])},
|
"delete": { _ in return (item: .staticButton(title: "del"), action: .keyPress(keycode: 117), longAction: .none, parameters: [:])},
|
||||||
"brightnessUp": { _ in
|
"brightnessUp": { _ in
|
||||||
let imageParameter = GeneralParameter.image(source: #imageLiteral(resourceName: "brightnessUp"))
|
let imageParameter = GeneralParameter.image(source: #imageLiteral(resourceName: "brightnessUp"))
|
||||||
return (item: .staticButton(title: ""), action: .keyPress(keycode: 113), longAction: .none, parameters: [.image: imageParameter])
|
return (item: .staticButton(title: ""), action: .keyPress(keycode: 144), longAction: .none, parameters: [.image: imageParameter])
|
||||||
},
|
},
|
||||||
"brightnessDown": { _ in
|
"brightnessDown": { _ in
|
||||||
let imageParameter = GeneralParameter.image(source: #imageLiteral(resourceName: "brightnessDown"))
|
let imageParameter = GeneralParameter.image(source: #imageLiteral(resourceName: "brightnessDown"))
|
||||||
return (item: .staticButton(title: ""), action: .keyPress(keycode: 107), longAction: .none, parameters: [.image: imageParameter])
|
return (item: .staticButton(title: ""), action: .keyPress(keycode: 145), longAction: .none, parameters: [.image: imageParameter])
|
||||||
},
|
},
|
||||||
"volumeDown": { _ in
|
"volumeDown": { _ in
|
||||||
let imageParameter = GeneralParameter.image(source: NSImage(named: .touchBarVolumeDownTemplate)!)
|
let imageParameter = GeneralParameter.image(source: NSImage(named: .touchBarVolumeDownTemplate)!)
|
||||||
|
|||||||
@ -29,24 +29,7 @@ extension KeyPress {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func doKey(_ key: UInt16, down: Bool) {
|
|
||||||
let ev = NSEvent.keyEvent(
|
|
||||||
with: down ? .keyDown : .keyUp,
|
|
||||||
location: .zero,
|
|
||||||
modifierFlags: [],
|
|
||||||
timestamp: TimeInterval(0),
|
|
||||||
windowNumber: 0,
|
|
||||||
context: nil,
|
|
||||||
characters: "",
|
|
||||||
charactersIgnoringModifiers: "",
|
|
||||||
isARepeat: false,
|
|
||||||
keyCode: key)
|
|
||||||
let cev = ev!.cgEvent!
|
|
||||||
cev.post(tap: CGEventTapLocation(rawValue: 0)!)
|
|
||||||
}
|
|
||||||
|
|
||||||
func HIDPostAuxKey(_ key: Int32) {
|
func HIDPostAuxKey(_ key: Int32) {
|
||||||
let key = UInt16(key)
|
let key = UInt8(key)
|
||||||
doKey(key, down: true)
|
MediaKeys.hidPostAuxKey(key)
|
||||||
doKey(key, down: false)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -63,6 +63,15 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
var scrollArea: NSCustomTouchBarItem?
|
var scrollArea: NSCustomTouchBarItem?
|
||||||
var centerScrollArea = NSTouchBarItem.Identifier("com.toxblh.mtmr.scrollArea.".appending(UUID().uuidString))
|
var centerScrollArea = NSTouchBarItem.Identifier("com.toxblh.mtmr.scrollArea.".appending(UUID().uuidString))
|
||||||
|
|
||||||
|
var controlStripState: Bool {
|
||||||
|
get {
|
||||||
|
return UserDefaults.standard.bool(forKey: "com.toxblh.mtmr.settings.showControlStrip")
|
||||||
|
}
|
||||||
|
set {
|
||||||
|
UserDefaults.standard.set(!controlStripState, forKey: "com.toxblh.mtmr.settings.showControlStrip")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private override init() {
|
private override init() {
|
||||||
super.init()
|
super.init()
|
||||||
SupportedTypesHolder.sharedInstance.register(typename: "exitTouchbar", item: .staticButton(title: "exit"), action: .custom(closure: { [weak self] in self?.dismissTouchBar()}), longAction: .none)
|
SupportedTypesHolder.sharedInstance.register(typename: "exitTouchbar", item: .staticButton(title: "exit"), action: .custom(closure: { [weak self] in self?.dismissTouchBar()}), longAction: .none)
|
||||||
@ -154,8 +163,17 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@objc private func presentTouchBar() {
|
@objc private func presentTouchBar() {
|
||||||
|
if self.controlStripState {
|
||||||
|
NSTouchBar.presentSystemModalFunctionBar(touchBar, systemTrayItemIdentifier: .controlStripItem)
|
||||||
|
} else {
|
||||||
NSTouchBar.presentSystemModalFunctionBar(touchBar, placement: 1, systemTrayItemIdentifier: .controlStripItem)
|
NSTouchBar.presentSystemModalFunctionBar(touchBar, placement: 1, systemTrayItemIdentifier: .controlStripItem)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@objc func resetControlStrip() {
|
||||||
|
NSTouchBar.minimizeSystemModalFunctionBar(self.touchBar)
|
||||||
|
presentTouchBar()
|
||||||
|
}
|
||||||
|
|
||||||
@objc private func dismissTouchBar() {
|
@objc private func dismissTouchBar() {
|
||||||
NSTouchBar.minimizeSystemModalFunctionBar(touchBar)
|
NSTouchBar.minimizeSystemModalFunctionBar(touchBar)
|
||||||
@ -212,21 +230,21 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
|
|||||||
barItem = MusicBarItem(identifier: identifier, interval: interval, onLongTap: longAction)
|
barItem = MusicBarItem(identifier: identifier, interval: interval, onLongTap: longAction)
|
||||||
}
|
}
|
||||||
|
|
||||||
if case .width(let value)? = item.additionalParameters[.width], let widthBarItem = barItem as? CanSetWidth {
|
|
||||||
widthBarItem.setWidth(value: value)
|
|
||||||
}
|
|
||||||
if case .bordered(let bordered)? = item.additionalParameters[.bordered], let item = barItem as? CustomButtonTouchBarItem {
|
if case .bordered(let bordered)? = item.additionalParameters[.bordered], let item = barItem as? CustomButtonTouchBarItem {
|
||||||
item.isBordered = bordered
|
item.isBordered = bordered
|
||||||
}
|
}
|
||||||
if case .background(let color)? = item.additionalParameters[.background], let item = barItem as? CustomButtonTouchBarItem {
|
if case .background(let color)? = item.additionalParameters[.background], let item = barItem as? CustomButtonTouchBarItem {
|
||||||
item.backgroundColor = color
|
item.backgroundColor = color
|
||||||
}
|
}
|
||||||
|
if case .width(let value)? = item.additionalParameters[.width], let widthBarItem = barItem as? CanSetWidth {
|
||||||
|
widthBarItem.setWidth(value: value)
|
||||||
|
}
|
||||||
if case .image(let source)? = item.additionalParameters[.image], let item = barItem as? CustomButtonTouchBarItem {
|
if case .image(let source)? = item.additionalParameters[.image], let item = barItem as? CustomButtonTouchBarItem {
|
||||||
let button = item.button!
|
let button = item.button!
|
||||||
button.imageScaling = .scaleProportionallyDown
|
button.imageScaling = .scaleProportionallyDown
|
||||||
button.imagePosition = .imageLeading
|
button.imagePosition = .imageLeading
|
||||||
|
|
||||||
if (button.title == "") {
|
if (item.title == "") {
|
||||||
button.imagePosition = .imageOnly
|
button.imagePosition = .imageOnly
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -10,20 +10,28 @@ import IOKit.ps
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
class BatteryBarItem: CustomButtonTouchBarItem {
|
class BatteryBarItem: CustomButtonTouchBarItem {
|
||||||
private var timer: Timer!
|
private let batteryInfo = BatteryInfo()
|
||||||
|
|
||||||
init(identifier: NSTouchBarItem.Identifier, onTap: @escaping () -> (), onLongTap: @escaping () -> ()) {
|
init(identifier: NSTouchBarItem.Identifier, onTap: @escaping () -> (), onLongTap: @escaping () -> ()) {
|
||||||
super.init(identifier: identifier, title: " ", onTap: onTap, onLongTap: onLongTap)
|
super.init(identifier: identifier, title: " ", onTap: onTap, onLongTap: onLongTap)
|
||||||
self.view = button
|
|
||||||
|
|
||||||
let batteryInfo = BatteryInfo(button: button)
|
batteryInfo.start { [weak self] in
|
||||||
batteryInfo.start()
|
self?.refresh()
|
||||||
batteryInfo.updateInfo()
|
}
|
||||||
|
self.refresh()
|
||||||
}
|
}
|
||||||
|
|
||||||
required init?(coder: NSCoder) {
|
required init?(coder: NSCoder) {
|
||||||
fatalError("init(coder:) has not been implemented")
|
fatalError("init(coder:) has not been implemented")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func refresh() {
|
||||||
|
self.attributedTitle = self.batteryInfo.formattedInfo()
|
||||||
|
}
|
||||||
|
|
||||||
|
deinit {
|
||||||
|
batteryInfo.stop()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class BatteryInfo: NSObject {
|
class BatteryInfo: NSObject {
|
||||||
@ -34,17 +42,11 @@ class BatteryInfo: NSObject {
|
|||||||
var isCharging: Bool = false
|
var isCharging: Bool = false
|
||||||
var ACPower: String = ""
|
var ACPower: String = ""
|
||||||
var timeRemaining: String = ""
|
var timeRemaining: String = ""
|
||||||
|
var notifyBlock: ()->() = {}
|
||||||
var button: NSButton
|
|
||||||
var loop:CFRunLoopSource?
|
var loop:CFRunLoopSource?
|
||||||
|
|
||||||
init(button: NSButton) {
|
func start(notifyBlock: @escaping ()->()) {
|
||||||
self.button = button
|
self.notifyBlock = notifyBlock
|
||||||
super.init()
|
|
||||||
self.start()
|
|
||||||
}
|
|
||||||
|
|
||||||
func start() {
|
|
||||||
let opaque = Unmanaged.passRetained(self).toOpaque()
|
let opaque = Unmanaged.passRetained(self).toOpaque()
|
||||||
let context = UnsafeMutableRawPointer(opaque)
|
let context = UnsafeMutableRawPointer(opaque)
|
||||||
loop = IOPSNotificationCreateRunLoopSource({ (context) in
|
loop = IOPSNotificationCreateRunLoopSource({ (context) in
|
||||||
@ -53,16 +55,17 @@ class BatteryInfo: NSObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let watcher = Unmanaged<BatteryInfo>.fromOpaque(ctx).takeUnretainedValue()
|
let watcher = Unmanaged<BatteryInfo>.fromOpaque(ctx).takeUnretainedValue()
|
||||||
watcher.updateInfo()
|
watcher.notifyBlock()
|
||||||
}, context).takeRetainedValue() as CFRunLoopSource
|
}, context).takeRetainedValue() as CFRunLoopSource
|
||||||
CFRunLoopAddSource(CFRunLoopGetCurrent(), loop, CFRunLoopMode.defaultMode)
|
CFRunLoopAddSource(CFRunLoopGetCurrent(), loop, CFRunLoopMode.defaultMode)
|
||||||
}
|
}
|
||||||
|
|
||||||
func stop() {
|
func stop() {
|
||||||
if !(self.loop != nil) {
|
self.notifyBlock = {}
|
||||||
|
guard let loop = self.loop else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), self.loop, CFRunLoopMode.defaultMode)
|
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), loop, CFRunLoopMode.defaultMode)
|
||||||
self.loop = nil
|
self.loop = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,7 +117,7 @@ class BatteryInfo: NSObject {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
public func updateInfo() {
|
public func formattedInfo() -> NSAttributedString {
|
||||||
var title = ""
|
var title = ""
|
||||||
self.getPSInfo()
|
self.getPSInfo()
|
||||||
|
|
||||||
@ -134,12 +137,11 @@ class BatteryInfo: NSObject {
|
|||||||
color = NSColor.red
|
color = NSColor.red
|
||||||
}
|
}
|
||||||
|
|
||||||
let regularFont = button.attributedTitle.attribute(.font, at: 0, effectiveRange: nil) as? NSFont ?? NSFont.systemFont(ofSize: 15)
|
let newTitle = NSMutableAttributedString(string: title as String, attributes: [.foregroundColor: color, .font: NSFont.systemFont(ofSize: 15), .baselineOffset: 1])
|
||||||
let newTitle = NSMutableAttributedString(string: title as String, attributes: [.foregroundColor: color, .font: regularFont, .baselineOffset: 1])
|
|
||||||
let newTitleSecond = NSMutableAttributedString(string: timeRemaining as String, attributes: [NSAttributedStringKey.foregroundColor: color, NSAttributedStringKey.font: NSFont.systemFont(ofSize: 8, weight: .regular), NSAttributedStringKey.baselineOffset: 7])
|
let newTitleSecond = NSMutableAttributedString(string: timeRemaining as String, attributes: [NSAttributedStringKey.foregroundColor: color, NSAttributedStringKey.font: NSFont.systemFont(ofSize: 8, weight: .regular), NSAttributedStringKey.baselineOffset: 7])
|
||||||
newTitle.append(newTitleSecond)
|
newTitle.append(newTitleSecond)
|
||||||
newTitle.setAlignment(.center, range: NSRange(location: 0, length: title.count))
|
newTitle.setAlignment(.center, range: NSRange(location: 0, length: title.count))
|
||||||
button.attributedTitle = newTitle
|
return newTitle
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -109,9 +109,9 @@ class CurrencyBarItem: CustomButtonTouchBarItem {
|
|||||||
|
|
||||||
let title = String(format: "%@%.2f", self.prefix, value)
|
let title = String(format: "%@%.2f", self.prefix, value)
|
||||||
|
|
||||||
let regularFont = button.attributedTitle.attribute(.font, at: 0, effectiveRange: nil) as? NSFont ?? NSFont.systemFont(ofSize: 15)
|
let regularFont = self.attributedTitle.attribute(.font, at: 0, effectiveRange: nil) as? NSFont ?? NSFont.systemFont(ofSize: 15)
|
||||||
let newTitle = NSMutableAttributedString(string: title as String, attributes: [.foregroundColor: color, .font: regularFont])
|
let newTitle = NSMutableAttributedString(string: title as String, attributes: [.foregroundColor: color, .font: regularFont])
|
||||||
newTitle.setAlignment(.center, range: NSRange(location: 0, length: title.count))
|
newTitle.setAlignment(.center, range: NSRange(location: 0, length: title.count))
|
||||||
button.attributedTitle = newTitle
|
self.attributedTitle = newTitle
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -58,8 +58,9 @@ class InputSourceBarItem: CustomButtonTouchBarItem {
|
|||||||
if (iconImage != nil) {
|
if (iconImage != nil) {
|
||||||
self.button.cell?.image = iconImage
|
self.button.cell?.image = iconImage
|
||||||
self.button.cell?.image?.size = buttonSize
|
self.button.cell?.image?.size = buttonSize
|
||||||
|
self.title = ""
|
||||||
} else {
|
} else {
|
||||||
self.button.title = currentSource.name
|
self.title = currentSource.name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -19,7 +19,7 @@ class TimeTouchBarItem: CustomButtonTouchBarItem {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@objc func updateTime() {
|
@objc func updateTime() {
|
||||||
button.title = self.dateFormatter.string(from: Date())
|
self.title = self.dateFormatter.string(from: Date())
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -117,7 +117,7 @@ class WeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func setWeather(text: String) {
|
func setWeather(text: String) {
|
||||||
button.title = text
|
self.title = text
|
||||||
}
|
}
|
||||||
|
|
||||||
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
|
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
|
||||||
|
|||||||
4
build.sh
4
build.sh
@ -16,7 +16,9 @@ cd Release
|
|||||||
rm -r App.xcarchive
|
rm -r App.xcarchive
|
||||||
|
|
||||||
# Prerequisite: npm i -g create-dmg
|
# Prerequisite: npm i -g create-dmg
|
||||||
create-dmg "${NAME}.app"
|
NAME_DMG="${NAME}.app"
|
||||||
|
echo $NAME_DMG
|
||||||
|
create-dmg $NAME_DMG
|
||||||
|
|
||||||
VERSION=`mdls -raw -name kMDItemVersion ${NAME}.app`
|
VERSION=`mdls -raw -name kMDItemVersion ${NAME}.app`
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user