-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathtrayiconmanager.cpp
More file actions
149 lines (123 loc) · 4.57 KB
/
trayiconmanager.cpp
File metadata and controls
149 lines (123 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#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, bool noTray) : QObject(parent), m_noTray(noTray)
{
// 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);
// Only show tray icon if not disabled
if (!noTray) {
trayIcon->show();
}
}
void TrayIconManager::showNotification(const QString &title, const QString &message)
{
if (!m_notificationsEnabled)
return;
trayIcon->showMessage(title, message, QSystemTrayIcon::Information, 3000);
}
void TrayIconManager::TrayIconManager::updateBatteryStatus(const QString &status)
{
trayIcon->setToolTip(tr("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()
{
// Open action
QAction *openAction = new QAction(tr("Open"), trayMenu);
trayMenu->addAction(openAction);
connect(openAction, &QAction::triggered, qApp, [this](){emit openApp();});
// Settings Menu
QAction *settingsMenu = new QAction(tr("Settings"), trayMenu);
trayMenu->addAction(settingsMenu);
connect(settingsMenu, &QAction::triggered, qApp, [this](){emit openSettings();});
trayMenu->addSeparator();
// Conversational Awareness Toggle
caToggleAction = new QAction(tr("Toggle Conversational Awareness"), trayMenu);
caToggleAction->setCheckable(true);
trayMenu->addAction(caToggleAction);
connect(caToggleAction, &QAction::triggered, this, [this](bool checked)
{ emit conversationalAwarenessToggled(checked); });
trayMenu->addSeparator();
// Noise Control Options
noiseControlGroup = new QActionGroup(trayMenu);
const QPair<QString, NoiseControlMode> noiseOptions[] = {
{tr("Adaptive"), NoiseControlMode::Adaptive},
{tr("Transparency"), NoiseControlMode::Transparency},
{tr("Noise Cancellation"), NoiseControlMode::NoiseCancellation},
{tr("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); });
}
trayMenu->addSeparator();
// Quit action
QAction *quitAction = new QAction(tr("Quit"), trayMenu);
trayMenu->addAction(quitAction);
connect(quitAction, &QAction::triggered, qApp, &QApplication::quit);
}
void TrayIconManager::updateIconFromBattery(const QString &status)
{
int leftLevel = 0;
int rightLevel = 0;
int minLevel = 0;
if (!status.isEmpty())
{
// Parse the battery status string
QStringList parts = status.split(", ");
if (parts.size() >= 2) {
leftLevel = parts[0].split(": ")[1].replace("%", "").toInt();
rightLevel = parts[1].split(": ")[1].replace("%", "").toInt();
minLevel = (leftLevel == 0) ? rightLevel : (rightLevel == 0) ? leftLevel
: qMin(leftLevel, rightLevel);
} else if (parts.size() == 1) {
minLevel = parts[0].split(": ")[1].replace("%", "").toInt();
}
}
QPixmap pixmap(32, 32);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
painter.setPen(Qt::white);
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();
}
}