Skip to content

Commit d1553a8

Browse files
committed
Refactor menu and menu item ID accessors, remove FindItemByText
Replaces direct access to menu and menu item IDs with GetId() accessor methods across all platforms. Removes the FindItemByText method and related C API, cleaning up unused code and simplifying the menu interface.
1 parent 3fff1a4 commit d1553a8

File tree

8 files changed

+178
-222
lines changed

8 files changed

+178
-222
lines changed

examples/menu_example/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ int main() {
1212
try {
1313
// Create a menu
1414
auto menu = std::make_shared<Menu>();
15-
std::cout << "Created menu with ID: " << menu->id << std::endl;
15+
std::cout << "Created menu with ID: " << menu->GetId() << std::endl;
1616

1717
// Create menu items with different types
1818
auto file_item =

examples/tray_icon_c_example/main.c

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -202,23 +202,6 @@ int main() {
202202
bounds.x, bounds.y, bounds.width, bounds.height);
203203
}
204204

205-
// Demonstrate menu item lookup
206-
native_menu_item_t found_item = native_menu_find_item_by_text(menu, "Exit");
207-
if (found_item) {
208-
printf("Found 'Exit' menu item with ID: %ld\n",
209-
native_menu_item_get_id(found_item));
210-
211-
// Get accelerator info
212-
native_keyboard_accelerator_t accel;
213-
if (native_menu_item_get_accelerator(found_item, &accel)) {
214-
char* accel_str = native_keyboard_accelerator_to_string(&accel);
215-
if (accel_str) {
216-
printf("Exit item accelerator: %s\n", accel_str);
217-
free_c_str(accel_str); // Free the allocated string
218-
}
219-
}
220-
}
221-
222205
// Show all managed tray icons
223206
native_tray_icon_list_t tray_list = native_tray_manager_get_all();
224207
printf("Total managed tray icons: %zu\n", tray_list.count);

src/capi/menu_c.cpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ native_menu_item_id_t native_menu_item_get_id(native_menu_item_t item) {
198198

199199
try {
200200
auto menu_item = static_cast<MenuItem*>(item);
201-
return menu_item->id;
201+
return menu_item->GetId();
202202
} catch (...) {
203203
return -1;
204204
}
@@ -688,7 +688,7 @@ native_menu_id_t native_menu_get_id(native_menu_t menu) {
688688

689689
try {
690690
auto menu_ptr = static_cast<Menu*>(menu);
691-
return menu_ptr->id;
691+
return menu_ptr->GetId();
692692
} catch (...) {
693693
return -1;
694694
}
@@ -893,20 +893,6 @@ native_menu_item_list_t native_menu_get_all_items(native_menu_t menu) {
893893
}
894894
}
895895

896-
native_menu_item_t native_menu_find_item_by_text(native_menu_t menu,
897-
const char* text) {
898-
if (!menu || !text)
899-
return nullptr;
900-
901-
try {
902-
auto menu_ptr = static_cast<Menu*>(menu);
903-
auto item = menu_ptr->FindItemByText(text);
904-
return item ? static_cast<native_menu_item_t>(item.get()) : nullptr;
905-
} catch (...) {
906-
return nullptr;
907-
}
908-
}
909-
910896
bool native_menu_open_at(native_menu_t menu, double x, double y) {
911897
if (!menu)
912898
return false;

src/capi/menu_c.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -497,15 +497,6 @@ native_menu_item_t native_menu_get_item_by_id(native_menu_t menu, native_menu_it
497497
FFI_PLUGIN_EXPORT
498498
native_menu_item_list_t native_menu_get_all_items(native_menu_t menu);
499499

500-
/**
501-
* Find a menu item by text
502-
* @param menu The menu
503-
* @param text The text to search for
504-
* @return The first menu item with matching text, or NULL if not found
505-
*/
506-
FFI_PLUGIN_EXPORT
507-
native_menu_item_t native_menu_find_item_by_text(native_menu_t menu, const char* text);
508-
509500
/**
510501
* Open the menu as a context menu at specified coordinates
511502
* @param menu The menu

src/menu.h

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,14 @@ class MenuItem : public EventEmitter<MenuEvent>, public NativeObjectProvider {
214214
virtual ~MenuItem();
215215

216216
/**
217-
* @brief Unique identifier for this menu item.
217+
* @brief Get the unique identifier for this menu item.
218218
*
219219
* This ID is assigned when the item is created and can be used to
220220
* reference the item in event handlers and other operations.
221+
*
222+
* @return The unique identifier for this menu item
221223
*/
222-
MenuItemId id;
224+
MenuItemId GetId() const;
223225

224226
/**
225227
* @brief Get the type of this menu item.
@@ -540,12 +542,14 @@ class Menu : public EventEmitter<MenuEvent>, public NativeObjectProvider {
540542
virtual ~Menu();
541543

542544
/**
543-
* @brief Unique identifier for this menu.
545+
* @brief Get the unique identifier for this menu.
544546
*
545547
* This ID is assigned when the menu is created and can be used to
546548
* reference the menu in various operations.
549+
*
550+
* @return The unique identifier for this menu
547551
*/
548-
MenuId id;
552+
MenuId GetId() const;
549553

550554
/**
551555
* @brief Add a menu item to the end of the menu.
@@ -663,18 +667,6 @@ class Menu : public EventEmitter<MenuEvent>, public NativeObjectProvider {
663667
*/
664668
std::vector<std::shared_ptr<MenuItem>> GetAllItems() const;
665669

666-
/**
667-
* @brief Find a menu item by its text.
668-
*
669-
* Searches for the first menu item with matching text.
670-
*
671-
* @param text The text to search for
672-
* @param case_sensitive Whether the search should be case-sensitive
673-
* @return Shared pointer to the menu item, or nullptr if not found
674-
*/
675-
std::shared_ptr<MenuItem> FindItemByText(const std::string& text,
676-
bool case_sensitive = true) const;
677-
678670
/**
679671
* @brief Display the menu as a context menu at the specified screen
680672
* coordinates.

src/platform/linux/menu_linux.cpp

Lines changed: 33 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,23 @@ static void OnGtkMenuItemActivate(GtkMenuItem* /*item*/, gpointer user_data) {
2323
if (auto label = menu_item->GetLabel(); label.has_value()) {
2424
text = *label;
2525
}
26-
menu_item->Emit(MenuItemClickedEvent(menu_item->id, text));
26+
menu_item->Emit(MenuItemClickedEvent(menu_item->GetId(), text));
2727
}
2828

2929
static void OnGtkMenuShow(GtkWidget* /*menu*/, gpointer user_data) {
3030
Menu* menu_obj = static_cast<Menu*>(user_data);
3131
if (!menu_obj) {
3232
return;
3333
}
34-
menu_obj->Emit(MenuOpenedEvent(menu_obj->id));
34+
menu_obj->Emit(MenuOpenedEvent(menu_obj->GetId()));
3535
}
3636

3737
static void OnGtkMenuHide(GtkWidget* /*menu*/, gpointer user_data) {
3838
Menu* menu_obj = static_cast<Menu*>(user_data);
3939
if (!menu_obj) {
4040
return;
4141
}
42-
menu_obj->Emit(MenuClosedEvent(menu_obj->id));
42+
menu_obj->Emit(MenuClosedEvent(menu_obj->GetId()));
4343
}
4444

4545
static void OnGtkSubmenuShow(GtkWidget* /*submenu*/, gpointer user_data) {
@@ -48,7 +48,7 @@ static void OnGtkSubmenuShow(GtkWidget* /*submenu*/, gpointer user_data) {
4848
return;
4949
}
5050
// Emit submenu opened on the item
51-
menu_item->Emit(MenuItemSubmenuOpenedEvent(menu_item->id));
51+
menu_item->Emit(MenuItemSubmenuOpenedEvent(menu_item->GetId()));
5252
}
5353

5454
static void OnGtkSubmenuHide(GtkWidget* /*submenu*/, gpointer user_data) {
@@ -57,14 +57,15 @@ static void OnGtkSubmenuHide(GtkWidget* /*submenu*/, gpointer user_data) {
5757
return;
5858
}
5959
// Emit submenu closed on the item
60-
menu_item->Emit(MenuItemSubmenuClosedEvent(menu_item->id));
60+
menu_item->Emit(MenuItemSubmenuClosedEvent(menu_item->GetId()));
6161
}
6262

6363
// Private implementation class for MenuItem
6464
class MenuItem::Impl {
6565
public:
66-
Impl(GtkWidget* menu_item, MenuItemType type)
67-
: gtk_menu_item_(menu_item),
66+
Impl(MenuItemId id, GtkWidget* menu_item, MenuItemType type)
67+
: id_(id),
68+
gtk_menu_item_(menu_item),
6869
title_(""),
6970
tooltip_(""),
7071
type_(type),
@@ -74,6 +75,7 @@ class MenuItem::Impl {
7475
radio_group_(-1),
7576
accelerator_("", KeyboardAccelerator::None) {}
7677

78+
MenuItemId id_;
7779
GtkWidget* gtk_menu_item_;
7880
std::optional<std::string> title_;
7981
std::shared_ptr<Image> image_;
@@ -87,8 +89,8 @@ class MenuItem::Impl {
8789
std::shared_ptr<Menu> submenu_;
8890
};
8991

90-
MenuItem::MenuItem(const std::string& text, MenuItemType type)
91-
: id(IdAllocator::Allocate<MenuItem>()) {
92+
MenuItem::MenuItem(const std::string& text, MenuItemType type) {
93+
MenuItemId id = IdAllocator::Allocate<MenuItem>();
9294
GtkWidget* gtk_item = nullptr;
9395

9496
switch (type) {
@@ -108,7 +110,7 @@ MenuItem::MenuItem(const std::string& text, MenuItemType type)
108110
break;
109111
}
110112

111-
pimpl_ = std::unique_ptr<Impl>(new Impl(gtk_item, type));
113+
pimpl_ = std::unique_ptr<Impl>(new Impl(id, gtk_item, type));
112114

113115
if (!text.empty()) {
114116
pimpl_->title_ = text;
@@ -123,9 +125,9 @@ MenuItem::MenuItem(const std::string& text, MenuItemType type)
123125
}
124126
}
125127

126-
MenuItem::MenuItem(void* menu_item)
127-
: id(IdAllocator::Allocate<MenuItem>()),
128-
pimpl_(new Impl((GtkWidget*)menu_item, MenuItemType::Normal)) {
128+
MenuItem::MenuItem(void* menu_item) {
129+
MenuItemId id = IdAllocator::Allocate<MenuItem>();
130+
pimpl_ = std::unique_ptr<Impl>(new Impl(id, (GtkWidget*)menu_item, MenuItemType::Normal));
129131
if (pimpl_->gtk_menu_item_ && pimpl_->type_ != MenuItemType::Separator) {
130132
const char* label =
131133
gtk_menu_item_get_label(GTK_MENU_ITEM(pimpl_->gtk_menu_item_));
@@ -144,6 +146,10 @@ MenuItem::MenuItem(void* menu_item)
144146

145147
MenuItem::~MenuItem() {}
146148

149+
MenuItemId MenuItem::GetId() const {
150+
return pimpl_->id_;
151+
}
152+
147153
MenuItemType MenuItem::GetType() const {
148154
return pimpl_->type_;
149155
}
@@ -294,17 +300,18 @@ void* MenuItem::GetNativeObjectInternal() const {
294300
// Private implementation class for Menu
295301
class Menu::Impl {
296302
public:
297-
Impl(GtkWidget* menu) : gtk_menu_(menu), enabled_(true), visible_(false) {}
303+
Impl(MenuId id, GtkWidget* menu) : id_(id), gtk_menu_(menu), enabled_(true), visible_(false) {}
298304

305+
MenuId id_;
299306
GtkWidget* gtk_menu_;
300307
std::vector<std::shared_ptr<MenuItem>> items_;
301308
bool enabled_;
302309
bool visible_;
303310
};
304311

305-
Menu::Menu()
306-
: id(IdAllocator::Allocate<Menu>()),
307-
pimpl_(std::unique_ptr<Impl>(new Impl(gtk_menu_new()))) {
312+
Menu::Menu() {
313+
MenuId id = IdAllocator::Allocate<Menu>();
314+
pimpl_ = std::unique_ptr<Impl>(new Impl(id, gtk_menu_new()));
308315
// Connect menu show/hide to emit open/close events
309316
if (pimpl_->gtk_menu_) {
310317
g_signal_connect(G_OBJECT(pimpl_->gtk_menu_), "show",
@@ -314,8 +321,9 @@ Menu::Menu()
314321
}
315322
}
316323

317-
Menu::Menu(void* menu)
318-
: id(IdAllocator::Allocate<Menu>()), pimpl_(new Impl((GtkWidget*)menu)) {
324+
Menu::Menu(void* menu) {
325+
MenuId id = IdAllocator::Allocate<Menu>();
326+
pimpl_ = std::unique_ptr<Impl>(new Impl(id, (GtkWidget*)menu));
319327
if (pimpl_->gtk_menu_) {
320328
g_signal_connect(G_OBJECT(pimpl_->gtk_menu_), "show",
321329
G_CALLBACK(OnGtkMenuShow), this);
@@ -330,6 +338,10 @@ Menu::~Menu() {
330338
}
331339
}
332340

341+
MenuId Menu::GetId() const {
342+
return pimpl_->id_;
343+
}
344+
333345
void Menu::AddItem(std::shared_ptr<MenuItem> item) {
334346
if (pimpl_->gtk_menu_ && item && item->GetNativeObject()) {
335347
pimpl_->items_.push_back(item);
@@ -369,7 +381,7 @@ bool Menu::RemoveItem(std::shared_ptr<MenuItem> item) {
369381

370382
bool Menu::RemoveItemById(MenuItemId item_id) {
371383
for (auto& item : pimpl_->items_) {
372-
if (item->id == item_id) {
384+
if (item->GetId() == item_id) {
373385
return RemoveItem(item);
374386
}
375387
}
@@ -413,7 +425,7 @@ std::shared_ptr<MenuItem> Menu::GetItemAt(size_t index) const {
413425

414426
std::shared_ptr<MenuItem> Menu::GetItemById(MenuItemId item_id) const {
415427
for (const auto& item : pimpl_->items_) {
416-
if (item->id == item_id) {
428+
if (item->GetId() == item_id) {
417429
return item;
418430
}
419431
}
@@ -424,34 +436,6 @@ std::vector<std::shared_ptr<MenuItem>> Menu::GetAllItems() const {
424436
return pimpl_->items_;
425437
}
426438

427-
std::shared_ptr<MenuItem> Menu::FindItemByText(const std::string& text,
428-
bool case_sensitive) const {
429-
for (const auto& item : pimpl_->items_) {
430-
auto itemTextOpt = item->GetLabel();
431-
if (!itemTextOpt.has_value()) {
432-
continue;
433-
}
434-
435-
const std::string& item_text = itemTextOpt.value();
436-
if (case_sensitive) {
437-
if (item_text == text) {
438-
return item;
439-
}
440-
} else {
441-
std::string lower_item_text = item_text;
442-
std::string lower_search_text = text;
443-
std::transform(lower_item_text.begin(), lower_item_text.end(),
444-
lower_item_text.begin(), ::tolower);
445-
std::transform(lower_search_text.begin(), lower_search_text.end(),
446-
lower_search_text.begin(), ::tolower);
447-
if (lower_item_text == lower_search_text) {
448-
return item;
449-
}
450-
}
451-
}
452-
return nullptr;
453-
}
454-
455439
bool Menu::Open(double x, double y) {
456440
if (pimpl_->gtk_menu_) {
457441
pimpl_->visible_ = true;

0 commit comments

Comments
 (0)