Skip to content
This repository has been archived by the owner on Sep 17, 2021. It is now read-only.

NowPlayingWidget: Saves last music player used to start it from the the touchBar + display album art if available #40

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions rsc/Base.lproj/MainWindow.xib
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14313.18" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="14460.31" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaDFRPlugin" version="14215.3"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14313.18"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaDFRPlugin" version="14460.1"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="14460.31"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
<capability name="the Touch Bar" minToolsVersion="8.1" minSystemVersion="10.12.2" requiredIntegratedClassName="NSTouchBar"/>
</dependencies>
Expand Down Expand Up @@ -438,7 +438,7 @@
<window title="EnergyBar" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" restorable="NO" releasedWhenClosed="NO" visibleAtLaunch="NO" animationBehavior="default" id="QvC-M9-y7g">
<windowStyleMask key="styleMask" titled="YES" closable="YES"/>
<rect key="contentRect" x="335" y="390" width="480" height="480"/>
<rect key="screenRect" x="0.0" y="0.0" width="1440" height="877"/>
<rect key="screenRect" x="0.0" y="0.0" width="1680" height="1027"/>
<view key="contentView" wantsLayer="YES" id="EiT-Mj-1SZ">
<rect key="frame" x="0.0" y="0.0" width="480" height="480"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
Expand Down Expand Up @@ -650,11 +650,11 @@ In general the folder should contain application aliases. The order of the folde
<point key="canvasLocation" x="85" y="-793.5"/>
</customView>
<customView id="th2-os-EZ4">
<rect key="frame" x="0.0" y="0.0" width="480" height="178"/>
<rect key="frame" x="0.0" y="0.0" width="480" height="198"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="3AN-6g-R3A">
<rect key="frame" x="18" y="141" width="142" height="17"/>
<rect key="frame" x="18" y="161" width="142" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" alignment="right" title="Clock:" id="NnM-it-hT6">
<font key="font" metaFont="system"/>
Expand All @@ -663,7 +663,7 @@ In general the folder should contain application aliases. The order of the folde
</textFieldCell>
</textField>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="oNf-Lg-afs">
<rect key="frame" x="164" y="140" width="145" height="18"/>
<rect key="frame" x="164" y="160" width="145" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="Show 24-hour clock" bezelStyle="regularSquare" imagePosition="left" inset="2" id="uhm-Cd-4yr">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
Expand All @@ -675,7 +675,7 @@ In general the folder should contain application aliases. The order of the folde
</connections>
</button>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="bGo-2c-vUc">
<rect key="frame" x="164" y="120" width="144" height="18"/>
<rect key="frame" x="164" y="140" width="144" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="Show battery status" bezelStyle="regularSquare" imagePosition="left" inset="2" id="UB5-QM-a6y">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
Expand All @@ -687,7 +687,7 @@ In general the folder should contain application aliases. The order of the folde
</connections>
</button>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="dcS-av-i8x">
<rect key="frame" x="180" y="100" width="149" height="18"/>
<rect key="frame" x="180" y="120" width="149" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="Show remaining time" bezelStyle="regularSquare" imagePosition="left" inset="2" id="2Gu-J5-ToS">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
Expand All @@ -700,7 +700,7 @@ In general the folder should contain application aliases. The order of the folde
</connections>
</button>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="8PH-bF-9qF">
<rect key="frame" x="164" y="78" width="150" height="18"/>
<rect key="frame" x="164" y="98" width="150" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="Show weather on tap" bezelStyle="regularSquare" imagePosition="left" inset="2" id="t1E-dT-KGT">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
Expand All @@ -712,7 +712,7 @@ In general the folder should contain application aliases. The order of the folde
</connections>
</button>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="KcL-eW-UhJ">
<rect key="frame" x="18" y="59" width="142" height="17"/>
<rect key="frame" x="18" y="79" width="142" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" alignment="right" title="Weather:" id="Nts-QT-qCg">
<font key="font" metaFont="system"/>
Expand All @@ -721,7 +721,7 @@ In general the folder should contain application aliases. The order of the folde
</textFieldCell>
</textField>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="wTf-Nv-Cvq">
<rect key="frame" x="164" y="58" width="176" height="18"/>
<rect key="frame" x="164" y="78" width="176" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="Show Fahrenheit degrees" bezelStyle="regularSquare" imagePosition="left" inset="2" id="55e-xt-ZZb">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
Expand All @@ -733,7 +733,7 @@ In general the folder should contain application aliases. The order of the folde
</connections>
</button>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="fiq-GT-v0s">
<rect key="frame" x="18" y="39" width="142" height="17"/>
<rect key="frame" x="18" y="59" width="142" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" alignment="right" title="Now Playing:" id="L02-hZ-zyI">
<font key="font" metaFont="system"/>
Expand All @@ -742,7 +742,7 @@ In general the folder should contain application aliases. The order of the folde
</textFieldCell>
</textField>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="eqO-VX-2bv">
<rect key="frame" x="164" y="38" width="167" height="18"/>
<rect key="frame" x="164" y="58" width="167" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="Show Active App on tap" bezelStyle="regularSquare" imagePosition="left" inset="2" id="uz8-aB-xYU">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
Expand All @@ -754,7 +754,7 @@ In general the folder should contain application aliases. The order of the folde
</connections>
</button>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="RyZ-fr-A00">
<rect key="frame" x="164" y="18" width="135" height="18"/>
<rect key="frame" x="164" y="38" width="135" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="Show small widget" bezelStyle="regularSquare" imagePosition="left" inset="2" id="UGf-rj-KDL">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
Expand All @@ -765,6 +765,18 @@ In general the folder should contain application aliases. The order of the folde
<binding destination="pJh-SC-QEV" name="value" keyPath="values.nowPlayingShowsSmallWidget" id="Dkh-Ho-azL"/>
</connections>
</button>
<button verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="CyB-mC-LuD" userLabel="Show album art">
<rect key="frame" x="164" y="18" width="117" height="18"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="check" title="Show album art" bezelStyle="regularSquare" imagePosition="left" inset="2" id="OFz-tM-5o2" userLabel="Show album art">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<action selector="nowPlayingWidgetSettingsChange:" target="Voe-Tx-rLC" id="BW9-Ds-FDd"/>
<binding destination="pJh-SC-QEV" name="value" keyPath="values.nowPlayingShowsAlbumArt" id="Hn9-4I-KUa"/>
</connections>
</button>
</subviews>
<point key="canvasLocation" x="85" y="-473"/>
</customView>
Expand Down Expand Up @@ -906,9 +918,9 @@ Click to acknowledge.</string>
</objects>
<resources>
<image name="AppIcon" width="128" height="128"/>
<image name="NSAdvanced" width="128" height="128"/>
<image name="NSCaution" width="128" height="128"/>
<image name="NSPreferencesGeneral" width="128" height="128"/>
<image name="NSAdvanced" width="32" height="32"/>
<image name="NSCaution" width="32" height="32"/>
<image name="NSPreferencesGeneral" width="32" height="32"/>
<image name="NSStatusPartiallyAvailable" width="16" height="16"/>
<image name="Widgets" width="32" height="32"/>
</resources>
Expand Down
2 changes: 2 additions & 0 deletions rsc/defaults.plist
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,7 @@
<true/>
<key>weatherShowsFahrenheit</key>
<false/>
<key>showsAlbumArt</key>
<true/>
</dict>
</plist>
2 changes: 2 additions & 0 deletions src/AppController.m
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@ - (IBAction)nowPlayingWidgetSettingsChange:(id)sender
boolForKey:@"showsActiveAppOnTap"];
widget.showsSmallWidget = [[NSUserDefaults standardUserDefaults]
boolForKey:@"nowPlayingShowsSmallWidget"];
widget.showsAlbumArt = [[NSUserDefaults standardUserDefaults]
boolForKey:@"nowPlayingShowsAlbumArt"];
}

