From 9b2f27b7a1fb4c167a7633eb8754969eb65c11f7 Mon Sep 17 00:00:00 2001 From: Kasper Hirvikoski Date: Sat, 5 Feb 2022 22:09:39 +0200 Subject: [PATCH] Add support for device sleep events (#282) --- CHANGELOG.md | 6 ++++++ Phoenix/PHEventTranslator.m | 12 +++++++++++- docs/docs/api/2-events.md | 7 +++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71f27481..59814cd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ Release: dd.mm.yyyy ### New - Improved documentation! 👩🏼‍💻 The documentation is now powered by Docusaurus and hosted at https://kasper.github.io/phoenix/. +- Add support for device sleep events ([#282](https://github.com/kasper/phoenix/issues/282)). - Add support for moving windows to spaces in macOS 12.0 with `Space#moveWindows(...)` ([#289](https://github.com/kasper/phoenix/issues/289)). ### Changes @@ -30,6 +31,11 @@ Release: dd.mm.yyyy ### API +#### Events + +- New: Event `deviceWillSleep` is triggered when the device will sleep ([#282](https://github.com/kasper/phoenix/issues/282)). +- New: Event `deviceDidWake` is triggered when the device did wake ([#282](https://github.com/kasper/phoenix/issues/282)). + #### Space - New: Function `moveWindows(...)` moves the given windows to the space (macOS 10.13+) ([#289](https://github.com/kasper/phoenix/issues/289)). diff --git a/Phoenix/PHEventTranslator.m b/Phoenix/PHEventTranslator.m index 782309ab..5523c0d3 100644 --- a/Phoenix/PHEventTranslator.m +++ b/Phoenix/PHEventTranslator.m @@ -20,7 +20,12 @@ + (NSNotificationCenter *) notificationCenterForNotification:(NSString *)notific NSNotificationCenter *workspaceNotificationCenter = [NSWorkspace sharedWorkspace].notificationCenter; - notificationToNotificationCenter = @{ /* Space Notifications */ + notificationToNotificationCenter = @{ /* Device Notifications */ + + NSWorkspaceWillSleepNotification: workspaceNotificationCenter, + NSWorkspaceDidWakeNotification: workspaceNotificationCenter, + + /* Space Notifications */ NSWorkspaceActiveSpaceDidChangeNotification: workspaceNotificationCenter, @@ -55,6 +60,11 @@ + (NSString *) notificationForEvent:(NSString *)event { @"didLaunch": PHEventDidLaunchNotification, @"willTerminate": PHEventWillTerminateNotification, + /* Device Notifications */ + + @"deviceWillSleep": NSWorkspaceWillSleepNotification, + @"deviceDidWake": NSWorkspaceDidWakeNotification, + /* Screen Notifications */ @"screensDidChange": NSApplicationDidChangeScreenParametersNotification, diff --git a/docs/docs/api/2-events.md b/docs/docs/api/2-events.md index 819cef55..b91188d2 100644 --- a/docs/docs/api/2-events.md +++ b/docs/docs/api/2-events.md @@ -7,6 +7,13 @@ Phoenix supports the following (case sensitive) events. Use these to construct a - `didLaunch` triggered once when Phoenix has launched and the context is ready - `willTerminate` triggered when Phoenix will terminate, use this event to perform any tasks before the application terminates +## Device + +### 2.7.0+ + +- `deviceWillSleep` triggered when the device will sleep +- `deviceDidWake` triggered when the device did wake + ## Screen - `screensDidChange` triggered when screens (i.e. displays) are added, removed, or dynamically reconfigured