Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove MenuBarLyrics content when iTunes pauses #53

Open
wants to merge 7 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
6 changes: 4 additions & 2 deletions DynamicLyrics/LyricX.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@
02710EA9152A839F006D4BFF /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_IDENTITY = "Developer ID Application: Zhu Zheng (R5RMP6G3GQ)";
CODE_SIGN_IDENTITY = "";
COMBINE_HIDPI_IMAGES = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand All @@ -703,6 +703,7 @@
GCC_PREFIX_HEADER = "LyricX/LyricX-Prefix.pch";
INFOPLIST_FILE = "LyricX/LyricX-Info.plist";
MACOSX_DEPLOYMENT_TARGET = 10.7;
OTHER_CODE_SIGN_FLAGS = "";
OTHER_LDFLAGS = "-licucore";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
Expand All @@ -714,7 +715,7 @@
02710EAA152A839F006D4BFF /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CODE_SIGN_IDENTITY = "Developer ID Application: Zhu Zheng (R5RMP6G3GQ)";
CODE_SIGN_IDENTITY = "";
COMBINE_HIDPI_IMAGES = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
Expand All @@ -725,6 +726,7 @@
GCC_PREFIX_HEADER = "LyricX/LyricX-Prefix.pch";
INFOPLIST_FILE = "LyricX/LyricX-Info.plist";
MACOSX_DEPLOYMENT_TARGET = 10.7;
OTHER_CODE_SIGN_FLAGS = "";
OTHER_LDFLAGS = "-licucore";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
Expand Down
2 changes: 1 addition & 1 deletion DynamicLyrics/LyricX/LyricX-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>2.1</string>
<key>CFBundleVersion</key>
<string>1320</string>
<string>1538</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.music</string>
<key>LSMinimumSystemVersion</key>
Expand Down
2 changes: 1 addition & 1 deletion DynamicLyrics/LyricX/LyricsView.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ - (id)initWithFrame:(NSRect)frame
self = [super initWithFrame:frame];
if (self) {
nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(iTunesLyricsChanged:) name:@"LyricsChanged" object:nil];
[nc addObserver:self selector:@selector(iTunesLyricsChanged:) name:@NC_LyricsChanged object:nil];


rootLayer = [[CALayer layer] retain];
Expand Down
85 changes: 48 additions & 37 deletions DynamicLyrics/LyricX/MainController.m
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,30 @@ - (void) iTunesPlayerInfo:(NSNotification *)note

iTunesApplication *iTunes = [SBApplication applicationWithBundleIdentifier:@"com.apple.iTunes"];

if ([[[note userInfo] objectForKey:@"Player State"] isEqualToString:@"Stopped"]) {
NSString*state=[[note userInfo] objectForKey:@"Player State"];

if ([state isEqualToString:@"Stopped"]) {
[nc postNotificationName:@"LyricsChanged" object:self userInfo:[NSDictionary dictionaryWithObject:@"DynamicLyrics!" forKey:@"Lyrics"]];
return;
}else if([state isEqualToString:@"Paused"]){
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"iTunesPause" forKey:@"Type"];
[dict setObject:@YES forKey:@"isPausing"];
[self performSelectorOnMainThread:@selector(WorkingThread:) withObject:dict waitUntilDone:YES];
}else if([state isEqualToString:@"Playing"]){
{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"iTunesPause" forKey:@"Type"];
[dict setObject:@NO forKey:@"isPausing"];
[self performSelectorOnMainThread:@selector(WorkingThread:) withObject:dict waitUntilDone:YES];
}

if (self.iTunesCurrentTrack == [iTunes currentTrack])return;
self.iTunesCurrentTrack = [iTunes currentTrack];
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"iTunesSongChanged" forKey:@"Type"];
[self WorkingThread:dict];
}


if ((note != nil) && (![[[note userInfo] objectForKey:@"Player State"] isEqualToString:@"Playing"])) {
return;
}