- (void)updateToggleMacOSDockButton
Expand Down
1 change: 1 addition & 0 deletions src/System/NowPlaying.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
49 changes: 36 additions & 13 deletions src/System/NowPlaying.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -36,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
Expand Down Expand Up @@ -82,7 +85,7 @@ - (id)init
selector:@selector(playingDidChange:)
name:kMRMediaRemoteNowPlayingApplicationIsPlayingDidChangeNotification
object:nil];

[self updateApp];
[self updateInfo];
[self updateState];
Expand All @@ -99,6 +102,7 @@ - (void)dealloc
self.appName = nil;
self.appIcon = nil;
self.album = nil;
self.albumArt = nil;
self.artist = nil;
self.title = nil;

Expand All @@ -113,22 +117,27 @@ - (void)updateApp
NSString *appBundleIdentifier = nil;
NSString *appName = nil;
NSImage *appIcon = nil;



if (nil != clientObj)
{
appBundleIdentifier = MRNowPlayingClientGetBundleIdentifier(clientObj);
if (nil == appBundleIdentifier)
appBundleIdentifier = MRNowPlayingClientGetParentAppBundleIdentifier(clientObj);

if (nil != appBundleIdentifier)
}
else
{
appBundleIdentifier = [[NSUserDefaults standardUserDefaults] 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];
}
}

Expand All @@ -139,10 +148,16 @@ - (void)updateApp
self.appBundleIdentifier = appBundleIdentifier;
self.appName = appName;
self.appIcon = appIcon;

[[NSNotificationCenter defaultCenter]
postNotificationName:NowPlayingInfoNotification
object:self];

if (nil != self.appBundleIdentifier)
{
[[NSUserDefaults standardUserDefaults] setObject:self.appBundleIdentifier forKey:LastAppBundleIdentifier];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
});
}
Expand All @@ -155,12 +170,20 @@ - (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];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is always nil on my machine. I am running macOS 10.14.3.

I have tried with iTunes, Spotify and Hermes and I never get the album art.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's weird, I'm also running macOS 10.14.3.

Logging info var using iTunes returns:
{
kMRMediaRemoteNowPlayingInfoAlbum = "Whatever People Say I Am, That's What I'm Not";
kMRMediaRemoteNowPlayingInfoAlbumiTunesStoreAdamIdentifier = 111153953;
kMRMediaRemoteNowPlayingInfoArtist = "Arctic Monkeys";
kMRMediaRemoteNowPlayingInfoArtistiTunesStoreAdamIdentifier = 62820413;
kMRMediaRemoteNowPlayingInfoArtworkData = <xxxxxxxx xxxxxxxx ...>
}

On my machine: https://www.youtube.com/watch?v=BEqfOcQq_Xk


NSImage *albumart = nil;
if (nil != artworkData && ![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
Expand Down
1 change: 1 addition & 0 deletions src/Widgets/NowPlayingWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading