Skip to content

Commit

Permalink
patch. work on macos version >=10.12.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Toxblh committed Apr 9, 2018
1 parent 2dc1148 commit 59a8bb6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
4 changes: 2 additions & 2 deletions MTMR.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.13;
MACOSX_DEPLOYMENT_TARGET = 10.12;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx;
Expand Down Expand Up @@ -457,7 +457,7 @@
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.13;
MACOSX_DEPLOYMENT_TARGET = 10.12;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = macosx;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
Expand Down
6 changes: 5 additions & 1 deletion MTMR/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {


func applicationDidFinishLaunching(_ aNotification: Notification) {
TouchBarController.shared.setupControlStripPresence()
if #available(OSX 10.12.2, *) {
TouchBarController.shared.setupControlStripPresence()
} else {
// Fallback on earlier versions
}
// Insert code here to initialize your application
}

Expand Down
55 changes: 28 additions & 27 deletions MTMR/TouchBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,33 @@

import Cocoa

@available(OSX 10.12.2, *)
class TouchBarController: NSObject, NSTouchBarDelegate {

static let shared = TouchBarController()

let touchBar = NSTouchBar()

var timer = Timer()
var timeButton: NSButton = NSButton()

private override init() {
super.init()
touchBar.delegate = self
touchBar.defaultItemIdentifiers = [
.escButton,
.dismissButton,

.brightDown,
.brightUp,

.prev,
.play,
.next,

.sleep,
.weather,

.volumeDown,
.volumeUp,
.battery,
Expand All @@ -50,19 +51,19 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
DFRElementSetControlStripPresenceForIdentifier(.controlStripItem, true)
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.updateTime), userInfo: nil, repeats: true)
}

func updateControlStripPresence() {
DFRElementSetControlStripPresenceForIdentifier(.controlStripItem, true)
}

@objc private func presentTouchBar() {
NSTouchBar.presentSystemModalFunctionBar(touchBar, placement: 1, systemTrayItemIdentifier: .controlStripItem)
}

@objc private func dismissTouchBar() {
NSTouchBar.minimizeSystemModalFunctionBar(touchBar)
}

func touchBar(_ touchBar: NSTouchBar, makeItemForIdentifier identifier: NSTouchBarItem.Identifier) -> NSTouchBarItem? {
switch identifier {
case .escButton:
Expand All @@ -73,7 +74,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
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))
Expand All @@ -91,7 +92,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
let item = NSCustomTouchBarItem(identifier: identifier)
item.view = NSButton(title: "🔊", target: self, action: #selector(handleVolumeUp))
return item

case .prev:
let item = NSCustomTouchBarItem(identifier: identifier)
item.view = NSButton(title: "", target: self, action: #selector(handlePrev))
Expand All @@ -104,69 +105,69 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
let item = NSCustomTouchBarItem(identifier: identifier)
item.view = NSButton(title: "", target: self, action: #selector(handleNext))
return item

case .time:
let item = NSCustomTouchBarItem(identifier: identifier)
timeButton = NSButton(title: self.getCurrentTime(), target: self, action: nil)
item.view = timeButton
return item

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()
}

@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#>) {
Expand Down
1 change: 1 addition & 0 deletions MTMR/TouchBarItems.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import Cocoa

@available(OSX 10.12.2, *)
extension NSTouchBarItem.Identifier {
static let escButton = NSTouchBarItem.Identifier("com.toxblh.mtmr.escButton")
static let dismissButton = NSTouchBarItem.Identifier("com.toxblh.mtmr.dismissButton")
Expand Down

0 comments on commit 59a8bb6

Please sign in to comment.