forked from fxsound2/fxsound-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFxSystemTrayView.cpp
More file actions
362 lines (296 loc) · 10.1 KB
/
Copy pathFxSystemTrayView.cpp
File metadata and controls
362 lines (296 loc) · 10.1 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
FxSound
Copyright (C) 2023 FxSound LLC
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <JuceHeader.h>
#include "FxSystemTrayView.h"
// {A8E96325-5269-443C-A0D8-0D02562FE553}
const GUID FxSystemTrayView::trayIconGuid_ =
{ 0xa8e96325, 0x5269, 0x443c, { 0xa0, 0xd8, 0xd, 0x2, 0x56, 0x2f, 0xe5, 0x53 } };
FxSystemTrayView::FxSystemTrayView()
{
FxModel::getModel().addListener(this);
auto os = SystemStats::getOperatingSystemType();
if (os == SystemStats::OperatingSystemType::Windows7 ||
os == SystemStats::OperatingSystemType::Windows8_0 ||
os == SystemStats::OperatingSystemType::Windows8_1)
{
custom_notification_ = true;
}
else
{
custom_notification_ = false;
}
addToDesktop(0);
NOTIFYICONDATA nid = { sizeof(nid) };
HINSTANCE hInst = GetModuleHandle(NULL);
HWND hWnd = (HWND)getWindowHandle();
SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this));
componentWndProc_ = (WNDPROC)GetWindowLongPtr(hWnd, GWLP_WNDPROC);
SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)wndProc);
nid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE | NIF_SHOWTIP | NIF_GUID;
nid.guidItem = trayIconGuid_;
nid.uCallbackMessage = WMAPP_FXTRAYICON;
nid.hIcon = LoadIcon(hInst, L"IDI_LOGO_WHITE");
nid.hWnd = hWnd;
lstrcpy(nid.szTip, L"FxSound");
Shell_NotifyIcon(NIM_ADD, &nid);
// NOTIFYICON_VERSION_4 is prefered
nid.uVersion = NOTIFYICON_VERSION_4;
Shell_NotifyIcon(NIM_SETVERSION, &nid);
setVisible(true);
}
FxSystemTrayView::~FxSystemTrayView()
{
HWND hWnd = (HWND)getWindowHandle();
SetWindowLongPtr(hWnd, GWLP_USERDATA, NULL);
SetWindowLongPtr(hWnd, GWLP_WNDPROC, NULL);
NOTIFYICONDATA nid = { sizeof(nid) };
nid.uFlags = NIF_GUID;
nid.guidItem = trayIconGuid_;
Shell_NotifyIcon(NIM_DELETE, &nid);
removeFromDesktop();
}
void FxSystemTrayView::modelChanged(FxModel::Event model_event)
{
if (model_event == FxModel::Event::Notification)
{
showNotification();
}
}
void FxSystemTrayView::setStatus(bool power, bool processing)
{
HINSTANCE hInst = GetModuleHandle(NULL);
String param = power ? TRANS(L"on") : TRANS(L"off");
wchar_t tool_tip[1024];
swprintf_s(tool_tip, String(TRANS("FxSound is %s.")).toWideCharPointer(), param.toWideCharPointer());
NOTIFYICONDATA nid = { sizeof(nid) };
nid.uFlags = NIF_ICON | NIF_TIP | NIF_SHOWTIP | NIF_GUID;
nid.guidItem = trayIconGuid_;
if (power)
{
if (processing)
{
nid.hIcon = LoadIcon(hInst, L"IDI_LOGO_RED");
}
else
{
nid.hIcon = LoadIcon(hInst, L"IDI_LOGO_WHITE");
}
}
else
{
nid.hIcon = LoadIcon(hInst, L"IDI_LOGO_GRAY");
}
if (nid.hIcon == NULL)
{
return;
}
lstrcpy(nid.szTip, tool_tip);
Shell_NotifyIcon(NIM_MODIFY, &nid);
}
void FxSystemTrayView::showContextMenu()
{
PopupMenu context_menu;
PopupMenu preset_menu;
auto id = PRESET_MENU_ID_START;
auto count = FxModel::getModel().getPresetCount();
auto preset_type = FxModel::PresetType::AppPreset;
for (auto i = 0; i < count; i++)
{
auto preset = FxModel::getModel().getPreset(i);
PopupMenu::Item menu_item(preset.name);
menu_item.setID(id);
if (id - PRESET_MENU_ID_START == FxModel::getModel().getSelectedPreset())
{
menu_item.setTicked(true);
if (FxModel::getModel().isPresetModified())
{
menu_item.text = preset.name + L" *";
}
}
if (preset_type != preset.type)
{
preset_menu.addSeparator();
preset_type = preset.type;
}
auto handler = [preset = id]() {
FxController::getInstance().setPreset(preset - PRESET_MENU_ID_START);
};
menu_item.setAction(handler);
preset_menu.addItem(menu_item);
id++;
}
PopupMenu output_menu;
StringArray output_names = FxModel::getModel().getOutputNames();
id = OUTPUT_MENU_ID_START;
for (auto name : output_names)
{
PopupMenu::Item menu_item(name);
menu_item.setID(id);
if (id - OUTPUT_MENU_ID_START == FxModel::getModel().getSelectedOutput())
{
menu_item.setTicked(true);
}
auto handler = [output = id]() {
FxController::getInstance().setOutput(output - OUTPUT_MENU_ID_START);
};
menu_item.setAction(handler);
output_menu.addItem(menu_item);
id++;
}
auto openClicked = []() {
FxController::getInstance().showMainWindow();
};
auto powerClicked = []() {
FxController::getInstance().setPowerState(!FxModel::getModel().getPowerState());
};
auto settingsClicked = []() {
FxSettingsDialog settings_dialog;
settings_dialog.runModalLoop();
};
auto donateClicked = []() {
URL url("https://www.paypal.com/donate/?hosted_button_id=JVNQGYXCQ2GPG");
url.launchInDefaultBrowser();
};
auto exitClicked = []() {
FxController::getInstance().exit();
};
PopupMenu::Item open(TRANS("Open"));
PopupMenu::Item power;
PopupMenu::Item settings(TRANS("Settings"));
PopupMenu::Item donate(TRANS("Donate"));
PopupMenu::Item exit(TRANS("Exit"));
open.setID(MENU_ID_OPEN);
open.setAction(openClicked);
power.text = FxModel::getModel().getPowerState() ? String(TRANS("Turn Off")) : String(TRANS("Turn On"));
power.setID(MENU_ID_POWER);
power.setAction(powerClicked);
settings.setID(MENU_ID_SETTINGS);
settings.setAction(settingsClicked);
donate.setID(MENU_ID_DONATE);
donate.setAction(donateClicked);
exit.setID(MENU_ID_EXIT);
exit.setAction(exitClicked);
context_menu.addItem(open);
context_menu.addItem(power);
context_menu.addSubMenu(TRANS("Preset Select"), preset_menu, FxModel::getModel().getPowerState());
context_menu.addSubMenu(TRANS("Playback Device Select"), output_menu);
context_menu.addItem(settings);
context_menu.addItem(donate);
context_menu.addItem(exit);
HWND hWnd = (HWND)getWindowHandle();
SetFocus(hWnd);
SetForegroundWindow(hWnd);
context_menu.show();
}
void FxSystemTrayView::showNotification()
{
String message;
std::pair<String, String> link;
FxModel::getModel().popMessage(message, link);
HWND hWnd = (HWND)getWindowHandle();
if (message.isNotEmpty())
{
if (custom_notification_ || link.first.isNotEmpty())
{
NOTIFYICONIDENTIFIER icon_id = {};
RECT rect;
icon_id.cbSize = sizeof(NOTIFYICONIDENTIFIER);
icon_id.hWnd = hWnd;
icon_id.guidItem = trayIconGuid_;
if (FAILED(Shell_NotifyIconGetRect(&icon_id, &rect)))
{
return;
}
QUERY_USER_NOTIFICATION_STATE quns;
if (FAILED(SHQueryUserNotificationState(&quns)) || quns != QUNS_ACCEPTS_NOTIFICATIONS)
{
return;
}
notification_.setMessage(message, link);
auto display = Desktop::getInstance().getDisplays().getPrimaryDisplay();
if (display != nullptr)
{
auto area = display->userArea;
Point<int> pos = {};
if (rect.left < area.getCentreX())
{
pos.x = area.getX() + 20;
}
else
{
pos.x = area.getRight() - notification_.getWidth() - 20;
}
if (rect.top < area.getCentreY())
{
pos.y = area.getY() + 20;
}
else
{
pos.y = area.getBottom() - notification_.getHeight() - 20;
}
notification_.setBounds(pos.x, pos.y, notification_.getWidth(), notification_.getHeight());
notification_.showMessage();
}
}
else
{
NOTIFYICONDATA nid = { sizeof(nid) };
nid.uFlags = NIF_INFO | NIF_GUID | NIF_REALTIME;
nid.guidItem = trayIconGuid_;
nid.dwInfoFlags = NIIF_NOSOUND | NIIF_RESPECT_QUIET_TIME;
String title = L"FxSound";
title.copyToUTF16(nid.szInfoTitle, sizeof(nid.szInfoTitle) - 1);
message.copyToUTF16(nid.szInfo, sizeof(nid.szInfo) - 1);
Shell_NotifyIcon(NIM_MODIFY, &nid);
}
}
}
LRESULT CALLBACK FxSystemTrayView::wndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
auto tray_view = reinterpret_cast<FxSystemTrayView*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
if (message == WMAPP_FXTRAYICON)
{
switch (LOWORD(lParam))
{
case NIN_SELECT:
if (FxController::getInstance().isMainWindowVisible())
{
FxController::getInstance().hideMainWindow();
}
else
{
FxController::getInstance().showMainWindow();
}
break;
case NIN_BALLOONTIMEOUT:
case NIN_BALLOONUSERCLICK:
{
NOTIFYICONDATA nid = { sizeof(nid) };
nid.uFlags = NIF_SHOWTIP | NIF_GUID;
nid.guidItem = trayIconGuid_;
Shell_NotifyIcon(NIM_MODIFY, &nid);
}
break;
case WM_CONTEXTMENU:
tray_view->showContextMenu();
break;
}
}
else
{
return CallWindowProc(tray_view->componentWndProc_, hwnd, message, wParam, lParam);
}
return 0;
}