Skip to content

Commit 7a3d3f0

Browse files
committed
Remove button info from tray icon click event
Refactored tray icon click event handling to no longer include button information. Updated event structures, callbacks, and related code on macOS and Windows to simplify the event payload and usage.
1 parent 7457f4b commit 7a3d3f0

File tree

7 files changed

+84
-35
lines changed

7 files changed

+84
-35
lines changed

examples/tray_icon_c_example/main.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ void on_menu_item_clicked(const void* event, void* user_data) {
3333
void on_tray_clicked(const void* event, void* user_data) {
3434
const native_tray_icon_clicked_event_t* clicked_event =
3535
(const native_tray_icon_clicked_event_t*)event;
36-
printf("Tray icon clicked! ID=%ld, Button='%s'\n",
37-
clicked_event->tray_icon_id, clicked_event->button);
36+
printf("Tray icon clicked! ID=%ld\n", clicked_event->tray_icon_id);
3837
}
3938

4039
void on_tray_right_clicked(const void* event, void* user_data) {

src/capi/tray_icon_c.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,6 @@ int native_tray_icon_add_listener(native_tray_icon_t tray_icon,
312312
if (listener_data && listener_data->callback) {
313313
native_tray_icon_clicked_event_t c_event;
314314
c_event.tray_icon_id = event.GetTrayIconId();
315-
strncpy(c_event.button, event.GetButton().c_str(),
316-
sizeof(c_event.button) - 1);
317-
c_event.button[sizeof(c_event.button) - 1] = '\0';
318315
listener_data->callback(&c_event, listener_data->user_data);
319316
}
320317
});

src/capi/tray_icon_c.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ typedef long native_tray_icon_id_t;
3333
*/
3434
typedef struct {
3535
native_tray_icon_id_t tray_icon_id;
36-
char button[16]; // "left", "right", etc.
3736
} native_tray_icon_clicked_event_t;
3837

3938
/**

src/platform/macos/tray_icon_macos.mm

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// for proper memory management of Objective-C objects.
1414

1515
// Forward declarations
16-
typedef void (^TrayIconClickedBlock)(nativeapi::TrayIconId tray_icon_id, const std::string& button);
16+
typedef void (^TrayIconClickedBlock)(nativeapi::TrayIconId tray_icon_id);
1717
typedef void (^TrayIconRightClickedBlock)(nativeapi::TrayIconId tray_icon_id);
1818
typedef void (^TrayIconDoubleClickedBlock)(nativeapi::TrayIconId tray_icon_id);
1919

@@ -133,14 +133,13 @@ - (void)statusItemClicked:(id)sender;
133133
pimpl_->ns_status_bar_button_target_.tray_icon = this;
134134

135135
// 设置默认的 Block 处理器,直接发送事件
136-
pimpl_->ns_status_bar_button_target_.leftClickedBlock =
137-
^(TrayIconId tray_icon_id, const std::string& button) {
138-
try {
139-
Emit<TrayIconClickedEvent>(tray_icon_id, button);
140-
} catch (...) {
141-
// Protect against event emission exceptions
142-
}
143-
};
136+
pimpl_->ns_status_bar_button_target_.leftClickedBlock = ^(TrayIconId tray_icon_id) {
137+
try {
138+
Emit<TrayIconClickedEvent>(tray_icon_id);
139+
} catch (...) {
140+
// Protect against event emission exceptions
141+
}
142+
};
144143

145144
pimpl_->ns_status_bar_button_target_.rightClickedBlock = ^(TrayIconId tray_icon_id) {
146145
try {
@@ -393,7 +392,7 @@ - (void)statusItemClicked:(id)sender {
393392
}
394393
} else {
395394
if (_leftClickedBlock) {
396-
_leftClickedBlock(tray_icon->GetId(), "left");
395+
_leftClickedBlock(tray_icon->GetId());
397396
}
398397
}
399398
}

src/platform/windows/menu_windows.cpp

Lines changed: 68 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <memory>
33
#include <optional>
44
#include <vector>
5+
#include <iostream>
56
#include "../../foundation/id_allocator.h"
67
#include "../../image.h"
78
#include "../../menu.h"
@@ -85,6 +86,71 @@ std::pair<UINT, UINT> ConvertAccelerator(
8586
return std::make_pair(key, modifiers);
8687
}
8788

89+
// Helper function to get the main window for the current thread
90+
HWND GetMainWindowForCurrentThread() {
91+
static HWND main_window = nullptr;
92+
93+
// First try to get the foreground window
94+
HWND hwnd = nullptr;
95+
96+
97+
// Use EnumWindows to find the main window
98+
struct EnumData {
99+
HWND main_window;
100+
DWORD current_thread_id;
101+
};
102+
103+
EnumData data = {nullptr, GetCurrentThreadId()};
104+
105+
EnumWindows(
106+
[](HWND hwnd, LPARAM lParam) -> BOOL {
107+
EnumData* data = reinterpret_cast<EnumData*>(lParam);
108+
109+
// Check if window belongs to current thread
110+
DWORD window_thread_id = GetWindowThreadProcessId(hwnd, nullptr);
111+
if (window_thread_id != data->current_thread_id) {
112+
return TRUE; // Continue enumeration
113+
}
114+
115+
// Check if window is visible and not a tool window
116+
if (!IsWindowVisible(hwnd)) {
117+
return TRUE; // Continue enumeration
118+
}
119+
120+
// Skip tool windows
121+
if (GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TOOLWINDOW) {
122+
return TRUE; // Continue enumeration
123+
}
124+
125+
// Skip message-only windows
126+
if (hwnd == HWND_MESSAGE) {
127+
return TRUE; // Continue enumeration
128+
}
129+
130+
// Check if it's a main window (has a title bar, not a child window)
131+
if (GetParent(hwnd) != nullptr) {
132+
return TRUE; // Continue enumeration
133+
}
134+
135+
// Found a candidate main window
136+
data->main_window = hwnd;
137+
return FALSE; // Stop enumeration
138+
},
139+
reinterpret_cast<LPARAM>(&data));
140+
141+
if (data.main_window) {
142+
return data.main_window;
143+
}
144+
145+
// Fallback: try to find any top-level window
146+
hwnd = FindWindow(nullptr, nullptr);
147+
if (hwnd && IsWindow(hwnd)) {
148+
return hwnd;
149+
}
150+
151+
return nullptr;
152+
}
153+
88154
} // namespace nativeapi
89155

90156
namespace nativeapi {
@@ -485,17 +551,8 @@ bool Menu::Open(double x, double y) {
485551
pimpl_->visible_ = true;
486552

487553
POINT pt = {static_cast<int>(x), static_cast<int>(y)};
488-
// Try to get the foreground window first
489-
HWND hwnd = GetForegroundWindow();
490-
if (!hwnd) {
491-
hwnd = GetActiveWindow();
492-
return hwnd;
493-
}
494-
495-
// If still no window, try to find any top-level window
496-
if (!hwnd) {
497-
hwnd = FindWindow(nullptr, nullptr);
498-
}
554+
// Use the helper function to get the main window for current thread
555+
HWND hwnd = GetMainWindowForCurrentThread();
499556

500557
// Show the context menu
501558
TrackPopupMenu(pimpl_->hmenu_, TPM_BOTTOMALIGN | TPM_LEFTALIGN, pt.x, pt.y, 0,

src/platform/windows/tray_icon_windows.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class TrayIcon::Impl {
2929
std::shared_ptr<Image> image_;
3030

3131
// Callback function types
32-
using ClickedCallback = std::function<void(TrayIconId, const std::string&)>;
32+
using ClickedCallback = std::function<void(TrayIconId)>;
3333
using RightClickedCallback = std::function<void(TrayIconId)>;
3434
using DoubleClickedCallback = std::function<void(TrayIconId)>;
3535

@@ -91,7 +91,7 @@ class TrayIcon::Impl {
9191
<< std::endl;
9292
// Call clicked callback
9393
if (clicked_callback_) {
94-
clicked_callback_(tray_icon_id_, "left");
94+
clicked_callback_(tray_icon_id_);
9595
}
9696
} else if (lparam == WM_RBUTTONUP) {
9797
std::cout << "TrayIcon: Right button clicked, tray_icon_id = " << tray_icon_id_
@@ -180,8 +180,8 @@ TrayIcon::TrayIcon(void* native_tray_icon) {
180180
// The tray_icon_id will be allocated inside Impl constructor
181181
if (hwnd) {
182182
// Create callback functions that emit events
183-
auto clicked_callback = [this](TrayIconId id, const std::string& button) {
184-
this->Emit<TrayIconClickedEvent>(id, button);
183+
auto clicked_callback = [this](TrayIconId id) {
184+
this->Emit<TrayIconClickedEvent>(id);
185185
};
186186

187187
auto right_clicked_callback = [this](TrayIconId id) {

src/tray_icon_event.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,15 @@ class TrayIconEvent : public Event {
2828
*/
2929
class TrayIconClickedEvent : public TrayIconEvent {
3030
public:
31-
TrayIconClickedEvent(TrayIconId tray_icon_id, const std::string& button)
32-
: tray_icon_id_(tray_icon_id), button_(button) {}
31+
TrayIconClickedEvent(TrayIconId tray_icon_id)
32+
: tray_icon_id_(tray_icon_id) {}
3333

3434
TrayIconId GetTrayIconId() const { return tray_icon_id_; }
35-
const std::string& GetButton() const { return button_; }
3635

3736
std::string GetTypeName() const override { return "TrayIconClickedEvent"; }
3837

3938
private:
4039
TrayIconId tray_icon_id_;
41-
std::string button_;
4240
};
4341

4442
/**

0 commit comments

Comments
 (0)