mirror of
https://github.com/kavishdevar/librepods.git
synced 2026-01-28 22:01:50 +00:00
[Linux] Simplify code, implement tray manager
Implement tray manager Bug fixes Bug fixes
This commit is contained in:
committed by
Tim Gromeyer
parent
e72b4a116e
commit
1946857ca5
110
linux/trayiconmanager.cpp
Normal file
110
linux/trayiconmanager.cpp
Normal file
@@ -0,0 +1,110 @@
|
||||
#include "trayiconmanager.h"
|
||||
|
||||
#include <QSystemTrayIcon>
|
||||
#include <QMenu>
|
||||
#include <QAction>
|
||||
#include <QApplication>
|
||||
#include <QPainter>
|
||||
#include <QFont>
|
||||
#include <QColor>
|
||||
#include <QActionGroup>
|
||||
|
||||
using namespace AirpodsTrayApp::Enums;
|
||||
|
||||
TrayIconManager::TrayIconManager(QObject *parent) : QObject(parent)
|
||||
{
|
||||
// Initialize tray icon
|
||||
trayIcon = new QSystemTrayIcon(QIcon(":/icons/assets/airpods.png"), this);
|
||||
trayMenu = new QMenu();
|
||||
|
||||
// Setup basic menu actions
|
||||
setupMenuActions();
|
||||
|
||||
// Connect signals
|
||||
trayIcon->setContextMenu(trayMenu);
|
||||
connect(trayIcon, &QSystemTrayIcon::activated, this, &TrayIconManager::onTrayIconActivated);
|
||||
|
||||
trayIcon->show();
|
||||
}
|
||||
|
||||
void TrayIconManager::TrayIconManager::updateBatteryStatus(const QString &status)
|
||||
{
|
||||
trayIcon->setToolTip("Battery Status: " + status);
|
||||
updateIconFromBattery(status);
|
||||
}
|
||||
|
||||
void TrayIconManager::updateNoiseControlState(NoiseControlMode mode)
|
||||
{
|
||||
QList<QAction *> actions = noiseControlGroup->actions();
|
||||
for (QAction *action : actions)
|
||||
{
|
||||
action->setChecked(action->data().toInt() == (int)mode);
|
||||
}
|
||||
}
|
||||
|
||||
void TrayIconManager::updateConversationalAwareness(bool enabled)
|
||||
{
|
||||
caToggleAction->setChecked(enabled);
|
||||
}
|
||||
|
||||
void TrayIconManager::setupMenuActions()
|
||||
{
|
||||
// Conversational Awareness Toggle
|
||||
caToggleAction = new QAction("Toggle Conversational Awareness", trayMenu);
|
||||
caToggleAction->setCheckable(true);
|
||||
trayMenu->addAction(caToggleAction);
|
||||
connect(caToggleAction, &QAction::triggered, this, [this](bool checked)
|
||||
{ emit conversationalAwarenessToggled(checked); });
|
||||
|
||||
// Noise Control Options
|
||||
noiseControlGroup = new QActionGroup(trayMenu);
|
||||
const QPair<QString, NoiseControlMode> noiseOptions[] = {
|
||||
{"Adaptive", NoiseControlMode::Adaptive},
|
||||
{"Transparency", NoiseControlMode::Transparency},
|
||||
{"Noise Cancellation", NoiseControlMode::NoiseCancellation},
|
||||
{"Off", NoiseControlMode::Off}};
|
||||
|
||||
for (auto option : noiseOptions)
|
||||
{
|
||||
QAction *action = new QAction(option.first, trayMenu);
|
||||
action->setCheckable(true);
|
||||
action->setData((int)option.second);
|
||||
noiseControlGroup->addAction(action);
|
||||
trayMenu->addAction(action);
|
||||
connect(action, &QAction::triggered, this, [this, mode = option.second]()
|
||||
{ emit noiseControlChanged(mode); });
|
||||
}
|
||||
|
||||
// Quit action
|
||||
QAction *quitAction = new QAction("Quit", trayMenu);
|
||||
trayMenu->addAction(quitAction);
|
||||
connect(quitAction, &QAction::triggered, qApp, &QApplication::quit);
|
||||
}
|
||||
|
||||
void TrayIconManager::updateIconFromBattery(const QString &status)
|
||||
{
|
||||
QStringList parts = status.split(", ");
|
||||
int leftLevel = parts[0].split(": ")[1].replace("%", "").toInt();
|
||||
int rightLevel = parts[1].split(": ")[1].replace("%", "").toInt();
|
||||
|
||||
int minLevel = (leftLevel == 0) ? rightLevel : (rightLevel == 0) ? leftLevel
|
||||
: qMin(leftLevel, rightLevel);
|
||||
|
||||
QPixmap pixmap(32, 32);
|
||||
pixmap.fill(Qt::transparent);
|
||||
QPainter painter(&pixmap);
|
||||
painter.setPen(QApplication::palette().color(QPalette::WindowText));
|
||||
painter.setFont(QFont("Arial", 12, QFont::Bold));
|
||||
painter.drawText(pixmap.rect(), Qt::AlignCenter, QString::number(minLevel) + "%");
|
||||
painter.end();
|
||||
|
||||
trayIcon->setIcon(QIcon(pixmap));
|
||||
}
|
||||
|
||||
void TrayIconManager::onTrayIconActivated(QSystemTrayIcon::ActivationReason reason)
|
||||
{
|
||||
if (reason == QSystemTrayIcon::Trigger)
|
||||
{
|
||||
emit trayClicked();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user