if (self.iTunesCurrentTrack == [iTunes currentTrack]) return;
self.iTunesCurrentTrack = [iTunes currentTrack];

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"iTunesSongChanged" forKey:@"Type"];
[self WorkingThread:dict];
}
}

Expand Down Expand Up @@ -221,7 +229,8 @@ - (void) WorkingThread:(NSMutableDictionary*)tmpDict
//this thread should work in main thread
//iTunesPosition or iTunesSongChanged handler
@autoreleasepool {
if ([[tmpDict objectForKey:@"Type"] isEqualToString:@"iTunesSongChanged"])
NSString*type=[tmpDict objectForKey:@"Type"];
if ([type isEqualToString:@"iTunesSongChanged"])
{
//iTunesSongChanged
NSLog(@"%@",[iTunesCurrentTrack name]);
Expand Down Expand Up @@ -260,7 +269,7 @@ - (void) WorkingThread:(NSMutableDictionary*)tmpDict


}
else
else if([type isEqualToString:@"iTunesPosition"])
{
//iTunesPosition
NSString *_currentPlayerPosition = [NSString stringWithString:[tmpDict objectForKey:@"currentPlayerPosition"]];
Expand Down Expand Up @@ -316,6 +325,9 @@ - (void) WorkingThread:(NSMutableDictionary*)tmpDict

}

}else if([type isEqualToString:@"iTunesPause"]){
id obj=[tmpDict objectForKey:@"isPausing"];
[nc postNotificationName:@"iTunesPaused" object:self userInfo:[NSDictionary dictionaryWithObject:obj forKey:@"isPausing"]];
}
}
}
Expand Down Expand Up @@ -407,29 +419,28 @@ - (void)iTunesMonitoringThread
[[NSApplication sharedApplication] terminate:self];
//exit(0); //现在不通过Helper结束DynamicLyrics了,因为SandBox的缘故,我又懒得弄NSConnection,直接自己退出=。=
}
if ([iTunes isRunning] && [iTunes playerState] == iTunesEPlSPlaying) {
if (iTunesVersion >= 1103042001000000)
{
PlayerPosition = [iTunesNEW playerPosition];
if ([iTunes isRunning]) {
if([iTunes playerState] == iTunesEPlSPlaying){

} else
{
PlayerPosition = [iTunesOLD playerPosition];
}

if ((currentPlayerPosition / 1000) != PlayerPosition && currentPlayerPosition % 1000 < 900)
currentPlayerPosition = PlayerPosition * 1000;
NSMutableDictionary *dict = [NSMutableDictionary dictionary];


[dict setObject:@"iTunesPosition" forKey:@"Type"];
[dict setObject:[NSString stringWithFormat:@"%lu",currentPlayerPosition] forKey:@"currentPlayerPosition"];
[self performSelectorOnMainThread:@selector(WorkingThread:) withObject:dict waitUntilDone:YES];

}
else {
sleep(1);
}
if (iTunesVersion >= 1103042001000000)
{
PlayerPosition = [iTunesNEW playerPosition];

} else
{
PlayerPosition = [iTunesOLD playerPosition];
}

if ((currentPlayerPosition / 1000) != PlayerPosition && currentPlayerPosition % 1000 < 900)
currentPlayerPosition = PlayerPosition * 1000;
NSMutableDictionary *dict = [NSMutableDictionary dictionary];


[dict setObject:@"iTunesPosition" forKey:@"Type"];
[dict setObject:[NSString stringWithFormat:@"%lu",currentPlayerPosition] forKey:@"currentPlayerPosition"];
[self performSelectorOnMainThread:@selector(WorkingThread:) withObject:dict waitUntilDone:YES];
}else sleep(1);
}else sleep(1);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion DynamicLyrics/LyricX/MenuBarLyrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
NSNotificationCenter *nc;
NSStatusItem *_statusItem;
NSOperationQueue *_queue;

}

