Skip to content

Commit

Permalink
Use custom code over setIconForFile: NSMenuItem custom category method
Browse files Browse the repository at this point in the history
While more code, it’s a step toward getting rid of the category extension method.
  • Loading branch information
sorbits committed May 17, 2021
1 parent c7a7878 commit 33f006e
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions Frameworks/Preferences/src/ProjectsPreferences.mm
Expand Up @@ -2,7 +2,6 @@
#import "Keys.h"
#import <settings/settings.h>
#import <OakAppKit/NSImage Additions.h>
#import <OakAppKit/NSMenuItem Additions.h>
#import <OakAppKit/OakUIConstructionFunctions.h>
#import <OakTabBarView/OakTabBarView.h>
#import <OakFoundation/NSString Additions.h>
Expand Down Expand Up @@ -67,11 +66,23 @@ - (void)takeFileBrowserPathFrom:(id)sender

- (NSMenuItem*)menuItemForURL:(NSURL*)aURL
{
NSMenuItem* res = [[NSMenuItem alloc] initWithTitle:[NSFileManager.defaultManager displayNameAtPath:[aURL path]] action:@selector(takeFileBrowserPathFrom:) keyEquivalent:@""];
[res setTarget:self];
[res setRepresentedObject:aURL];
if([aURL isFileURL])
[res setIconForFile:[aURL path]];
NSMenuItem* res = [[NSMenuItem alloc] initWithTitle:[NSFileManager.defaultManager displayNameAtPath:aURL.path] action:@selector(takeFileBrowserPathFrom:) keyEquivalent:@""];
res.target = self;
res.representedObject = aURL;

NSImage* image;
NSError* error;
if([aURL getResourceValue:&image forKey:NSURLEffectiveIconKey error:&error])
{
image = [image copy];
image.size = NSMakeSize(16, 16);
res.image = image;
}
else
{
os_log_error(OS_LOG_DEFAULT, "No NSURLEffectiveIconKey for %{public}@: %{public}@", aURL, error);
}

return res;
}

Expand Down

0 comments on commit 33f006e

Please sign in to comment.