33# https://github.com/mathoudebine/turing-smart-screen-python
44import locale
55import os
6+ import platform
67import signal
78import sys
89import time
1516 except :
1617 os ._exit (0 )
1718
19+ from PIL import Image
20+
21+ try :
22+ import pystray
23+ except :
24+ pass
25+
1826from library .log import logger
1927import library .scheduler as scheduler
2028from library .display import display
2432 # Apply system locale to this program
2533 locale .setlocale (locale .LC_ALL , '' )
2634
27- def sighandler (signum , frame ):
28- logger .info (" Caught signal %d, exiting" % signum )
2935
36+ def clean_stop (tray_icon = None ):
3037 # Do not stop the program now in case data transmission was in progress
3138 # Instead, ask the scheduler to empty the action queue before stopping
3239 scheduler .STOPPING = True
@@ -41,13 +48,47 @@ def sighandler(signum, frame):
4148
4249 logger .debug ("(%.1fs)" % (5 - wait_time ))
4350
51+ # Remove tray icon just before exit
52+ if tray_icon :
53+ tray_icon .visible = False
54+
4455 # We force the exit to avoid waiting for other scheduled tasks: they may have a long delay!
4556 try :
4657 sys .exit (0 )
4758 except :
4859 os ._exit (0 )
4960
5061
62+ def sighandler (signum , frame = None ):
63+ logger .info ("Caught signal %d, exiting" % signum )
64+ clean_stop ()
65+
66+
67+ def on_exit_tray (tray_icon , item ):
68+ logger .info ("Exit from tray icon" )
69+ clean_stop (tray_icon )
70+
71+
72+ # Create a tray icon for the program, with an Exit entry in menu
73+ try :
74+ tray_icon = pystray .Icon (
75+ name = 'Turing System Monitor' ,
76+ title = 'Turing System Monitor' ,
77+ icon = Image .open ("res/icons/monitor-icon-17865/64.png" ),
78+ menu = pystray .Menu (
79+ pystray .MenuItem (
80+ 'Exit' ,
81+ on_exit_tray ))
82+ )
83+
84+ # For platforms != macOS, display the tray icon now with non-blocking function
85+ if platform .system () != "Darwin" :
86+ tray_icon .run_detached ()
87+ logger .info ("Tray icon has been displayed" )
88+ except :
89+ tray_icon = None
90+ logger .warning ("Tray icon is not supported on your platform" )
91+
5192 # Set the signal handlers, to send a complete frame to the LCD before exit
5293 signal .signal (signal .SIGINT , sighandler )
5394 signal .signal (signal .SIGTERM , sighandler )
@@ -81,3 +122,7 @@ def sighandler(signum, frame):
81122 scheduler .NetStats ()
82123 scheduler .DateStats ()
83124 scheduler .QueueHandler ()
125+
126+ if tray_icon and platform .system () == "Darwin" :
127+ # For macOS: display the tray icon now with blocking function
128+ tray_icon .run ()
0 commit comments