Skip to content

Commit c82306b

Browse files
committed
Remove item text from MenuItemClickedEvent
Refactored MenuItemClickedEvent to no longer require or emit the menu item's text. Updated Linux, macOS, and Windows platform code to match the new event signature, simplifying event handling and reducing unnecessary data passing.
1 parent 7d6a443 commit c82306b

File tree

4 files changed

+10
-22
lines changed

4 files changed

+10
-22
lines changed

src/menu_event.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,14 @@ class MenuClosedEvent : public MenuEvent {
6565
*/
6666
class MenuItemClickedEvent : public MenuEvent {
6767
public:
68-
MenuItemClickedEvent(MenuItemId item_id, const std::string& item_text)
69-
: item_id_(item_id), item_text_(item_text) {}
68+
MenuItemClickedEvent(MenuItemId item_id) : item_id_(item_id) {}
7069

7170
MenuItemId GetItemId() const { return item_id_; }
72-
const std::string& GetItemText() const { return item_text_; }
7371

7472
std::string GetTypeName() const override { return "MenuItemClickedEvent"; }
7573

7674
private:
7775
MenuItemId item_id_;
78-
std::string item_text_;
7976
};
8077

8178
/**

src/platform/linux/menu_linux.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ static void OnGtkMenuItemActivate(GtkMenuItem* /*item*/, gpointer user_data) {
1919
if (!menu_item) {
2020
return;
2121
}
22-
std::string text = "";
23-
if (auto label = menu_item->GetLabel(); label.has_value()) {
24-
text = *label;
25-
}
26-
menu_item->Emit(MenuItemClickedEvent(menu_item->GetId(), text));
22+
menu_item->Emit(MenuItemClickedEvent(menu_item->GetId()));
2723
}
2824

2925
static void OnGtkMenuShow(GtkWidget* /*menu*/, gpointer user_data) {

src/platform/macos/menu_macos.mm

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
static const void* kMenuIdKey = &kMenuIdKey;
2020

2121
// Forward declarations - moved to global scope
22-
typedef void (^MenuItemClickedBlock)(nativeapi::MenuItemId item_id, const std::string& item_label);
22+
typedef void (^MenuItemClickedBlock)(nativeapi::MenuItemId item_id);
2323
typedef void (^MenuOpenedBlock)(nativeapi::MenuId menu_id);
2424
typedef void (^MenuClosedBlock)(nativeapi::MenuId menu_id);
2525

@@ -125,14 +125,11 @@ - (void)menuItemClicked:(id)sender {
125125

126126
// Call the block if it exists
127127
if (_clickedBlock) {
128-
NSString* title = [menu_item title];
129-
std::string item_label = title ? [title UTF8String] : "";
130-
131128
// Get the MenuItemId from the menu item's associated object
132129
NSNumber* item_id_obj = objc_getAssociatedObject(menu_item, kMenuItemIdKey);
133130
if (item_id_obj) {
134131
nativeapi::MenuItemId item_id = [item_id_obj longValue];
135-
_clickedBlock(item_id, item_label);
132+
_clickedBlock(item_id);
136133
}
137134
}
138135
} @catch (NSException* exception) {
@@ -277,9 +274,9 @@ - (void)menuDidClose:(NSMenu*)menu {
277274
pimpl_->label_ = text.empty() ? std::nullopt : std::optional<std::string>(text);
278275

279276
// 设置默认的 Block 处理器,直接发送事件
280-
pimpl_->ns_menu_item_target_.clickedBlock = ^(MenuItemId item_id, const std::string& item_label) {
277+
pimpl_->ns_menu_item_target_.clickedBlock = ^(MenuItemId item_id) {
281278
try {
282-
Emit<MenuItemClickedEvent>(item_id, item_label);
279+
Emit<MenuItemClickedEvent>(item_id);
283280
} catch (...) {
284281
// Protect against event emission exceptions
285282
}
@@ -294,9 +291,9 @@ - (void)menuDidClose:(NSMenu*)menu {
294291
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
295292

296293
// 设置默认的 Block 处理器,直接发送事件
297-
pimpl_->ns_menu_item_target_.clickedBlock = ^(MenuItemId item_id, const std::string& item_label) {
294+
pimpl_->ns_menu_item_target_.clickedBlock = ^(MenuItemId item_id) {
298295
try {
299-
Emit<MenuItemClickedEvent>(item_id, item_label);
296+
Emit<MenuItemClickedEvent>(item_id);
300297
} catch (...) {
301298
// Protect against event emission exceptions
302299
}
@@ -547,8 +544,7 @@ - (void)menuDidClose:(NSMenu*)menu {
547544

548545
// Call the block directly instead of going through target-action
549546
if (pimpl_->ns_menu_item_target_.clickedBlock) {
550-
std::string itemText = pimpl_->label_.value_or("");
551-
pimpl_->ns_menu_item_target_.clickedBlock(pimpl_->id_, itemText);
547+
pimpl_->ns_menu_item_target_.clickedBlock(pimpl_->id_);
552548
}
553549
return true;
554550
}

src/platform/windows/menu_windows.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,7 @@ bool MenuItem::Trigger() {
284284
return false;
285285

286286
try {
287-
std::string text = pimpl_->label_.has_value() ? pimpl_->label_.value() : "";
288-
Emit<MenuItemClickedEvent>(id, text);
287+
Emit<MenuItemClickedEvent>(pimpl_->id_);
289288
} catch (...) {
290289
// Protect against event emission exceptions
291290
}

0 commit comments

Comments
 (0)