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#>) {