From 21bc525a04ac0fa2cfe722dbef4f4ff5f90c1981 Mon Sep 17 00:00:00 2001 From: Serg Date: Thu, 10 May 2018 09:02:30 +0700 Subject: [PATCH 1/8] fix setting width for borderless buttons --- MTMR/TouchBarController.swift | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index bef8cf8..4a2e3e8 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -208,15 +208,15 @@ class TouchBarController: NSObject, NSTouchBarDelegate { barItem = InputSourceBarItem(identifier: identifier, onTap: action, 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 { item.isBordered = bordered } if case .background(let color)? = item.additionalParameters[.background], let item = barItem as? CustomButtonTouchBarItem { 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 { let button = item.button! button.imageScaling = .scaleProportionallyDown From 1da4e2795fcec90cafc83dea835005b9ad05ef4a Mon Sep 17 00:00:00 2001 From: Serg Date: Thu, 10 May 2018 09:31:37 +0700 Subject: [PATCH 2/8] fix taps for borderless buttons --- MTMR/CustomButtonTouchBarItem.swift | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/MTMR/CustomButtonTouchBarItem.swift b/MTMR/CustomButtonTouchBarItem.swift index c3ede21..5848892 100644 --- a/MTMR/CustomButtonTouchBarItem.swift +++ b/MTMR/CustomButtonTouchBarItem.swift @@ -21,7 +21,6 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat self.longTapClosure = callbackLong super.init(identifier: identifier) - installButton(titled: title, bordered: true, backgroundColor: nil) longClick = NSPressGestureRecognizer(target: self, action: #selector(handleGestureLong)) longClick.allowedTouchTypes = .direct @@ -31,8 +30,7 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat singleClick.allowedTouchTypes = .direct singleClick.delegate = self - self.view.addGestureRecognizer(longClick) - self.view.addGestureRecognizer(singleClick) + installButton(titled: title, bordered: true, backgroundColor: nil) } required init?(coder: NSCoder) { @@ -65,6 +63,9 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat } button.title = title self.view = button + + self.view.addGestureRecognizer(longClick) + self.view.addGestureRecognizer(singleClick) } func gestureRecognizer(_ gestureRecognizer: NSGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: NSGestureRecognizer) -> Bool { From 33efbbebaa3db7a9f55dc202ddd12997d65fef99 Mon Sep 17 00:00:00 2001 From: Toxblh Date: Thu, 10 May 2018 09:56:04 +0200 Subject: [PATCH 3/8] switch hidKeys to obj-c realesation --- MTMR/CBridge/TouchBarSupport.h | 4 +--- MTMR/CBridge/TouchBarSupport.m | 14 +++----------- MTMR/KeyPress.swift | 21 ++------------------- 3 files changed, 6 insertions(+), 33 deletions(-) diff --git a/MTMR/CBridge/TouchBarSupport.h b/MTMR/CBridge/TouchBarSupport.h index 0452f39..a9d3b3a 100644 --- a/MTMR/CBridge/TouchBarSupport.h +++ b/MTMR/CBridge/TouchBarSupport.h @@ -10,8 +10,6 @@ @interface MediaKeys : NSObject -+ (void)decreaseVolume; -+ (void)increaseVolume; -+ (void)muteVolume; ++ (void)HIDPostAuxKey:(UInt8)keyCode; @end diff --git a/MTMR/CBridge/TouchBarSupport.m b/MTMR/CBridge/TouchBarSupport.m index 6e92a5f..f6c4956 100644 --- a/MTMR/CBridge/TouchBarSupport.m +++ b/MTMR/CBridge/TouchBarSupport.m @@ -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; 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 ); } -+ (void)decreaseVolume { - HIDPostAuxKey(NX_KEYTYPE_SOUND_DOWN); -} - -+ (void)increaseVolume { - HIDPostAuxKey(NX_KEYTYPE_SOUND_UP); -} - -+ (void)muteVolume { - HIDPostAuxKey(NX_KEYTYPE_MUTE); ++ (void)HIDPostAuxKey: (UInt8)keyCode { + HIDReleaseAuxKey(keyCode); } @end diff --git a/MTMR/KeyPress.swift b/MTMR/KeyPress.swift index 23870dc..54766d9 100644 --- a/MTMR/KeyPress.swift +++ b/MTMR/KeyPress.swift @@ -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) { - let key = UInt16(key) - doKey(key, down: true) - doKey(key, down: false) + let key = UInt8(key) + MediaKeys.hidPostAuxKey(key) } From 9e10b2d3d1deb8edfaef1e356a7c99d6df404d77 Mon Sep 17 00:00:00 2001 From: Toxblh Date: Thu, 10 May 2018 10:11:50 +0200 Subject: [PATCH 4/8] upd. Build.sh --- build.sh | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/build.sh b/build.sh index 8871423..04340af 100755 --- a/build.sh +++ b/build.sh @@ -16,7 +16,9 @@ cd Release rm -r App.xcarchive # 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` From d46827832e8210bbf8283e4bebdf03ea864714b6 Mon Sep 17 00:00:00 2001 From: Serg Date: Fri, 11 May 2018 00:24:32 +0700 Subject: [PATCH 5/8] another try to fix button titles and images --- MTMR/AppleScriptTouchBarItem.swift | 6 +-- MTMR/CustomButtonTouchBarItem.swift | 73 ++++++++++++++++++--------- MTMR/TouchBarController.swift | 2 +- MTMR/Widgets/BatteryBarItem.swift | 46 +++++++++-------- MTMR/Widgets/InputSourceBarItem.swift | 3 +- MTMR/Widgets/TimeTouchBarItem.swift | 2 +- MTMR/Widgets/WeatherBarItem.swift | 2 +- 7 files changed, 80 insertions(+), 54 deletions(-) diff --git a/MTMR/AppleScriptTouchBarItem.swift b/MTMR/AppleScriptTouchBarItem.swift index ac66db7..9659a9b 100644 --- a/MTMR/AppleScriptTouchBarItem.swift +++ b/MTMR/AppleScriptTouchBarItem.swift @@ -10,7 +10,7 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem { super.init(identifier: identifier, title: "⏳", onTap: onTap, onLongTap: onLongTap) self.forceHideConstraint = self.view.widthAnchor.constraint(equalToConstant: 0) guard let script = source.appleScript else { - button.title = "no script" + self.title = "no script" return } self.script = script @@ -22,7 +22,7 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem { print(error?.description ?? "unknown error") #endif DispatchQueue.main.async { - self.button.title = "error" + self.title = "error" } return } @@ -40,7 +40,7 @@ class AppleScriptTouchBarItem: CustomButtonTouchBarItem { #endif let scriptResult = self.execute() DispatchQueue.main.async { - self.button.title = scriptResult + self.title = scriptResult self.forceHideConstraint.isActive = scriptResult == "" } DispatchQueue.main.asyncAfter(deadline: .now() + self.interval) { [weak self] in diff --git a/MTMR/CustomButtonTouchBarItem.swift b/MTMR/CustomButtonTouchBarItem.swift index 5848892..2db3971 100644 --- a/MTMR/CustomButtonTouchBarItem.swift +++ b/MTMR/CustomButtonTouchBarItem.swift @@ -19,8 +19,10 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat init(identifier: NSTouchBarItem.Identifier, title: String, onTap callback: @escaping () -> (), onLongTap callbackLong: @escaping () -> (), bezelColor: NSColor? = .clear) { self.tapClosure = callback self.longTapClosure = callbackLong + self.attributedTitle = title.defaultTouchbarAttributedString super.init(identifier: identifier) + button = CustomHeightButton(title: title, target: nil, action: nil) longClick = NSPressGestureRecognizer(target: self, action: #selector(handleGestureLong)) longClick.allowedTouchTypes = .direct @@ -30,7 +32,8 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat singleClick.allowedTouchTypes = .direct singleClick.delegate = self - installButton(titled: title, bordered: true, backgroundColor: nil) + reinstallButton() + button.attributedTitle = attributedTitle } required init?(coder: NSCoder) { @@ -39,29 +42,46 @@ class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegat var isBordered: Bool = true { didSet { - installButton(titled: self.button.title, bordered: isBordered, backgroundColor: backgroundColor) + reinstallButton() } } var backgroundColor: NSColor? { didSet { - installButton(titled: self.button.title, bordered: isBordered, backgroundColor: backgroundColor) + reinstallButton() } } - private func installButton(titled title: String, bordered: Bool, backgroundColor: NSColor?) { - button = CustomHeightButton(title: title, target: nil, action: nil) - let cell = CustomButtonCell() + var title: String { + get { + 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 if let color = backgroundColor { cell.isBordered = true button.bezelColor = color cell.backgroundColor = color } else { - button.isBordered = bordered - button.bezelStyle = bordered ? .rounded : .inline + button.isBordered = isBordered + button.bezelStyle = isBordered ? .rounded : .inline } - button.title = title + button.attributedTitle = title + button.image = image self.view = button self.view.addGestureRecognizer(longClick) @@ -118,15 +138,21 @@ class CustomHeightButton : NSButton { } class CustomButtonCell: NSButtonCell { + weak var parentItem: CustomButtonTouchBarItem? - init() { + init(parentItem: CustomButtonTouchBarItem) { super.init(textCell: "") + self.parentItem = parentItem } override func highlight(_ flag: Bool, withFrame cellFrame: NSRect, in controlView: NSView) { super.highlight(flag, withFrame: cellFrame, in: controlView) 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 + } } } @@ -134,22 +160,19 @@ class CustomButtonCell: NSButtonCell { fatalError("init(coder:) has not been implemented") } - override var title: String! { - get { - return self.attributedTitle.string - } - - 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)) - + func setAttributedTitle(_ title: NSAttributedString, withColor color: NSColor) { + let attrTitle = NSMutableAttributedString(attributedString: title) + attrTitle.addAttributes([.foregroundColor: color], range: NSRange(location: 0, length: attrTitle.length)) 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 + } +} + diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 4a2e3e8..b981a29 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -222,7 +222,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate { button.imageScaling = .scaleProportionallyDown button.imagePosition = .imageLeading - if (button.title == "") { + if (item.title == "") { button.imagePosition = .imageOnly } diff --git a/MTMR/Widgets/BatteryBarItem.swift b/MTMR/Widgets/BatteryBarItem.swift index 6d07354..02cd2c7 100644 --- a/MTMR/Widgets/BatteryBarItem.swift +++ b/MTMR/Widgets/BatteryBarItem.swift @@ -10,20 +10,28 @@ import IOKit.ps import Foundation class BatteryBarItem: CustomButtonTouchBarItem { - private var timer: Timer! + private let batteryInfo = BatteryInfo() init(identifier: NSTouchBarItem.Identifier, onTap: @escaping () -> (), onLongTap: @escaping () -> ()) { super.init(identifier: identifier, title: " ", onTap: onTap, onLongTap: onLongTap) - self.view = button - let batteryInfo = BatteryInfo(button: button) - batteryInfo.start() - batteryInfo.updateInfo() + batteryInfo.start { [weak self] in + self?.refresh() + } + self.refresh() } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } + + func refresh() { + self.attributedTitle = self.batteryInfo.formattedInfo() + } + + deinit { + batteryInfo.stop() + } } class BatteryInfo: NSObject { @@ -34,17 +42,11 @@ class BatteryInfo: NSObject { var isCharging: Bool = false var ACPower: String = "" var timeRemaining: String = "" - - var button: NSButton + var notifyBlock: ()->() = {} var loop:CFRunLoopSource? - init(button: NSButton) { - self.button = button - super.init() - self.start() - } - - func start() { + func start(notifyBlock: @escaping ()->()) { + self.notifyBlock = notifyBlock let opaque = Unmanaged.passRetained(self).toOpaque() let context = UnsafeMutableRawPointer(opaque) loop = IOPSNotificationCreateRunLoopSource({ (context) in @@ -53,16 +55,17 @@ class BatteryInfo: NSObject { } let watcher = Unmanaged.fromOpaque(ctx).takeUnretainedValue() - watcher.updateInfo() - }, context).takeRetainedValue() as CFRunLoopSource + watcher.notifyBlock() + }, context).takeRetainedValue() as CFRunLoopSource CFRunLoopAddSource(CFRunLoopGetCurrent(), loop, CFRunLoopMode.defaultMode) } func stop() { - if !(self.loop != nil) { + self.notifyBlock = {} + guard let loop = self.loop else { return } - CFRunLoopRemoveSource(CFRunLoopGetCurrent(), self.loop, CFRunLoopMode.defaultMode) + CFRunLoopRemoveSource(CFRunLoopGetCurrent(), loop, CFRunLoopMode.defaultMode) self.loop = nil } @@ -114,7 +117,7 @@ class BatteryInfo: NSObject { return "" } - public func updateInfo() { + public func formattedInfo() -> NSAttributedString { var title = "" self.getPSInfo() @@ -134,12 +137,11 @@ class BatteryInfo: NSObject { 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: regularFont, .baselineOffset: 1]) + let newTitle = NSMutableAttributedString(string: title as String, attributes: [.foregroundColor: color, .font: NSFont.systemFont(ofSize: 15), .baselineOffset: 1]) 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.setAlignment(.center, range: NSRange(location: 0, length: title.count)) - button.attributedTitle = newTitle + return newTitle } } diff --git a/MTMR/Widgets/InputSourceBarItem.swift b/MTMR/Widgets/InputSourceBarItem.swift index f32abd0..1d9f49d 100644 --- a/MTMR/Widgets/InputSourceBarItem.swift +++ b/MTMR/Widgets/InputSourceBarItem.swift @@ -58,8 +58,9 @@ class InputSourceBarItem: CustomButtonTouchBarItem { if (iconImage != nil) { self.button.cell?.image = iconImage self.button.cell?.image?.size = buttonSize + self.title = "" } else { - self.button.title = currentSource.name + self.title = currentSource.name } } diff --git a/MTMR/Widgets/TimeTouchBarItem.swift b/MTMR/Widgets/TimeTouchBarItem.swift index 21d98d3..5785c52 100644 --- a/MTMR/Widgets/TimeTouchBarItem.swift +++ b/MTMR/Widgets/TimeTouchBarItem.swift @@ -19,7 +19,7 @@ class TimeTouchBarItem: CustomButtonTouchBarItem { } @objc func updateTime() { - button.title = self.dateFormatter.string(from: Date()) + self.title = self.dateFormatter.string(from: Date()) } } diff --git a/MTMR/Widgets/WeatherBarItem.swift b/MTMR/Widgets/WeatherBarItem.swift index ef088a8..257febc 100644 --- a/MTMR/Widgets/WeatherBarItem.swift +++ b/MTMR/Widgets/WeatherBarItem.swift @@ -117,7 +117,7 @@ class WeatherBarItem: CustomButtonTouchBarItem, CLLocationManagerDelegate { } func setWeather(text: String) { - button.title = text + self.title = text } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { From 55177a7f900ed88297e09edbd2214a5e7455a544 Mon Sep 17 00:00:00 2001 From: Serg Date: Fri, 11 May 2018 09:32:42 +0700 Subject: [PATCH 6/8] fix currency widget updating --- MTMR/CustomButtonTouchBarItem.swift | 2 +- MTMR/Widgets/CurrencyBarItem.swift | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/MTMR/CustomButtonTouchBarItem.swift b/MTMR/CustomButtonTouchBarItem.swift index 2db3971..5804842 100644 --- a/MTMR/CustomButtonTouchBarItem.swift +++ b/MTMR/CustomButtonTouchBarItem.swift @@ -11,7 +11,7 @@ import Cocoa class CustomButtonTouchBarItem: NSCustomTouchBarItem, NSGestureRecognizerDelegate { private let tapClosure: (() -> ())? private let longTapClosure: (() -> ())? - private(set) var button: NSButton! + private(set) var button: NSButton! //todo hide completely private var singleClick: NSClickGestureRecognizer! private var longClick: NSPressGestureRecognizer! diff --git a/MTMR/Widgets/CurrencyBarItem.swift b/MTMR/Widgets/CurrencyBarItem.swift index 9e9137d..eddff4f 100644 --- a/MTMR/Widgets/CurrencyBarItem.swift +++ b/MTMR/Widgets/CurrencyBarItem.swift @@ -109,9 +109,9 @@ class CurrencyBarItem: CustomButtonTouchBarItem { 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]) newTitle.setAlignment(.center, range: NSRange(location: 0, length: title.count)) - button.attributedTitle = newTitle + self.attributedTitle = newTitle } } From 57e8af90ddab7acb624616dfbf9d977029728bc1 Mon Sep 17 00:00:00 2001 From: Toxblh Date: Fri, 11 May 2018 13:26:52 +0200 Subject: [PATCH 7/8] close #10 --- MTMR/ItemsParsing.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MTMR/ItemsParsing.swift b/MTMR/ItemsParsing.swift index 4cfe593..be75788 100644 --- a/MTMR/ItemsParsing.swift +++ b/MTMR/ItemsParsing.swift @@ -47,11 +47,11 @@ class SupportedTypesHolder { "delete": { _ in return (item: .staticButton(title: "del"), action: .keyPress(keycode: 117), longAction: .none, parameters: [:])}, "brightnessUp": { _ in 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 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 let imageParameter = GeneralParameter.image(source: NSImage(named: .touchBarVolumeDownTemplate)!) From 852df9d6b61e0b96bf891359c55c1f34b55369f8 Mon Sep 17 00:00:00 2001 From: ad Date: Fri, 11 May 2018 14:40:02 +0300 Subject: [PATCH 8/8] + toggle Control Strip --- MTMR/AppDelegate.swift | 7 +++++++ MTMR/TouchBarController.swift | 20 +++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/MTMR/AppDelegate.swift b/MTMR/AppDelegate.swift index 6c55260..9838b67 100644 --- a/MTMR/AppDelegate.swift +++ b/MTMR/AppDelegate.swift @@ -41,6 +41,12 @@ class AppDelegate: NSObject, NSApplicationDelegate { TouchBarController.shared.createAndUpdatePreset() } + @objc func toggleControlStrip(_ sender: Any?) { + TouchBarController.shared.controlStripState = !TouchBarController.shared.controlStripState + createMenu() + TouchBarController.shared.resetControlStrip() + } + @objc func openPreset(_ sender: Any?) { let dialog = NSOpenPanel(); @@ -71,6 +77,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { menu.addItem(withTitle: "Preferences", action: #selector(openPrefereces(_:)), keyEquivalent: ",") menu.addItem(withTitle: "Reload Preset", action: #selector(updatePreset(_:)), keyEquivalent: "r") 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(withTitle: "Quit", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q") statusItem.menu = menu diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index b981a29..a396add 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -61,6 +61,15 @@ class TouchBarController: NSObject, NSTouchBarDelegate { var scrollArea: NSCustomTouchBarItem? 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() { super.init() SupportedTypesHolder.sharedInstance.register(typename: "exitTouchbar", item: .staticButton(title: "exit"), action: .custom(closure: { [weak self] in self?.dismissTouchBar()}), longAction: .none) @@ -152,7 +161,16 @@ class TouchBarController: NSObject, NSTouchBarDelegate { } @objc private func presentTouchBar() { - NSTouchBar.presentSystemModalFunctionBar(touchBar, placement: 1, systemTrayItemIdentifier: .controlStripItem) + if self.controlStripState { + NSTouchBar.presentSystemModalFunctionBar(touchBar, systemTrayItemIdentifier: .controlStripItem) + } else { + NSTouchBar.presentSystemModalFunctionBar(touchBar, placement: 1, systemTrayItemIdentifier: .controlStripItem) + } + } + + @objc func resetControlStrip() { + NSTouchBar.minimizeSystemModalFunctionBar(self.touchBar) + presentTouchBar() } @objc private func dismissTouchBar() {