-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for custom Dock icons while running
The emoji labeled buttons will convert and save their respective state icon to the settings folder, and refresh the current icon as necessary. Signed-off-by: Christopher Snowhill <kode54@gmail.com>
- Loading branch information
Showing
18 changed files
with
305 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// AppearancePane.h | ||
// General | ||
// | ||
// Created by Christopher Snowhill on 11/24/24. | ||
// | ||
// | ||
|
||
#import "GeneralPreferencePane.h" | ||
#import <Cocoa/Cocoa.h> | ||
|
||
@interface AppearancePane : GeneralPreferencePane { | ||
} | ||
|
||
- (IBAction)setDockIconStop:(id)sender; | ||
- (IBAction)setDockIconPlay:(id)sender; | ||
- (IBAction)setDockIconPause:(id)sender; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// | ||
// AppearancePane.m | ||
// General | ||
// | ||
// Created by Christopher Snowhill on 11/24/24. | ||
// | ||
// | ||
|
||
#import "AppearancePane.h" | ||
|
||
static NSString *CogCustomDockIconsReloadNotification = @"CogCustomDockIconsReloadNotification"; | ||
|
||
@implementation AppearancePane | ||
|
||
- (NSString *)title { | ||
return NSLocalizedPrefString(@"Appearance"); | ||
} | ||
|
||
- (NSImage *)icon { | ||
if(@available(macOS 11.0, *)) | ||
return [NSImage imageWithSystemSymbolName:@"paintpalette.fill" accessibilityDescription:nil]; | ||
return [[NSImage alloc] initWithContentsOfFile:[[NSBundle bundleForClass:[self class]] pathForImageResource:@"appearance"]]; | ||
} | ||
|
||
- (void)setDockIcon:(NSString *)baseName { | ||
NSArray *fileTypes = @[@"jpg", @"jpeg", @"png", @"gif", @"webp", @"avif", @"heic"]; | ||
NSOpenPanel *panel = [NSOpenPanel openPanel]; | ||
[panel setAllowsMultipleSelection:NO]; | ||
[panel setCanChooseDirectories:NO]; | ||
[panel setCanChooseFiles:YES]; | ||
[panel setFloatingPanel:YES]; | ||
[panel setAllowedFileTypes:fileTypes]; | ||
NSInteger result = [panel runModal]; | ||
if(result == NSModalResponseOK) { | ||
NSError *error = nil; | ||
NSData *iconData = [NSData dataWithContentsOfURL:[panel URL] options:NSDataReadingMappedIfSafe error:&error]; | ||
if(iconData && !error) { | ||
NSImage *icon = [[NSImage alloc] initWithData:iconData]; | ||
if(icon) { | ||
CGImageRef cgRef = [icon CGImageForProposedRect:NULL | ||
context:nil | ||
hints:nil]; | ||
|
||
if(cgRef) { | ||
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES); | ||
NSString *basePath = [[paths firstObject] stringByAppendingPathComponent:@"Cog"]; | ||
basePath = [basePath stringByAppendingPathComponent:@"Icons"]; | ||
NSFileManager *fileManager = [NSFileManager defaultManager]; | ||
BOOL isDirectory = NO; | ||
if(![fileManager fileExistsAtPath:basePath isDirectory:&isDirectory] || !isDirectory) { | ||
if(!isDirectory) { | ||
[fileManager removeItemAtPath:basePath error:&error]; | ||
} | ||
[fileManager createDirectoryAtURL:[NSURL fileURLWithPath:basePath] withIntermediateDirectories:YES attributes:nil error:&error]; | ||
} | ||
|
||
NSString *filePath = [basePath stringByAppendingPathComponent:baseName]; | ||
filePath = [filePath stringByAppendingPathExtension:@"png"]; | ||
|
||
NSBitmapImageRep *newRep = | ||
[[NSBitmapImageRep alloc] initWithCGImage:cgRef]; | ||
NSData *pngData = [newRep | ||
representationUsingType:NSBitmapImageFileTypePNG | ||
properties:@{}]; | ||
[pngData writeToURL:[NSURL fileURLWithPath:filePath] atomically:YES]; | ||
|
||
// Now to refresh the icons by a little trickery, if enabled | ||
BOOL enabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"customDockIcons"]; | ||
if(enabled) { | ||
[[NSNotificationCenter defaultCenter] postNotificationName:CogCustomDockIconsReloadNotification object:nil]; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
- (IBAction)setDockIconStop:(id)sender { | ||
[self setDockIcon:@"Stop"]; | ||
} | ||
|
||
- (IBAction)setDockIconPlay:(id)sender { | ||
[self setDockIcon:@"Play"]; | ||
} | ||
|
||
- (IBAction)setDockIconPause:(id)sender { | ||
[self setDockIcon:@"Pause"]; | ||
} | ||
|
||
@end |
Oops, something went wrong.