-
Notifications
You must be signed in to change notification settings - Fork 20
/
menuet.m
254 lines (230 loc) · 7.36 KB
/
menuet.m
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#import <Cocoa/Cocoa.h>
#import "NSImage+Resize.h"
#import "menuet.h"
void itemClicked(const char *);
void notificationRespond(const char *, const char *);
const char *children(const char *);
void menuClosed(const char *);
bool hideStartup();
bool runningAtStartup();
void toggleStartup();
void shutdownWait();
NSStatusItem *_statusItem;
@interface MenuetMenu : NSMenu <NSMenuDelegate>
@property(nonatomic, copy) NSString *unique;
@property(nonatomic, assign) BOOL root;
@property(nonatomic, assign) BOOL open;
@end
@implementation MenuetMenu
- (id)init {
self = [super init];
if (self) {
self.delegate = self;
self.autoenablesItems = false;
}
return self;
}
- (void)refreshVisibleMenus {
if (!self.open) {
return;
}
[self menuWillOpen:self];
for (NSMenuItem *item in self.itemArray) {
MenuetMenu *menu = (MenuetMenu *)item.submenu;
if (menu != NULL) {
[menu refreshVisibleMenus];
}
}
}
- (void)populate:(NSArray *)items {
for (int i = 0; i < items.count; i++) {
NSMenuItem *item = nil;
if (i < self.numberOfItems) {
item = [self itemAtIndex:i];
}
NSDictionary *dict = [items objectAtIndex:i];
NSString *type = dict[@"Type"];
if ([type isEqualTo:@"separator"]) {
if (!item || !item.isSeparatorItem) {
[self insertItem:[NSMenuItem separatorItem] atIndex:i];
}
continue;
}
NSString *unique = dict[@"Unique"];
NSString *text = dict[@"Text"];
NSString *imageName = dict[@"Image"];
NSNumber *fontSize = dict[@"FontSize"];
NSNumber *fontWeight = dict[@"FontWeight"];
BOOL state = [dict[@"State"] boolValue];
BOOL hasChildren = [dict[@"HasChildren"] boolValue];
BOOL clickable = [dict[@"Clickable"] boolValue];
if (!item || item.isSeparatorItem) {
item =
[self insertItemWithTitle:@"" action:nil keyEquivalent:@"" atIndex:i];
}
NSMutableDictionary *attributes = [NSMutableDictionary new];
float size = fontSize.floatValue;
if (fontSize == 0) {
size = 14;
}
attributes[NSFontAttributeName] =
[NSFont monospacedDigitSystemFontOfSize:size
weight:fontWeight.floatValue];
item.attributedTitle =
[[NSMutableAttributedString alloc] initWithString:text
attributes:attributes];
item.target = self;
if (clickable) {
item.action = @selector(press:);
item.representedObject = unique;
} else {
item.action = nil;
item.representedObject = nil;
}
if (state) {
item.state = NSControlStateValueOn;
} else {
item.state = NSControlStateValueOff;
}
if (hasChildren) {
if (!item.submenu) {
item.submenu = [MenuetMenu new];
}
MenuetMenu *menu = (MenuetMenu *)item.submenu;
menu.unique = unique;
} else if (item.submenu) {
item.submenu = nil;
}
item.enabled = clickable || hasChildren;
item.image = [NSImage imageFromName:imageName withHeight:16];
}
while (self.numberOfItems > items.count) {
[self removeItemAtIndex:self.numberOfItems - 1];
}
}
// The documentation says not to make changes here, but it seems to work.
// submenuAction does not appear to be called, and menuNeedsUpdate is only
// called once per tracking session.
- (void)menuWillOpen:(MenuetMenu *)menu {
if (self.root) {
// For the root menu, we generate a new unique every time it's opened. Go
// handles all other unique generation.
self.unique = [[[[NSProcessInfo processInfo] globallyUniqueString]
substringFromIndex:51] stringByAppendingString:@":root"];
}
const char *str = children(self.unique.UTF8String);
NSArray *items = @[];
if (str != NULL) {
items = [NSJSONSerialization
JSONObjectWithData:[[NSString stringWithUTF8String:str]
dataUsingEncoding:NSUTF8StringEncoding]
options:0
error:nil];
free((char *)str);
}
if (self.root) {
items = [items arrayByAddingObjectsFromArray:@[
@{@"Type" : @"separator",
@"Clickable" : @YES},
]];
if (!hideStartup()) {
items = [items arrayByAddingObjectsFromArray:@[
@{@"Text" : @"Start at Login",
@"Clickable" : @YES},
]];
}
items = [items arrayByAddingObjectsFromArray:@[
@{@"Text" : @"Quit",
@"Clickable" : @YES},
]];
}
[self populate:items];
if (self.root) {
NSMenuItem *item = nil;
if (!hideStartup()) {
item = [self itemAtIndex:items.count - 2];
item.action = @selector(toggleStartup:);
if (runningAtStartup()) {
item.state = NSControlStateValueOn;
} else {
item.state = NSControlStateValueOff;
}
}
item = [self itemAtIndex:items.count - 1];
item.action = @selector(prepareShutdown:);
}
self.open = YES;
}
- (void)menuDidClose:(MenuetMenu *)menu {
self.open = NO;
menuClosed(self.unique.UTF8String);
}
- (void)press:(id)sender {
NSString *callback = [sender representedObject];
itemClicked(callback.UTF8String);
}
- (void)toggleStartup:(id)sender {
toggleStartup();
}
- (void)prepareShutdown:(id)sender {
shutdownWait();
[NSApp terminate: nil];
}
@end
@interface MenuetAppDelegate : NSObject <NSApplicationDelegate, NSMenuDelegate, NSUserNotificationCenterDelegate>
@end
void setState(const char *jsonString) {
NSDictionary *state = [NSJSONSerialization
JSONObjectWithData:[[NSString stringWithUTF8String:jsonString]
dataUsingEncoding:NSUTF8StringEncoding]
options:0
error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
_statusItem.button.attributedTitle = [[NSAttributedString alloc]
initWithString:state[@"Title"]
attributes:@{
NSFontAttributeName :
[NSFont monospacedDigitSystemFontOfSize:14
weight:NSFontWeightRegular]
}];
NSString *imageName = state[@"Image"];
NSImage *image = [NSImage imageFromName:imageName withHeight:22];
_statusItem.button.image = image;
_statusItem.button.image.template = true;
_statusItem.button.imagePosition = NSImageLeft;
});
}
void menuChanged() {
dispatch_async(dispatch_get_main_queue(), ^{
MenuetMenu *menu = (MenuetMenu *)_statusItem.menu;
[menu refreshVisibleMenus];
});
}
void createAndRunApplication() {
[NSAutoreleasePool new];
NSApplication *a = NSApplication.sharedApplication;
MenuetAppDelegate *d = [MenuetAppDelegate new];
[a setDelegate:d];
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:d];
[a setActivationPolicy:NSApplicationActivationPolicyAccessory];
_statusItem = [[NSStatusBar systemStatusBar]
statusItemWithLength:NSVariableStatusItemLength];
MenuetMenu *menu = [MenuetMenu new];
menu.root = true;
_statusItem.menu = menu;
[a run];
}
@implementation MenuetAppDelegate
- (NSApplicationTerminateReply)applicationShouldTerminate:
(NSApplication *)sender {
return NSTerminateNow;
}
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification {
if (notification.activationType == NSUserNotificationActivationTypeReplied) {
NSString* userResponse = notification.response.string;
notificationRespond(notification.identifier.UTF8String, userResponse.UTF8String);
} else {
notificationRespond(notification.identifier.UTF8String, @"".UTF8String);
}
}
@end