-
-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathIconController.m
More file actions
487 lines (396 loc) · 16.1 KB
/
Copy pathIconController.m
File metadata and controls
487 lines (396 loc) · 16.1 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/*
Copyright (c) 2003-2025, Sveinbjorn Thordarson <sveinbjorn@sveinbjorn.org>
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may
be used to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#import "IconController.h"
#import "AGIconFamily.h"
#import "NSWorkspace+Additions.h"
#import "Common.h"
#import "VDKQueue.h"
typedef NS_ENUM(NSUInteger, PlatypusIconPreset) {
PlatypusPresetIconDefault = 0,
PlatypusPresetIconGenericApplication = 1,
PlatypusPresetIconGlobe = 2,
PlatypusPresetIconNetwork = 3,
PlatypusPresetIconSync = 4
};
@interface IconController()
{
IBOutlet STDragImageView *iconImageView;
IBOutlet NSWindow *window;
IBOutlet NSStepper *iconToggleButton;
IBOutlet NSTextField *iconNameTextField;
IBOutlet NSMenu *iconContextualMenu;
IBOutlet NSButton *iconActionButton;
VDKQueue *fileWatcherQueue;
dispatch_queue_t iconWritingDispatchQueue;
}
@end
@interface NSMenu (PopupMenuUsingAppearance)
- (BOOL)popUpMenuPositioningItem:(id)item atLocation:(struct CGPoint)loc inView:(id)view appearance:(id)appearance;
@end
@implementation IconController
- (instancetype)init {
if (self = [super init]) {
fileWatcherQueue = [[VDKQueue alloc] init];
iconWritingDispatchQueue = dispatch_queue_create("platypus.iconDispatchQueue", NULL);
}
return self;
}
- (void)dealloc {
[[WORKSPACE notificationCenter] removeObserver:self];
}
- (void)awakeFromNib {
// We list ourself as an observer of changes to watched file paths, in case icns file is deleted or moved
[[WORKSPACE notificationCenter] addObserver:self selector:@selector(updateIcnsStatus) name:VDKQueueRenameNotification object:nil];
[[WORKSPACE notificationCenter] addObserver:self selector:@selector(updateIcnsStatus) name:VDKQueueDeleteNotification object:nil];
}
#pragma mark - Interface actions
- (IBAction)copyIconPath:(id)sender {
[[NSPasteboard generalPasteboard] declareTypes:@[NSPasteboardTypeString] owner:self];
[[NSPasteboard generalPasteboard] setString:[self icnsFilePath] forType:NSPasteboardTypeString];
}
- (IBAction)copyIcon:(id)sender {
[[NSPasteboard generalPasteboard] declareTypes:@[NSPasteboardTypeTIFF] owner:self];
[[NSPasteboard generalPasteboard] setData:[[iconImageView image] TIFFRepresentation] forType:NSPasteboardTypeTIFF];
}
- (IBAction)pasteIcon:(id)sender {
[self loadImageFromPasteboard];
}
- (IBAction)revealIconInFinder:(id)sender {
[WORKSPACE selectFile:[self icnsFilePath] inFileViewerRootedAtPath:[self icnsFilePath]];
}
- (IBAction)selectImageFile:(id)sender {
[window setTitle:[NSString stringWithFormat:@"%@ - Select an image file", PROGRAM_NAME]];
// Create open panel
NSOpenPanel *oPanel = [NSOpenPanel openPanel];
[oPanel setPrompt:@"Select"];
[oPanel setAllowsMultipleSelection:NO];
[oPanel setCanChooseDirectories:NO];
[oPanel setAllowedFileTypes:[NSImage imageTypes]];
// Run open panel sheet
[oPanel beginSheetModalForWindow:window completionHandler:^(NSInteger result) {
[window setTitle:PROGRAM_NAME];
if (result != NSModalResponseOK) {
return;
}
NSString *filename = [[oPanel URLs][0] path];
NSString *fileType = [WORKSPACE typeOfFile:filename error:nil];
if ([WORKSPACE type:fileType conformsToType:(NSString *)kUTTypeAppleICNS]) {
[self loadIcnsFile:filename];
} else {
[self loadImageFile:filename];
}
}];
}
- (IBAction)selectIcnsFile:(id)sender {
[window setTitle:[NSString stringWithFormat:@"%@ - Select an icns file", PROGRAM_NAME]];
// Create open panel
NSOpenPanel *oPanel = [NSOpenPanel openPanel];
[oPanel setPrompt:@"Select"];
[oPanel setAllowsMultipleSelection:NO];
[oPanel setCanChooseDirectories:NO];
[oPanel setAllowedFileTypes:@[(NSString *)kUTTypeAppleICNS]];
// Run open panel
[oPanel beginSheetModalForWindow:window completionHandler:^(NSInteger result) {
[window setTitle:PROGRAM_NAME];
if (result == NSModalResponseOK) {
NSString *filename = [[oPanel URLs][0] path];
[self loadIcnsFile:filename];
}
}];
}
- (IBAction)iconActionButtonPressed:(id)sender {
NSRect screenRect = [window convertRectToScreen:[(NSButton *)sender frame]];
[iconContextualMenu popUpMenuPositioningItem:nil
atLocation:screenRect.origin
inView:nil
appearance:[NSAppearance currentAppearance]];
}
- (IBAction)nextIcon:(id)sender {
if ([iconToggleButton intValue] + 1 > [iconToggleButton maxValue]) {
[iconToggleButton setIntValue:[iconToggleButton minValue]];
} else {
[iconToggleButton setIntValue:[iconToggleButton intValue] + 1];
}
[self setAppIconForType:(PlatypusIconPreset)[iconToggleButton intValue]];
}
- (IBAction)previousIcon:(id)sender {
if ([iconToggleButton intValue] - 1 < [iconToggleButton minValue]) {
[iconToggleButton setIntValue:[iconToggleButton maxValue]];
} else {
[iconToggleButton setIntValue:[iconToggleButton intValue] - 1];
}
[self setAppIconForType:(PlatypusIconPreset)[iconToggleButton intValue]];
}
- (IBAction)switchIcons:(id)sender {
[self setAppIconForType:(PlatypusIconPreset)[sender intValue]];
}
#pragma mark -
- (void)updateIcnsStatus {
// Show question mark if icon file is missing
if ([self hasIconFile] && [FILEMGR fileExistsAtPath:_icnsFilePath] == FALSE) {
NSImage *qmarkIcon = [WORKSPACE iconForFileType:NSFileTypeForHFSTypeCode(kQuestionMarkIcon)];
[qmarkIcon setSize:NSMakeSize(128, 128)];
[iconImageView setImage:qmarkIcon];
[iconNameTextField setTextColor:[NSColor redColor]];
} else {
// Otherwise, read icns image from path and put it in image view
NSImage *img = [[NSImage alloc] initByReferencingFile:_icnsFilePath];
if (img == nil) {
img = [NSImage imageNamed:@"NSDefaultApplicationIcon"];
}
[iconImageView setImage:img];
[iconNameTextField setTextColor:[NSColor controlTextColor]];
}
}
- (void)createAndLoadCustomImageAsIcon:(NSImage *)image {
NSString *tmpIconPath;
do {
tmpIconPath = [NSString stringWithFormat:@"%@/PlatypusIcon-%d.icns", PROGRAM_APP_SUPPORT_PATH, arc4random() % 99999];
} while ([FILEMGR fileExistsAtPath:tmpIconPath]);
dispatch_async(iconWritingDispatchQueue, ^{
BOOL success = [self writeImageAsIcon:image toPath:tmpIconPath];
// UI updates on main thread
dispatch_async(dispatch_get_main_queue(), ^{
if (success) {
[iconNameTextField setStringValue:@"Custom Icon"];
[self setIcnsFilePath:tmpIconPath];
} else {
[self setToDefaults];
}
});
});
}
- (void)setAppIconForType:(PlatypusIconPreset)type {
[self loadPresetIcon:[self getIconInfoForType:type]];
}
- (NSDictionary *)getIconInfoForType:(PlatypusIconPreset)type {
NSImage *iconImage;
NSString *iconName;
NSString *iconPath;
switch (type) {
case PlatypusPresetIconDefault:
{
iconImage = [NSImage imageNamed:@"PlatypusDefault"];
iconName = @"Default Icon";
[iconImage setSize:NSMakeSize(512, 512)];
iconPath = [[NSBundle mainBundle] pathForResource:@"PlatypusDefault" ofType:@"icns"];
}
break;
case PlatypusPresetIconGenericApplication:
{
iconImage = [NSImage imageNamed:@"NSDefaultApplicationIcon"];
[iconImage setSize:NSMakeSize(512, 512)];
iconName = @"Generic Application";
return @{@"Name": iconName, @"Image": iconImage};
}
break;
case PlatypusPresetIconGlobe:
{
NSString *path = \
@"/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/BookmarkIcon.icns";
iconImage = [[NSImage alloc] initByReferencingFile:path];
[iconImage setSize:NSMakeSize(512, 512)];
iconName = @"Globe";
iconPath = path;
}
break;
case PlatypusPresetIconNetwork:
{
NSString *path = \
@"/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/GenericNetworkIcon.icns";
iconImage = [[NSImage alloc] initByReferencingFile:path];
[iconImage setSize:NSMakeSize(512, 512)];
iconName = @"Network";
iconPath = path;
}
break;
case PlatypusPresetIconSync:
{
NSString *path = \
@"/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/Sync.icns";
iconImage = [[NSImage alloc] initByReferencingFile:path];
[iconImage setSize:NSMakeSize(512, 512)];
iconName = @"Sync";
iconPath = path;
}
break;
}
return @{@"Name": iconName, @"Path": iconPath, @"Image": iconImage};
}
- (IBAction)setToDefaults {
[self setAppIconForType:PlatypusPresetIconDefault];
}
#pragma mark -
- (BOOL)writeImageAsIcon:(NSImage *)iconImage toPath:(NSString *)path {
if (iconImage == nil) {
return NO;
}
AGIconFamily *iconFamily = [[AGIconFamily alloc] initWithThumbnailsOfImage:iconImage
imageInterpolation:NSImageInterpolationHigh];
if (iconFamily == nil) {
DLog(@"Failed to create IconFamily from image");
return NO;
}
NSError *err;
BOOL succ = [iconFamily writeToURL:[NSURL fileURLWithPath:path] error:&err];
if (!succ) {
DLog(@"%@", [err localizedDescription]);
return NO;
}
return YES;
}
- (BOOL)hasIconFile {
return (_icnsFilePath != nil);
}
- (void)setIcnsFilePath:(NSString *)path {
if (_icnsFilePath != nil) {
[fileWatcherQueue removePath:_icnsFilePath];
_icnsFilePath = nil;
}
if (path != nil && [_icnsFilePath isEqualToString:@""] == NO) {
_icnsFilePath = [[NSString alloc] initWithString:path];
[fileWatcherQueue addPath:_icnsFilePath];
}
[self updateIcnsStatus];
[[NSNotificationCenter defaultCenter] postNotificationName:PLATYPUS_APP_SIZE_CHANGED_NOTIFICATION object:nil];
}
- (BOOL)isPresetIcon:(NSString *)str {
return [str hasPrefix:[[NSBundle mainBundle] resourcePath]];
}
#pragma mark - Load icon
- (BOOL)loadIcnsFile:(NSString *)filePath {
if (filePath == nil || [filePath isEqualToString:@""]) {
[self setAppIconForType:PlatypusPresetIconDefault];
return YES;
}
NSImage *img = [[NSImage alloc] initWithContentsOfFile:filePath];
if (img == nil) {
NSBeep();
[self setAppIconForType:PlatypusPresetIconDefault];
return NO;
}
[iconNameTextField setStringValue:[filePath lastPathComponent]];
[self setIcnsFilePath:filePath];
return YES;
}
- (BOOL)loadImageFile:(NSString *)filePath {
NSImage *img = [[NSImage alloc] initByReferencingFile:filePath];
if (img == nil) {
return NO;
}
[self createAndLoadCustomImageAsIcon:img];
return YES;
}
- (BOOL)loadImageFromPasteboard {
NSImage *img = [[NSImage alloc] initWithPasteboard:[NSPasteboard generalPasteboard]];
if (img == nil) {
return NO;
}
[self createAndLoadCustomImageAsIcon:img];
return YES;
}
- (BOOL)loadPresetIcon:(NSDictionary *)iconInfo {
if (!iconInfo) {
return NO;
}
[iconNameTextField setStringValue:iconInfo[@"Name"]];
NSImage *img = iconInfo[@"Image"];
if (img == nil) {
return NO;
}
[self setIcnsFilePath:iconInfo[@"Path"]];
return YES;
}
- (void)loadGenericAppIcon {
[self setAppIconForType:PlatypusPresetIconDefault];
}
#pragma mark -
- (BOOL)validateMenuItem:(NSMenuItem *)anItem {
if ([[anItem title] isEqualToString:@"Paste Icon"]) {
NSArray *pbTypes = @[NSTIFFPboardType, NSPDFPboardType, NSPostScriptPboardType];
NSString *type = [[NSPasteboard generalPasteboard] availableTypeFromArray:pbTypes];
if (type == nil) {
return NO;
}
}
if ([[anItem title] isEqualToString:@"Copy Icon Path"] || [[anItem title] isEqualToString:@"Show in Finder"]) {
return [FILEMGR fileExistsAtPath:[self icnsFilePath]];
}
return YES;
}
#pragma mark - STDragImageViewDelegate
- (BOOL)performDragOperation:(id <NSDraggingInfo> )sender {
NSPasteboard *pboard = [sender draggingPasteboard];
if (![[pboard types] containsObject:NSFilenamesPboardType]) {
return NO;
}
NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
// First, we look for an icns file, and load it if there is one
for (NSString *filename in files) {
NSString *fileType = [WORKSPACE typeOfFile:filename error:nil];
if ([WORKSPACE type:fileType conformsToType:(NSString *)kUTTypeAppleICNS]) {
return [self loadIcnsFile:filename];
}
}
// Since no icns file, search for an image, load the first one we find
NSArray *supportedImageTypes = [NSImage imageTypes];
for (NSString *filename in files) {
NSString *uti = [[NSWorkspace sharedWorkspace] typeOfFile:filename error:nil];
if ([supportedImageTypes containsObject:uti]) {
return [self loadImageFile:filename];
}
}
return NO;
}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo> )sender {
// We accept dragged files only
if ([[[sender draggingPasteboard] types] containsObject:NSFilenamesPboardType] == NO) {
return NSDragOperationNone;
}
NSArray *files = [[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType];
// Link operation for icns file, but not if it's a preset icon
for (NSString *filename in files) {
if ([self isPresetIcon:filename]) {
return NSDragOperationNone;
}
NSString *fileType = [WORKSPACE typeOfFile:filename error:nil];
if ([WORKSPACE type:fileType conformsToType:(NSString *)kUTTypeAppleICNS]) {
return NSDragOperationLink;
}
}
// Copy operation icon for image file
NSArray *supportedImageTypes = [NSImage imageTypes];
for (NSString *filename in files) {
NSString *uti = [[NSWorkspace sharedWorkspace] typeOfFile:filename error:nil];
if ([supportedImageTypes containsObject:uti]) {
return NSDragOperationCopy;
}
}
return NSDragOperationNone;
}
- (void)concludeDragOperation:(id <NSDraggingInfo>)sender {
// This is here to prevent superclass method from being invoked
}
@end