-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathITApplicationController.m
103 lines (85 loc) · 2.83 KB
/
ITApplicationController.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
#import "ITApplicationController.h"
#import "ITCategory-NSApplication.h"
@implementation ITApplicationController
- (id)init {
if (self = [super init]) {
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"ITDebugMode"]) {
SetITDebugMode(YES);
}
_plugins = nil;
_dockMenu = [[NSMenu alloc] initWithTitle:[[NSApplication sharedApplication] applicationName]];
_debugMenu = [[NSMenu alloc] initWithTitle:@"Debug"];
_debugMenuItem = [[NSMenuItem alloc] initWithTitle:@"Debug" action:nil keyEquivalent:@""];
[_debugMenuItem setSubmenu:_debugMenu];
_dockDebugMenuItem = [[NSMenuItem alloc] initWithTitle:@"Debug" action:nil keyEquivalent:@""];
[_dockDebugMenuItem setSubmenu:_debugMenu];
}
return self;
}
- (void)reloadPlugins {
if (_plugins) {
[_plugins release];
}
_plugins = [[NSMutableArray alloc] init];
NSArray *pluginPaths = [NSBundle pathsForResourcesOfType:@"plugin" inDirectory:[[NSBundle mainBundle] builtInPlugInsPath]];
NSEnumerator *pluginPathEnumerator = [pluginPaths objectEnumerator];
id pluginPath;
while (pluginPath = [pluginPathEnumerator nextObject]) {
NSBundle *plugin = [NSBundle bundleWithPath:pluginPath];
if ([plugin load]) {
Class pluginClass = [plugin principalClass];
id pluginInstance;
if ([pluginClass instancesRespondToSelector:@selector(initWithApplicationController:)]) {
pluginInstance = [(id <ITApplicationControllerGenericPlugin>)[pluginClass alloc] initWithApplicationController:self];
} else {
pluginInstance = [[pluginClass alloc] init];
}
if (pluginInstance) {
[_plugins addObject:[pluginInstance autorelease]]; // autoreleasing so that when we reload plugins, and the _plugins array is released, the accompanying previously-loaded plugins die with it.
}
}
}
}
- (NSArray *)plugins {
return _plugins;
}
- (NSMenu *)dockMenu {
return _dockMenu;
}
- (NSMenu *)debugMenu {
return _debugMenu;
}
- (void)enableDebugMenu {
NSMenu *mainMenu = [[NSApplication sharedApplication] mainMenu];
int helpIndex = [mainMenu indexOfItemWithTitle:@"Help"];
if (helpIndex != -1) {
[mainMenu insertItem:_debugMenuItem atIndex:helpIndex];
} else {
[mainMenu addItem:_debugMenuItem];
}
[_dockMenu insertItem:_dockDebugMenuItem atIndex:0];
if ([_dockMenu numberOfItems] > 1) {
[_dockMenu insertItem:[NSMenuItem separatorItem] atIndex:1];
}
}
- (void)disableDebugMenu {
[[[NSApplication sharedApplication] mainMenu] removeItem:_debugMenuItem];
[_dockMenu removeItem:_dockDebugMenuItem];
if ([_dockMenu numberOfItems] > 1) {
NSMenuItem *sep = [_dockMenu itemAtIndex:0];
if ([sep isSeparatorItem]) {
[_dockMenu removeItem:sep];
}
}
}
- (void)dealloc {
[_dockDebugMenuItem release];
[_debugMenuItem release];
[_debugMenu release];
[_dockMenu release];
[super dealloc];
}
- (NSMenu *)applicationDockMenu:(NSApplication *)sender {
return _dockMenu;
}
@end