-
Notifications
You must be signed in to change notification settings - Fork 0
/
FinderLabelPatch.m
79 lines (68 loc) · 2.62 KB
/
FinderLabelPatch.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
//
// FinderLabelPatch.m
// FinderPatch
//
// Created by toy on 16.01.11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "FinderLabelPatch.h"
@implementation FinderLabelPatch
+ (void) loadPatch {
NSArray *keyEquivalents = [@"n e g p b y r o" componentsSeparatedByString:@" "];
NSUInteger modifierMask = NSControlKeyMask | NSAlternateKeyMask;
NSUInteger i, _i = [keyEquivalents count];
for (i = 0; i < _i; i++) {
NSString *keyEquivalent = [keyEquivalents objectAtIndex:i];
[self shiftItemWithKeyEquivalent:keyEquivalent andModifierMask:modifierMask];
NSMenuItem *setLabelItem = [[self patchMenu] addItemWithTitle:@"" action:@selector(cmdSetLabel:) keyEquivalent:keyEquivalent];
[setLabelItem setKeyEquivalentModifierMask:modifierMask];
[setLabelItem setTarget:self];
[setLabelItem setTag:i];
}
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(updateLabels:)
name:NSWorkspaceDidChangeFileLabelsNotification
object:nil];
[self updateLabels:nil];
}
+ (void) updateLabels:(NSNotification *)notification {
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
NSArray *names = [workspace fileLabels];
NSArray *colors = [workspace fileLabelColors];
[[[self patchMenu] itemArray] enumerateObjectsUsingBlock:^(id menuItem, NSUInteger idx, BOOL *stop) {
if ([menuItem target] == self) {
NSUInteger labelId = [menuItem tag];
NSString *name = [names objectAtIndex:labelId];
[menuItem setTitle:[NSString stringWithFormat:@"Label %@", name]];
NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(16, 16)];
NSRect rect = NSMakeRect(4, 4, 8, 8);
[image lockFocus];
if (labelId == 0) {
NSImage *xImage = [NSImage imageNamed:NSImageNameStopProgressTemplate];
[xImage drawInRect:rect fromRect:NSZeroRect operation:NSCompositeCopy fraction:0.68f];
} else {
NSColor *color = [colors objectAtIndex:labelId];
NSShadow* theShadow = [[NSShadow alloc] init];
[theShadow setShadowOffset:NSMakeSize(0, -1)];
[theShadow setShadowBlurRadius:2.0];
[theShadow setShadowColor:[[NSColor blackColor] colorWithAlphaComponent:0.8]];
[theShadow set];
[color drawSwatchInRect:rect];
[theShadow release];
}
[image unlockFocus];
[menuItem setImage:image];
[image release];
}
}];
}
+ (BOOL) validateMenuItem:(NSMenuItem *)item {
return YES;
}
+ (void) cmdSetLabel:(id)sender {
if (sender) {
SEL selector = NSSelectorFromString([NSString stringWithFormat:@"cmdSetLabel%d:", [sender tag]]);
[NSApp sendAction:selector to:nil from:nil];
}
}
@end