forked from lilyball/NotificationWatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WatcherController.m
274 lines (251 loc) · 10.2 KB
/
WatcherController.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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#import "WatcherController.h"
#import "Globals.h"
#import "Extensions.h"
#import <mach-o/dyld.h>
static NSDictionary *italicAttributesForFont(NSFont *aFont) {
NSFont *newFont = [[NSFontManager sharedFontManager] convertFont:aFont
toHaveTrait:NSItalicFontMask];
NSDictionary *attrDict;
if (![newFont isEqual:aFont]) {
attrDict = [NSDictionary dictionaryWithObject:newFont
forKey:NSFontAttributeName];
} else {
attrDict = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.16]
forKey:NSObliquenessAttributeName];
}
return attrDict;
}
@interface WatcherController ()
- (NSArray *)filteredDistNotificationsWithString:(NSString *)filterValue;
- (NSArray *)filteredWorkspaceNotificationsWithString:(NSString *)filterValue;
@end
@implementation WatcherController
- (id)init {
if (self = [super init]) {
distNotifications = [[NSMutableArray alloc] init];
wsNotifications = [[NSMutableArray alloc] init];
[[NSDistributedNotificationCenter defaultCenter] addObserver:self
selector:@selector(distNotificationHook:)
name:nil object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(wsNotificationHook:)
name:nil object:nil];
NSMutableDictionary *defaults = [NSMutableDictionary dictionary];
[defaults setObject:@"NO" forKey:kHideProcessSwitchNotificationPref];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
}
return self;
}
- (void)awakeFromNib {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(selectNotification:)
name:NSTableViewSelectionDidChangeNotification object:distNotificationList];
[nc addObserver:self selector:@selector(selectNotification:)
name:NSTableViewSelectionDidChangeNotification object:wsNotificationList];
}
- (void)dealloc {
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[selectedDistNotification release];
[selectedWSNotification release];
[distNotifications release];
[wsNotifications release];
[super dealloc];
}
- (IBAction)showPrefs:(id)sender {
[prefsWindow makeKeyAndOrderFront:sender];
}
- (IBAction)didChangeFilter:(NSSearchField *)sender {
[distNotificationList noteNumberOfRowsChanged];
[wsNotificationList noteNumberOfRowsChanged];
}
- (IBAction)selectSearchField:(id)sender {
[searchField becomeFirstResponder];
}
- (void)distNotificationHook:(NSNotification*)aNotification {
if ([[NSUserDefaults standardUserDefaults] boolForKey:kHideProcessSwitchNotificationPref]) {
if ([[aNotification name] isEqualToString:@"com.apple.HIToolbox.menuBarShownNotification"] ||
[[aNotification name] isEqualToString:@"AppleSystemUIModeChanged"])
return;
}
NSRect visibleRect = [distNotificationList visibleRect];
NSRect bounds = [distNotificationList bounds];
BOOL atBottom = ((visibleRect.origin.y + visibleRect.size.height) ==
(bounds.origin.y + bounds.size.height));
[distNotifications addObject:[[aNotification copy] autorelease]];
[distNotificationList noteNumberOfRowsChanged];
if (atBottom) {
[distNotificationList scrollRowToVisible:[distNotificationList numberOfRows] - 1];
}
}
- (void)wsNotificationHook:(NSNotification*)aNotification {
NSRect visibleRect = [wsNotificationList visibleRect];
NSRect bounds = [wsNotificationList bounds];
BOOL atBottom = ((visibleRect.origin.y + visibleRect.size.height) ==
(bounds.origin.y + bounds.size.height));
[wsNotifications addObject:[[aNotification copy] autorelease]];
[wsNotificationList noteNumberOfRowsChanged];
if (atBottom) {
[wsNotificationList scrollRowToVisible:[wsNotificationList numberOfRows] - 1];
}
}
- (void)selectNotification:(NSNotification*)aNotification {
id sender = [aNotification object];
[selectedDistNotification release];
selectedDistNotification = nil;
[selectedWSNotification release];
selectedWSNotification = nil;
[savedRowHeights release];
savedRowHeights = nil;
NSNotification **targetVar;
NSArray *targetList;
if (sender == distNotificationList) {
targetVar = &selectedDistNotification;
targetList = [self filteredDistNotificationsWithString:[searchField stringValue]];
} else {
targetVar = &selectedWSNotification;
targetList = [self filteredWorkspaceNotificationsWithString:[searchField stringValue]];
}
if ([sender selectedRow] != -1) {
[*targetVar autorelease];
*targetVar = [[targetList objectAtIndex:[sender selectedRow]] retain];
}
if (*targetVar == nil) {
[objectText setStringValue:@""];
} else {
id obj = [*targetVar object];
NSMutableAttributedString *objStr = nil;
if (obj == nil) {
NSFont *aFont = [objectText font];
NSDictionary *attrDict = italicAttributesForFont(aFont);
objStr = [[NSMutableAttributedString alloc] initWithString:@"(null)"
attributes:attrDict];
} else {
objStr = [[NSMutableAttributedString alloc] initWithString:
[NSString stringWithFormat:@" (%@)", [obj className]]];
[objStr addAttributes:italicAttributesForFont([objectText font])
range:NSMakeRange(1,[[obj className] length]+2)];
if ([obj isKindOfClass:[NSString class]]) {
[objStr replaceCharactersInRange:NSMakeRange(0,0) withString:obj];
} else if ([obj respondsToSelector:@selector(stringValue)]) {
[objStr replaceCharactersInRange:NSMakeRange(0,0)
withString:[obj performSelector:@selector(stringValue)]];
} else {
// Remove the space since we have no value to display
[objStr replaceCharactersInRange:NSMakeRange(0,1) withString:@""];
}
}
[objectText setObjectValue:objStr];
[objStr release];
}
[userInfoList reloadData];
}
- (IBAction)clearNotifications:(id)sender {
[selectedDistNotification release];
selectedDistNotification = nil;
[selectedWSNotification release];
selectedWSNotification = nil;
[savedRowHeights release];
savedRowHeights = nil;
[distNotifications removeAllObjects];
[wsNotifications removeAllObjects];
[distNotificationList reloadData];
[wsNotificationList reloadData];
[objectText setStringValue:@""];
[userInfoList reloadData];
}
- (NSArray *)filteredDistNotificationsWithString:(NSString *)filterValue {
NSPredicate *filterPredicate = [NSPredicate predicateWithValue:YES];
if ([filterValue length] > 0) {
filterPredicate = [NSPredicate predicateWithFormat:@"name CONTAINS[c] %@ OR (object CONTAINS[c] %@)", filterValue, filterValue];
}
return [distNotifications filteredArrayUsingPredicate:filterPredicate];
}
- (NSArray *)filteredWorkspaceNotificationsWithString:(NSString *)filterValue {
NSPredicate *filterPredicate = [NSPredicate predicateWithValue:YES];
if ([filterValue length] > 0) {
filterPredicate = [NSPredicate predicateWithFormat:@"name CONTAINS[c] %@", filterValue];
}
return [wsNotifications filteredArrayUsingPredicate:filterPredicate];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView {
NSString *filterValue = [searchField stringValue];
if (aTableView == distNotificationList) {
return [[self filteredDistNotificationsWithString:filterValue] count];
} else if (aTableView == wsNotificationList) {
return [[self filteredWorkspaceNotificationsWithString:filterValue] count];
} else {
if (selectedDistNotification == nil && selectedWSNotification == nil) {
return 0;
} else if (selectedDistNotification == nil) {
return [[selectedWSNotification userInfo] count];
} else {
return [[selectedDistNotification userInfo] count];
}
}
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
NSString *filterValue = [searchField stringValue];
if (aTableView == distNotificationList) {
return [[[self filteredDistNotificationsWithString:filterValue] objectAtIndex:rowIndex] name];
} else if (aTableView == wsNotificationList) {
return [[[self filteredWorkspaceNotificationsWithString:filterValue] objectAtIndex:rowIndex] name];
} else {
if (selectedDistNotification == nil && selectedWSNotification == nil) {
return @"";
} else {
NSNotification **targetVar;
if (selectedDistNotification == nil) {
targetVar = &selectedWSNotification;
} else {
targetVar = &selectedDistNotification;
}
if ([[aTableColumn identifier] isEqualToString:@"key"]) {
return [[[*targetVar userInfo] allKeys] objectAtIndex:rowIndex];
} else {
return [[[[*targetVar userInfo] allValues] objectAtIndex:rowIndex] description];
}
}
}
}
- (BOOL)tableView:(NSTableView *)tableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard {
NSMutableArray *rowStrings = [NSMutableArray array];
NSUInteger rowIdx;
for (rowIdx = [rowIndexes firstIndex]; rowIdx != NSNotFound; rowIdx = [rowIndexes indexGreaterThanIndex:rowIdx]) {
NSMutableArray *columnStrings = [NSMutableArray array];
NSArray *columns = [tableView tableColumns];
NSEnumerator *colEnum = [columns objectEnumerator];
NSTableColumn *aColumn;
while (aColumn = [colEnum nextObject]) {
[columnStrings addObject:[self tableView:tableView
objectValueForTableColumn:aColumn
row:rowIdx]];
}
[rowStrings addObject:[columnStrings componentsJoinedByString:@"\t"]];
}
[pboard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
[pboard setString:[rowStrings componentsJoinedByString:@"\r"] forType:NSStringPboardType];
return YES;
}
- (CGFloat)tableView:(NSTableView *)aTableView heightOfRow:(NSInteger)row {
if (aTableView != userInfoList || (selectedDistNotification == nil && selectedWSNotification == nil) ) {
return 14;
}
NSNotification *targetVar;
if (selectedDistNotification == nil) {
targetVar = selectedWSNotification;
} else {
targetVar = selectedDistNotification;
}
if (savedRowHeights == nil) {
savedRowHeights = [[NSMutableArray alloc] init];
}
if ([savedRowHeights count] < row + 1) {
NSString *str = [[[[targetVar userInfo] allValues] objectAtIndex:row] description];
NSSize size = [str sizeWithAttributes:[NSDictionary dictionaryWithObject:[NSFont fontWithName:@"Lucida Grande" size:11] forKey:NSFontAttributeName]];
[savedRowHeights insertObject:[NSNumber numberWithFloat:size.height] atIndex:row];
}
return [[savedRowHeights objectAtIndex:row] floatValue];
}
@end