-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatus.cpp
More file actions
189 lines (148 loc) · 4.45 KB
/
Copy pathStatus.cpp
File metadata and controls
189 lines (148 loc) · 4.45 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "Status.h"
#include "Server.h"
#include "resource.h"
#include "Localization.h"
#include <windows.h>
#include <shellapi.h>
#include <cwchar>
#include <string>
extern NOTIFYICONDATA nid;
namespace
{
ServerStatus g_status = ServerStatus::Offline;
ULONGLONG g_startingUntil = 0;
bool g_startingDotVisible = true;
std::wstring GetStatusLabel()
{
switch (g_status)
{
case ServerStatus::Starting:
return Translate(L"menu.status.starting", L"Server: STARTING");
case ServerStatus::Online:
return Translate(L"menu.status.online", L"Server: ONLINE");
default:
return Translate(L"menu.status.offline", L"Server: OFFLINE");
}
}
void ApplyTrayStatus()
{
if (!nid.hWnd)
return;
switch (g_status)
{
case ServerStatus::Starting:
nid.hIcon = LoadIcon(nullptr, IDI_WARNING);
wcscpy_s(nid.szTip, Translate(L"tray.starting", L"AudioCpp Server - Starting").c_str());
break;
case ServerStatus::Online:
nid.hIcon = LoadIcon(nullptr, IDI_INFORMATION);
wcscpy_s(nid.szTip, Translate(L"tray.online", L"AudioCpp Server - Online").c_str());
break;
default:
nid.hIcon = LoadIcon(nullptr, IDI_ERROR);
wcscpy_s(nid.szTip, Translate(L"tray.offline", L"AudioCpp Server - Offline").c_str());
break;
}
Shell_NotifyIconW(NIM_MODIFY, &nid);
}
}
void SetServerStatus(ServerStatus status)
{
g_status = status;
if (status == ServerStatus::Starting)
{
g_startingUntil = GetTickCount64() + 1500;
g_startingDotVisible = true;
}
ApplyTrayStatus();
}
ServerStatus GetServerStatus()
{
return g_status;
}
void UpdateTrayStatus()
{
if (g_status == ServerStatus::Starting)
{
if (!IsServerRunning())
g_status = ServerStatus::Offline;
else if (GetTickCount64() >= g_startingUntil)
g_status = ServerStatus::Online;
}
else if (g_status == ServerStatus::Online && !IsServerRunning())
{
g_status = ServerStatus::Offline;
}
else if (g_status == ServerStatus::Offline && IsServerRunning())
{
g_status = ServerStatus::Online;
}
ApplyTrayStatus();
}
void TickTrayStatus()
{
if (g_status == ServerStatus::Starting)
g_startingDotVisible = !g_startingDotVisible;
UpdateTrayStatus();
HWND foreground = GetForegroundWindow();
wchar_t className[32]{};
if (foreground && GetClassNameW(foreground, className, 32) > 0 &&
wcscmp(className, L"#32768") == 0)
{
InvalidateRect(foreground, nullptr, TRUE);
}
}
void MeasureStatusMenuItem(MEASUREITEMSTRUCT* item)
{
if (!item)
return;
item->itemWidth = 190;
item->itemHeight = 26;
}
void DrawStatusMenuItem(const DRAWITEMSTRUCT* item)
{
if (!item)
return;
HDC dc = item->hDC;
RECT bounds = item->rcItem;
bool selected = (item->itemState & ODS_SELECTED) != 0;
FillRect(dc, &bounds, GetSysColorBrush(selected ? COLOR_HIGHLIGHT : COLOR_MENU));
COLORREF dotColor = RGB(220, 35, 35);
if (g_status == ServerStatus::Online)
dotColor = RGB(35, 180, 70);
else if (g_status == ServerStatus::Starting)
dotColor = g_startingDotVisible ? RGB(235, 185, 35) : RGB(115, 95, 20);
RECT dot{
bounds.left + 9,
bounds.top + 8,
bounds.left + 19,
bounds.top + 18
};
HBRUSH dotBrush = CreateSolidBrush(dotColor);
HBRUSH oldBrush = reinterpret_cast<HBRUSH>(SelectObject(dc, dotBrush));
HPEN dotPen = CreatePen(PS_SOLID, 1, dotColor);
HPEN oldPen = reinterpret_cast<HPEN>(SelectObject(dc, dotPen));
Ellipse(dc, dot.left, dot.top, dot.right, dot.bottom);
SelectObject(dc, oldPen);
SelectObject(dc, oldBrush);
DeleteObject(dotPen);
DeleteObject(dotBrush);
SetBkMode(dc, TRANSPARENT);
SetTextColor(dc, GetSysColor(selected ? COLOR_HIGHLIGHTTEXT : COLOR_GRAYTEXT));
RECT textBounds = bounds;
textBounds.left += 28;
std::wstring label = GetStatusLabel();
DrawTextW(dc, label.c_str(), -1, &textBounds, DT_SINGLELINE | DT_VCENTER | DT_LEFT);
}
void NotifyServerEvent(const wchar_t* message)
{
if (!message || !nid.hWnd)
return;
NOTIFYICONDATA notification = nid;
notification.uFlags = NIF_INFO;
wcscpy_s(notification.szInfoTitle, L"AudioCpp");
wcscpy_s(notification.szInfo, message);
notification.dwInfoFlags = NIIF_INFO;
notification.uTimeout = 4000;
Shell_NotifyIconW(NIM_MODIFY, ¬ification);
}