-
Notifications
You must be signed in to change notification settings - Fork 2
/
ReMate.mm
219 lines (173 loc) · 6.35 KB
/
ReMate.mm
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
#import "ReMate.h"
#import "MethodSwizzle.h"
static NSMenuItem* refreshMenuItem;
#define DEFAULTS_KEY @"ReMate Volumes"
@implementation NSWindowController (AppSwitching)
- (NSString*)volumeName
{
// For new projects
if([[self valueForKey:@"rootItems"] count] == 0)
return nil;
NSString* rootPath = [[[self valueForKey:@"rootItems"] objectAtIndex:0] objectForKey:@"sourceDirectory"];
NSString* volumeName = nil;
if([rootPath hasPrefix:@"/Volumes/"])
volumeName = [[rootPath componentsSeparatedByString:@"/"] objectAtIndex:2];
return volumeName;
}
- (NSMutableDictionary*)configurationForVolume:(NSString*)volumeName
{
id volumeConfiguration = nil;
unsigned int volumeCount = [[ReMate defaults] count];
for(unsigned int index = 0; index < volumeCount; index += 1)
{
NSDictionary* volume = [[ReMate defaults] objectAtIndex:index];
if([[volume objectForKey:@"name"] isEqualToString:volumeName])
{
volumeConfiguration = volume;
break;
}
}
return volumeConfiguration;
}
- (void)ReMate_windowDidLoad
{
[self ReMate_windowDidLoad];
NSString* volumeName = [self volumeName];
if(volumeName && [self configurationForVolume:volumeName] == nil)
{
NSArray* currentDefaults = [ReMate defaults];
// Disable refreshing for new volumes by default
NSDictionary* newVolume = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],@"refreshIsDisabled",
volumeName,@"name",
nil];
[ReMate setDefaults:[currentDefaults arrayByAddingObject:newVolume]];
}
}
- (void)swizzledApplicationDidBecomeActiveNotification:(id)sender
{
BOOL refreshingIsEnabled = YES;
if(NSString* volumeName = [self volumeName])
refreshingIsEnabled = ! [[[self configurationForVolume:volumeName] objectForKey:@"refreshIsDisabled"] boolValue];
if(refreshingIsEnabled)
[self swizzledApplicationDidBecomeActiveNotification:sender];
}
@end
@implementation NSButton (ProjectContextMenu)
- (void)projectAwakeFromNib
{
[self projectAwakeFromNib];
if (![[self valueForKey:@"delegate"] isKindOfClass:NSClassFromString(@"OakProjectController")])
return;
NSMenu* menu = [self valueForKey:@"actionMenu"];
[menu addItem:[NSMenuItem separatorItem]];
refreshMenuItem = [menu addItemWithTitle:@"Refresh Project Files" action:@selector(refreshProjects:) keyEquivalent:@""];
[refreshMenuItem setTarget:self];
}
- (void)refreshProjects:(id)sender
{
[((OakMenuButton*)self)->delegate swizzledApplicationDidBecomeActiveNotification:sender];
}
- (BOOL)ReMate_validateMenuItem:(id <NSMenuItem>)menuItem
{
return ([menuItem action] == @selector(refreshProjects:)) ? YES : [self ReMate_validateMenuItem:menuItem];
}
@end
@implementation ReMate
static id ReMateDefaults = nil;
- (float)version
{
return 0.1;
}
+ (id)defaults
{
if (!ReMateDefaults)
ReMateDefaults = [NSUserDefaults standardUserDefaults];
return [ReMateDefaults arrayForKey:DEFAULTS_KEY];
}
+ (void)setDefaults:(NSArray*)defaults
{
[ReMateDefaults setObject:defaults forKey:DEFAULTS_KEY];
}
- (id)initWithPlugInController:(id <TMPlugInController>)aController
{
if (![ReMate defaults])
[ReMate setDefaults:[NSArray array]];
MethodSwizzle(NSClassFromString(@"OakProjectController"), @selector(applicationDidBecomeActiveNotification:), @selector(swizzledApplicationDidBecomeActiveNotification:));
MethodSwizzle(NSClassFromString(@"OakProjectController"), @selector(windowDidLoad), @selector(ReMate_windowDidLoad));
MethodSwizzle(NSClassFromString(@"OakMenuButton"), @selector(awakeFromNib), @selector(projectAwakeFromNib));
MethodSwizzle(NSClassFromString(@"OakMenuButton"), @selector(validateMenuItem:), @selector(ReMate_validateMenuItem:));
NSApp = [NSApplication sharedApplication];
if(self = [super init])
[self installMenuItem];
return self;
}
- (void)dealloc
{
[self uninstallMenuItem];
[super dealloc];
}
- (void)installMenuItem
{
if (windowMenu = [[[[NSApp mainMenu] itemWithTitle:@"Window"] submenu] retain]) {
unsigned index = 0;
NSArray* items = [windowMenu itemArray];
for (int separators = 0; index != [items count] && separators != 2; index++)
separators += [[items objectAtIndex:index] isSeparatorItem] ? 1 : 0;
configureMenuItem = [[NSMenuItem alloc] initWithTitle:@"Configure ReMate" action:@selector(orderFrontConfigureDialog:) keyEquivalent:@""];
[configureMenuItem setTarget:self];
[configureMenuItem setState:NSOffState];
[windowMenu insertItem:configureMenuItem atIndex:index ? index-1 : 0];
[configureMenuItem release];
}
}
- (void)orderFrontConfigureDialog:(id)sender
{
if (!configurationWindowController) {
ReMatePreferencesController* obj = [ReMatePreferencesController alloc]; // this is a little hacky, since initXYZ could change the obj, but the path variant of initWithNib needs an owner
NSString* nibPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"Configure" ofType:@"nib"];
configurationWindowController = [obj initWithWindowNibPath:nibPath owner:obj];
}
[configurationWindowController showWindow:self];
}
- (void)uninstallMenuItem
{
[windowMenu removeItem:configureMenuItem];
[configureMenuItem release];
configureMenuItem = nil;
[windowMenu release];
windowMenu = nil;
}
@end
@implementation ReMatePreferencesController
- (int)numberOfRowsInTableView:(NSTableView*)aTableView
{
return [[ReMate defaults] count];
}
- (id)tableView:(NSTableView*)aTableView objectValueForTableColumn:(NSTableColumn*)column row:(int)rowIndex
{
NSDictionary* config = [[ReMate defaults] objectAtIndex:rowIndex];
id value = nil;
if ([[column identifier] isEqualToString:@"name"])
value = [config objectForKey:@"name"];
else if ([[column identifier] isEqualToString:@"refreshingEnabled"])
value = [NSNumber numberWithBool:! [[config objectForKey:@"refreshIsDisabled"] boolValue]];
return value;
}
- (void)tableView:(NSTableView*)aTableView setObjectValue:(id)value forTableColumn:(NSTableColumn*)column row:(int)rowIndex
{
NSMutableArray* defaults = [[[ReMate defaults] mutableCopy] autorelease];
NSMutableDictionary* config = [[[defaults objectAtIndex:rowIndex] mutableCopy] autorelease];
[config setObject:[NSNumber numberWithBool:![value boolValue]] forKey:@"refreshIsDisabled"];
[defaults replaceObjectAtIndex:rowIndex withObject:config];
[ReMate setDefaults:defaults];
}
- (IBAction)saveAndClose:(id)sender
{
[[sender window] performClose:self];
}
- (IBAction)showWindow:(id)sender
{
[super showWindow:sender];
[tableView reloadData];
}
@end