Skip to content

Commit

Permalink
Change volume from tray icon using mouse wheel
Browse files Browse the repository at this point in the history
  • Loading branch information
redtide committed Aug 14, 2023
1 parent 5d7bae4 commit b5e9574
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
33 changes: 24 additions & 9 deletions src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
#include <QMenu>
#include <QProcess>
#include <QSystemTrayIcon>
#include <QToolTip>
#include <QWheelEvent>

#include <QDebug>

Expand Down Expand Up @@ -108,12 +110,9 @@ void Qtilities::Application::initUi()

if(channel_) {
channel_->setMute(settings_.isMuted());

int volume = settings_.volume();
if(volume >= 0 && volume <= 100)
channel_->setVolume(volume);
else
mnuVolume_->setVolume(channel_->volume());
int volume = std::clamp(settings_.volume(), 0, 100);
channel_->setVolume(volume);
mnuVolume_->setVolume(channel_->volume());
}
actAutoStart_->setCheckable(true);
actAutoStart_->setChecked(settings_.useAutostart());
Expand All @@ -134,6 +133,7 @@ void Qtilities::Application::initUi()

trayIcon_->setContextMenu(mnuActions);
trayIcon_->show();
trayIcon_->installEventFilter(this);

connect(actAbout, &QAction::triggered, this, &Application::about);
connect(actPrefs, &QAction::triggered, this, &Application::preferences);
Expand Down Expand Up @@ -298,18 +298,33 @@ void Qtilities::Application::updateDeviceList()
void Qtilities::Application::updateTrayIcon()
{
QString iconName;
if (channel_->volume() <= 0 || channel_->mute())
int volume = channel_->volume();
if (volume <= 0 || channel_->mute())
iconName = QLatin1String("audio-volume-muted");
else if (channel_->volume() <= 33)
else if (volume <= 33)
iconName = QLatin1String("audio-volume-low");
else if (channel_->volume() <= 66)
else if (volume <= 66)
iconName = QLatin1String("audio-volume-medium");
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;
}

int main(int argc, char* argv[])
Expand Down
2 changes: 2 additions & 0 deletions src/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class Application : public QApplication
Settings &settings() { return settings_; }

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

void initLocale();
void initUi();

Expand Down

0 comments on commit b5e9574

Please sign in to comment.