Have QSystemTrayIcon as member instead of inheriting

Favor composition over inheritence
This commit is contained in:
Nicolas Fella 2024-06-10 00:20:54 +02:00 committed by Bart De Vries
parent ebbed82703
commit ab24e3177d
2 changed files with 21 additions and 22 deletions

View File

@ -17,17 +17,17 @@
#include "kmediasession.h" #include "kmediasession.h"
#include "settingsmanager.h" #include "settingsmanager.h"
SystrayIcon::SystrayIcon(QObject *parent) SystrayIcon::SystrayIcon()
: QSystemTrayIcon(parent) : QObject()
{ {
setIconColor(intToIconColorEnum(SettingsManager::self()->trayIconType())); setIconColor(intToIconColorEnum(SettingsManager::self()->trayIconType()));
setToolTip(i18nc("@info:tooltip", m_trayIcon.setToolTip(i18nc("@info:tooltip",
"Kasts\n" "Kasts\n"
"Middle-click to play/pause")); "Middle-click to play/pause"));
QMenu *menu = new QMenu(); QMenu *menu = new QMenu();
connect(this, &QSystemTrayIcon::activated, this, [this](QSystemTrayIcon::ActivationReason reason) { connect(&m_trayIcon, &QSystemTrayIcon::activated, this, [this](QSystemTrayIcon::ActivationReason reason) {
if (reason == QSystemTrayIcon::Trigger) { if (reason == QSystemTrayIcon::Trigger) {
Q_EMIT raiseWindow(); Q_EMIT raiseWindow();
} else if (reason == QSystemTrayIcon::MiddleClick) { } else if (reason == QSystemTrayIcon::MiddleClick) {
@ -37,9 +37,9 @@ SystrayIcon::SystrayIcon(QObject *parent)
connect(SettingsManager::self(), &SettingsManager::showTrayIconChanged, this, [this]() { connect(SettingsManager::self(), &SettingsManager::showTrayIconChanged, this, [this]() {
if (SettingsManager::self()->showTrayIcon()) { if (SettingsManager::self()->showTrayIcon()) {
show(); m_trayIcon.show();
} else { } else {
hide(); m_trayIcon.hide();
} }
}); });
@ -120,15 +120,15 @@ SystrayIcon::SystrayIcon(QObject *parent)
connect(quitAction, &QAction::triggered, QCoreApplication::instance(), QCoreApplication::quit); connect(quitAction, &QAction::triggered, QCoreApplication::instance(), QCoreApplication::quit);
menu->addAction(quitAction); menu->addAction(quitAction);
setContextMenu(menu); m_trayIcon.setContextMenu(menu);
if (SettingsManager::self()->showTrayIcon()) { if (SettingsManager::self()->showTrayIcon()) {
show(); m_trayIcon.show();
} }
} }
#else #else
SystrayIcon::SystrayIcon(QObject *parent) SystrayIcon::SystrayIcon()
: QObject(parent) : QObject()
{ {
} }
#endif #endif
@ -148,13 +148,13 @@ void SystrayIcon::setIconColor(SystrayIcon::IconColor iconColor)
// do not specify svg-extension; icon will not be visible due to [QTBUG-53550] // do not specify svg-extension; icon will not be visible due to [QTBUG-53550]
switch (iconColor) { switch (iconColor) {
case SystrayIcon::IconColor::Colorful: case SystrayIcon::IconColor::Colorful:
setIcon(QIcon(QStringLiteral(":/icons/kasts"))); m_trayIcon.setIcon(QIcon(QStringLiteral(":/icons/kasts")));
break; break;
case SystrayIcon::IconColor::Light: case SystrayIcon::IconColor::Light:
setIcon(QIcon(QStringLiteral(":/icons/kasts-tray-light"))); m_trayIcon.setIcon(QIcon(QStringLiteral(":/icons/kasts-tray-light")));
break; break;
case SystrayIcon::IconColor::Dark: case SystrayIcon::IconColor::Dark:
setIcon(QIcon(QStringLiteral(":/icons/kasts-tray-dark"))); m_trayIcon.setIcon(QIcon(QStringLiteral(":/icons/kasts-tray-dark")));
break; break;
} }
#endif #endif

View File

@ -12,12 +12,7 @@
#include <QSystemTrayIcon> #include <QSystemTrayIcon>
#endif #endif
class SystrayIcon class SystrayIcon : public QObject
#ifndef Q_OS_ANDROID
: public QSystemTrayIcon
#else
: public QObject
#endif
{ {
Q_OBJECT Q_OBJECT
QML_ELEMENT QML_ELEMENT
@ -52,7 +47,11 @@ Q_SIGNALS:
void raiseWindow(); void raiseWindow();
private: private:
explicit SystrayIcon(QObject *parent = nullptr); #ifndef Q_OS_ANDROID
QSystemTrayIcon m_trayIcon;
#endif
SystrayIcon();
int iconColorEnumToInt(IconColor iconColor); int iconColorEnumToInt(IconColor iconColor);
IconColor intToIconColorEnum(int iconColorCode); IconColor intToIconColorEnum(int iconColorCode);
}; };