@property(nonatomic, retain) NSString *CurrentSongLyrics;
@property(retain)NSMenu*AppMenu;

-(id) initWithMenu:(NSMenu *)AppMenu;

Expand Down
75 changes: 67 additions & 8 deletions DynamicLyrics/LyricX/MenuBarLyrics.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "MenuBarLyrics.h"
#import "LyricXAppDelegate.h"
#import "Constants.h"

@interface NSStatusBar (NSStatusBar_Private)
Expand All @@ -33,37 +34,89 @@ -(id) initWithMenu:(NSMenu *)AppMenu;
[image setTemplate:YES];
[_statusItem setImage:image];
[_statusItem setHighlightMode:YES];
[_statusItem setMenu:AppMenu];
self.AppMenu=AppMenu;
[_statusItem setTarget:self];
[_statusItem setAction:@selector(toggleDesktopLyrics:)];
[_statusItem sendActionOn:NSLeftMouseUpMask|NSRightMouseUpMask];
nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(iTunesLyricsChanged:) name:@NC_LyricsChanged object:nil];
[nc addObserver:self selector:@selector(iTunesPaused:) name:@"iTunesPaused" object:nil];

NSLog(@"%@",@"MenuBarLyrics");

}
return self;
}

-(bool)_toggleDesktopLyrics:(NSMenuItem*)item{
CGKeyCode key=kVK_ANSI_X;NSString*kx=@"x";
CGEventRef kd=CGEventCreateKeyboardEvent(nil,key,true);
CGEventRef ku=CGEventCreateKeyboardEvent(nil,key,false);
if(!kd||!ku){
[_statusItem performSelectorOnMainThread:@selector(setEnabled:) withObject:true waitUntilDone:false];
if(kd)CFRelease(kd);
if(ku)CFRelease(ku);
return false;
}
NSString*kv=[item keyEquivalent];
NSUInteger km=[item keyEquivalentModifierMask];
[item setKeyEquivalent:kx];
[item setKeyEquivalentModifierMask:0];
[NSThread sleepForTimeInterval:0.1];
CGEventTapLocation loc=kCGHIDEventTap;
CGEventPost(loc,kd);
CGEventPost(loc,ku);
[NSThread sleepForTimeInterval:0.1];
[item setKeyEquivalent:kv];
[item setKeyEquivalentModifierMask:km];
CFRelease(kd);CFRelease(ku);
[_statusItem performSelectorOnMainThread:@selector(setEnabled:) withObject:true waitUntilDone:false];
return true;
}
-(void)toggleDesktopLyrics:(id)sender{
NSEvent*event=[NSApp currentEvent];
if([event type]!=NSRightMouseUp&&!([event modifierFlags]&NSControlKeyMask)){
[_statusItem setEnabled:false];
NSMenuItem*item=[self.AppMenu itemWithTag:100];
[self performSelectorInBackground:@selector(_toggleDesktopLyrics:) withObject:item];
[_statusItem popUpStatusItemMenu:self.AppMenu];
}else [_statusItem popUpStatusItemMenu:self.AppMenu];
}
-(void) dealloc
{
self.CurrentSongLyrics = nil;
[nc removeObserver:self];
[_statusItem release];
[super dealloc];
}

