-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtray_windows.cpp
More file actions
267 lines (224 loc) · 7.58 KB
/
tray_windows.cpp
File metadata and controls
267 lines (224 loc) · 7.58 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
#include <windows.h>
#include <shellapi.h>
#include <commctrl.h>
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include "../../tray.h"
#include "../../menu.h"
namespace nativeapi {
// Window class name for tray window
static const wchar_t* TRAY_WINDOW_CLASS = L"NativeAPITrayWindow";
static UINT g_tray_message = WM_USER + 1;
static UINT g_next_tray_uid = 1000;
// Helper function to convert string to wide string
std::wstring StringToWideString(const std::string& str) {
if (str.empty()) return std::wstring();
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), nullptr, 0);
std::wstring wstrTo(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
return wstrTo;
}
// Helper function to convert base64 to HICON
HICON Base64ToIcon(const std::string& base64Data) {
// This is a placeholder - in a full implementation you would:
// 1. Decode the base64 string
// 2. Create an HICON from the decoded image data
// For now, return nullptr
return nullptr;
}
// Window procedure for tray messages
LRESULT CALLBACK TrayWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
if (msg == g_tray_message) {
UINT tray_id = LOWORD(wParam);
UINT mouse_msg = LOWORD(lParam);
// Find the tray instance
Tray* tray = reinterpret_cast<Tray*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
switch (mouse_msg) {
case WM_RBUTTONUP: {
// Show context menu
if (tray) {
Menu contextMenu = tray->GetContextMenu();
HMENU hmenu = static_cast<HMENU>(contextMenu.GetNativeMenu());
if (hmenu) {
POINT pt;
GetCursorPos(&pt);
// Required for popup menus to work correctly
SetForegroundWindow(hwnd);
TrackPopupMenu(hmenu, TPM_RIGHTBUTTON, pt.x, pt.y, 0, hwnd, nullptr);
// Required after TrackPopupMenu
PostMessage(hwnd, WM_NULL, 0, 0);
}
}
break;
}
case WM_LBUTTONUP:
// Handle left click
break;
case WM_LBUTTONDBLCLK:
// Handle double click
break;
}
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
// Private implementation class
class Tray::Impl {
public:
Impl() : hwnd_(nullptr), tray_uid_(g_next_tray_uid++), icon_(nullptr) {
CreateTrayWindow();
InitializeTrayIcon();
}
~Impl() {
RemoveTrayIcon();
if (hwnd_) {
DestroyWindow(hwnd_);
}
if (icon_) {
DestroyIcon(icon_);
}
}
void CreateTrayWindow() {
// Register window class if not already registered
static bool class_registered = false;
if (!class_registered) {
WNDCLASSW wc = {};
wc.lpfnWndProc = TrayWindowProc;
wc.hInstance = GetModuleHandle(nullptr);
wc.lpszClassName = TRAY_WINDOW_CLASS;
RegisterClassW(&wc);
class_registered = true;
}
// Create hidden window for tray messages
hwnd_ = CreateWindowW(
TRAY_WINDOW_CLASS,
L"TrayWindow",
WS_OVERLAPPED,
0, 0, 0, 0,
nullptr, nullptr,
GetModuleHandle(nullptr),
nullptr
);
}
void InitializeTrayIcon() {
if (!hwnd_) return;
nid_.cbSize = sizeof(NOTIFYICONDATAW);
nid_.hWnd = hwnd_;
nid_.uID = tray_uid_;
nid_.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nid_.uCallbackMessage = g_tray_message;
nid_.hIcon = LoadIcon(nullptr, IDI_APPLICATION); // Default icon
wcscpy_s(nid_.szTip, sizeof(nid_.szTip)/sizeof(wchar_t), L"Tray Icon");
// Store tray pointer in window
SetWindowLongPtr(hwnd_, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(tray_ptr_));
Shell_NotifyIconW(NIM_ADD, &nid_);
}
void RemoveTrayIcon() {
if (hwnd_) {
Shell_NotifyIconW(NIM_DELETE, &nid_);
}
}
void UpdateIcon() {
if (hwnd_ && icon_) {
nid_.hIcon = icon_;
Shell_NotifyIconW(NIM_MODIFY, &nid_);
}
}
void UpdateTooltip(const std::string& tooltip) {
if (!hwnd_) return;
std::wstring wtooltip = StringToWideString(tooltip);
wcsncpy_s(nid_.szTip, sizeof(nid_.szTip) / sizeof(wchar_t), wtooltip.c_str(), _TRUNCATE);
Shell_NotifyIconW(NIM_MODIFY, &nid_);
}
HWND hwnd_;
UINT tray_uid_;
NOTIFYICONDATAW nid_;
HICON icon_;
std::string title_;
std::string tooltip_;
Menu context_menu_;
Tray* tray_ptr_ = nullptr;
};
Tray::Tray() : pimpl_(new Impl()) {
id = -1;
pimpl_->tray_ptr_ = this;
}
Tray::Tray(void* tray) : pimpl_(new Impl()) {
id = -1; // Will be set by TrayManager when created
pimpl_->tray_ptr_ = this;
}
Tray::~Tray() {
delete pimpl_;
}
void Tray::SetIcon(std::string icon) {
if (pimpl_->icon_) {
DestroyIcon(pimpl_->icon_);
pimpl_->icon_ = nullptr;
}
// Check if the icon is a base64 string
if (icon.find("data:image") != std::string::npos) {
// Extract the base64 part
size_t pos = icon.find("base64,");
if (pos != std::string::npos) {
std::string base64Icon = icon.substr(pos + 7);
pimpl_->icon_ = Base64ToIcon(base64Icon);
}
} else if (!icon.empty()) {
// Load icon from file path
std::wstring wicon = StringToWideString(icon);
pimpl_->icon_ = (HICON)LoadImageW(
nullptr,
wicon.c_str(),
IMAGE_ICON,
0, 0, // Use default size
LR_LOADFROMFILE | LR_DEFAULTSIZE
);
}
if (pimpl_->icon_) {
pimpl_->UpdateIcon();
}
}
void Tray::SetTitle(std::string title) {
pimpl_->title_ = title;
// Note: Windows tray icons don't display titles like macOS status items
// The title could be used as part of the tooltip or for identification
}
std::string Tray::GetTitle() {
return pimpl_->title_;
}
void Tray::SetTooltip(std::string tooltip) {
pimpl_->tooltip_ = tooltip;
pimpl_->UpdateTooltip(tooltip);
}
std::string Tray::GetTooltip() {
return pimpl_->tooltip_;
}
void Tray::SetContextMenu(Menu menu) {
pimpl_->context_menu_ = menu;
}
Menu Tray::GetContextMenu() {
return pimpl_->context_menu_;
}
Rectangle Tray::GetBounds() {
Rectangle bounds = {0, 0, 0, 0};
if (pimpl_->hwnd_) {
// Get the position of the notification area
HWND hTray = FindWindowW(L"Shell_TrayWnd", nullptr);
if (hTray) {
HWND hNotify = FindWindowExW(hTray, nullptr, L"TrayNotifyWnd", nullptr);
if (hNotify) {
RECT rect;
GetWindowRect(hNotify, &rect);
// This is approximate - getting exact icon position is complex
bounds.x = static_cast<double>(rect.left);
bounds.y = static_cast<double>(rect.top);
bounds.width = static_cast<double>(rect.right - rect.left);
bounds.height = static_cast<double>(rect.bottom - rect.top);
}
}
}
return bounds;
}
} // namespace nativeapi