8
8
9
9
import Cocoa
10
10
11
+ @available ( OSX 10 . 12 . 2 , * )
11
12
class TouchBarController : NSObject , NSTouchBarDelegate {
12
13
13
14
static let shared = TouchBarController ( )
14
-
15
+
15
16
let touchBar = NSTouchBar ( )
16
-
17
+
17
18
var timer = Timer ( )
18
19
var timeButton : NSButton = NSButton ( )
19
-
20
+
20
21
private override init ( ) {
21
22
super. init ( )
22
23
touchBar. delegate = self
23
24
touchBar. defaultItemIdentifiers = [
24
25
. escButton,
25
26
. dismissButton,
26
-
27
+
27
28
. brightDown,
28
29
. brightUp,
29
-
30
+
30
31
. prev,
31
32
. play,
32
33
. next,
33
-
34
+
34
35
. sleep,
35
36
. weather,
36
-
37
+
37
38
. volumeDown,
38
39
. volumeUp,
39
40
. battery,
@@ -50,19 +51,19 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
50
51
DFRElementSetControlStripPresenceForIdentifier ( . controlStripItem, true )
51
52
timer = Timer . scheduledTimer ( timeInterval: 1 , target: self , selector: #selector( self . updateTime) , userInfo: nil , repeats: true )
52
53
}
53
-
54
+
54
55
func updateControlStripPresence( ) {
55
56
DFRElementSetControlStripPresenceForIdentifier ( . controlStripItem, true )
56
57
}
57
-
58
+
58
59
@objc private func presentTouchBar( ) {
59
60
NSTouchBar . presentSystemModalFunctionBar ( touchBar, placement: 1 , systemTrayItemIdentifier: . controlStripItem)
60
61
}
61
-
62
+
62
63
@objc private func dismissTouchBar( ) {
63
64
NSTouchBar . minimizeSystemModalFunctionBar ( touchBar)
64
65
}
65
-
66
+
66
67
func touchBar( _ touchBar: NSTouchBar , makeItemForIdentifier identifier: NSTouchBarItem . Identifier ) -> NSTouchBarItem ? {
67
68
switch identifier {
68
69
case . escButton:
@@ -73,7 +74,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
73
74
let item = NSCustomTouchBarItem ( identifier: identifier)
74
75
item. view = NSButton ( title: " exit " , target: self , action: #selector( dismissTouchBar) )
75
76
return item
76
-
77
+
77
78
case . brightUp:
78
79
let item = NSCustomTouchBarItem ( identifier: identifier)
79
80
item. view = NSButton ( title: " 🔆 " , target: self , action: #selector( handleBrightUp) )
@@ -91,7 +92,7 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
91
92
let item = NSCustomTouchBarItem ( identifier: identifier)
92
93
item. view = NSButton ( title: " 🔊 " , target: self , action: #selector( handleVolumeUp) )
93
94
return item
94
-
95
+
95
96
case . prev:
96
97
let item = NSCustomTouchBarItem ( identifier: identifier)
97
98
item. view = NSButton ( title: " ⏪ " , target: self , action: #selector( handlePrev) )
@@ -104,69 +105,69 @@ class TouchBarController: NSObject, NSTouchBarDelegate {
104
105
let item = NSCustomTouchBarItem ( identifier: identifier)
105
106
item. view = NSButton ( title: " ⏩ " , target: self , action: #selector( handleNext) )
106
107
return item
107
-
108
+
108
109
case . time:
109
110
let item = NSCustomTouchBarItem ( identifier: identifier)
110
111
timeButton = NSButton ( title: self . getCurrentTime ( ) , target: self , action: nil )
111
112
item. view = timeButton
112
113
return item
113
-
114
+
114
115
default :
115
116
return nil
116
117
}
117
118
}
118
-
119
+
119
120
func getCurrentTime( ) -> String {
120
121
let date = Date ( )
121
122
let dateFormatter = DateFormatter ( )
122
123
dateFormatter. setLocalizedDateFormatFromTemplate ( " HH:mm " )
123
124
let timestamp = dateFormatter. string ( from: date)
124
125
return timestamp
125
126
}
126
-
127
+
127
128
@objc func updateTime( ) {
128
129
timeButton. title = getCurrentTime ( )
129
130
}
130
-
131
+
131
132
@objc func handleEsc( ) {
132
133
let sender = ESCKeyPress ( )
133
134
sender. send ( )
134
135
}
135
-
136
+
136
137
@objc func handleVolumeUp( ) {
137
138
HIDPostAuxKey ( Int ( NX_KEYTYPE_SOUND_UP) )
138
139
}
139
-
140
+
140
141
@objc func handleVolumeDown( ) {
141
142
HIDPostAuxKey ( Int ( NX_KEYTYPE_SOUND_DOWN) )
142
143
}
143
-
144
+
144
145
@objc func handleBrightDown( ) {
145
146
// HIDPostAuxKey(Int(NX_KEYTYPE_BRIGHTNESS_DOWN))
146
-
147
+
147
148
let sender = BrightnessUpPress ( )
148
149
sender. send ( )
149
150
}
150
-
151
+
151
152
@objc func handleBrightUp( ) {
152
153
// HIDPostAuxKey(Int(NX_KEYTYPE_BRIGHTNESS_UP))
153
154
154
155
let sender = BrightnessDownPress ( )
155
156
sender. send ( )
156
157
}
157
-
158
+
158
159
@objc func handlePrev( ) {
159
160
HIDPostAuxKey ( Int ( NX_KEYTYPE_PREVIOUS) )
160
161
}
161
-
162
+
162
163
@objc func handlePlay( ) {
163
164
HIDPostAuxKey ( Int ( NX_KEYTYPE_PLAY) )
164
165
}
165
-
166
+
166
167
@objc func handleNext( ) {
167
168
HIDPostAuxKey ( Int ( NX_KEYTYPE_NEXT) )
168
169
}
169
-
170
+
170
171
// func getBattery() {
171
172
// var error: NSDictionary?
172
173
// if let scriptObject = NSAppleScript(source: <#T##String#>) {
0 commit comments