From ee0eab6a763e036de83d94a68bba588b5ae38b71 Mon Sep 17 00:00:00 2001 From: Serg Date: Mon, 9 Apr 2018 16:32:39 +0700 Subject: [PATCH 1/3] convenient keycode buttons --- MTMR/TouchBarController.swift | 84 +++++++++-------------------------- MTMR/TouchBarItems.swift | 19 ++++++++ 2 files changed, 40 insertions(+), 63 deletions(-) diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index faad755..c63d887 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -66,44 +66,28 @@ class TouchBarController: NSObject, NSTouchBarDelegate { func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? { switch identifier { case .escButton: - let item = NSCustomTouchBarItem(identifier: identifier) - item.view = NSButton(title: "esc", target: self, action: #selector(handleEsc)) - return item + return CustomButtonTouchBarItem(identifier: identifier, title: "esc", key: ESCKeyPress()) case .dismissButton: let item = NSCustomTouchBarItem(identifier: identifier) item.view = NSButton(title: "exit", target: self, action: #selector(dismissTouchBar)) return item case .brightUp: - let item = NSCustomTouchBarItem(identifier: identifier) - item.view = NSButton(title: "🔆", target: self, action: #selector(handleBrightUp)) - return item + return CustomButtonTouchBarItem(identifier: identifier, title: "🔆", key: BrightnessUpPress()) case .brightDown: - let item = NSCustomTouchBarItem(identifier: identifier) - item.view = NSButton(title: "🔅", target: self, action: #selector(handleBrightDown)) - return item + return CustomButtonTouchBarItem(identifier: identifier, title: "🔅", key: BrightnessDownPress()) case .volumeDown: - let item = NSCustomTouchBarItem(identifier: identifier) - item.view = NSButton(title: "🔉", target: self, action: #selector(handleVolumeDown)) - return item + return CustomButtonTouchBarItem(identifier: identifier, title: "🔉", HIDKeycode: NX_KEYTYPE_SOUND_DOWN) case .volumeUp: - let item = NSCustomTouchBarItem(identifier: identifier) - item.view = NSButton(title: "🔊", target: self, action: #selector(handleVolumeUp)) - return item + return CustomButtonTouchBarItem(identifier: identifier, title: "🔊", HIDKeycode: NX_KEYTYPE_SOUND_UP) case .prev: - let item = NSCustomTouchBarItem(identifier: identifier) - item.view = NSButton(title: "⏪", target: self, action: #selector(handlePrev)) - return item + return CustomButtonTouchBarItem(identifier: identifier, title: "⏪", HIDKeycode: NX_KEYTYPE_PREVIOUS) case .play: - let item = NSCustomTouchBarItem(identifier: identifier) - item.view = NSButton(title: "⏯", target: self, action: #selector(handlePlay)) - return item + return CustomButtonTouchBarItem(identifier: identifier, title: "⏯", HIDKeycode: NX_KEYTYPE_PLAY) case .next: - let item = NSCustomTouchBarItem(identifier: identifier) - item.view = NSButton(title: "⏩", target: self, action: #selector(handleNext)) - return item + return CustomButtonTouchBarItem(identifier: identifier, title: "⏩", HIDKeycode: NX_KEYTYPE_NEXT) case .time: let item = NSCustomTouchBarItem(identifier: identifier) @@ -128,45 +112,6 @@ class TouchBarController: NSObject, NSTouchBarDelegate { timeButton.title = getCurrentTime() } - @objc func handleEsc() { - let sender = ESCKeyPress() - sender.send() - } - - @objc func handleVolumeUp() { - HIDPostAuxKey(Int(NX_KEYTYPE_SOUND_UP)) - } - - @objc func handleVolumeDown() { - HIDPostAuxKey(Int(NX_KEYTYPE_SOUND_DOWN)) - } - - @objc func handleBrightDown() { -// HIDPostAuxKey(Int(NX_KEYTYPE_BRIGHTNESS_DOWN)) - - let sender = BrightnessUpPress() - sender.send() - } - - @objc func handleBrightUp() { -// HIDPostAuxKey(Int(NX_KEYTYPE_BRIGHTNESS_UP)) - - let sender = BrightnessDownPress() - sender.send() - } - - @objc func handlePrev() { - HIDPostAuxKey(Int(NX_KEYTYPE_PREVIOUS)) - } - - @objc func handlePlay() { - HIDPostAuxKey(Int(NX_KEYTYPE_PLAY)) - } - - @objc func handleNext() { - HIDPostAuxKey(Int(NX_KEYTYPE_NEXT)) - } - // func getBattery() { // var error: NSDictionary? // if let scriptObject = NSAppleScript(source: <#T##String#>) { @@ -180,3 +125,16 @@ class TouchBarController: NSObject, NSTouchBarDelegate { // } } + +extension CustomButtonTouchBarItem { + convenience init(identifier: NSTouchBarItem.Identifier, title: String, HIDKeycode: Int) { + self.init(identifier: identifier, title: title) { _ in + HIDPostAuxKey(HIDKeycode) + } + } + convenience init(identifier: NSTouchBarItem.Identifier, title: String, key: KeyPress) { + self.init(identifier: identifier, title: title) { _ in + key.send() + } + } +} diff --git a/MTMR/TouchBarItems.swift b/MTMR/TouchBarItems.swift index 5da58c0..af2ffcd 100644 --- a/MTMR/TouchBarItems.swift +++ b/MTMR/TouchBarItems.swift @@ -34,3 +34,22 @@ extension NSTouchBarItem.Identifier { static let controlStripItem = NSTouchBarItem.Identifier("com.toxblh.mtmr.controlStrip") } + +class CustomButtonTouchBarItem: NSCustomTouchBarItem { + let tapClosure: (NSCustomTouchBarItem) -> () + + init(identifier: NSTouchBarItem.Identifier, title: String, onTap callback: @escaping (NSCustomTouchBarItem) -> ()) { + self.tapClosure = callback + super.init(identifier: identifier) + self.view = NSButton(title: title, target: self, action: #selector(didTapped)) + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + @objc func didTapped() { + self.tapClosure(self) + } +} + From b3f461db4546bc3705c01a611cf9a7d61bfd989b Mon Sep 17 00:00:00 2001 From: Serg Date: Mon, 9 Apr 2018 17:07:25 +0700 Subject: [PATCH 2/3] time in separate file, divide bar in two parts --- MTMR.xcodeproj/project.pbxproj | 3 +++ MTMR/TimeTouchBarItem.swift | 25 +++++++++++++++++++++++++ MTMR/TouchBarController.swift | 23 +++-------------------- 3 files changed, 31 insertions(+), 20 deletions(-) create mode 100644 MTMR/TimeTouchBarItem.swift diff --git a/MTMR.xcodeproj/project.pbxproj b/MTMR.xcodeproj/project.pbxproj index 1660ec5..fa95f8f 100644 --- a/MTMR.xcodeproj/project.pbxproj +++ b/MTMR.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 36C2ECD7207B6DAE003CDA33 /* TimeTouchBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36C2ECD6207B6DAE003CDA33 /* TimeTouchBarItem.swift */; }; B059D622205E03F5006E6B86 /* TouchBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B059D621205E03F5006E6B86 /* TouchBarController.swift */; }; B059D624205E04F3006E6B86 /* TouchBarItems.swift in Sources */ = {isa = PBXBuildFile; fileRef = B059D623205E04F3006E6B86 /* TouchBarItems.swift */; }; B059D62D205F11E8006E6B86 /* DFRFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B059D62C205F11E8006E6B86 /* DFRFoundation.framework */; }; @@ -41,6 +42,7 @@ /* Begin PBXFileReference section */ 36C2ECD2207B3B1D003CDA33 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; + 36C2ECD6207B6DAE003CDA33 /* TimeTouchBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimeTouchBarItem.swift; sourceTree = ""; }; B059D621205E03F5006E6B86 /* TouchBarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TouchBarController.swift; sourceTree = ""; }; B059D623205E04F3006E6B86 /* TouchBarItems.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TouchBarItems.swift; sourceTree = ""; }; B059D629205E13E5006E6B86 /* TouchBarPrivateApi.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TouchBarPrivateApi.h; sourceTree = ""; }; @@ -132,6 +134,7 @@ B082B256205C7D8000BC04DC /* Assets.xcassets */, B0A7E9A9205D6AA400EEF070 /* KeyPress.swift */, B059D623205E04F3006E6B86 /* TouchBarItems.swift */, + 36C2ECD6207B6DAE003CDA33 /* TimeTouchBarItem.swift */, B059D621205E03F5006E6B86 /* TouchBarController.swift */, B0C1CFC9205C97D30021C862 /* WindowController.swift */, B082B258205C7D8000BC04DC /* Main.storyboard */, diff --git a/MTMR/TimeTouchBarItem.swift b/MTMR/TimeTouchBarItem.swift new file mode 100644 index 0000000..045db1c --- /dev/null +++ b/MTMR/TimeTouchBarItem.swift @@ -0,0 +1,25 @@ +import Cocoa + +class TimeTouchBarItem: NSCustomTouchBarItem { + private let dateFormatter = DateFormatter() + private var timer: Timer! + private let button = NSButton(title: "", target: nil, action: nil) + + init(identifier: NSTouchBarItem.Identifier, formatTemplate: String) { + dateFormatter.setLocalizedDateFormatFromTemplate(formatTemplate) + super.init(identifier: identifier) + timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true) + self.view = button + button.bezelColor = .clear + updateTime() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + @objc func updateTime() { + button.title = self.dateFormatter.string(from: Date()) + } + +} diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index c63d887..59d852e 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -14,9 +14,6 @@ class TouchBarController: NSObject, NSTouchBarDelegate { let touchBar = NSTouchBar() - var timer = Timer() - var timeButton: NSButton = NSButton() - private override init() { super.init() touchBar.delegate = self @@ -27,6 +24,8 @@ class TouchBarController: NSObject, NSTouchBarDelegate { .brightDown, .brightUp, + .flexibleSpace, + .prev, .play, .next, @@ -48,7 +47,6 @@ class TouchBarController: NSObject, NSTouchBarDelegate { item.view = NSButton(image: #imageLiteral(resourceName: "Strip"), target: self, action: #selector(presentTouchBar)) NSTouchBarItem.addSystemTrayItem(item) DFRElementSetControlStripPresenceForIdentifier(.controlStripItem, true) - timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.updateTime), userInfo: nil, repeats: true) } func updateControlStripPresence() { @@ -90,28 +88,13 @@ class TouchBarController: NSObject, NSTouchBarDelegate { return CustomButtonTouchBarItem(identifier: identifier, title: "⏩", HIDKeycode: NX_KEYTYPE_NEXT) case .time: - let item = NSCustomTouchBarItem(identifier: identifier) - timeButton = NSButton(title: self.getCurrentTime(), target: self, action: nil) - item.view = timeButton - return item + return TimeTouchBarItem(identifier: identifier, formatTemplate: "HH:mm") default: return nil } } - func getCurrentTime() -> String { - let date = Date() - let dateFormatter = DateFormatter() - dateFormatter.setLocalizedDateFormatFromTemplate("HH:mm") - let timestamp = dateFormatter.string(from: date) - return timestamp - } - - @objc func updateTime() { - timeButton.title = getCurrentTime() - } - // func getBattery() { // var error: NSDictionary? // if let scriptObject = NSAppleScript(source: <#T##String#>) { From d48d5904623074caff0a8b692ee8c51378065aa3 Mon Sep 17 00:00:00 2001 From: Serg Date: Mon, 9 Apr 2018 17:32:27 +0700 Subject: [PATCH 3/3] AppleScript and battery items --- MTMR.xcodeproj/project.pbxproj | 5 ++++ MTMR/AppleScriptTouchBarItem.swift | 37 ++++++++++++++++++++++++++++++ MTMR/TouchBarController.swift | 16 ++++--------- 3 files changed, 46 insertions(+), 12 deletions(-) create mode 100644 MTMR/AppleScriptTouchBarItem.swift diff --git a/MTMR.xcodeproj/project.pbxproj b/MTMR.xcodeproj/project.pbxproj index fa95f8f..edea1c7 100644 --- a/MTMR.xcodeproj/project.pbxproj +++ b/MTMR.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ 36C2ECD7207B6DAE003CDA33 /* TimeTouchBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36C2ECD6207B6DAE003CDA33 /* TimeTouchBarItem.swift */; }; + 36C2ECD9207B74B4003CDA33 /* AppleScriptTouchBarItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = 36C2ECD8207B74B4003CDA33 /* AppleScriptTouchBarItem.swift */; }; B059D622205E03F5006E6B86 /* TouchBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B059D621205E03F5006E6B86 /* TouchBarController.swift */; }; B059D624205E04F3006E6B86 /* TouchBarItems.swift in Sources */ = {isa = PBXBuildFile; fileRef = B059D623205E04F3006E6B86 /* TouchBarItems.swift */; }; B059D62D205F11E8006E6B86 /* DFRFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B059D62C205F11E8006E6B86 /* DFRFoundation.framework */; }; @@ -43,6 +44,7 @@ /* Begin PBXFileReference section */ 36C2ECD2207B3B1D003CDA33 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; 36C2ECD6207B6DAE003CDA33 /* TimeTouchBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimeTouchBarItem.swift; sourceTree = ""; }; + 36C2ECD8207B74B4003CDA33 /* AppleScriptTouchBarItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppleScriptTouchBarItem.swift; sourceTree = ""; }; B059D621205E03F5006E6B86 /* TouchBarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TouchBarController.swift; sourceTree = ""; }; B059D623205E04F3006E6B86 /* TouchBarItems.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TouchBarItems.swift; sourceTree = ""; }; B059D629205E13E5006E6B86 /* TouchBarPrivateApi.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TouchBarPrivateApi.h; sourceTree = ""; }; @@ -134,6 +136,7 @@ B082B256205C7D8000BC04DC /* Assets.xcassets */, B0A7E9A9205D6AA400EEF070 /* KeyPress.swift */, B059D623205E04F3006E6B86 /* TouchBarItems.swift */, + 36C2ECD8207B74B4003CDA33 /* AppleScriptTouchBarItem.swift */, 36C2ECD6207B6DAE003CDA33 /* TimeTouchBarItem.swift */, B059D621205E03F5006E6B86 /* TouchBarController.swift */, B0C1CFC9205C97D30021C862 /* WindowController.swift */, @@ -305,12 +308,14 @@ buildActionMask = 2147483647; files = ( B059D622205E03F5006E6B86 /* TouchBarController.swift in Sources */, + 36C2ECD9207B74B4003CDA33 /* AppleScriptTouchBarItem.swift in Sources */, B082B255205C7D8000BC04DC /* ViewController.swift in Sources */, B0C1CFCA205C97D30021C862 /* WindowController.swift in Sources */, B0F8771A207AC1EA00D6E430 /* TouchBarSupport.m in Sources */, B082B253205C7D8000BC04DC /* AppDelegate.swift in Sources */, B059D624205E04F3006E6B86 /* TouchBarItems.swift in Sources */, B0A7E9AA205D6AA400EEF070 /* KeyPress.swift in Sources */, + 36C2ECD7207B6DAE003CDA33 /* TimeTouchBarItem.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/MTMR/AppleScriptTouchBarItem.swift b/MTMR/AppleScriptTouchBarItem.swift new file mode 100644 index 0000000..7e10d53 --- /dev/null +++ b/MTMR/AppleScriptTouchBarItem.swift @@ -0,0 +1,37 @@ +import Foundation + +class AppleScriptTouchBarItem: NSCustomTouchBarItem { + let script: NSAppleScript + private var timer: Timer! + private let button = NSButton(title: "", target: nil, action: nil) + + init?(identifier: NSTouchBarItem.Identifier, appleScript: String, interval: TimeInterval) { + guard let script = NSAppleScript(source: appleScript) else { + return nil + } + self.script = script + super.init(identifier: identifier) + timer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(refresh), userInfo: nil, repeats: true) + self.view = button + button.bezelColor = .clear + refresh() + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + @objc func refresh() { + self.button.title = self.execute() + } + + func execute() -> String { + var error: NSDictionary? + let output = script.executeAndReturnError(&error) + if let error = error { + print(error) + return "error" + } + return output.stringValue ?? "empty value" + } +} diff --git a/MTMR/TouchBarController.swift b/MTMR/TouchBarController.swift index 59d852e..34868ee 100644 --- a/MTMR/TouchBarController.swift +++ b/MTMR/TouchBarController.swift @@ -87,6 +87,10 @@ class TouchBarController: NSObject, NSTouchBarDelegate { case .next: return CustomButtonTouchBarItem(identifier: identifier, title: "⏩", HIDKeycode: NX_KEYTYPE_NEXT) + case .battery: + let url = Bundle.main.url(forResource: "battery", withExtension: "scpt")! + let script = try! String.init(contentsOf: url) + return AppleScriptTouchBarItem(identifier: identifier, appleScript: script, interval: 60) case .time: return TimeTouchBarItem(identifier: identifier, formatTemplate: "HH:mm") @@ -94,18 +98,6 @@ class TouchBarController: NSObject, NSTouchBarDelegate { return nil } } - -// func getBattery() { -// var error: NSDictionary? -// if let scriptObject = NSAppleScript(source: <#T##String#>) { -// if let output: NSAppleEventDescriptor = scriptObject.executeAndReturnError( -// &error) { -// print(output.stringValue) -// } else if (error != nil) { -// print("error: \(error)") -// } -// } -// } }