X-Git-Url: http://git.ithinksw.org/MenuTunes.git/blobdiff_plain/545c9e281e8ac3ce259afc85d6a554849d8efe86..ce07e58f56b64ce6eea89ecbd66fa7bb8af57519:/MenuController.m diff --git a/MenuController.m b/MenuController.m index 72bee7a..43a1a65 100755 --- a/MenuController.m +++ b/MenuController.m @@ -8,12 +8,16 @@ #import "MenuController.h" #import "NewMainController.h" +#import "HotKeyCenter.h" +#import "KeyCombo.h" @interface MenuController (SubmenuMethods) - (NSMenu *)ratingMenu; - (NSMenu *)upcomingSongsMenu; - (NSMenu *)playlistsMenu; - (NSMenu *)eqMenu; +- (void)setKeyEquivalentForCode:(short)code andModifiers:(long)modifiers + onItem:(NSMenuItem *)item; @end @implementation MenuController @@ -29,17 +33,35 @@ - (NSMenu *)menu { NSMenu *menu = [[NSMenu alloc] initWithTitle:@""]; - NSArray *menuArray = [[NSUserDefaults standardUserDefaults] arrayForKey:@"menu"]; + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + NSArray *menuArray = [defaults arrayForKey:@"menu"]; NSEnumerator *enumerator = [menuArray objectEnumerator]; NSString *nextObject; NSMenuItem *tempItem; + NSEnumerator *itemEnum; + KeyCombo *keyCombo; - //Get the current playlist, track index, etc. - currentRemote = [[MainController sharedController] currentRemote]; + //Get the information _currentPlaylist = [currentRemote currentPlaylistIndex]; - _currentTrack = [currentRemote currentSongIndex]; _playingRadio = ([currentRemote currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist); + //Kill the old submenu items + if ( (tempItem = [_currentMenu itemWithTag:1]) ) { + [tempItem setSubmenu:nil]; + } + + if ( (tempItem = [_currentMenu itemWithTag:2]) ) { + [tempItem setSubmenu:nil]; + } + + if ( (tempItem = [_currentMenu itemWithTag:3]) ) { + [tempItem setSubmenu:nil]; + } + + if ( (tempItem = [_currentMenu itemWithTag:4]) ) { + [tempItem setSubmenu:nil]; + } + //create our menu while ( (nextObject = [enumerator nextObject]) ) { //Main menu items @@ -50,6 +72,12 @@ [tempItem setTag:MTMenuPlayPauseItem]; [tempItem setTarget:self]; + if ( (keyCombo = [[HotKeyCenter sharedCenter] keyComboForName:@"PlayPause"]) ) { + [self setKeyEquivalentForCode:[keyCombo keyCode] + andModifiers:[keyCombo modifiers] + onItem:tempItem]; + } + switch ([currentRemote playerPlayingState]) { case ITMTRemotePlayerPlaying: [tempItem setTitle:@"Pause"]; @@ -65,6 +93,13 @@ tempItem = [menu addItemWithTitle:@"Next Track" action:@selector(performMainMenuAction:) keyEquivalent:@""]; + + if ( (keyCombo = [[HotKeyCenter sharedCenter] keyComboForName:@"NextTrack"]) ) { + [self setKeyEquivalentForCode:[keyCombo keyCode] + andModifiers:[keyCombo modifiers] + onItem:tempItem]; + } + if (_currentPlaylist) { [tempItem setTag:MTMenuNextTrackItem]; [tempItem setTarget:self]; @@ -73,6 +108,13 @@ tempItem = [menu addItemWithTitle:@"Previous Track" action:@selector(performMainMenuAction:) keyEquivalent:@""]; + + if ( (keyCombo = [[HotKeyCenter sharedCenter] keyComboForName:@"PrevTrack"]) ) { + [self setKeyEquivalentForCode:[keyCombo keyCode] + andModifiers:[keyCombo modifiers] + onItem:tempItem]; + } + if (_currentPlaylist) { [tempItem setTag:MTMenuPreviousTrackItem]; [tempItem setTarget:self]; @@ -93,6 +135,17 @@ [tempItem setTag:MTMenuRewindItem]; [tempItem setTarget:self]; } + } else if ([nextObject isEqualToString:@"Show Player"]) { + tempItem = [menu addItemWithTitle:[NSString stringWithFormat:@"Show %@", [[[MainController sharedController] currentRemote] playerSimpleName]] action:@selector(performMainMenuAction:) keyEquivalent:@""]; + + if ( (keyCombo = [[HotKeyCenter sharedCenter] keyComboForName:@"ShowPlayer"]) ) { + [self setKeyEquivalentForCode:[keyCombo keyCode] + andModifiers:[keyCombo modifiers] + onItem:tempItem]; + } + + [tempItem setTarget:self]; + [tempItem setTag:MTMenuShowPlayerItem]; } else if ([nextObject isEqualToString:@"Preferences"]) { tempItem = [menu addItemWithTitle:@"Preferences..." action:@selector(performMainMenuAction:) @@ -106,15 +159,46 @@ [tempItem setTag:MTMenuQuitItem]; [tempItem setTarget:self]; } else if ([nextObject isEqualToString:@"Current Track Info"]) { + //Handle playing radio too if (_currentPlaylist) { NSString *title = [currentRemote currentSongTitle]; [menu addItemWithTitle:@"Now Playing" action:NULL keyEquivalent:@""]; if ([title length] > 0) { - [menu addItemWithTitle:[NSString stringWithFormat:@" %@", title] action:nil keyEquivalent:@""]; + [menu addItemWithTitle:[NSString stringWithFormat:@" %@", title] + action:nil + keyEquivalent:@""]; + } + + if ([defaults boolForKey:@"showAlbum"]) { + [menu addItemWithTitle:[NSString stringWithFormat:@" %@", [currentRemote currentSongAlbum]] + action:nil + keyEquivalent:@""]; + } + + if ([defaults boolForKey:@"showArtist"]) { + [menu addItemWithTitle:[NSString stringWithFormat:@" %@", [currentRemote currentSongArtist]] + action:nil + keyEquivalent:@""]; + } + + if ([defaults boolForKey:@"showTrackNumber"]) { + int track = [currentRemote currentSongTrack]; + if (track) { + [menu addItemWithTitle:[NSString stringWithFormat:@" Track %i", track] + action:nil + keyEquivalent:@""]; + } + } + + if ([defaults boolForKey:@"showTime"]) { + int left = [[currentRemote currentSongRemaining] intValue]; + NSString *remaining = [NSString stringWithFormat:@"%i:%02i", left / 60, left % 60]; + [menu addItemWithTitle:[NSString stringWithFormat:@" %@/%@", remaining, [currentRemote currentSongLength]] + action:nil + keyEquivalent:@""]; } - //Gotta add artist, album, track, time, etc, blah, blah, blah... } else { [menu addItemWithTitle:@"No Song" action:NULL keyEquivalent:@""]; } @@ -125,25 +209,47 @@ tempItem = [menu addItemWithTitle:@"Song Rating" action:nil keyEquivalent:@""]; - [tempItem setSubmenu:[self ratingMenu]]; + [tempItem setSubmenu:_ratingMenu]; + [tempItem setTag:1]; + + itemEnum = [[_ratingMenu itemArray] objectEnumerator]; + while ( (tempItem = [itemEnum nextObject]) ) { + [tempItem setState:NSOffState]; + } + + [[_ratingMenu itemAtIndex:([currentRemote currentSongRating] * 5)] setState:NSOnState]; + if (_playingRadio || !_currentPlaylist) { + [tempItem setEnabled:NO]; + } } else if ([nextObject isEqualToString:@"Upcoming Songs"]) { tempItem = [menu addItemWithTitle:@"Upcoming Songs" action:nil keyEquivalent:@""]; - [tempItem setSubmenu:[self upcomingSongsMenu]]; + [tempItem setSubmenu:_upcomingSongsMenu]; + [tempItem setTag:2]; + if (_playingRadio || !_currentPlaylist) { + [tempItem setEnabled:NO]; + } } else if ([nextObject isEqualToString:@"Playlists"]) { tempItem = [menu addItemWithTitle:@"Playlists" action:nil keyEquivalent:@""]; - [tempItem setSubmenu:[self playlistsMenu]]; + [tempItem setSubmenu:_playlistsMenu]; + [tempItem setTag:3]; } else if ([nextObject isEqualToString:@"EQ Presets"]) { tempItem = [menu addItemWithTitle:@"EQ Presets" action:nil keyEquivalent:@""]; - [tempItem setSubmenu:[self eqMenu]]; + [tempItem setSubmenu:_eqMenu]; + [tempItem setTag:4]; + + itemEnum = [[_eqMenu itemArray] objectEnumerator]; + while ( (tempItem = [itemEnum nextObject]) ) { + [tempItem setState:NSOffState]; + } + [[_eqMenu itemAtIndex:([currentRemote currentEQPresetIndex] - 1)] setState:NSOnState]; } } - [_currentMenu release]; _currentMenu = menu; return _currentMenu; @@ -166,10 +272,48 @@ return [menu autorelease]; } +- (void)rebuildSubmenus +{ + currentRemote = [[MainController sharedController] currentRemote]; + _currentPlaylist = [currentRemote currentPlaylistIndex]; + _currentTrack = [currentRemote currentSongIndex]; + _playingRadio = ([currentRemote currentPlaylistClass] == ITMTRemotePlayerRadioPlaylist); + + [_ratingMenu release]; + [_upcomingSongsMenu release]; + [_playlistsMenu release]; + [_eqMenu release]; + _ratingMenu = [self ratingMenu]; + _upcomingSongsMenu = [self upcomingSongsMenu]; + _playlistsMenu = [self playlistsMenu]; + _eqMenu = [self eqMenu]; +} + - (NSMenu *)ratingMenu { NSMenu *ratingMenu = [[NSMenu alloc] initWithTitle:@""]; - return [ratingMenu autorelease]; + if (_currentPlaylist && !_playingRadio) { + NSEnumerator *itemEnum; + id anItem; + int itemTag = 0; + SEL itemSelector = @selector(performRatingMenuAction:); + + [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"☆☆☆☆☆"] action:nil keyEquivalent:@""]; + [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★☆☆☆☆"] action:nil keyEquivalent:@""]; + [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★☆☆☆"] action:nil keyEquivalent:@""]; + [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★☆☆"] action:nil keyEquivalent:@""]; + [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★★☆"] action:nil keyEquivalent:@""]; + [ratingMenu addItemWithTitle:[NSString stringWithUTF8String:"★★★★★"] action:nil keyEquivalent:@""]; + + itemEnum = [[ratingMenu itemArray] objectEnumerator]; + while ( (anItem = [itemEnum nextObject]) ) { + [anItem setAction:itemSelector]; + [anItem setTarget:self]; + [anItem setTag:itemTag]; + itemTag += 20; + } + } + return ratingMenu; } - (NSMenu *)upcomingSongsMenu @@ -195,19 +339,44 @@ } } } - return [upcomingSongsMenu autorelease]; + return upcomingSongsMenu; } - (NSMenu *)playlistsMenu { NSMenu *playlistsMenu = [[NSMenu alloc] initWithTitle:@""]; - return [playlistsMenu autorelease]; + NSArray *playlists = [currentRemote playlists]; + NSMenuItem *tempItem; + int i; + + for (i = 0; i < [playlists count]; i++) { + tempItem = [playlistsMenu addItemWithTitle:[playlists objectAtIndex:i] action:@selector(performPlaylistMenuAction:) keyEquivalent:@""]; + [tempItem setTag:i + 1]; + [tempItem setTarget:self]; + } + + if (!_playingRadio && _currentPlaylist) { + [[playlistsMenu itemAtIndex:_currentPlaylist - 1] setState:NSOnState]; + } + return playlistsMenu; } - (NSMenu *)eqMenu { NSMenu *eqMenu = [[NSMenu alloc] initWithTitle:@""]; - return [eqMenu autorelease]; + NSArray *eqPresets = [currentRemote eqPresets]; + NSMenuItem *tempItem; + int i; + + for (i = 0; i < [eqPresets count]; i++) { + NSString *name; + if ( ( name = [eqPresets objectAtIndex:i] ) ) { + tempItem = [eqMenu addItemWithTitle:name action:@selector(performEqualizerMenuAction:) keyEquivalent:@""]; + [tempItem setTag:i]; + [tempItem setTarget:self]; + } + } + return eqMenu; } - (void)performMainMenuAction:(id)sender @@ -217,17 +386,14 @@ case MTMenuPlayPauseItem: NSLog(@"MenuController: Play/Pause"); [[MainController sharedController] playPause]; - //We're gonna have to change the Play menu item to Pause here too. break; case MTMenuFastForwardItem: NSLog(@"MenuController: Fast Forward"); [[MainController sharedController] fastForward]; - //make sure play/pause item says sane through this break; case MTMenuRewindItem: NSLog(@"MenuController: Rewind"); [[MainController sharedController] rewind]; - //make sure play/pause item says sane through this break; case MTMenuPreviousTrackItem: NSLog(@"MenuController: Previous Track"); @@ -487,4 +653,4 @@ } } -@end +@end \ No newline at end of file