forked from mrmekon/touchtest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtouchhorn.m
More file actions
145 lines (118 loc) · 4.96 KB
/
Copy pathtouchhorn.m
File metadata and controls
145 lines (118 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright 2018 Colin Houghton
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import <Cocoa/Cocoa.h>
static const NSTouchBarItemIdentifier kGroupButton = @"com.colout.touchhorn.group";
static const NSTouchBarItemIdentifier buttonMute = @"com.colout.touchhorn.b1";
static const NSTouchBarItemIdentifier buttonAirhornLow = @"com.colout.touchhorn.b2";
static const NSTouchBarItemIdentifier buttonAirhornMid = @"com.colout.touchhorn.b3";
static const NSTouchBarItemIdentifier buttonAirhornHigh = @"com.colout.touchhorn.b4";
static const NSTouchBarItemIdentifier scrubberVolume = @"com.colout.touchhorn.b6";
extern void DFRElementSetControlStripPresenceForIdentifier(NSString *, BOOL);
extern void DFRSystemModalShowsCloseBoxWhenFrontMost(BOOL);
NSSound * airhorn;
@interface NSTouchBarItem ()
+ (void)addSystemTrayItem:(NSTouchBarItem *)item;
@end
@interface NSTouchBar ()
+ (void)presentSystemModalFunctionBar:(NSTouchBar *)touchBar systemTrayItemIdentifier:(NSString *)identifier;
@end
@interface AppDelegate : NSObject <NSApplicationDelegate,NSTouchBarDelegate>
@end
NSTouchBar *_groupTouchBar;
@implementation AppDelegate
- (NSTouchBar *)groupTouchBar {
if (!_groupTouchBar) {
NSTouchBar *groupTouchBar = [[NSTouchBar alloc] init];
groupTouchBar.defaultItemIdentifiers = @[ buttonAirhornLow, buttonAirhornMid, buttonAirhornHigh, buttonMute, scrubberVolume ];
groupTouchBar.delegate = self;
_groupTouchBar = groupTouchBar;
}
return _groupTouchBar;
}
-(void)setGroupTouchBar: (NSTouchBar*)bar {
_groupTouchBar = bar;
}
- (void)airhornlow:(id)sender {
NSAppleScript* as = [[NSAppleScript alloc] initWithSource:@"set volume 1"];
[as executeAndReturnError:nil];
[airhorn stop];
[airhorn play];
}
- (void)airhornmid:(id)sender {
NSAppleScript* as = [[NSAppleScript alloc] initWithSource:@"set volume 3"];
[as executeAndReturnError:nil];
[airhorn stop];
[airhorn play];
}
- (void)airhornhigh:(id)sender {
NSAppleScript* as = [[NSAppleScript alloc] initWithSource:@"set volume 10"];
[as executeAndReturnError:nil];
[airhorn stop];
[airhorn play];
}
- (void)mute:(id)sender {
NSAppleScript* as = [[NSAppleScript alloc] initWithSource:@"set volume 0"];
[as executeAndReturnError:nil];
}
- (void)present:(id)sender {
[NSTouchBar presentSystemModalFunctionBar:self.groupTouchBar
systemTrayItemIdentifier:kGroupButton];
}
- (NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar
makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier
{
NSCustomTouchBarItem *item = nil;
if ([identifier isEqualToString:buttonAirhornLow]) {
item = [[NSCustomTouchBarItem alloc] initWithIdentifier:buttonAirhornLow];
item.view = [NSButton buttonWithTitle:@"\U0001F508 \U0001F4E3" target:self action:@selector(airhornlow:)];
}
else if ([identifier isEqualToString:buttonAirhornMid]) {
item = [[NSCustomTouchBarItem alloc] initWithIdentifier:buttonAirhornMid];
item.view = [NSButton buttonWithTitle:@"\U0001F509 \U0001F4E3" target:self action:@selector(airhornmid:)];
}
else if ([identifier isEqualToString:buttonAirhornHigh]) {
item = [[NSCustomTouchBarItem alloc] initWithIdentifier:buttonAirhornHigh];
item.view = [NSButton buttonWithTitle:@"\U0001F50A \U0001F4E3" target:self action:@selector(airhornhigh:)];
}
else if ([identifier isEqualToString:buttonMute]) {
item = [[NSCustomTouchBarItem alloc] initWithIdentifier:buttonMute];
item.view = [NSButton buttonWithTitle:@"\U0001F507" target:self action:@selector(mute:)];
}
return item;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
DFRSystemModalShowsCloseBoxWhenFrontMost(YES);
NSCustomTouchBarItem *item = [[NSCustomTouchBarItem alloc] initWithIdentifier:kGroupButton];
item.view = [NSButton buttonWithTitle:@"\U0001F4A9" target:self action:@selector(present:)];
[NSTouchBarItem addSystemTrayItem:item];
DFRElementSetControlStripPresenceForIdentifier(kGroupButton, YES);
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
[_groupTouchBar release];
_groupTouchBar = nil;
}
@end
int main(){
airhorn = [NSSound soundNamed:@"airhorn"];
[NSAutoreleasePool new];
[NSApplication sharedApplication];
AppDelegate *del = [[AppDelegate alloc] init];
[NSApp setDelegate: del];
[NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory];
[NSApp run];
return 0;
}