Check if system has system tray using QSystemTrayIcon::isSystemTrayAvailable()

This commit is contained in:
Jonas Kvinge 2018-09-30 15:33:27 +02:00
parent 6d888eb51a
commit 298dbe96c8
5 changed files with 79 additions and 55 deletions

View File

@ -29,6 +29,7 @@
#include <QApplication>
#include <QObject>
#include <QWidget>
#include <QSystemTrayIcon>
#include <QSortFilterProxyModel>
#include <QByteArray>
#include <QDir>
@ -574,15 +575,16 @@ MainWindow::MainWindow(Application *app, SystemTrayIcon *tray_icon, OSD *osd, co
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_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_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<QAction*>() << ui_->action_previous_track << ui_->action_play_pause << ui_->action_stop << ui_->action_next_track << nullptr); // spacer
@ -720,31 +722,32 @@ MainWindow::MainWindow(Application *app, SystemTrayIcon *tray_icon, OSD *osd, co
// Reload playlist settings, for BG and glowing
ui_->playlist->view()->ReloadSettings();
#ifndef Q_OS_MACOS
#ifdef Q_OS_MACOS // Always show mainwindow on startup if on macos
show();
#else
QSettings settings;
settings.beginGroup(BehaviourSettingsPage::kSettingsGroup);
StartupBehaviour behaviour = StartupBehaviour(settings.value("startupbehaviour", Startup_Remember).toInt());
settings.endGroup();
bool hidden = settings_.value("hidden", false).toBool();
switch (behaviour) {
case Startup_AlwaysHide: hide(); break;
case Startup_AlwaysShow: show(); break;
case Startup_Remember:
setVisible(!hidden);
break;
}
// 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())) {
hidden = false;
settings_.setValue("hidden", false);
show();
}
#else // Q_OS_MACOS
// Always show mainwindow on startup on OS X.
show();
else {
switch (behaviour) {
case Startup_AlwaysHide:
hide();
break;
case Startup_AlwaysShow:
show();
break;
case Startup_Remember:
setVisible(!hidden);
break;
}
}
#endif
QShortcut *close_window_shortcut = new QShortcut(this);
@ -759,7 +762,7 @@ MainWindow::MainWindow(Application *app, SystemTrayIcon *tray_icon, OSD *osd, co
qLog(Debug) << "Started";
RefreshStyleSheet();
initialised_ = true;
}
@ -774,13 +777,11 @@ void MainWindow::ReloadSettings() {
QSettings settings;
#ifndef Q_OS_MACOS
settings.beginGroup(BehaviourSettingsPage::kSettingsGroup);
bool showtrayicon = settings.value("showtrayicon", true).toBool();
settings.endGroup();
tray_icon_->SetVisible(showtrayicon);
if (!showtrayicon && !isVisible()) show();
if (tray_icon_) tray_icon_->SetVisible(showtrayicon);
if ((!showtrayicon || !QSystemTrayIcon::isSystemTrayAvailable()) && !isVisible()) show();
#endif
settings.beginGroup(PlaylistSettingsPage::kSettingsGroup);
@ -828,8 +829,10 @@ void MainWindow::MediaStopped() {
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();
}
song_playing_ = Song();
song_ = Song();
@ -849,7 +852,7 @@ void MainWindow::MediaPaused() {
track_position_timer_->stop();
track_slider_timer_->stop();
tray_icon_->SetPaused();
if (tray_icon_) tray_icon_->SetPaused();
}
@ -866,7 +869,7 @@ void MainWindow::MediaPlaying() {
bool can_seek = !(app_->player()->GetCurrentItem()->options() & PlaylistItem::SeekDisabled);
ui_->track_slider->SetCanSeek(can_seek);
tray_icon_->SetPlaying(enable_play_pause);
if (tray_icon_) tray_icon_->SetPlaying(enable_play_pause);
track_position_timer_->start();
track_slider_timer_->start();
@ -876,7 +879,7 @@ 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) {
@ -884,7 +887,7 @@ void MainWindow::SongChanged(const Song &song) {
song_playing_ = song;
song_ = song;
setWindowTitle(song.PrettyTitleWithArtist());
tray_icon_->SetProgress(0);
if (tray_icon_) tray_icon_->SetProgress(0);
}
@ -1078,7 +1081,7 @@ void MainWindow::closeEvent(QCloseEvent *event) {
bool keep_running = settings.value("keeprunning", false).toBool();
settings.endGroup();
if (keep_running && event->spontaneous()) {
if (keep_running && event->spontaneous() && QSystemTrayIcon::isSystemTrayAvailable()) {
event->ignore();
SetHiddenInTray(true);
}
@ -1111,7 +1114,7 @@ void MainWindow::Seeked(qlonglong microseconds) {
const int position = microseconds / kUsecPerSec;
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);
}
@ -1132,9 +1135,8 @@ void MainWindow::UpdateTrackPosition() {
}
// Update the tray icon every 10 seconds
if (position % 10 == 0) {
tray_icon_->SetProgress(double(position) / length * 100);
}
if (position % 10 == 0 && tray_icon_) tray_icon_->SetProgress(double(position) / length * 100);
}
void MainWindow::UpdateTrackSliderPosition() {
@ -2202,7 +2204,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
}
}

View File

@ -44,8 +44,7 @@ QtSystemTrayIcon::QtSystemTrayIcon(QObject *parent)
action_play_pause_(nullptr),
action_stop_(nullptr),
action_stop_after_this_track_(nullptr),
action_mute_(nullptr)
{
action_mute_(nullptr) {
QIcon theme_icon = IconLoader::Load("strawberry", 48);
QIcon theme_icon_grey = IconLoader::Load("strawberry-grey", 48);

View File

@ -23,6 +23,7 @@
#include <cmath>
#include <QObject>
#include <QSystemTrayIcon>
#include <QPixmap>
#include <QPainter>
#include <QPoint>
@ -105,10 +106,11 @@ void SystemTrayIcon::SetStopped() {
UpdateIcon();
}
SystemTrayIcon* SystemTrayIcon::CreateSystemTrayIcon(QObject *parent) {
SystemTrayIcon *SystemTrayIcon::CreateSystemTrayIcon(QObject *parent) {
#ifdef Q_OS_MACOS
return new MacSystemTrayIcon(parent);
#else
return new QtSystemTrayIcon(parent);
if (QSystemTrayIcon::isSystemTrayAvailable()) return new QtSystemTrayIcon(parent);
else return nullptr;
#endif
}

View File

@ -22,6 +22,7 @@
#include <QVariant>
#include <QSettings>
#include <QSystemTrayIcon>
#include <QCheckBox>
#include <QRadioButton>
#include <QSpinBox>
@ -46,6 +47,15 @@ BehaviourSettingsPage::BehaviourSettingsPage(SettingsDialog *dialog) : SettingsP
#ifdef Q_OS_MACOS
ui_->checkbox_showtrayicon->setEnabled(false);
ui_->groupbox_startup->setEnabled(false);
#else
if (QSystemTrayIcon::isSystemTrayAvailable()) {
ui_->checkbox_showtrayicon->setEnabled(true);
ui_->groupbox_startup->setEnabled(true);
}
else {
ui_->checkbox_showtrayicon->setEnabled(false);
ui_->groupbox_startup->setEnabled(false);
}
#endif
}
@ -59,9 +69,22 @@ void BehaviourSettingsPage::Load() {
QSettings s;
s.beginGroup(kSettingsGroup);
ui_->checkbox_showtrayicon->setChecked(s.value("showtrayicon", true).toBool());
ui_->checkbox_scrolltrayicon->setChecked(s.value("scrolltrayicon", ui_->checkbox_showtrayicon->isChecked()).toBool());
ui_->checkbox_keeprunning->setChecked(s.value("keeprunning", false).toBool());
#ifdef Q_OS_MACOS
ui_->checkbox_showtrayicon->setChecked(false);
ui_->checkbox_scrolltrayicon->setChecked(false);
ui_->checkbox_keeprunning->setChecked(false);
#else
if (QSystemTrayIcon::isSystemTrayAvailable()) {
ui_->checkbox_showtrayicon->setChecked(s.value("showtrayicon", true).toBool());
ui_->checkbox_scrolltrayicon->setChecked(s.value("scrolltrayicon", ui_->checkbox_showtrayicon->isChecked()).toBool());
ui_->checkbox_keeprunning->setChecked(s.value("keeprunning", false).toBool());
}
else {
ui_->checkbox_showtrayicon->setChecked(false);
ui_->checkbox_scrolltrayicon->setChecked(false);
ui_->checkbox_keeprunning->setChecked(false);
}
#endif
MainWindow::StartupBehaviour behaviour = MainWindow::StartupBehaviour(s.value("startupbehaviour", MainWindow::Startup_Remember).toInt());
switch (behaviour) {

View File

@ -112,9 +112,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_) {
tray_icon_->SetNowPlaying(song, uri);
}
if (!preview_mode_ && tray_icon_) tray_icon_->SetNowPlaying(song, uri);
last_song_ = song;
last_image_ = image;
@ -179,7 +177,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;
@ -222,7 +220,7 @@ void OSD::ShowMessage(const QString &summary, const QString &message, const QStr
#ifndef Q_OS_MACOS
case TrayPopup:
tray_icon_->ShowPopup(summary, message, timeout_msec_);
if (tray_icon_) tray_icon_->ShowPopup(summary, message, timeout_msec_);
break;
#endif