Use notification center on OS X when available.

This commit is contained in:
John Maguire 2012-09-13 16:15:28 +02:00
parent 0fae2e1ae1
commit 340e6dabce
3 changed files with 52 additions and 4 deletions

View File

@ -23,14 +23,20 @@ class PlatformInterface;
}
- (id) initWithHandler: (PlatformInterface*)handler;
// NSApplicationDelegate
- (BOOL) applicationShouldHandleReopen: (NSApplication*)app hasVisibleWindows:(BOOL)flag;
- (NSMenu*) applicationDockMenu: (NSApplication*)sender;
- (void)applicationDidFinishLaunching:(NSNotification*)aNotification;
- (NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication*)sender;
// NSUserNotificationCenterDelegate
- (BOOL) userNotificationCenter: (id)center
shouldPresentNotification: (id)notification;
- (void) setDockMenu: (NSMenu*)menu;
- (MacGlobalShortcutBackend*) shortcut_handler;
- (void) setShortcutHandler: (MacGlobalShortcutBackend*)backend;
- (void)applicationDidFinishLaunching:(NSNotification*)aNotification;
- (NSApplicationTerminateReply) applicationShouldTerminate:(NSApplication*)sender;
- (void) mediaKeyTap: (SPMediaKeyTap*)keyTap receivedMediaKeyEvent:(NSEvent*)event;
@end

View File

@ -200,6 +200,12 @@ static BreakpadRef InitBreakpad() {
#endif
return NSTerminateNow;
}
- (BOOL) userNotificationCenter: (id)center shouldPresentNotification: (id)notification {
// Always show notifications, even if Clementine is in the foreground.
return YES;
}
@end
@implementation MacApplication
@ -232,12 +238,18 @@ static BreakpadRef InitBreakpad() {
// this makes sure the delegate's shortcut_handler is set
[delegate_ setShortcutHandler:shortcut_handler_];
[self setDelegate:delegate_];
Class notification_center_class = NSClassFromString(@"NSUserNotificationCenter");
if (notification_center_class) {
id notification_center = [notification_center_class defaultUserNotificationCenter];
[notification_center setDelegate: delegate_];
}
}
-(void) sendEvent: (NSEvent*)event {
// If event tap is not installed, handle events that reach the app instead
BOOL shouldHandleMediaKeyEventLocally = ![SPMediaKeyTap usesGlobalMediaKeyTap];
if(shouldHandleMediaKeyEventLocally && [event type] == NSSystemDefined && [event subtype] == SPSystemDefinedEventMediaKeys) {
[(id)[self delegate] mediaKeyTap: nil receivedMediaKeyEvent: event];
}

View File

@ -25,6 +25,7 @@
#import <GrowlApplicationBridge.h>
#include "core/scoped_nsautorelease_pool.h"
#include "core/scoped_nsobject.h"
@interface GrowlInterface :NSObject <GrowlApplicationBridgeDelegate> {
}
@ -137,8 +138,37 @@ bool OSD::SupportsTrayPopups() {
return false;
}
namespace {
bool NotificationCenterSupported() {
return NSClassFromString(@"NSUserNotificationCenter");
}
void SendNotificationCenterMessage(NSString* title, NSString* subtitle) {
Class clazz = NSClassFromString(@"NSUserNotificationCenter");
id notification_center = [clazz defaultUserNotificationCenter];
Class user_notification_class = NSClassFromString(@"NSUserNotification");
id notification = [[user_notification_class alloc] init];
[notification setTitle: title];
[notification setSubtitle: subtitle];
[notification_center deliverNotification: notification];
}
}
void OSD::ShowMessageNative(const QString& summary, const QString& message,
const QString& icon, const QImage& image) {
Q_UNUSED(icon);
wrapper_->ShowMessage(summary, message, image);
if (NotificationCenterSupported()) {
scoped_nsobject<NSString> mac_message(
[[NSString alloc] initWithUTF8String:message.toUtf8().constData()]);
scoped_nsobject<NSString> mac_summary(
[[NSString alloc] initWithUTF8String:summary.toUtf8().constData()]);
SendNotificationCenterMessage(mac_summary.get(), mac_message.get());
} else {
wrapper_->ShowMessage(summary, message, image);
}
}