Skip to content

Commit ffe1fdd

Browse files
committed
Add WindowProcDelegateManager for tray icon events
Introduces WindowProcDelegateManager to manage window procedure delegates in Windows platform code. TrayIcon now registers and unregisters its window procedure delegate for handling tray icon events, improving encapsulation and event management.
1 parent 9a62b35 commit ffe1fdd

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

src/platform/windows/tray_icon_windows.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include <windows.h>
21
#include <shellapi.h>
2+
#include <windows.h>
33
#include <memory>
44
#include <string>
55

@@ -36,6 +36,15 @@ class TrayIcon::Impl {
3636
nid_.uID = icon_id_;
3737
nid_.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
3838
nid_.uCallbackMessage = WM_USER + 1; // Custom message for tray icon events
39+
40+
window_proc_id = WindowProcDelegateManager::GetInstance().RegisterDelegate(
41+
[this](HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) {
42+
return HandleWindowProc(hwnd, message, wparam, lparam);
43+
});
44+
}
45+
46+
~Impl() {
47+
WindowProcDelegateManager::GetInstance().UnregisterDelegate(window_proc_id);
3948
}
4049

4150
// Windows-specific method to set internal data
@@ -99,6 +108,7 @@ class TrayIcon::Impl {
99108
}
100109
}
101110

111+
int window_proc_id_;
102112
HWND hwnd_;
103113
UINT icon_id_;
104114
NOTIFYICONDATAW nid_;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "window_proc_delegate_manager.h"
2+
3+
namespace nativeapi {
4+
5+
WindowProcDelegateManager& WindowProcDelegateManager::GetInstance() {
6+
static WindowProcDelegateManager instance;
7+
return instance;
8+
}
9+
10+
int WindowProcDelegateManager::RegisterDelegate(WindowProcDelegate delegate) {
11+
if (!delegate) {
12+
return -1;
13+
}
14+
15+
std::lock_guard<std::mutex> lock(mutex_);
16+
17+
int id = next_id_++;
18+
delegates_[id] = std::move(delegate);
19+
return id;
20+
}
21+
22+
bool WindowProcDelegateManager::UnregisterDelegate(int id) {
23+
if (id < 0) {
24+
return false;
25+
}
26+
27+
std::lock_guard<std::mutex> lock(mutex_);
28+
29+
auto it = delegates_.find(id);
30+
if (it != delegates_.end()) {
31+
delegates_.erase(it);
32+
return true;
33+
}
34+
35+
return false;
36+
}
37+
38+
} // namespace nativeapi
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#pragma once
2+
3+
#include <windows.h>
4+
#include <functional>
5+
#include <mutex>
6+
#include <optional>
7+
#include <unordered_map>
8+
9+
namespace nativeapi {
10+
11+
using WindowProcDelegate = std::function<std::optional<
12+
LRESULT>(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)>;
13+
14+
class WindowProcDelegateManager {
15+
public:
16+
// Register a delegate
17+
// Returns a unique ID if registration was successful, -1 if failed
18+
int RegisterDelegate(WindowProcDelegate delegate);
19+
20+
// Unregister the delegate by ID
21+
// Returns true if a delegate was found and removed, false if no delegate was
22+
// found
23+
bool UnregisterDelegate(int id);
24+
25+
private:
26+
mutable std::mutex mutex_;
27+
std::unordered_map<int, WindowProcDelegate> delegates_;
28+
int next_id_ = 1;
29+
};
30+
31+
} // namespace nativeapi

0 commit comments

Comments
 (0)