From 25ef0ff2117bd694f3ef19bf70a4e70421ce34f6 Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Mon, 1 Oct 2018 10:40:31 +0200 Subject: [PATCH] Only use system tray if it's available on the system (#6154) --- src/ui/mainwindow.cpp | 91 ++++++++++++++++++++++----------------- src/ui/systemtrayicon.cpp | 8 +++- src/widgets/osd.cpp | 6 +-- 3 files changed, 61 insertions(+), 44 deletions(-) diff --git a/src/ui/mainwindow.cpp b/src/ui/mainwindow.cpp index de62186d8..f55abdc43 100644 --- a/src/ui/mainwindow.cpp +++ b/src/ui/mainwindow.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -792,20 +793,22 @@ MainWindow::MainWindow(Application* app, SystemTrayIcon* tray_icon, OSD* osd, mac::SetApplicationHandler(this); #endif // Tray icon - tray_icon_->SetupMenu(ui_->action_previous_track, ui_->action_play_pause, - ui_->action_stop, ui_->action_stop_after_this_track, - ui_->action_next_track, ui_->action_mute, - ui_->action_love, ui_->action_quit); - connect(tray_icon_, SIGNAL(PlayPause()), app_->player(), SLOT(PlayPause())); - connect(tray_icon_, SIGNAL(SeekForward()), app_->player(), - SLOT(SeekForward())); - connect(tray_icon_, SIGNAL(SeekBackward()), app_->player(), - SLOT(SeekBackward())); - connect(tray_icon_, SIGNAL(NextTrack()), app_->player(), SLOT(Next())); - connect(tray_icon_, SIGNAL(PreviousTrack()), app_->player(), - SLOT(Previous())); - connect(tray_icon_, SIGNAL(ShowHide()), SLOT(ToggleShowHide())); - connect(tray_icon_, SIGNAL(ChangeVolume(int)), SLOT(VolumeWheelEvent(int))); + if (tray_icon_) { + tray_icon_->SetupMenu(ui_->action_previous_track, ui_->action_play_pause, + ui_->action_stop, ui_->action_stop_after_this_track, + ui_->action_next_track, ui_->action_mute, + ui_->action_love, ui_->action_quit); + connect(tray_icon_, SIGNAL(PlayPause()), app_->player(), SLOT(PlayPause())); + connect(tray_icon_, SIGNAL(SeekForward()), app_->player(), + SLOT(SeekForward())); + connect(tray_icon_, SIGNAL(SeekBackward()), app_->player(), + SLOT(SeekBackward())); + connect(tray_icon_, SIGNAL(NextTrack()), app_->player(), SLOT(Next())); + connect(tray_icon_, SIGNAL(PreviousTrack()), app_->player(), + SLOT(Previous())); + connect(tray_icon_, SIGNAL(ShowHide()), SLOT(ToggleShowHide())); + connect(tray_icon_, SIGNAL(ChangeVolume(int)), SLOT(VolumeWheelEvent(int))); + } // Windows 7 thumbbar buttons thumbbar_->SetActions(QList() @@ -1015,7 +1018,10 @@ MainWindow::MainWindow(Application* app, SystemTrayIcon* tray_icon, OSD* osd, // Reload playlist settings, for BG and glowing ui_->playlist->view()->ReloadSettings(); -#ifndef Q_OS_DARWIN +#ifdef Q_OS_DARWIN + // Always show mainwindow on startup on OS X. + show(); +#else StartupBehaviour behaviour = StartupBehaviour( settings_.value("startupbehaviour", Startup_Remember).toInt()); bool hidden = settings_.value("hidden", false).toBool(); @@ -1034,13 +1040,11 @@ MainWindow::MainWindow(Application* app, SystemTrayIcon* tray_icon, OSD* osd, // Force the window to show in case somehow the config has tray and window set // to hide - if (hidden && !tray_icon_->IsVisible()) { + if (hidden && (!QSystemTrayIcon::isSystemTrayAvailable() || !tray_icon_ || + !tray_icon_->IsVisible())) { settings_.setValue("hidden", false); show(); } -#else // Q_OS_DARWIN - // Always show mainwindow on startup on OS X. - show(); #endif QShortcut* close_window_shortcut = new QShortcut(this); @@ -1071,10 +1075,13 @@ MainWindow::~MainWindow() { void MainWindow::ReloadSettings() { #ifndef Q_OS_DARWIN - bool show_tray = settings_.value("showtray", true).toBool(); + bool show_tray = + settings_.value("showtray", QSystemTrayIcon::isSystemTrayAvailable()) + .toBool(); - tray_icon_->SetVisible(show_tray); - if (!show_tray && !isVisible()) show(); + if (tray_icon_) tray_icon_->SetVisible(show_tray); + if ((!show_tray || !QSystemTrayIcon::isSystemTrayAvailable()) && !isVisible()) + show(); #endif QSettings s; @@ -1128,13 +1135,15 @@ void MainWindow::MediaStopped() { ui_->action_play_pause->setEnabled(true); ui_->action_love->setEnabled(false); - tray_icon_->LastFMButtonLoveStateChanged(false); + if (tray_icon_) tray_icon_->LastFMButtonLoveStateChanged(false); track_position_timer_->stop(); track_slider_timer_->stop(); ui_->track_slider->SetStopped(); - tray_icon_->SetProgress(0); - tray_icon_->SetStopped(); + if (tray_icon_) { + tray_icon_->SetProgress(0); + tray_icon_->SetStopped(); + } } void MainWindow::MediaPaused() { @@ -1149,7 +1158,7 @@ void MainWindow::MediaPaused() { track_position_timer_->stop(); track_slider_timer_->stop(); - tray_icon_->SetPaused(); + if (tray_icon_) tray_icon_->SetPaused(); } void MainWindow::MediaPlaying() { @@ -1170,10 +1179,12 @@ void MainWindow::MediaPlaying() { #ifdef HAVE_LIBLASTFM bool enable_love = app_->scrobbler()->IsScrobblingEnabled(); ui_->action_love->setEnabled(enable_love); - tray_icon_->LastFMButtonLoveStateChanged(enable_love); - tray_icon_->SetPlaying(enable_play_pause, enable_love); + if (tray_icon_) { + tray_icon_->LastFMButtonLoveStateChanged(enable_love); + tray_icon_->SetPlaying(enable_play_pause, enable_love); + } #else - tray_icon_->SetPlaying(enable_play_pause); + if (tray_icon_) tray_icon_->SetPlaying(enable_play_pause); #endif track_position_timer_->start(); @@ -1183,12 +1194,12 @@ void MainWindow::MediaPlaying() { void MainWindow::VolumeChanged(int volume) { ui_->action_mute->setChecked(!volume); - tray_icon_->MuteButtonStateChanged(!volume); + if (tray_icon_) tray_icon_->MuteButtonStateChanged(!volume); } void MainWindow::SongChanged(const Song& song) { setWindowTitle(song.PrettyTitleWithArtist()); - tray_icon_->SetProgress(0); + if (tray_icon_) tray_icon_->SetProgress(0); #ifdef HAVE_LIBLASTFM if (ui_->action_toggle_scrobbling->isVisible()) @@ -1237,14 +1248,14 @@ void MainWindow::ScrobblingEnabledChanged(bool value) { } ui_->action_love->setEnabled(value); - tray_icon_->LastFMButtonLoveStateChanged(value); + if (tray_icon_) tray_icon_->LastFMButtonLoveStateChanged(value); } #endif void MainWindow::LastFMButtonVisibilityChanged(bool value) { ui_->action_love->setVisible(value); ui_->last_fm_controls->setVisible(value); - tray_icon_->LastFMButtonVisibilityChanged(value); + if (tray_icon_) tray_icon_->LastFMButtonVisibilityChanged(value); } void MainWindow::ScrobbleButtonVisibilityChanged(bool value) { @@ -1414,7 +1425,9 @@ void MainWindow::closeEvent(QCloseEvent* event) { QSettings s; s.beginGroup(kSettingsGroup); - bool keep_running = s.value("keeprunning", tray_icon_->IsVisible()).toBool(); + bool keep_running(false); + if (tray_icon_) + keep_running = s.value("keeprunning", tray_icon_->IsVisible()).toBool(); if (keep_running && event->spontaneous()) { event->ignore(); @@ -1448,7 +1461,7 @@ void MainWindow::Seeked(qlonglong microseconds) { const int length = app_->player()->GetCurrentItem()->Metadata().length_nanosec() / kNsecPerSec; - tray_icon_->SetProgress(double(position) / length * 100); + if (tray_icon_) tray_icon_->SetProgress(double(position) / length * 100); // if we seeked, scrobbling is canceled, update the icon if (ui_->action_toggle_scrobbling->isVisible()) SetToggleScrobblingIcon(true); @@ -1507,7 +1520,7 @@ void MainWindow::UpdateTrackPosition() { if (position % 10 == 0) { qLog(Debug) << "position" << position << "scrobble point" << scrobble_point << "status" << playlist->get_lastfm_status(); - tray_icon_->SetProgress(double(position) / length * 100); + if (tray_icon_) tray_icon_->SetProgress(double(position) / length * 100); // if we're waiting for the scrobble point, update the icon #ifdef HAVE_LIBLASTFM @@ -1536,13 +1549,13 @@ void MainWindow::UpdateTrackSliderPosition() { #ifdef HAVE_LIBLASTFM void MainWindow::ScrobbledRadioStream() { ui_->action_love->setEnabled(true); - tray_icon_->LastFMButtonLoveStateChanged(true); + if (tray_icon_) tray_icon_->LastFMButtonLoveStateChanged(true); } void MainWindow::Love() { app_->scrobbler()->Love(); ui_->action_love->setEnabled(false); - tray_icon_->LastFMButtonLoveStateChanged(false); + if (tray_icon_) tray_icon_->LastFMButtonLoveStateChanged(false); } #endif @@ -2775,7 +2788,7 @@ void MainWindow::Exit() { if (app_->player()->GetState() == Engine::Playing) { app_->player()->Stop(); hide(); - tray_icon_->SetVisible(false); + if (tray_icon_) tray_icon_->SetVisible(false); return; // Don't quit the application now: wait for the fadeout finished // signal } diff --git a/src/ui/systemtrayicon.cpp b/src/ui/systemtrayicon.cpp index 069143197..7aaeb5e51 100644 --- a/src/ui/systemtrayicon.cpp +++ b/src/ui/systemtrayicon.cpp @@ -21,8 +21,9 @@ #include #include -#include #include +#include +#include #include #include @@ -105,6 +106,9 @@ SystemTrayIcon* SystemTrayIcon::CreateSystemTrayIcon(QObject* parent) { #ifdef Q_OS_DARWIN return new MacSystemTrayIcon(parent); #else - return new QtSystemTrayIcon(parent); + if (QSystemTrayIcon::isSystemTrayAvailable()) + return new QtSystemTrayIcon(parent); + else + return nullptr; #endif } diff --git a/src/widgets/osd.cpp b/src/widgets/osd.cpp index 54ef30881..2d81eb12f 100644 --- a/src/widgets/osd.cpp +++ b/src/widgets/osd.cpp @@ -95,7 +95,7 @@ void OSD::ReshowCurrentSong() { void OSD::AlbumArtLoaded(const Song& song, const QString& uri, const QImage& image) { // Don't change tray icon details if it's a preview - if (!preview_mode_) { + if (!preview_mode_ && tray_icon_) { tray_icon_->SetNowPlaying(song, uri); } @@ -159,7 +159,7 @@ void OSD::Paused() { } void OSD::Stopped() { - tray_icon_->ClearNowPlaying(); + if (tray_icon_) tray_icon_->ClearNowPlaying(); if (ignore_next_stopped_) { ignore_next_stopped_ = false; return; @@ -215,7 +215,7 @@ void OSD::ShowMessage(const QString& summary, const QString& message, #ifndef Q_OS_DARWIN case TrayPopup: - tray_icon_->ShowPopup(summary, message, timeout_msec_); + if (tray_icon_) tray_icon_->ShowPopup(summary, message, timeout_msec_); break; #endif