Skip to content

Commit

Permalink
Replaced QSystemTrayIcon with StatusNotifierItem, fixes issue #3
Browse files Browse the repository at this point in the history
  • Loading branch information
redtide committed Mar 18, 2024
1 parent 70d7a54 commit 587cb92
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 43 deletions.
18 changes: 6 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ endif()
if(PROJECT_USE_PULSEAUDIO)
find_package(PulseAudio REQUIRED)
endif()
find_package(StatusNotifierItemQt${QT_VERSION_MAJOR} REQUIRED)
#===============================================================================
# Project files
#===============================================================================
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
#===============================================================================
Expand All @@ -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})
Expand All @@ -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}")

Expand Down Expand Up @@ -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)
Expand Down
63 changes: 37 additions & 26 deletions src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,24 @@
#include "audio/engine/pulseaudio.hpp"
#endif

#if QT_VERSION < 0x060000
#include <StatusNotifierItemQt5/statusnotifieritem.h>
#else
#include <StatusNotifierItemQt6/statusnotifieritem.h>
#endif

#include <QAction>
#include <QIcon>
#include <QLibraryInfo>
#include <QMenu>
#include <QProcess>
#include <QSystemTrayIcon>
#include <QToolTip>
#include <QWheelEvent>

#include <QDebug>

Qtilities::Application::Application(int argc, char* argv[])
: QApplication(argc, argv)
, trayIcon_(new QSystemTrayIcon(this))
, engine_(nullptr)
, channel_(nullptr)
{
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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()
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<QWheelEvent *>(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[])
Expand Down
13 changes: 8 additions & 5 deletions src/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@

#include <QApplication>
#include <QTranslator>
#include <QSystemTrayIcon>

class AudioDevice;
class AudioEngine;
class StatusNotifierItem;

QT_BEGIN_NAMESPACE
class QAction;
QT_END_NAMESPACE

namespace Qtilities {

Expand All @@ -44,8 +46,6 @@ class Application : public QApplication
Settings &settings() { return settings_; }

private:
bool eventFilter(QObject *, QEvent *event);

void initLocale();
void initUi();

Expand All @@ -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_;
Expand Down

0 comments on commit 587cb92

Please sign in to comment.