Repository navigation
Expand file tree
/
Copy pathmenu_darwin.m
More file actions
88 lines (77 loc) · 2.33 KB
/
Copy pathmenu_darwin.m
File metadata and controls
88 lines (77 loc) · 2.33 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
// The macOS menu bar. Go builds it with the calls below, from any
// goroutine; each one runs on the main thread in call order. A chosen
// item calls lensmMenuAction with its tag.
#import <AppKit/AppKit.h>
#include "_cgo_export.h"
@interface LensmMenuTarget : NSObject
@end
@implementation LensmMenuTarget
- (void)choose:(NSMenuItem *)item {
lensmMenuAction((int)item.tag);
}
@end
static LensmMenuTarget *target;
static NSMenu *menuBar;
static NSMenu *current;
static NSMutableDictionary<NSNumber *, NSMenuItem *> *items;
static void onMain(dispatch_block_t block) {
dispatch_async(dispatch_get_main_queue(), block);
}
void lensmMenuAdd(const char *title) {
NSString *t = [NSString stringWithUTF8String:title];
onMain(^{
if (menuBar == nil) {
target = [LensmMenuTarget new];
menuBar = [NSMenu new];
items = [NSMutableDictionary new];
}
NSMenuItem *top = [NSMenuItem new];
current = [[NSMenu alloc] initWithTitle:t];
current.autoenablesItems = NO;
top.submenu = current;
[menuBar addItem:top];
if ([t isEqualToString:@"Window"]) {
NSApp.windowsMenu = current;
}
});
}
// lensmMenuItem adds an item that reports tag, or with a selector
// name sends that action to the first responder instead. key is the
// shortcut with Command, plus Option when alt is set.
void lensmMenuItem(const char *label, const char *key, int alt, int tag, const char *selector) {
NSString *l = [NSString stringWithUTF8String:label];
NSString *k = [NSString stringWithUTF8String:key];
NSString *s = [NSString stringWithUTF8String:selector];
onMain(^{
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:l action:nil keyEquivalent:k];
if (alt) {
item.keyEquivalentModifierMask = NSEventModifierFlagCommand | NSEventModifierFlagOption;
}
if (s.length > 0) {
item.action = NSSelectorFromString(s);
} else {
item.action = @selector(choose:);
item.target = target;
item.tag = tag;
items[@(tag)] = item;
}
[current addItem:item];
});
}
void lensmMenuSeparator(void) {
onMain(^{
[current addItem:[NSMenuItem separatorItem]];
});
}
void lensmMenuInstall(void) {
onMain(^{
NSApp.mainMenu = menuBar;
});
}
void lensmMenuSetState(int tag, int checked, int enabled) {
onMain(^{
NSMenuItem *item = items[@(tag)];
item.state = checked ? NSControlStateValueOn : NSControlStateValueOff;
item.enabled = enabled ? YES : NO;
});
}