Skip to content

Commit e12967f

Browse files
committed
LightSystemStateController: turn lights on on wake up
1 parent be8b119 commit e12967f

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

Lolgato/LightSystemStateController.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class LightSystemStateController {
1212
category: "LightSystemStateController"
1313
)
1414
private var screenLockMonitor: Any?
15+
private var lightsWereTurnedOff = false
1516

1617
init(deviceManager: ElgatoDeviceManager, appState: AppState) {
1718
self.deviceManager = deviceManager
@@ -42,6 +43,18 @@ class LightSystemStateController {
4243
name: NSWorkspace.screensDidSleepNotification,
4344
object: nil
4445
)
46+
NotificationCenter.default.addObserver(
47+
self,
48+
selector: #selector(handleWakeUp),
49+
name: NSWorkspace.didWakeNotification,
50+
object: nil
51+
)
52+
NotificationCenter.default.addObserver(
53+
self,
54+
selector: #selector(handleWakeUp),
55+
name: NSWorkspace.screensDidWakeNotification,
56+
object: nil
57+
)
4558
}
4659

4760
private func setupScreenLockMonitor() {
@@ -53,6 +66,14 @@ class LightSystemStateController {
5366
) { [weak self] _ in
5467
self?.handleScreenLock()
5568
}
69+
70+
distributed.addObserver(
71+
forName: .init("com.apple.screenIsUnlocked"),
72+
object: nil,
73+
queue: .main
74+
) { [weak self] _ in
75+
self?.handleWakeUp(notification: Notification(name: Notification.Name("screenUnlock")))
76+
}
5677
}
5778

5879
deinit {
@@ -91,6 +112,7 @@ class LightSystemStateController {
91112
Task {
92113
do {
93114
try await device.turnOff()
115+
lightsWereTurnedOff = true
94116
logger
95117
.info(
96118
"Turned off device: \(device.name, privacy: .public) due to \(reason, privacy: .public)"
@@ -104,4 +126,39 @@ class LightSystemStateController {
104126
}
105127
}
106128
}
129+
130+
@objc private func handleWakeUp(notification: Notification) {
131+
guard appState.lightsOffOnSleep, lightsWereTurnedOff else { return }
132+
133+
let reason: String
134+
switch notification.name {
135+
case NSWorkspace.didWakeNotification:
136+
reason = "computer wake"
137+
case NSWorkspace.screensDidWakeNotification:
138+
reason = "screen wake"
139+
default:
140+
reason = "unknown reason"
141+
}
142+
143+
logger.info("System waking up due to \(reason, privacy: .public). Turning on lights.")
144+
145+
for device in deviceManager.devices where device.isOnline {
146+
Task {
147+
do {
148+
try await device.turnOn()
149+
logger
150+
.info(
151+
"Turned on device: \(device.name, privacy: .public) due to \(reason, privacy: .public)"
152+
)
153+
} catch {
154+
logger
155+
.error(
156+
"Failed to turn on device: \(device.name) due to \(reason). Error: \(error.localizedDescription)"
157+
)
158+
}
159+
}
160+
}
161+
162+
lightsWereTurnedOff = false
163+
}
107164
}

Lolgato/Settings.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ struct GeneralSettingsView: View {
3434
}
3535

3636
settingRow(label: "Sleep:") {
37-
Toggle("Lights off automatically", isOn: $appState.lightsOffOnSleep)
37+
Toggle("Lights on and off automatically", isOn: $appState.lightsOffOnSleep)
3838
} caption: {
39-
Text("Turn off lights when system goes to sleep or is locked.")
39+
Text("Turn off lights when system goes to sleep or is locked, and turn them back on when waking up.")
4040
}
4141

4242
Divider()

0 commit comments

Comments
 (0)