Skip to content

Commit 47f3a49

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 47f3a49

File tree

4 files changed

+82
-3
lines changed

4 files changed

+82
-3
lines changed

src/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ file(GLOB CAPI_SOURCES "capi/*.cpp" "capi/*.c")
2323

2424
# Platform-specific source files
2525
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
26-
file(GLOB PLATFORM_SOURCES "platform/linux/*_linux.cpp")
26+
file(GLOB PLATFORM_SOURCES "platform/linux/*.cpp")
2727
# Find packages for Linux
2828
find_package(PkgConfig REQUIRED)
2929
pkg_check_modules(GTK REQUIRED IMPORTED_TARGET gtk+-3.0)
3030
pkg_check_modules(X11 REQUIRED IMPORTED_TARGET x11)
3131
pkg_check_modules(XI REQUIRED IMPORTED_TARGET xi)
3232
pkg_check_modules(AYATANA_APPINDICATOR REQUIRED IMPORTED_TARGET ayatana-appindicator3-0.1)
3333
elseif(APPLE)
34-
file(GLOB PLATFORM_SOURCES "platform/macos/*_macos.mm")
34+
file(GLOB PLATFORM_SOURCES "platform/macos/*.mm")
3535
elseif(WIN32)
36-
file(GLOB PLATFORM_SOURCES "platform/windows/*_windows.cpp")
36+
file(GLOB PLATFORM_SOURCES "platform/windows/*.cpp")
3737
else()
3838
set(PLATFORM_SOURCES "")
3939
endif()

src/platform/windows/tray_icon_windows.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)