@@ -53,11 +53,6 @@ using namespace std::literals;
5353namespace system_tray {
5454 static std::atomic tray_initialized = false ;
5555
56- // Threading variables for all platforms
57- static std::thread tray_thread;
58- static std::atomic tray_thread_running = false ;
59- static std::atomic tray_thread_should_exit = false ;
60-
6156 void tray_open_ui_cb ([[maybe_unused]] struct tray_menu *item) {
6257 BOOST_LOG (info) << " Opening UI from system tray" sv;
6358 launch_ui ();
@@ -207,16 +202,12 @@ namespace system_tray {
207202
208203 int process_tray_events () {
209204 if (!tray_initialized) {
205+ BOOST_LOG (error) << " System tray is not initialized" sv;
210206 return 1 ;
211207 }
212208
213- // Process one iteration of the tray loop with non-blocking mode (0)
214- if (const int result = tray_loop (0 ); result != 0 ) {
215- BOOST_LOG (warning) << " System tray loop failed" sv;
216- return result;
217- }
218-
219- return 0 ;
209+ // Block until an event is processed or tray_quit() is called
210+ return tray_loop (1 );
220211 }
221212
222213 int end_tray () {
@@ -319,63 +310,22 @@ namespace system_tray {
319310 // Initialize the tray in this thread
320311 if (init_tray () != 0 ) {
321312 BOOST_LOG (error) << " Failed to initialize tray in thread" sv;
322- tray_thread_running = false ;
323313 return ;
324314 }
325315
326- tray_thread_running = true ;
327-
328316 // Main tray event loop
329- while (!tray_thread_should_exit) {
330- if (process_tray_events () != 0 ) {
331- BOOST_LOG (warning) << " Tray event processing failed in thread" sv;
332- break ;
333- }
317+ while (process_tray_events () == 0 );
334318
335- // Sleep to avoid busy waiting
336- std::this_thread::sleep_for (std::chrono::milliseconds (50 ));
337- }
338-
339- // Clean up the tray
340- end_tray ();
341- tray_thread_running = false ;
342319 BOOST_LOG (info) << " System tray thread ended" sv;
343320 }
344321
345322 int init_tray_threaded () {
346- if (tray_thread_running) {
347- BOOST_LOG (warning) << " Tray thread is already running" sv;
348- return 1 ;
349- }
350-
351- tray_thread_should_exit = false ;
352-
353323 try {
354- tray_thread = std::thread (tray_thread_worker);
355-
356- // Wait for the thread to start and initialize
357- const auto start_time = std::chrono::steady_clock::now ();
358- while (!tray_thread_running && !tray_thread_should_exit) {
359- std::this_thread::sleep_for (std::chrono::milliseconds (10 ));
360-
361- // Timeout after 10 seconds
362- if (std::chrono::steady_clock::now () - start_time > std::chrono::seconds (10 )) {
363- BOOST_LOG (error) << " Tray thread initialization timeout" sv;
364- tray_thread_should_exit = true ;
365- if (tray_thread.joinable ()) {
366- tray_thread.join ();
367- }
368- return 1 ;
369- }
370- }
324+ auto tray_thread = std::thread (tray_thread_worker);
371325
372- if (!tray_thread_running) {
373- BOOST_LOG (error) << " Tray thread failed to start" sv;
374- if (tray_thread.joinable ()) {
375- tray_thread.join ();
376- }
377- return 1 ;
378- }
326+ // The tray thread doesn't require strong lifetime management.
327+ // It will exit asynchronously when tray_exit() is called.
328+ tray_thread.detach ();
379329
380330 BOOST_LOG (info) << " System tray thread initialized successfully" sv;
381331 return 0 ;
@@ -385,21 +335,5 @@ namespace system_tray {
385335 }
386336 }
387337
388- int end_tray_threaded () {
389- if (!tray_thread_running) {
390- return 0 ;
391- }
392-
393- BOOST_LOG (info) << " Stopping system tray thread" sv;
394- tray_thread_should_exit = true ;
395-
396- if (tray_thread.joinable ()) {
397- tray_thread.join ();
398- }
399-
400- BOOST_LOG (info) << " System tray thread stopped" sv;
401- return 0 ;
402- }
403-
404338} // namespace system_tray
405339#endif
0 commit comments