-(bool)isStatusBarWideEnoughToDisplayLyrics{
// TODO
return true;
}
-(void) showSmoothTitle:(NSString *)title
{
NSString*font=@"Bradley Hand";
// font=@"Jxixinkai";
NSMutableDictionary *d = [NSMutableDictionary dictionary];
[d setObject:[NSFont fontWithName: font size: 15] forKey:NSFontAttributeName];
[d setObject:[NSNumber numberWithInt: 1] forKey:NSBaselineOffsetAttributeName];


NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *style = [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"];
CGFloat white = (style && [style isEqualToString:@"Dark"]) ? 1 : 0;
{
NSAttributedString *shadowTitle = [[NSAttributedString alloc] initWithString:title attributes:d];
[_statusItem setAttributedTitle:shadowTitle];
if(![self isStatusBarWideEnoughToDisplayLyrics])
title=@"…";
}


for (float alpha = 0.3; alpha < 1.01; alpha+=0.02)
{

NSColor *color = [NSColor colorWithCalibratedWhite:white alpha:alpha];
NSMutableDictionary *d = [NSMutableDictionary dictionary];
[d setObject:color forKey:NSForegroundColorAttributeName];
[d setObject:[NSFont fontWithName: @"Lucida Grande" size: 15] forKey:NSFontAttributeName];

NSAttributedString *shadowTitle = [[NSAttributedString alloc] initWithString:title attributes:d];

Expand Down Expand Up @@ -103,8 +156,12 @@ -(void) hideSmoothTitle:(NSDictionary *)dict
[_statusItem setTitle:@""];
[pool release];
}


-(void)iTunesPaused:(NSNotification*)notification{
bool pausing=[[[notification userInfo]valueForKey:@"isPausing"]boolValue];
NSLog(@"%s",pausing?"Paused":"Playing");
[_statusItem setAttributedTitle:nil];
[_statusItem setImage:[NSImage imageNamed:@"StatusIcon"]];
}
-(void)iTunesLyricsChanged:(NSNotification *)note
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Expand Down Expand Up @@ -134,7 +191,9 @@ -(void)iTunesLyricsChanged:(NSNotification *)note

[_queue cancelAllOperations];
if ([ud boolForKey:@Pref_Enable_MenuBar_Lyrics] || forceUpdate) {
if ([self.CurrentSongLyrics isEqualToString:@""]) {
// NSString*trimmed=[self.CurrentSongLyrics stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString*trimmed=self.CurrentSongLyrics;
if ([trimmed isEqualToString:@""]){
[_statusItem setAttributedTitle:nil];
[_statusItem setImage:[NSImage imageNamed:@"StatusIcon"]];
}else{
Expand Down
12 changes: 6 additions & 6 deletions DynamicLyrics/LyricX/en.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="4514" systemVersion="13B42" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="6254" systemVersion="14B25" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none">
<dependencies>
<deployment defaultVersion="1070" identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="4514"/>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="6254"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="NSApplication">
Expand All @@ -11,7 +11,7 @@
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customObject id="420" customClass="NSFontManager"/>
<menu id="753">
<items>
Expand All @@ -26,7 +26,7 @@
</binding>
</connections>
</menuItem>
<menuItem title="Enable Desktop Lyrics" id="796">
<menuItem title="Enable Desktop Lyrics" tag="100" id="796">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="DisabledDesktopLyrics:" target="768" id="811"/>
Expand Down Expand Up @@ -231,4 +231,4 @@
<customObject id="847" customClass="SUUpdater"/>
<userDefaultsController representsSharedInstance="YES" id="798"/>
</objects>
</document>
</document>
12 changes: 6 additions & 6 deletions DynamicLyrics/LyricX/zh-Hans.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="4514" systemVersion="13B42" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="6254" systemVersion="14B25" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none">
<dependencies>
<deployment defaultVersion="1070" identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="4514"/>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="6254"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="NSApplication">
Expand All @@ -11,7 +11,7 @@
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customObject id="420" customClass="NSFontManager"/>
<menu id="753">
<items>
Expand All @@ -26,7 +26,7 @@
</binding>
</connections>
</menuItem>
<menuItem title="启用桌面歌词" id="796">
<menuItem title="启用桌面歌词" tag="100" id="796">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="DisabledDesktopLyrics:" target="768" id="811"/>
Expand Down Expand Up @@ -231,4 +231,4 @@
<customObject id="847" customClass="SUUpdater"/>
<userDefaultsController representsSharedInstance="YES" id="798"/>
</objects>
</document>
</document>
Loading