33#include < memory>
44#include < thread>
55
6+ #include " ../../src/application.h"
7+ #include " ../../src/application_event.h"
68#include " ../../src/image.h"
79#include " ../../src/menu.h"
810#include " ../../src/tray_icon.h"
911#include " ../../src/tray_icon_event.h"
1012#include " ../../src/tray_manager.h"
1113
12- #ifdef __APPLE__
13- #import < Cocoa/Cocoa.h>
14- #endif
15-
1614using namespace nativeapi ;
1715using nativeapi::Menu;
1816using nativeapi::MenuItem;
@@ -22,11 +20,8 @@ using nativeapi::MenuItemType;
2220int main () {
2321 std::cout << " Starting TrayIcon Example..." << std::endl;
2422
25- #ifdef __APPLE__
26- // Initialize Cocoa application for macOS
27- [NSApplication sharedApplication];
28- [NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory];
29- #endif
23+ // Get the Application instance - this handles platform initialization
24+ Application& app = Application::GetInstance ();
3025
3126 // Check if tray icons are supported
3227 TrayManager& trayManager = TrayManager::GetInstance ();
@@ -53,11 +48,11 @@ int main() {
5348 std::cout << " Tray icon ID: " << event.GetTrayIconId () << std::endl;
5449 });
5550
56- trayIcon->AddListener <TrayIconRightClickedEvent>([](const TrayIconRightClickedEvent& event) {
51+ trayIcon->AddListener <TrayIconRightClickedEvent>([trayIcon ](const TrayIconRightClickedEvent& event) {
5752 std::cout << " *** TRAY ICON RIGHT CLICKED! ***" << std::endl;
5853 std::cout << " This is the right click handler working!" << std::endl;
5954 std::cout << " Tray icon ID: " << event.GetTrayIconId () << std::endl;
60- // Context menu will be shown automatically
55+ trayIcon-> OpenContextMenu ();
6156 });
6257
6358 trayIcon->AddListener <TrayIconDoubleClickedEvent>([](const TrayIconDoubleClickedEvent& event) {
@@ -100,10 +95,9 @@ int main() {
10095
10196 // Add exit item
10297 auto exit_item = std::make_shared<MenuItem>(" Exit" , MenuItemType::Normal);
103- bool * should_exit = new bool (false );
104- exit_item->AddListener <MenuItemClickedEvent>([should_exit](const MenuItemClickedEvent& event) {
98+ exit_item->AddListener <MenuItemClickedEvent>([&app](const MenuItemClickedEvent& event) {
10599 std::cout << " Exit clicked from context menu" << std::endl;
106- *should_exit = true ;
100+ app. Quit ( 0 ) ;
107101 });
108102 context_menu->AddItem (exit_item);
109103
@@ -130,36 +124,21 @@ int main() {
130124 std::cout << " - Right click: Opens context menu" << std::endl;
131125 std::cout << " - Double click: Quick double click" << std::endl;
132126 std::cout << " - Context menu: Right-click to see options including Exit" << std::endl;
133- std::cout << " The application will run for 60 seconds, or until you click Exit ." << std::endl;
127+ std::cout << " Use the Exit menu item to quit the application ." << std::endl;
134128 std::cout << " ========================================" << std::endl;
135129
136- // Keep the application running for 60 seconds or until exit is clicked
137- int countdown = 60 ;
138- while (countdown > 0 && !*should_exit) {
139- std::this_thread::sleep_for (std::chrono::seconds (1 ));
140- countdown--;
141-
142- // Check if tray icon is still visible
143- if (!trayIcon->IsVisible ()) {
144- std::cout << " Tray icon is no longer visible!" << std::endl;
145- break ;
146- }
147-
148- // Print countdown every 10 seconds
149- if (countdown % 10 == 0 ) {
150- std::cout << " Application will exit in " << countdown << " seconds..." << std::endl;
130+ // Set up application event listeners
131+ app.AddListener <ApplicationExitingEvent>([&trayIcon](const ApplicationExitingEvent& event) {
132+ std::cout << " Application is exiting with code: " << event.GetExitCode () << std::endl;
133+ // Hide the tray icon before exiting
134+ if (trayIcon) {
135+ trayIcon->SetVisible (false );
151136 }
152- }
137+ });
153138
154- if (*should_exit) {
155- std::cout << " Exit requested from context menu." << std::endl;
156- }
139+ // Run the application event loop - this will block until app.Quit() is called
140+ int exit_code = app.Run ();
157141
158- // Hide the tray icon before exiting
159- trayIcon->SetVisible (false );
160142 std::cout << " Exiting TrayIcon Example..." << std::endl;
161-
162- // Cleanup
163- delete should_exit;
164- return 0 ;
143+ return exit_code;
165144}
0 commit comments