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 "settingsmanager.h"
SystrayIcon::SystrayIcon(QObject *parent)
: QSystemTrayIcon(parent)
SystrayIcon::SystrayIcon()
: QObject()
{
setIconColor(intToIconColorEnum(SettingsManager::self()->trayIconType()));
setToolTip(i18nc("@info:tooltip",
"Kasts\n"
"Middle-click to play/pause"));
m_trayIcon.setToolTip(i18nc("@info:tooltip",
"Kasts\n"
"Middle-click to play/pause"));
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) {
Q_EMIT raiseWindow();
} else if (reason == QSystemTrayIcon::MiddleClick) {
@ -37,9 +37,9 @@ SystrayIcon::SystrayIcon(QObject *parent)
connect(SettingsManager::self(), &SettingsManager::showTrayIconChanged, this, [this]() {
if (SettingsManager::self()->showTrayIcon()) {
show();
m_trayIcon.show();
} else {
hide();
m_trayIcon.hide();
}
});
@ -120,15 +120,15 @@ SystrayIcon::SystrayIcon(QObject *parent)
connect(quitAction, &QAction::triggered, QCoreApplication::instance(), QCoreApplication::quit);
menu->addAction(quitAction);
setContextMenu(menu);
m_trayIcon.setContextMenu(menu);
if (SettingsManager::self()->showTrayIcon()) {
show();
m_trayIcon.show();
}
}
#else
SystrayIcon::SystrayIcon(QObject *parent)
: QObject(parent)
SystrayIcon::SystrayIcon()
: QObject()
{
}
#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]
switch (iconColor) {
case SystrayIcon::IconColor::Colorful:
setIcon(QIcon(QStringLiteral(":/icons/kasts")));
m_trayIcon.setIcon(QIcon(QStringLiteral(":/icons/kasts")));
break;
case SystrayIcon::IconColor::Light:
setIcon(QIcon(QStringLiteral(":/icons/kasts-tray-light")));
m_trayIcon.setIcon(QIcon(QStringLiteral(":/icons/kasts-tray-light")));
break;
case SystrayIcon::IconColor::Dark:
setIcon(QIcon(QStringLiteral(":/icons/kasts-tray-dark")));
m_trayIcon.setIcon(QIcon(QStringLiteral(":/icons/kasts-tray-dark")));
break;
}
#endif

View File

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