Fix a few mac UI bugs.

* Cmd+W closes the main window
* Clicking the dock icon shows the main window
This commit is contained in:
John Maguire 2010-04-14 21:27:27 +00:00
parent d1966aa7f6
commit aeaebdb8d6
5 changed files with 97 additions and 41 deletions

View File

@ -2,11 +2,13 @@
#define MAC_STARTUP_H
class GlobalShortcuts;
class QObject;
namespace mac {
void MacMain();
void SetShortcutHandler(GlobalShortcuts* handler);
void SetApplicationHandler(QObject* handler);
} // namespace mac

View File

@ -5,36 +5,65 @@
#include "globalshortcuts/globalshortcuts.h"
#include "mac_startup.h"
#include <QCoreApplication>
#include <QEvent>
#include <QObject>
// Capture global media keys on Mac (Cocoa only!)
// See: http://www.rogueamoeba.com/utm/2007/09/29/apple-keyboard-media-key-event-handling/
@interface MacApplication :NSApplication {
GlobalShortcuts* handler_;
@interface MacApplication :NSApplication <NSApplicationDelegate> {
GlobalShortcuts* shortcut_handler_;
QObject* application_handler_;
}
- (GlobalShortcuts*) handler;
- (void) SetHandler: (GlobalShortcuts*)handler;
- (GlobalShortcuts*) shortcut_handler;
- (void) SetShortcutHandler: (GlobalShortcuts*)handler;
-(void) mediaKeyEvent: (int)key state: (BOOL)state repeat: (BOOL)repeat;
- (QObject*) application_handler;
- (void) SetApplicationHandler: (QObject*)handler;
- (void) mediaKeyEvent: (int)key state: (BOOL)state repeat: (BOOL)repeat;
// NSApplicationDelegate
- (BOOL) applicationShouldHandleReopen: (NSApplication*)app hasVisibleWindows:(BOOL)flag;
@end
@implementation MacApplication
- (id) init {
if ((self = [super init])) {
[self SetHandler:nil];
[self SetShortcutHandler:nil];
[self SetApplicationHandler:nil];
[self setDelegate:self];
}
return self;
}
- (GlobalShortcuts*) handler {
return handler_;
- (GlobalShortcuts*) shortcut_handler {
return shortcut_handler_;
}
- (void) SetHandler: (GlobalShortcuts*)handler {
handler_ = handler;
- (void) SetShortcutHandler: (GlobalShortcuts*)handler {
shortcut_handler_ = handler;
}
- (QObject*) application_handler {
return application_handler_;
}
- (void) SetApplicationHandler: (QObject*)handler {
application_handler_ = handler;
}
- (BOOL) applicationShouldHandleReopen: (NSApplication*)app hasVisibleWindows:(BOOL)flag {
if (application_handler_) {
qApp->postEvent(application_handler_, new QEvent(QEvent::User));
}
return YES;
}
-(void) sendEvent: (NSEvent*)event {
if ([event type] == NSSystemDefined && [event subtype] == 8) {
int keycode = (([event data1] & 0xFFFF0000) >> 16);
@ -49,21 +78,21 @@
}
-(void) mediaKeyEvent: (int)key state: (BOOL)state repeat: (BOOL)repeat {
if (!handler_) {
if (!shortcut_handler_) {
return;
}
if (state == 0) {
switch (key) {
case NX_KEYTYPE_PLAY:
// Play pressed.
handler_->MacMediaKeyPressed("Play");
shortcut_handler_->MacMediaKeyPressed("Play");
break;
case NX_KEYTYPE_FAST:
// Next pressed.
handler_->MacMediaKeyPressed("Next");
shortcut_handler_->MacMediaKeyPressed("Next");
break;
case NX_KEYTYPE_REWIND:
handler_->MacMediaKeyPressed("Previous");
shortcut_handler_->MacMediaKeyPressed("Previous");
break;
default:
break;
@ -81,7 +110,11 @@ void MacMain() {
}
void SetShortcutHandler(GlobalShortcuts* handler) {
[NSApp SetHandler: handler];
[NSApp SetShortcutHandler: handler];
}
void SetApplicationHandler(QObject* handler) {
[NSApp SetApplicationHandler: handler];
}
} // namespace mac

View File

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

View File

@ -56,6 +56,7 @@
#include <QSignalMapper>
#include <QFileDialog>
#include <QTimer>
#include <QShortcut>
#include <cmath>
@ -79,19 +80,19 @@ MainWindow::MainWindow(QNetworkAccessManager* network, QWidget *parent)
osd_(new OSD(tray_icon_, this)),
track_slider_(new TrackSlider(this)),
playlist_sequence_(new PlaylistSequence(this)),
edit_tag_dialog_(new EditTagDialog(this)),
edit_tag_dialog_(new EditTagDialog),
multi_loading_indicator_(new MultiLoadingIndicator(this)),
library_config_dialog_(new LibraryConfigDialog(this)),
about_dialog_(new About(this)),
library_config_dialog_(new LibraryConfigDialog),
about_dialog_(new About),
radio_model_(new RadioModel(this)),
playlist_(new Playlist(this)),
player_(new Player(playlist_, radio_model_->GetLastFMService(), this)),
library_(new Library(player_->GetEngine(), this)),
global_shortcuts_(new GlobalShortcuts(this)),
settings_dialog_(new SettingsDialog(this)),
add_stream_dialog_(new AddStreamDialog(this)),
settings_dialog_(new SettingsDialog),
add_stream_dialog_(new AddStreamDialog),
cover_manager_(new AlbumCoverManager(network, this)),
group_by_dialog_(new GroupByDialog(this)),
group_by_dialog_(new GroupByDialog),
equalizer_(new Equalizer(this)),
playlist_menu_(new QMenu(this)),
library_sort_model_(new QSortFilterProxyModel(this)),
@ -154,8 +155,8 @@ MainWindow::MainWindow(QNetworkAccessManager* network, QWidget *parent)
connect(ui_.action_renumber_tracks, SIGNAL(triggered()), SLOT(RenumberTracks()));
connect(ui_.action_selection_set_value, SIGNAL(triggered()), SLOT(SelectionSetValue()));
connect(ui_.action_edit_value, SIGNAL(triggered()), SLOT(EditValue()));
connect(ui_.action_configure, SIGNAL(triggered()), settings_dialog_, SLOT(show()));
connect(ui_.action_about, SIGNAL(triggered()), about_dialog_, SLOT(show()));
connect(ui_.action_configure, SIGNAL(triggered()), settings_dialog_.get(), SLOT(show()));
connect(ui_.action_about, SIGNAL(triggered()), about_dialog_.get(), SLOT(show()));
connect(ui_.action_shuffle, SIGNAL(triggered()), playlist_, SLOT(Shuffle()));
connect(ui_.action_open_media, SIGNAL(triggered()), SLOT(AddMedia()));
connect(ui_.action_add_media, SIGNAL(triggered()), SLOT(AddMedia()));
@ -218,7 +219,7 @@ MainWindow::MainWindow(QNetworkAccessManager* network, QWidget *parent)
connect(library_, SIGNAL(Error(QString)), SLOT(ReportError(QString)));
connect(ui_.library_view, SIGNAL(doubleClicked(QModelIndex)), SLOT(AddLibraryItemToPlaylist(QModelIndex)));
connect(ui_.library_view, SIGNAL(AddToPlaylist(QModelIndex)), SLOT(AddLibraryItemToPlaylist(QModelIndex)));
connect(ui_.library_view, SIGNAL(ShowConfigDialog()), library_config_dialog_, SLOT(show()));
connect(ui_.library_view, SIGNAL(ShowConfigDialog()), library_config_dialog_.get(), SLOT(show()));
connect(library_, SIGNAL(TotalSongCountUpdated(int)), ui_.library_view, SLOT(TotalSongCountUpdated(int)));
connect(library_, SIGNAL(ScanStarted()), SLOT(LibraryScanStarted()));
connect(library_, SIGNAL(ScanFinished()), SLOT(LibraryScanFinished()));
@ -284,10 +285,10 @@ MainWindow::MainWindow(QNetworkAccessManager* network, QWidget *parent)
connect(group_by_group_, SIGNAL(triggered(QAction*)), SLOT(GroupByClicked(QAction*)));
connect(library_, SIGNAL(GroupingChanged(Library::Grouping)),
group_by_dialog_, SLOT(LibraryGroupingChanged(Library::Grouping)));
group_by_dialog_.get(), SLOT(LibraryGroupingChanged(Library::Grouping)));
connect(library_, SIGNAL(GroupingChanged(Library::Grouping)),
SLOT(LibraryGroupingChanged(Library::Grouping)));
connect(group_by_dialog_, SIGNAL(Accepted(Library::Grouping)),
connect(group_by_dialog_.get(), SIGNAL(Accepted(Library::Grouping)),
library_, SLOT(SetGroupBy(Library::Grouping)));
// Library config menu
@ -295,9 +296,9 @@ MainWindow::MainWindow(QNetworkAccessManager* network, QWidget *parent)
library_menu->addMenu(filter_age_menu);
library_menu->addMenu(group_by_menu);
library_menu->addSeparator();
library_menu->addAction(tr("Configure library..."), library_config_dialog_, SLOT(show()));
library_menu->addAction(tr("Configure library..."), library_config_dialog_.get(), SLOT(show()));
ui_.library_options->setMenu(library_menu);
connect(library_config_dialog_, SIGNAL(accepted()), ui_.library_view, SLOT(ReloadSettings()));
connect(library_config_dialog_.get(), SIGNAL(accepted()), ui_.library_view, SLOT(ReloadSettings()));
// Playlist menu
playlist_play_pause_ = playlist_menu_->addAction(tr("Play"), this, SLOT(PlaylistPlay()));
@ -356,14 +357,14 @@ MainWindow::MainWindow(QNetworkAccessManager* network, QWidget *parent)
connect(global_shortcuts_, SIGNAL(Previous()), ui_.action_previous_track, SLOT(trigger()));
// Settings
connect(settings_dialog_, SIGNAL(accepted()), SLOT(ReloadSettings()));
connect(settings_dialog_, SIGNAL(accepted()), player_, SLOT(ReloadSettings()));
connect(settings_dialog_, SIGNAL(accepted()), osd_, SLOT(ReloadSettings()));
connect(settings_dialog_, SIGNAL(accepted()), ui_.library_view, SLOT(ReloadSettings()));
connect(settings_dialog_, SIGNAL(accepted()), player_->GetEngine(), SLOT(ReloadSettings()));
connect(settings_dialog_.get(), SIGNAL(accepted()), SLOT(ReloadSettings()));
connect(settings_dialog_.get(), SIGNAL(accepted()), player_, SLOT(ReloadSettings()));
connect(settings_dialog_.get(), SIGNAL(accepted()), osd_, SLOT(ReloadSettings()));
connect(settings_dialog_.get(), SIGNAL(accepted()), ui_.library_view, SLOT(ReloadSettings()));
connect(settings_dialog_.get(), SIGNAL(accepted()), player_->GetEngine(), SLOT(ReloadSettings()));
// Add stream dialog
connect(add_stream_dialog_, SIGNAL(accepted()), SLOT(AddStreamAccepted()));
connect(add_stream_dialog_.get(), SIGNAL(accepted()), SLOT(AddStreamAccepted()));
// Analyzer
ui_.analyzer->set_engine(player_->GetEngine());
@ -404,6 +405,7 @@ MainWindow::MainWindow(QNetworkAccessManager* network, QWidget *parent)
Library::GroupBy(settings_.value("group_by2", int(Library::GroupBy_Album)).toInt()),
Library::GroupBy(settings_.value("group_by3", int(Library::GroupBy_None)).toInt())));
#ifndef Q_OS_DARWIN
StartupBehaviour behaviour =
StartupBehaviour(settings_.value("startupbehaviour", Startup_Remember).toInt());
bool hidden = settings_.value("hidden", false).toBool();
@ -415,7 +417,6 @@ MainWindow::MainWindow(QNetworkAccessManager* network, QWidget *parent)
case Startup_Remember: setVisible(!hidden); break;
}
#ifndef Q_OS_DARWIN
if (show_tray)
tray_icon_->show();
@ -429,6 +430,10 @@ MainWindow::MainWindow(QNetworkAccessManager* network, QWidget *parent)
show();
#endif
QShortcut* close_window_shortcut = new QShortcut(this);
close_window_shortcut->setKey(Qt::CTRL + Qt::Key_W);
connect(close_window_shortcut, SIGNAL(activated()), SLOT(hide()));
library_->Init();
library_->StartThreads();
}
@ -983,3 +988,11 @@ void MainWindow::ForceShowOSD(const Song &song) {
osd_->ForceShowNextNotification();
osd_->SongChanged(song);
}
bool MainWindow::event(QEvent* event) {
if (event->type() == QEvent::User) {
show();
return true;
}
return QMainWindow::event(event);
}

View File

@ -17,6 +17,8 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <boost/scoped_ptr.hpp>
#include <QMainWindow>
#include <QSettings>
#include <QSystemTrayIcon>
@ -72,6 +74,8 @@ class MainWindow : public QMainWindow {
void resizeEvent(QResizeEvent* event);
void closeEvent(QCloseEvent* event);
bool event(QEvent* event);
private slots:
void QueueFiles(const QList<QUrl>& urls);
void FilePathChanged(const QString& path);
@ -134,10 +138,10 @@ class MainWindow : public QMainWindow {
OSD* osd_;
TrackSlider* track_slider_;
PlaylistSequence* playlist_sequence_;
EditTagDialog* edit_tag_dialog_;
boost::scoped_ptr<EditTagDialog> edit_tag_dialog_;
MultiLoadingIndicator* multi_loading_indicator_;
LibraryConfigDialog* library_config_dialog_;
About* about_dialog_;
boost::scoped_ptr<LibraryConfigDialog> library_config_dialog_;
boost::scoped_ptr<About> about_dialog_;
RadioModel* radio_model_;
Playlist* playlist_;
@ -145,10 +149,10 @@ class MainWindow : public QMainWindow {
Library* library_;
GlobalShortcuts* global_shortcuts_;
SettingsDialog* settings_dialog_;
AddStreamDialog* add_stream_dialog_;
boost::scoped_ptr<SettingsDialog> settings_dialog_;
boost::scoped_ptr<AddStreamDialog> add_stream_dialog_;
AlbumCoverManager* cover_manager_;
GroupByDialog* group_by_dialog_;
boost::scoped_ptr<GroupByDialog> group_by_dialog_;
Equalizer* equalizer_;
QMenu* playlist_menu_;