Skip to content

Commit 99c6247

Browse files
committed
Refactor menu example to use application event loop
The menu example now initializes the Application instance and runs its event loop, opening the menu when the application starts and exiting cleanly via the Exit menu item. Removed direct programmatic event triggering and context menu opening from main(). Also, added deprecation ignore macros around AppIndicator creation in tray_icon_linux.cpp.
1 parent d1f44a5 commit 99c6247

File tree

2 files changed

+32
-41
lines changed

2 files changed

+32
-41
lines changed

examples/menu_example/main.cpp

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ using namespace nativeapi;
99
int main() {
1010
std::cout << "=== Menu Event System Example ===" << std::endl;
1111

12+
// Get the Application instance to initialize platform
13+
Application& app = Application::GetInstance();
14+
1215
try {
1316
// Create a menu
1417
auto menu = std::make_shared<Menu>();
@@ -57,9 +60,10 @@ int main() {
5760
<< " - Handle state manually" << std::endl;
5861
});
5962

60-
exit_item->AddListener<MenuItemClickedEvent>([](const MenuItemClickedEvent& event) {
63+
exit_item->AddListener<MenuItemClickedEvent>([&app](const MenuItemClickedEvent& event) {
6164
std::cout << "[EVENT] Exit item clicked: Exit" << std::endl;
62-
std::cout << "Application should exit now..." << std::endl;
65+
std::cout << "Application exiting..." << std::endl;
66+
app.Quit(0);
6367
});
6468

6569
// Listen to menu events
@@ -83,40 +87,6 @@ int main() {
8387

8488
std::cout << "Menu created with " << menu->GetItemCount() << " items" << std::endl;
8589

86-
// Demonstrate programmatic triggering by emitting events directly
87-
std::cout << "\n=== Testing Programmatic Event Triggering ===" << std::endl;
88-
89-
std::cout << "Triggering file item..." << std::endl;
90-
file_item->Emit(MenuItemClickedEvent(file_item->GetId()));
91-
92-
std::cout << "Triggering checkbox item..." << std::endl;
93-
checkbox_item->Emit(MenuItemClickedEvent(checkbox_item->GetId()));
94-
95-
std::cout << "Triggering checkbox item again..." << std::endl;
96-
checkbox_item->Emit(MenuItemClickedEvent(checkbox_item->GetId()));
97-
98-
std::cout << "Switching radio button..." << std::endl;
99-
radio_item2->Emit(MenuItemClickedEvent(radio_item2->GetId()));
100-
101-
std::cout << "Triggering exit item..." << std::endl;
102-
exit_item->Emit(MenuItemClickedEvent(exit_item->GetId()));
103-
104-
// Open menu as context menu (this may not work in console applications)
105-
std::cout << "\n=== Attempting to Open Context Menu ===" << std::endl;
106-
std::cout << "Note: Context menu display may not work in console applications" << std::endl;
107-
108-
// Try to show at screen coordinates (100, 100) with default placement (BottomStart)
109-
if (menu->Open(PositioningStrategy::Absolute({100, 100}))) {
110-
std::cout << "Context menu opened successfully (BottomStart placement)" << std::endl;
111-
} else {
112-
std::cout << "Failed to open context menu (expected in console app)" << std::endl;
113-
}
114-
115-
// Demonstrate placement parameter - open menu above the point
116-
if (menu->Open(PositioningStrategy::Absolute({100, 200}), Placement::Top)) {
117-
std::cout << "Context menu opened successfully (Top placement)" << std::endl;
118-
}
119-
12090
// Demonstrate submenu
12191
std::cout << "\n=== Testing Submenu ===" << std::endl;
12292
auto submenu = std::make_shared<Menu>();
@@ -152,11 +122,6 @@ int main() {
152122

153123
std::cout << "Added submenu with " << submenu->GetItemCount() << " items" << std::endl;
154124

155-
// Test submenu items
156-
std::cout << "Triggering submenu items..." << std::endl;
157-
submenu_item1->Emit(MenuItemClickedEvent(submenu_item1->GetId()));
158-
submenu_item2->Emit(MenuItemClickedEvent(submenu_item2->GetId()));
159-
160125
std::cout << "\n=== Event System Demo Complete ===" << std::endl;
161126
std::cout << "This example demonstrates:" << std::endl;
162127
std::cout << "1. Creating menus and menu items with different types" << std::endl;
@@ -169,6 +134,30 @@ int main() {
169134
std::cout << "6. Programmatic event emission using Emit()" << std::endl;
170135
std::cout << "7. Submenu support with event propagation" << std::endl;
171136

137+
std::cout << "\n========================================" << std::endl;
138+
std::cout << "Starting application event loop..." << std::endl;
139+
std::cout << "The menu will open shortly." << std::endl;
140+
std::cout << "Click the Exit menu item to quit the application." << std::endl;
141+
std::cout << "========================================" << std::endl;
142+
143+
// Set up application started listener to open menu after event loop starts
144+
app.AddListener<ApplicationStartedEvent>([menu](const ApplicationStartedEvent& event) {
145+
std::cout << "Application started - opening menu at (100, 100)" << std::endl;
146+
147+
// Open menu as context menu at screen coordinates (100, 100)
148+
if (menu->Open(PositioningStrategy::Absolute({100, 100}))) {
149+
std::cout << "Context menu opened successfully!" << std::endl;
150+
} else {
151+
std::cout << "Failed to open context menu" << std::endl;
152+
}
153+
});
154+
155+
// Run the application event loop - this will block until app.Quit() is called
156+
int exit_code = app.Run();
157+
158+
std::cout << "Exiting Menu Example..." << std::endl;
159+
return exit_code;
160+
172161
} catch (const std::exception& e) {
173162
std::cerr << "Error: " << e.what() << std::endl;
174163
return 1;

src/platform/linux/tray_icon_linux.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,11 @@ TrayIcon::TrayIcon() : pimpl_(std::make_unique<Impl>(nullptr)) {
6363
std::string indicator_id = "nativeapi-tray-" + std::to_string(next_indicator_id++);
6464

6565
// Create a new tray using AppIndicator
66+
G_GNUC_BEGIN_IGNORE_DEPRECATIONS // TODO: Use libayatana-appindicator-glib instead of libayatana-appindicator in the future
6667
AppIndicator* app_indicator = app_indicator_new(indicator_id.c_str(),
6768
"application-default-icon", // Default icon name
6869
APP_INDICATOR_CATEGORY_APPLICATION_STATUS);
70+
G_GNUC_END_IGNORE_DEPRECATIONS
6971

7072
if (app_indicator) {
7173
// Reinitialize the Impl with the created indicator

0 commit comments

Comments
 (0)