1313// for proper memory management of Objective-C objects.
1414
1515// Forward declarations
16- typedef void (^TrayIconClickedBlock)(nativeapi::TrayIconId tray_icon_id );
17- typedef void (^TrayIconRightClickedBlock)(nativeapi::TrayIconId tray_icon_id );
18- typedef void (^TrayIconDoubleClickedBlock)(nativeapi::TrayIconId tray_icon_id );
16+ typedef void (^TrayIconClickedBlock)(void );
17+ typedef void (^TrayIconRightClickedBlock)(void );
18+ typedef void (^TrayIconDoubleClickedBlock)(void );
1919
2020// Key for associated object to store tray icon ID
2121static const void * kTrayIconIdKey = &kTrayIconIdKey ;
@@ -24,8 +24,7 @@ @interface NSStatusBarButtonTarget : NSObject
2424@property (nonatomic , copy ) TrayIconClickedBlock leftClickedBlock;
2525@property (nonatomic , copy ) TrayIconRightClickedBlock rightClickedBlock;
2626@property (nonatomic , copy ) TrayIconDoubleClickedBlock doubleClickedBlock;
27- @property (nonatomic , assign ) nativeapi::TrayIcon* tray_icon;
28- - (void )statusItemClicked : (id )sender ;
27+ - (void )handleStatusItemEvent : (id )sender ;
2928@end
3029
3130namespace nativeapi {
@@ -54,11 +53,10 @@ - (void)statusItemClicked:(id)sender;
5453
5554 // Create and set up button target
5655 ns_status_bar_button_target_ = [[NSStatusBarButtonTarget alloc ] init ];
57- ns_status_bar_button_target_.tray_icon = nullptr ; // Will be set later
5856
59- // Set up click handlers
57+ // Set up event handlers
6058 [status_item.button setTarget: ns_status_bar_button_target_];
61- [status_item.button setAction: @selector (statusItemClicked : )];
59+ [status_item.button setAction: @selector (handleStatusItemEvent : )];
6260
6361 // Enable right-click handling
6462 [status_item.button sendActionOn: NSEventMaskLeftMouseUp | NSEventMaskRightMouseUp];
@@ -77,7 +75,6 @@ - (void)statusItemClicked:(id)sender;
7775 ns_status_bar_button_target_.leftClickedBlock = nil ;
7876 ns_status_bar_button_target_.rightClickedBlock = nil ;
7977 ns_status_bar_button_target_.doubleClickedBlock = nil ;
80- ns_status_bar_button_target_.tray_icon = nullptr ;
8178 ns_status_bar_button_target_ = nil ;
8279 }
8380
@@ -129,42 +126,23 @@ - (void)statusItemClicked:(id)sender;
129126 // Initialize the Impl with the status item
130127 pimpl_ = std::make_unique<Impl>(status_item);
131128
129+ // Set up click handlers
132130 if (pimpl_->ns_status_bar_button_target_ ) {
133- pimpl_->ns_status_bar_button_target_ .tray_icon = this ;
134-
135- // 设置默认的 Block 处理器,直接发送事件
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- }
131+ pimpl_->ns_status_bar_button_target_ .leftClickedBlock = ^{
132+ Emit<TrayIconClickedEvent>(pimpl_->id_ );
142133 };
143134
144- pimpl_->ns_status_bar_button_target_ .rightClickedBlock = ^(TrayIconId tray_icon_id) {
145- try {
146- Emit<TrayIconRightClickedEvent>(tray_icon_id);
147- } catch (...) {
148- // Protect against event emission exceptions
149- }
135+ pimpl_->ns_status_bar_button_target_ .rightClickedBlock = ^{
136+ Emit<TrayIconRightClickedEvent>(pimpl_->id_ );
150137 };
151138
152- pimpl_->ns_status_bar_button_target_ .doubleClickedBlock = ^(TrayIconId tray_icon_id) {
153- try {
154- Emit<TrayIconDoubleClickedEvent>(tray_icon_id);
155- } catch (...) {
156- // Protect against event emission exceptions
157- }
139+ pimpl_->ns_status_bar_button_target_ .doubleClickedBlock = ^{
140+ Emit<TrayIconDoubleClickedEvent>(pimpl_->id_ );
158141 };
159142 }
160143}
161144
162- TrayIcon::~TrayIcon () {
163- // Clear the button target's reference to this object before destruction
164- if (pimpl_ && pimpl_->ns_status_bar_button_target_ ) {
165- pimpl_->ns_status_bar_button_target_ .tray_icon = nullptr ;
166- }
167- }
145+ TrayIcon::~TrayIcon () = default ;
168146
169147TrayIconId TrayIcon::GetId () {
170148 return pimpl_->id_ ;
@@ -362,16 +340,7 @@ - (void)statusItemClicked:(id)sender;
362340// Implementation of NSStatusBarButtonTarget
363341@implementation NSStatusBarButtonTarget
364342
365- - (void )statusItemClicked : (id )sender {
366- // Check if tray_icon is still valid before proceeding
367- if (!self.tray_icon )
368- return ;
369-
370- // Create a local reference to prevent race conditions
371- nativeapi::TrayIcon* tray_icon = self.tray_icon ;
372- if (!tray_icon)
373- return ;
374-
343+ - (void )handleStatusItemEvent : (id )sender {
375344 NSEvent * event = [NSApp currentEvent ];
376345 if (!event)
377346 return ;
@@ -382,17 +351,17 @@ - (void)statusItemClicked:(id)sender {
382351 (event.modifierFlags & NSEventModifierFlagControl))) {
383352 // Right click or Ctrl+Left click
384353 if (_rightClickedBlock) {
385- _rightClickedBlock (tray_icon-> GetId () );
354+ _rightClickedBlock ();
386355 }
387356 } else if (event.type == NSEventTypeLeftMouseUp) {
388357 // Check for double click
389358 if (event.clickCount == 2 ) {
390359 if (_doubleClickedBlock) {
391- _doubleClickedBlock (tray_icon-> GetId () );
360+ _doubleClickedBlock ();
392361 }
393362 } else {
394363 if (_leftClickedBlock) {
395- _leftClickedBlock (tray_icon-> GetId () );
364+ _leftClickedBlock ();
396365 }
397366 }
398367 }
0 commit comments