|
4 | 4 | __author__ = 'Simon Robinson'
|
5 | 5 | __copyright__ = 'Copyright (c) 2021 Simon Robinson'
|
6 | 6 | __license__ = 'Apache 2.0'
|
7 |
| -__version__ = '2022-01-24' # ISO 8601 |
| 7 | +__version__ = '2022-01-26' # ISO 8601 |
8 | 8 |
|
9 | 9 | import argparse
|
10 | 10 | import asyncore
|
@@ -1071,15 +1071,37 @@ def __init__(self):
|
1071 | 1071 | self.icon.run(self.post_create)
|
1072 | 1072 |
|
1073 | 1073 | # noinspection PyUnresolvedReferences
|
1074 |
| - @staticmethod |
1075 |
| - def init_platforms(): |
| 1074 | + def init_platforms(self): |
1076 | 1075 | if sys.platform == 'darwin':
|
1077 | 1076 | # hide dock icon (but not LSBackgroundOnly as we need input via webview)
|
1078 | 1077 | info = AppKit.NSBundle.mainBundle().infoDictionary()
|
1079 | 1078 | info['LSUIElement'] = '1'
|
| 1079 | + |
| 1080 | + # track shutdown/sleep/wake events and stop/start servers appropriately (also saving activity+configuration) |
| 1081 | + # note: no need to manually remove this observer after OS X 10.11 (https://developer.apple.com/library |
| 1082 | + # /archive/releasenotes/Foundation/RN-FoundationOlderNotes/index.html#10_11NotificationCenter) |
| 1083 | + notification_centre = AppKit.NSWorkspace.sharedWorkspace().notificationCenter() |
| 1084 | + notification_centre.addObserver_selector_name_object_(self, 'macos_nsworkspace_notification_listener:', |
| 1085 | + AppKit.NSWorkspaceWillPowerOffNotification, None) |
| 1086 | + notification_centre.addObserver_selector_name_object_(self, 'macos_nsworkspace_notification_listener:', |
| 1087 | + AppKit.NSWorkspaceWillSleepNotification, None) |
| 1088 | + notification_centre.addObserver_selector_name_object_(self, 'macos_nsworkspace_notification_listener:', |
| 1089 | + AppKit.NSWorkspaceDidWakeNotification, None) |
| 1090 | + |
1080 | 1091 | else:
|
1081 | 1092 | pass # currently no special initialisation/configuration required for other platforms
|
1082 | 1093 |
|
| 1094 | + # noinspection PyUnresolvedReferences |
| 1095 | + def macos_nsworkspace_notification_listener_(self, notification): |
| 1096 | + notification_name = notification.name() |
| 1097 | + if notification_name in [AppKit.NSWorkspaceWillSleepNotification, AppKit.NSWorkspaceWillPowerOffNotification]: |
| 1098 | + Log.info('Detected imminent workspace sleep or shutdown - saving configuration and stopping servers') |
| 1099 | + AppConfig.save() |
| 1100 | + self.stop_servers() |
| 1101 | + elif notification_name == AppKit.NSWorkspaceDidWakeNotification: |
| 1102 | + Log.info('Detected resume from workspace sleep - restarting servers') |
| 1103 | + self.load_and_start_servers(self.icon) |
| 1104 | + |
1083 | 1105 | def create_icon(self):
|
1084 | 1106 | icon_class = RetinaIcon if sys.platform == 'darwin' else pystray.Icon
|
1085 | 1107 | return icon_class(APP_NAME, App.get_image(), APP_NAME, menu=pystray.Menu(
|
|
0 commit comments