mirror of
https://github.com/kyleneideck/BackgroundMusic
synced 2024-11-26 22:10:26 +00:00
Merge branch 'master' of https://github.com/hoke-t/BackgroundMusic into hoke-t-master
This commit is contained in:
commit
a229791ade
2 changed files with 56 additions and 2 deletions
|
@ -29,6 +29,7 @@
|
|||
|
||||
|
||||
static NSString* const kToggleAutoPauseMusicMenuItemTitleFormat = @"Auto-pause %@";
|
||||
static int const kUpdateMenuWaitTime = 1; // Wait time to disable/enable music player auto-pause in seconds
|
||||
static float const kMenuItemIconScalingFactor = 1.15f;
|
||||
static NSInteger const kPrefsMenuAutoPauseHeaderTag = 1;
|
||||
|
||||
|
@ -39,6 +40,8 @@ static NSInteger const kPrefsMenuAutoPauseHeaderTag = 1;
|
|||
NSArray<NSMenuItem*>* musicPlayerMenuItems;
|
||||
}
|
||||
|
||||
id didLaunchToken, didTerminateToken;
|
||||
|
||||
- (id) initWithPreferencesMenu:(NSMenu*)inPrefsMenu
|
||||
toggleAutoPauseMusicMenuItem:(NSMenuItem*)inToggleAutoPauseMusicMenuItem
|
||||
audioDevices:(BGMAudioDeviceManager*)inAudioDevices {
|
||||
|
@ -64,6 +67,41 @@ static NSInteger const kPrefsMenuAutoPauseHeaderTag = 1;
|
|||
// Get the currently selected music player from the driver and update the global in BGMMusicPlayerBase
|
||||
|
||||
// The bundle ID and PID set on the driver
|
||||
|
||||
BOOL (^isAboutThisMusicPlayer)(NSNotification*) = ^(NSNotification* note){
|
||||
return [[note.userInfo[NSWorkspaceApplicationKey] bundleIdentifier] isEqualToString:(__bridge NSString*)[[[BGMMusicPlayer selectedMusicPlayer] class] bundleID]];
|
||||
};
|
||||
|
||||
// Add observers that create/destroy the SBApplication when the music player is launched/terminated. We
|
||||
// only create the SBApplication when the music player is open because, if it isn't, creating the
|
||||
// SBApplication, or sending it events, could launch the music player. Whether it does or not depends on
|
||||
// the music player, and possibly the version of the music player, so to be safe we assume they all do.
|
||||
//
|
||||
// From the docs for SBApplication's applicationWithBundleIdentifier method:
|
||||
// "For applications that declare themselves to have a dynamic scripting interface, this method will
|
||||
// launch the application if it is not already running."
|
||||
NSNotificationCenter* center = [[NSWorkspace sharedWorkspace] notificationCenter];
|
||||
didLaunchToken = [center addObserverForName:NSWorkspaceDidLaunchApplicationNotification
|
||||
object:nil
|
||||
queue:nil
|
||||
usingBlock:^(NSNotification* note) {
|
||||
if (isAboutThisMusicPlayer(note)) {
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kUpdateMenuWaitTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||||
[self updateMenuItemTitle];
|
||||
});
|
||||
}
|
||||
}];
|
||||
didTerminateToken = [center addObserverForName:NSWorkspaceDidTerminateApplicationNotification
|
||||
object:nil
|
||||
queue:nil
|
||||
usingBlock:^(NSNotification* note) {
|
||||
if (isAboutThisMusicPlayer(note)) {
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(kUpdateMenuWaitTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||||
[self updateMenuItemTitle];
|
||||
});
|
||||
}
|
||||
}];
|
||||
|
||||
CFNumberRef selectedPID = static_cast<CFNumberRef>([audioDevices bgmDevice].GetPropertyData_CFType(kBGMMusicPlayerProcessIDAddress));
|
||||
CFStringRef selectedBundleID = [audioDevices bgmDevice].GetPropertyData_CFString(kBGMMusicPlayerBundleIDAddress);
|
||||
|
||||
|
@ -125,6 +163,7 @@ static NSInteger const kPrefsMenuAutoPauseHeaderTag = 1;
|
|||
|
||||
// The index to start inserting music player menu items at
|
||||
NSInteger musicPlayerItemsIndex = [prefsMenu indexOfItemWithTag:kPrefsMenuAutoPauseHeaderTag] + 1;
|
||||
[prefsMenu setAutoenablesItems:false];
|
||||
|
||||
// Insert the options to change the music player app
|
||||
for (Class musicPlayerClass in [BGMMusicPlayerBase musicPlayerClasses]) {
|
||||
|
@ -199,6 +238,23 @@ static NSInteger const kPrefsMenuAutoPauseHeaderTag = 1;
|
|||
NSString* musicPlayerName = [[[BGMMusicPlayer selectedMusicPlayer] class] name];
|
||||
NSString* title = [NSString stringWithFormat:kToggleAutoPauseMusicMenuItemTitleFormat, musicPlayerName];
|
||||
[toggleAutoPauseMusicMenuItem setTitle:title];
|
||||
|
||||
// Disable the Auto-pause option if the application is not runnning
|
||||
[[toggleAutoPauseMusicMenuItem menu] setAutoenablesItems:false];
|
||||
[toggleAutoPauseMusicMenuItem setEnabled:[[BGMMusicPlayer selectedMusicPlayer] isRunning]];
|
||||
}
|
||||
|
||||
- (void) dealloc {
|
||||
// Remove the application launch/termination observers.
|
||||
NSNotificationCenter* center = [[NSWorkspace sharedWorkspace] notificationCenter];
|
||||
|
||||
if (didLaunchToken) {
|
||||
[center removeObserver:didLaunchToken];
|
||||
}
|
||||
|
||||
if (didTerminateToken) {
|
||||
[center removeObserver:didTerminateToken];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
2
TODO.md
2
TODO.md
|
@ -13,8 +13,6 @@ There are also lots of other TODOs commented around the code.
|
|||
- Should we hide music players in the preferences menu if Launch Services says they aren't installed? (And they aren't
|
||||
running or otherwise obviously present on the system.) If so, should we have a "show all" menu item?
|
||||
|
||||
- Disable the Auto-pause Music menu item if the selected music player isn't running
|
||||
|
||||
- System-wide volume boost
|
||||
|
||||
- Listen for notifications when the output device is removed/disabled and revert to the previous output device
|
||||
|
|
Loading…
Reference in a new issue