From 587cb92856610e2f46be892c183c421f99003745 Mon Sep 17 00:00:00 2001 From: redtide Date: Mon, 18 Mar 2024 18:25:39 +0100 Subject: [PATCH] Replaced QSystemTrayIcon with StatusNotifierItem, fixes issue #3 --- CMakeLists.txt | 18 +++++-------- src/application.cpp | 63 ++++++++++++++++++++++++++------------------- src/application.hpp | 13 ++++++---- 3 files changed, 51 insertions(+), 43 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e2345e3..c70db07 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,6 +28,7 @@ endif() if(PROJECT_USE_PULSEAUDIO) find_package(PulseAudio REQUIRED) endif() +find_package(StatusNotifierItemQt${QT_VERSION_MAJOR} REQUIRED) #=============================================================================== # Project files #=============================================================================== @@ -41,8 +42,10 @@ set(PROJECT_SOURCES src/audio/engineid.hpp src/dialogabout.hpp src/dialogabout.cpp + src/dialogabout.ui src/dialogprefs.hpp src/dialogprefs.cpp + src/dialogprefs.ui src/menuvolume.hpp src/menuvolume.cpp src/qtilities.hpp @@ -63,10 +66,6 @@ if(PROJECT_USE_PULSEAUDIO) src/audio/engine/pulseaudio.cpp ) endif() -set(PROJECT_UI_FILES - src/dialogabout.ui - src/dialogprefs.ui -) set(PROJECT_OTHER_FILES .github/workflows/build.yml .clang-format @@ -75,8 +74,8 @@ set(PROJECT_OTHER_FILES .gitignore README.md ) -source_group("Other Files" FILES ${PROJECT_OTHER_FILES}) -source_group("UI Files" FILES ${PROJECT_UI_FILES}) +source_group("Source" FILES ${PROJECT_SOURCES}) +source_group("Misc" FILES ${PROJECT_OTHER_FILES}) #=============================================================================== # Resources #=============================================================================== @@ -92,7 +91,6 @@ set(PROJECT_ALL_FILES ${PROJECT_OTHER_FILES} ${PROJECT_QM_FILES} ${PROJECT_TRANSLATION_SOURCES} - ${PROJECT_UI_FILES} ) if(${QT_VERSION_MAJOR} GREATER_EQUAL 6) qt_add_executable(${PROJECT_NAME} MANUAL_FINALIZATION ${PROJECT_ALL_FILES}) @@ -115,6 +113,7 @@ target_link_libraries(${PROJECT_NAME} PRIVATE Qt::Widgets ${ALSA_LIBRARIES} ${PULSEAUDIO_LIBRARY} + StatusNotifierItemQt${QT_VERSION_MAJOR} ) set_target_properties(${PROJECT_NAME} PROPERTIES OUTPUT_NAME "${PROJECT_ID}") @@ -152,11 +151,6 @@ Qt version: ${QT_VERSION} Build type: ${CMAKE_BUILD_TYPE} Install prefix: ${CMAKE_INSTALL_PREFIX} Update translations before build: ${PROJECT_TRANSLATIONS_UPDATE} - -CXX Debug flags: ${CMAKE_CXX_FLAGS_DEBUG} -CXX Release flags: ${CMAKE_CXX_FLAGS_RELEASE} -CXX MinSize flags: ${CMAKE_CXX_FLAGS_MINSIZEREL} -CXX RelWithDebInfo flags: ${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ") if(QT_VERSION_MAJOR EQUAL 6) diff --git a/src/application.cpp b/src/application.cpp index d5e6d5e..3bd6eb9 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -32,12 +32,17 @@ #include "audio/engine/pulseaudio.hpp" #endif +#if QT_VERSION < 0x060000 + #include +#else + #include +#endif + #include #include #include #include #include -#include #include #include @@ -45,7 +50,6 @@ Qtilities::Application::Application(int argc, char* argv[]) : QApplication(argc, argv) - , trayIcon_(new QSystemTrayIcon(this)) , engine_(nullptr) , channel_(nullptr) { @@ -54,6 +58,11 @@ Qtilities::Application::Application(int argc, char* argv[]) setApplicationName(APPLICATION_NAME); setApplicationDisplayName(APPLICATION_DISPLAY_NAME); + trayIcon_ = new StatusNotifierItem(qApp->applicationName(), this); + trayIcon_->setCategory(StatusNotifierItem::SNICategory::ApplicationStatus); + trayIcon_->setStatus(StatusNotifierItem::SNIStatus::Active); + trayIcon_->setToolTipTitle(qApp->applicationDisplayName()); + setQuitOnLastWindowClosed(false); initLocale(); @@ -132,8 +141,6 @@ void Qtilities::Application::initUi() mnuActions->addAction(actQuit); trayIcon_->setContextMenu(mnuActions); - trayIcon_->show(); - trayIcon_->installEventFilter(this); connect(actAbout, &QAction::triggered, this, &Application::about); connect(actPrefs, &QAction::triggered, this, &Application::preferences); @@ -152,7 +159,9 @@ void Qtilities::Application::initUi() updateTrayIcon(); }); connect(mnuVolume_, &MenuVolume::sigVolumeChanged, this, &Application::onVolumeChanged); - connect(trayIcon_, &QSystemTrayIcon::activated, this, &Application::onTrayIconActivated); + connect(trayIcon_, &StatusNotifierItem::activateRequested, this, &Application::onActivateRequested); + connect(trayIcon_, &StatusNotifierItem::secondaryActivateRequested, this, &Application::onSecondaryActivateRequested); + connect(trayIcon_, &StatusNotifierItem::scrollRequested, this, &Application::onScrollRequested); } void Qtilities::Application::about() @@ -260,17 +269,35 @@ void Qtilities::Application::onAboutToQuit() settings_.save(); } -void Qtilities::Application::onTrayIconActivated(QSystemTrayIcon::ActivationReason reason) +void Qtilities::Application::onActivateRequested(const QPoint&) { - if (reason == QSystemTrayIcon::Trigger || reason == QSystemTrayIcon::DoubleClick) { + if (trayIcon_->status() == StatusNotifierItem::SNIStatus::Active) { + trayIcon_->setStatus(StatusNotifierItem::SNIStatus::Passive); + mnuVolume_->hide(); + } else { + trayIcon_->setStatus(StatusNotifierItem::SNIStatus::Active); mnuVolume_->show(); mnuVolume_->adjustSize(); mnuVolume_->popUp(); - } else if (channel_ && settings_.muteOnMiddleClick() && reason == QSystemTrayIcon::MiddleClick) { - channel_->toggleMute(); } } +void Qtilities::Application::onSecondaryActivateRequested(const QPoint&) +{ + if (channel_ && settings_.muteOnMiddleClick()) + channel_->toggleMute(); +} + +void Qtilities::Application::onScrollRequested(int delta, Qt::Orientation) +{ + int v = std::clamp(channel_->volume() + delta / 120, 0, 100); + channel_->setVolume(v); + mnuVolume_->setVolume(v); +// trayIcon_->setToolTipTitle(QString("%1\%").arg(v)); + QToolTip::showText(QCursor::pos(), QString("%1\%").arg(v)); + QToolTip::hideText(); +} + void Qtilities::Application::runMixer() { QString command = settings_.mixerCommand(); @@ -308,23 +335,7 @@ void Qtilities::Application::updateTrayIcon() else iconName = QLatin1String("audio-volume-high"); - QString fallbackIconName = QStringLiteral(":/") + iconName; - - trayIcon_->setIcon(QIcon::fromTheme(iconName, QIcon(fallbackIconName))); - trayIcon_->setToolTip(QString("%1\%").arg(volume)); -} - -bool Qtilities::Application::eventFilter(QObject *, QEvent *event) -{ - if (event->type() != QEvent::Wheel) - return false; - - QWheelEvent *wheelEvent = static_cast(event); - int v = std::clamp(channel_->volume() + wheelEvent->angleDelta().y() / 120, 0, 100); - channel_->setVolume(v); - mnuVolume_->setVolume(v); - QToolTip::showText(wheelEvent->globalPosition().toPoint(), QString("%1\%").arg(v)); - return true; + trayIcon_->setIconByName(iconName); } int main(int argc, char* argv[]) diff --git a/src/application.hpp b/src/application.hpp index 7d4c0fc..1a8bc4b 100644 --- a/src/application.hpp +++ b/src/application.hpp @@ -23,12 +23,14 @@ #include #include -#include class AudioDevice; class AudioEngine; +class StatusNotifierItem; +QT_BEGIN_NAMESPACE class QAction; +QT_END_NAMESPACE namespace Qtilities { @@ -44,8 +46,6 @@ class Application : public QApplication Settings &settings() { return settings_; } private: - bool eventFilter(QObject *, QEvent *event); - void initLocale(); void initUi(); @@ -57,13 +57,16 @@ class Application : public QApplication void onAudioDeviceChanged(int); void onAudioEngineChanged(int); void onPrefsChanged(); - void onTrayIconActivated(QSystemTrayIcon::ActivationReason); + void onActivateRequested(const QPoint&); + void onSecondaryActivateRequested(const QPoint&); + void onScrollRequested(int delta, Qt::Orientation); + void onVolumeChanged(int); QStringList deviceList_; QTranslator qtTranslator_, translator_; Settings settings_; - QSystemTrayIcon *trayIcon_; + StatusNotifierItem *trayIcon_; QAction *actAutoStart_; MenuVolume *mnuVolume_; AudioEngine *engine_;