From 0bd7ce7b6769f685affd0742c46c7da4a964b183 Mon Sep 17 00:00:00 2001 From: William Klkein Date: Wed, 27 Feb 2019 09:16:32 +0100 Subject: [PATCH 1/4] NowPlayingWidget: Saves last music player used to start it from the touchBar --- src/System/NowPlaying.m | 30 ++++++++++++++++++++---------- src/Widgets/NowPlayingWidget.m | 8 ++++++-- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/System/NowPlaying.m b/src/System/NowPlaying.m index 3bcc398..5017fd5 100644 --- a/src/System/NowPlaying.m +++ b/src/System/NowPlaying.m @@ -13,6 +13,8 @@ #import "NowPlaying.h" +#define LastAppBundleIdentifier @"LastAppBundleIdentifier" + typedef void (^MRMediaRemoteGetNowPlayingInfoBlock)(NSDictionary *info); typedef void (^MRMediaRemoteGetNowPlayingClientBlock)(id clientObj); typedef void (^MRMediaRemoteGetNowPlayingApplicationIsPlayingBlock)(BOOL playing); @@ -113,22 +115,25 @@ - (void)updateApp NSString *appBundleIdentifier = nil; NSString *appName = nil; NSImage *appIcon = nil; + + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; if (nil != clientObj) { appBundleIdentifier = MRNowPlayingClientGetBundleIdentifier(clientObj); if (nil == appBundleIdentifier) appBundleIdentifier = MRNowPlayingClientGetParentAppBundleIdentifier(clientObj); - - if (nil != appBundleIdentifier) + } else { + appBundleIdentifier = [defaults objectForKey:LastAppBundleIdentifier]; + } + if (nil != appBundleIdentifier) + { + NSString *path = [[NSWorkspace sharedWorkspace] + absolutePathForAppBundleWithIdentifier:appBundleIdentifier]; + if (nil != path) { - NSString *path = [[NSWorkspace sharedWorkspace] - absolutePathForAppBundleWithIdentifier:appBundleIdentifier]; - if (nil != path) - { - appName = [[NSFileManager defaultManager] displayNameAtPath:path]; - appIcon = [[NSWorkspace sharedWorkspace] iconForFile:path]; - } + appName = [[NSFileManager defaultManager] displayNameAtPath:path]; + appIcon = [[NSWorkspace sharedWorkspace] iconForFile:path]; } } @@ -139,10 +144,15 @@ - (void)updateApp self.appBundleIdentifier = appBundleIdentifier; self.appName = appName; self.appIcon = appIcon; - + [[NSNotificationCenter defaultCenter] postNotificationName:NowPlayingInfoNotification object:self]; + + if (nil != self.appBundleIdentifier) { + [defaults setObject:self.appBundleIdentifier forKey:LastAppBundleIdentifier]; + [defaults synchronize]; + } } }); } diff --git a/src/Widgets/NowPlayingWidget.m b/src/Widgets/NowPlayingWidget.m index d06952d..4aa2895 100644 --- a/src/Widgets/NowPlayingWidget.m +++ b/src/Widgets/NowPlayingWidget.m @@ -79,12 +79,16 @@ - (void)viewDidDisappear - (void)resetNowPlaying { NSImage *icon = [NowPlaying sharedInstance].appIcon; + NSString *appName = [NowPlaying sharedInstance].appName; NSString *title = [NowPlaying sharedInstance].title; NSString *subtitle = [NowPlaying sharedInstance].artist; - if (nil == icon && nil == title && nil == subtitle) + if (nil == icon && nil == title && nil == subtitle) { title = @"♫"; - + } else if (nil == title && nil == subtitle) { + title = appName; + } + ImageTitleViewLayoutOptions layoutOptions = 0; if (nil != icon) layoutOptions = layoutOptions | ImageTitleViewLayoutOptionImage; From 17152374b3375e86396641c12f6b5124ebd7101d Mon Sep 17 00:00:00 2001 From: William Klkein Date: Wed, 27 Feb 2019 11:07:39 +0100 Subject: [PATCH 2/4] NowPlayingWidget: Display album art if available Present in iTunes but not Deezer for example. Dictionnary key was kMRMediaRemoteNowPlayingInfoArtworkData --- src/System/NowPlaying.h | 1 + src/System/NowPlaying.m | 13 +++++++++++-- src/Widgets/NowPlayingWidget.m | 13 +++++++++---- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/System/NowPlaying.h b/src/System/NowPlaying.h index 5c73e87..c0b25c4 100644 --- a/src/System/NowPlaying.h +++ b/src/System/NowPlaying.h @@ -19,6 +19,7 @@ @property (retain) NSString *appName; @property (retain) NSImage *appIcon; @property (retain) NSString *album; +@property (retain) NSImage *albumArt; @property (retain) NSString *artist; @property (retain) NSString *title; @property (assign) BOOL playing; diff --git a/src/System/NowPlaying.m b/src/System/NowPlaying.m index 5017fd5..6d7b664 100644 --- a/src/System/NowPlaying.m +++ b/src/System/NowPlaying.m @@ -38,6 +38,7 @@ void MRMediaRemoteGetNowPlayingApplicationIsPlaying(dispatch_queue_t queue, extern NSString *kMRMediaRemoteNowPlayingInfoAlbum; extern NSString *kMRMediaRemoteNowPlayingInfoArtist; extern NSString *kMRMediaRemoteNowPlayingInfoTitle; +extern NSString *kMRMediaRemoteNowPlayingInfoArtworkData; @implementation NowPlaying + (void)load @@ -101,6 +102,7 @@ - (void)dealloc self.appName = nil; self.appIcon = nil; self.album = nil; + self.albumArt = nil; self.artist = nil; self.title = nil; @@ -165,12 +167,19 @@ - (void)updateInfo NSString *album = [info objectForKey:kMRMediaRemoteNowPlayingInfoAlbum]; NSString *artist = [info objectForKey:kMRMediaRemoteNowPlayingInfoArtist]; NSString *title = [info objectForKey:kMRMediaRemoteNowPlayingInfoTitle]; - - if (self.album != album || self.artist != artist || self.title != title) + NSData *artworkData = [info objectForKey:kMRMediaRemoteNowPlayingInfoArtworkData]; + + NSImage *albumart = nil; + if (![artworkData isEqual:[NSNull null]]) { + albumart = [[NSImage alloc] initWithData:artworkData]; + } + + if (self.album != album || self.artist != artist || self.title != title || self.albumArt != albumart) { self.album = album; self.artist = artist; self.title = title; + self.albumArt = albumart; [[NSNotificationCenter defaultCenter] postNotificationName:NowPlayingInfoNotification diff --git a/src/Widgets/NowPlayingWidget.m b/src/Widgets/NowPlayingWidget.m index 4aa2895..3802747 100644 --- a/src/Widgets/NowPlayingWidget.m +++ b/src/Widgets/NowPlayingWidget.m @@ -78,19 +78,20 @@ - (void)viewDidDisappear - (void)resetNowPlaying { - NSImage *icon = [NowPlaying sharedInstance].appIcon; + NSImage *appIcon = [NowPlaying sharedInstance].appIcon; NSString *appName = [NowPlaying sharedInstance].appName; NSString *title = [NowPlaying sharedInstance].title; NSString *subtitle = [NowPlaying sharedInstance].artist; + NSImage *albumArt = [NowPlaying sharedInstance].albumArt; - if (nil == icon && nil == title && nil == subtitle) { + if (nil == appIcon && nil == title && nil == subtitle) { title = @"♫"; } else if (nil == title && nil == subtitle) { title = appName; } ImageTitleViewLayoutOptions layoutOptions = 0; - if (nil != icon) + if (nil != appIcon || nil != albumArt) layoutOptions = layoutOptions | ImageTitleViewLayoutOptionImage; if (nil != title) layoutOptions = layoutOptions | ImageTitleViewLayoutOptionTitle; @@ -98,7 +99,11 @@ - (void)resetNowPlaying layoutOptions = layoutOptions | ImageTitleViewLayoutOptionSubtitle; NowPlayingWidgetView *view = self.view; - view.image = icon; + if (nil != albumArt) { + view.image = albumArt; + } else if (nil != appIcon) { + view.image = appIcon; + } view.title = title; view.subtitle = subtitle; view.layoutOptions = layoutOptions; From 3f836f0b7686ebe6dab542ac34281565278d50e3 Mon Sep 17 00:00:00 2001 From: William Klkein Date: Wed, 27 Feb 2019 11:20:12 +0100 Subject: [PATCH 3/4] NowPlayingWidget: typo fixes --- src/System/NowPlaying.m | 10 +++++++--- src/Widgets/NowPlayingWidget.m | 14 ++++++++++---- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/System/NowPlaying.m b/src/System/NowPlaying.m index 6d7b664..d538d7e 100644 --- a/src/System/NowPlaying.m +++ b/src/System/NowPlaying.m @@ -125,7 +125,9 @@ - (void)updateApp appBundleIdentifier = MRNowPlayingClientGetBundleIdentifier(clientObj); if (nil == appBundleIdentifier) appBundleIdentifier = MRNowPlayingClientGetParentAppBundleIdentifier(clientObj); - } else { + } + else + { appBundleIdentifier = [defaults objectForKey:LastAppBundleIdentifier]; } if (nil != appBundleIdentifier) @@ -151,7 +153,8 @@ - (void)updateApp postNotificationName:NowPlayingInfoNotification object:self]; - if (nil != self.appBundleIdentifier) { + if (nil != self.appBundleIdentifier) + { [defaults setObject:self.appBundleIdentifier forKey:LastAppBundleIdentifier]; [defaults synchronize]; } @@ -170,7 +173,8 @@ - (void)updateInfo NSData *artworkData = [info objectForKey:kMRMediaRemoteNowPlayingInfoArtworkData]; NSImage *albumart = nil; - if (![artworkData isEqual:[NSNull null]]) { + if (![artworkData isEqual:[NSNull null]]) + { albumart = [[NSImage alloc] initWithData:artworkData]; } diff --git a/src/Widgets/NowPlayingWidget.m b/src/Widgets/NowPlayingWidget.m index 3802747..bb04882 100644 --- a/src/Widgets/NowPlayingWidget.m +++ b/src/Widgets/NowPlayingWidget.m @@ -84,9 +84,12 @@ - (void)resetNowPlaying NSString *subtitle = [NowPlaying sharedInstance].artist; NSImage *albumArt = [NowPlaying sharedInstance].albumArt; - if (nil == appIcon && nil == title && nil == subtitle) { + if (nil == appIcon && nil == title && nil == subtitle) + { title = @"♫"; - } else if (nil == title && nil == subtitle) { + } + else if (nil == title && nil == subtitle) + { title = appName; } @@ -99,9 +102,12 @@ - (void)resetNowPlaying layoutOptions = layoutOptions | ImageTitleViewLayoutOptionSubtitle; NowPlayingWidgetView *view = self.view; - if (nil != albumArt) { + if (nil != albumArt) + { view.image = albumArt; - } else if (nil != appIcon) { + } + else if (nil != appIcon) + { view.image = appIcon; } view.title = title; From 5053265ca25e96be27a2194322b0ef8d0ac141e0 Mon Sep 17 00:00:00 2001 From: William Klkein Date: Thu, 28 Feb 2019 09:27:26 +0100 Subject: [PATCH 4/4] Fixes for PR#40 --- rsc/Base.lproj/MainWindow.xib | 48 +++++++++++++++++++++------------- rsc/defaults.plist | 2 ++ src/AppController.m | 2 ++ src/System/NowPlaying.m | 12 ++++----- src/Widgets/NowPlayingWidget.h | 1 + src/Widgets/NowPlayingWidget.m | 36 ++++++++++++++++++++----- 6 files changed, 71 insertions(+), 30 deletions(-) diff --git a/rsc/Base.lproj/MainWindow.xib b/rsc/Base.lproj/MainWindow.xib index afd3dff..e3391d5 100644 --- a/rsc/Base.lproj/MainWindow.xib +++ b/rsc/Base.lproj/MainWindow.xib @@ -1,9 +1,9 @@ - + - - + + @@ -438,7 +438,7 @@ - + @@ -650,11 +650,11 @@ In general the folder should contain application aliases. The order of the folde - + - + @@ -663,7 +663,7 @@ In general the folder should contain application aliases. The order of the folde - + @@ -721,7 +721,7 @@ In general the folder should contain application aliases. The order of the folde - + @@ -742,7 +742,7 @@ In general the folder should contain application aliases. The order of the folde + @@ -906,9 +918,9 @@ Click to acknowledge. - - - + + + diff --git a/rsc/defaults.plist b/rsc/defaults.plist index fd785d8..4c28076 100644 --- a/rsc/defaults.plist +++ b/rsc/defaults.plist @@ -153,5 +153,7 @@ weatherShowsFahrenheit + showsAlbumArt + diff --git a/src/AppController.m b/src/AppController.m index 729e657..340d00e 100644 --- a/src/AppController.m +++ b/src/AppController.m @@ -378,6 +378,8 @@ - (IBAction)nowPlayingWidgetSettingsChange:(id)sender boolForKey:@"showsActiveAppOnTap"]; widget.showsSmallWidget = [[NSUserDefaults standardUserDefaults] boolForKey:@"nowPlayingShowsSmallWidget"]; + widget.showsAlbumArt = [[NSUserDefaults standardUserDefaults] + boolForKey:@"nowPlayingShowsAlbumArt"]; } - (void)updateToggleMacOSDockButton diff --git a/src/System/NowPlaying.m b/src/System/NowPlaying.m index d538d7e..f81f3b7 100644 --- a/src/System/NowPlaying.m +++ b/src/System/NowPlaying.m @@ -85,7 +85,7 @@ - (id)init selector:@selector(playingDidChange:) name:kMRMediaRemoteNowPlayingApplicationIsPlayingDidChangeNotification object:nil]; - + [self updateApp]; [self updateInfo]; [self updateState]; @@ -118,7 +118,7 @@ - (void)updateApp NSString *appName = nil; NSImage *appIcon = nil; - NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + if (nil != clientObj) { @@ -128,7 +128,7 @@ - (void)updateApp } else { - appBundleIdentifier = [defaults objectForKey:LastAppBundleIdentifier]; + appBundleIdentifier = [[NSUserDefaults standardUserDefaults] objectForKey:LastAppBundleIdentifier]; } if (nil != appBundleIdentifier) { @@ -155,8 +155,8 @@ - (void)updateApp if (nil != self.appBundleIdentifier) { - [defaults setObject:self.appBundleIdentifier forKey:LastAppBundleIdentifier]; - [defaults synchronize]; + [[NSUserDefaults standardUserDefaults] setObject:self.appBundleIdentifier forKey:LastAppBundleIdentifier]; + [[NSUserDefaults standardUserDefaults] synchronize]; } } }); @@ -173,7 +173,7 @@ - (void)updateInfo NSData *artworkData = [info objectForKey:kMRMediaRemoteNowPlayingInfoArtworkData]; NSImage *albumart = nil; - if (![artworkData isEqual:[NSNull null]]) + if (nil != artworkData && ![artworkData isEqual:[NSNull null]]) { albumart = [[NSImage alloc] initWithData:artworkData]; } diff --git a/src/Widgets/NowPlayingWidget.h b/src/Widgets/NowPlayingWidget.h index 7ff5a31..0ddced1 100644 --- a/src/Widgets/NowPlayingWidget.h +++ b/src/Widgets/NowPlayingWidget.h @@ -17,4 +17,5 @@ @interface NowPlayingWidget : CustomMultiWidget @property (getter=showsActiveAppOnTap, setter=setShowsActiveAppOnTap:) BOOL showsActiveAppOnTap; @property (getter=showsSmallWidget, setter=setShowsSmallWidget:) BOOL showsSmallWidget; +@property (getter=showsAlbumArt, setter=setShowsAlbumArt:) BOOL showsAlbumArt; @end diff --git a/src/Widgets/NowPlayingWidget.m b/src/Widgets/NowPlayingWidget.m index bb04882..e40530f 100644 --- a/src/Widgets/NowPlayingWidget.m +++ b/src/Widgets/NowPlayingWidget.m @@ -18,6 +18,7 @@ @interface NowPlayingWidgetView : ImageTitleView @property (assign) BOOL showsSmallWidget; +@property (assign) BOOL showsAlbumArt; @end @implementation NowPlayingWidgetView @@ -102,7 +103,7 @@ - (void)resetNowPlaying layoutOptions = layoutOptions | ImageTitleViewLayoutOptionSubtitle; NowPlayingWidgetView *view = self.view; - if (nil != albumArt) + if (view.showsAlbumArt && nil != albumArt) { view.image = albumArt; } @@ -130,24 +131,36 @@ - (void)setShowsSmallWidget:(BOOL)value { NowPlayingWidgetView *imageTitleView = self.view; imageTitleView.showsSmallWidget = value; - + if (!value) { imageTitleView.imageSize = NSMakeSize(26, 26); imageTitleView.titleFont = [NSFont boldSystemFontOfSize:[NSFont - systemFontSizeForControlSize:NSControlSizeSmall]]; + systemFontSizeForControlSize:NSControlSizeSmall]]; imageTitleView.subtitleFont = [NSFont systemFontOfSize:[NSFont - systemFontSizeForControlSize:NSControlSizeSmall]]; + systemFontSizeForControlSize:NSControlSizeSmall]]; } else { imageTitleView.imageSize = NSMakeSize(16, 16); imageTitleView.titleFont = [NSFont boldSystemFontOfSize:[NSFont - systemFontSizeForControlSize:NSControlSizeMini]]; + systemFontSizeForControlSize:NSControlSizeMini]]; imageTitleView.subtitleFont = [NSFont systemFontOfSize:[NSFont - systemFontSizeForControlSize:NSControlSizeMini]]; + systemFontSizeForControlSize:NSControlSizeMini]]; } } + +- (BOOL)showsAlbumArt +{ + NowPlayingWidgetView *imageTitleView = self.view; + return imageTitleView.showsAlbumArt; +} + +- (void)setShowsAlbumArt:(BOOL)value +{ + NowPlayingWidgetView *imageTitleView = self.view; + imageTitleView.showsAlbumArt = value; +} @end @implementation NowPlayingWidget @@ -190,6 +203,17 @@ - (void)setShowsSmallWidget:(BOOL)value [self.view invalidateIntrinsicContentSize]; } +- (BOOL)showsAlbumArt +{ + return [(id)[self.widgets objectAtIndex:0] showsAlbumArt]; +} + +- (void)setShowsAlbumArt:(BOOL)value +{ + [(id)[self.widgets objectAtIndex:0] setShowsAlbumArt:value]; + [(id)[self.widgets objectAtIndex:0] resetNowPlaying]; +} + - (void)longPressAction:(id)sender { NSString *appBundleIdentifier = [NowPlaying sharedInstance].appBundleIdentifier;