Implement native dock menu on Mac.

Fixes issue #349
This commit is contained in:
John Maguire 2010-06-22 14:09:13 +00:00
parent c357b2d548
commit 15b8b90556
7 changed files with 238 additions and 9 deletions

View File

@ -360,7 +360,9 @@ endif(ENABLE_VISUALISATIONS)
if(APPLE)
list(APPEND SOURCES widgets/osd_mac.mm)
list(APPEND SOURCES core/macglobalshortcutbackend.mm)
list(APPEND SOURCES ui/macsystemtrayicon.mm)
list(APPEND HEADERS core/macglobalshortcutbackend.h)
list(APPEND HEADERS ui/macsystemtrayicon.h)
include_directories(${GROWL}/Headers)
else(APPLE)
if(WIN32)

View File

@ -45,11 +45,14 @@
@interface AppDelegate :NSObject {
#endif
PlatformInterface* application_handler_;
NSMenu* dock_menu_;
}
- (id) initWithHandler: (PlatformInterface*)handler;
// NSApplicationDelegate
- (BOOL) applicationShouldHandleReopen: (NSApplication*)app hasVisibleWindows:(BOOL)flag;
- (NSMenu*) applicationDockMenu: (NSApplication*)sender;
- (void) setDockMenu: (NSMenu*)menu;
@end
@implementation AppDelegate
@ -57,6 +60,7 @@
- (id) init {
if ((self = [super init])) {
application_handler_ = nil;
dock_menu_ = nil;
}
return self;
}
@ -73,6 +77,14 @@
return YES;
}
- (void) setDockMenu: (NSMenu*)menu {
dock_menu_ = menu;
}
- (NSMenu*) applicationDockMenu: (NSApplication*)sender {
return dock_menu_;
}
- (BOOL) application: (NSApplication*)app openFile:(NSString*)filename {
qDebug() << "Wants to open:" << [filename UTF8String];

View File

@ -167,10 +167,6 @@ int main(int argc, char *argv[]) {
// Window
MainWindow w(&network, options.engine());
#ifdef Q_OS_DARWIN
mac::SetApplicationHandler(&w);
#endif
QObject::connect(&a, SIGNAL(messageReceived(QByteArray)), &w, SLOT(CommandlineOptionsReceived(QByteArray)));
w.CommandlineOptionsReceived(options);

View File

@ -0,0 +1,63 @@
/* This file is part of Clementine.
Clementine is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Clementine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MACSYSTEMTRAYICON_H
#define MACSYSTEMTRAYICON_H
#include "systemtrayicon.h"
#include <QSignalMapper>
#include <boost/noncopyable.hpp>
#include <boost/scoped_ptr.hpp>
class MacSystemTrayIconPrivate;
class MacSystemTrayIcon : public SystemTrayIcon, boost::noncopyable {
Q_OBJECT
public:
MacSystemTrayIcon(QObject* parent = 0);
~MacSystemTrayIcon();
void SetupMenu(QAction* previous, QAction* play, QAction* stop,
QAction* stop_after, QAction* next, QAction* love,
QAction* ban, QAction* quit);
bool IsVisible() const;
void SetVisible(bool visible);
void ShowPopup(const QString &summary, const QString &message, int timeout);
private:
void SetupMenuItem(QAction* action);
private slots:
void ActionChanged();
protected:
// SystemTrayIcon
void UpdateIcon();
private:
QPixmap orange_icon_;
QPixmap grey_icon_;
QSignalMapper mapper_;
boost::scoped_ptr<MacSystemTrayIconPrivate> p_;
};
#endif // MACSYSTEMTRAYICON_H

151
src/ui/macsystemtrayicon.mm Normal file
View File

@ -0,0 +1,151 @@
/* This file is part of Clementine.
Clementine is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Clementine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "macsystemtrayicon.h"
#include <QAction>
#include <QApplication>
#include <QIcon>
#include <QtDebug>
#include <AppKit/NSMenu.h>
#include <AppKit/NSMenuItem.h>
@interface Target :NSObject {
QAction* action_;
}
- (id) initWithQAction: (QAction*)action;
- (void) clicked;
@end
@implementation Target // <NSMenuValidation>
- (id) init {
return [super init];
}
- (id) initWithQAction: (QAction*)action {
action_ = action;
return self;
}
- (BOOL) validateMenuItem: (NSMenuItem*)menuItem {
// This is called when the menu is shown.
return action_->isEnabled();
}
- (void) clicked {
action_->trigger();
}
@end
class MacSystemTrayIconPrivate : boost::noncopyable {
public:
MacSystemTrayIconPrivate() {
dock_menu_ = [[NSMenu alloc] initWithTitle:@"DockMenu"];
// Don't look now.
// This must be called after our custom NSApplicationDelegate has been set.
[[NSApp delegate] setDockMenu:dock_menu_];
}
void AddMenuItem(QAction* action) {
// Strip accelarators from name.
QString text = action->text().remove("&");
NSString* title = [[NSString alloc] initWithUTF8String: text.toUtf8().constData()];
// Create an object that can receive user clicks and pass them on to the QAction.
Target* target = [[Target alloc] initWithQAction:action];
NSMenuItem* item = [[[NSMenuItem alloc]
initWithTitle:title
action:@selector(clicked)
keyEquivalent:@""] autorelease];
[item setEnabled:action->isEnabled()];
[item setTarget:target];
[dock_menu_ addItem:item];
actions_[action] = item;
}
void ActionChanged(QAction* action) {
NSMenuItem* item = actions_[action];
[[item title] release];
NSString* title = [[NSString alloc] initWithUTF8String: action->text().toUtf8().constData()];
[item setTitle:title];
}
void AddSeparator() {
NSMenuItem* separator = [NSMenuItem separatorItem];
[dock_menu_ addItem:separator];
}
private:
QMap<QAction*, NSMenuItem*> actions_;
NSMenu* dock_menu_;
};
MacSystemTrayIcon::MacSystemTrayIcon(QObject* parent)
: SystemTrayIcon(parent) {
QIcon orange(":icon_large.png");
QIcon grey(":icon_large_grey.png");
orange_icon_ = orange.pixmap(128, QIcon::Normal);
grey_icon_ = grey.pixmap(128, QIcon::Normal);
QApplication::setWindowIcon(orange_icon_);
}
MacSystemTrayIcon::~MacSystemTrayIcon() {
}
void MacSystemTrayIcon::SetupMenu(
QAction* previous, QAction* play, QAction* stop, QAction* stop_after,
QAction* next, QAction* love, QAction* ban, QAction* quit) {
p_.reset(new MacSystemTrayIconPrivate());
SetupMenuItem(previous);
SetupMenuItem(play);
SetupMenuItem(stop);
SetupMenuItem(stop_after);
SetupMenuItem(next);
p_->AddSeparator();
SetupMenuItem(love);
SetupMenuItem(ban);
Q_UNUSED(quit); // Mac already has a Quit item.
}
void MacSystemTrayIcon::SetupMenuItem(QAction* action) {
p_->AddMenuItem(action);
connect(action, SIGNAL(changed()), SLOT(ActionChanged()));
}
void MacSystemTrayIcon::ShowPopup(const QString &summary,
const QString &message, int timeout) {
}
void MacSystemTrayIcon::UpdateIcon() {
QApplication::setWindowIcon(CreateIcon(orange_icon_, grey_icon_));
}
bool MacSystemTrayIcon::IsVisible() const {
return true;
}
void MacSystemTrayIcon::SetVisible(bool) {
}
void MacSystemTrayIcon::ActionChanged() {
QAction* action = qobject_cast<QAction*>(sender());
p_->ActionChanged(action);
}

View File

@ -51,6 +51,9 @@
#include "ui/globalshortcutsdialog.h"
#include "ui/iconloader.h"
#include "ui/qtsystemtrayicon.h"
#ifdef Q_OS_DARWIN
#include "ui/macsystemtrayicon.h"
#endif
#include "ui/settingsdialog.h"
#include "ui/systemtrayicon.h"
#include "widgets/errordialog.h"
@ -99,7 +102,11 @@ const char* MainWindow::kAllFilesFilterSpec =
MainWindow::MainWindow(NetworkAccessManager* network, Engine::Type engine, QWidget *parent)
: QMainWindow(parent),
ui_(new Ui_MainWindow),
#ifdef Q_OS_DARWIN
tray_icon_(new MacSystemTrayIcon(this)),
#else
tray_icon_(new QtSystemTrayIcon(this)),
#endif
osd_(new OSD(tray_icon_, network, this)),
edit_tag_dialog_(new EditTagDialog),
about_dialog_(new About),
@ -385,6 +392,9 @@ MainWindow::MainWindow(NetworkAccessManager* network, Engine::Type engine, QWidg
connect(saved_radio, SIGNAL(ShowAddStreamDialog()),
add_stream_dialog_.get(), SLOT(show()));
#ifdef Q_OS_DARWIN
mac::SetApplicationHandler(this);
#endif
// Tray icon
tray_icon_->SetupMenu(ui_->action_previous_track,
ui_->action_play_pause,
@ -403,8 +413,6 @@ MainWindow::MainWindow(NetworkAccessManager* network, Engine::Type engine, QWidg
QAction* check_updates = ui_->menuSettings->addAction(tr("Check for updates..."));
check_updates->setMenuRole(QAction::ApplicationSpecificRole);
connect(check_updates, SIGNAL(triggered(bool)), SLOT(CheckForUpdates()));
// We use the dock instead of the system tray on mac.
qt_mac_set_dock_menu(tray_menu);
// Force this menu to be the app "Preferences".
ui_->action_configure->setMenuRole(QAction::PreferencesRole);

View File

@ -31,9 +31,6 @@ SystemTrayIcon::SystemTrayIcon(QObject* parent)
playing_icon_(":/tiny-start.png"),
paused_icon_(":/tiny-pause.png")
{
#ifdef Q_OS_DARWIN
hide();
#endif
}
QPixmap SystemTrayIcon::CreateIcon(const QPixmap& icon, const QPixmap& grey_icon) {