Commit ba1291c
feat(application): cross-platform system sleep/wake events (#5425)
* feat(application/mac): emit power events from NSWorkspace observers
Mirrors the windows:APMSuspend / APMResumeAutomatic / APMResumeSuspend
events that Wails v3 already raises from WM_POWERBROADCAST on Windows.
NSWorkspace power notifications post to its own notification center
(not the default NSNotificationCenter that NSApplicationDelegate uses)
so the events are declared with the `!` suffix in events.txt to skip
the delegate-selector auto-generation, and observers are wired by hand
in application_darwin.go's init() alongside the existing theme-change
observer on NSDistributedNotificationCenter.
New events:
- mac:ApplicationWillSleep (NSWorkspaceWillSleepNotification)
- mac:ApplicationDidWake (NSWorkspaceDidWakeNotification)
- mac:ApplicationScreensDidSleep (NSWorkspaceScreensDidSleepNotification)
- mac:ApplicationScreensDidWake (NSWorkspaceScreensDidWakeNotification)
Verification: compile + vet + existing unit tests pass on macOS, linux,
windows. Runtime firing of the observers is not exercised by automated
tests (same gap as the rest of the AppDelegate selectors) — to be
confirmed via xbar2's #807 follow-up wiring and a manual sleep test.
Motivation: xbar2 needs DidWake to force a plugin refresh sweep after
sleep, addressing upstream xbar issue #807 (the most-upvoted open bug).
* feat(application/linux): emit sleep/wake events via logind D-Bus
Adds Linux equivalents to the mac:ApplicationDidWake / WillSleep events
introduced in the previous commit, completing power-event coverage
across macOS, Windows, and Linux.
Implementation subscribes to org.freedesktop.login1.Manager's
PrepareForSleep signal on the system bus. The signal fires twice with
a boolean arg — true just before suspend (→ linux:SystemWillSleep),
false on resume (→ linux:SystemDidWake). The pattern mirrors
monitorThemeChanges() in the same file (goroutine + AddMatchSignal +
range over Signal channel), differing only in bus (system vs session)
and dispatch logic.
Graceful degradation: distros without logind/elogind reachable on the
system bus (Alpine, Void, some Devuan setups) log a warning and the
goroutine exits — the rest of the application keeps running. This
matches the existing theme-monitor behaviour for missing session bus.
New events:
- linux:SystemWillSleep (PrepareForSleep arg=true)
- linux:SystemDidWake (PrepareForSleep arg=false)
Verification: darwin build + vet + tests pass. Linux cross-compile
requires a GTK toolchain not available in this environment, so runtime
behaviour will be confirmed when the branch is built on Linux + a
manual systemctl suspend test. The function structure is a near-copy
of the existing monitorThemeChanges() so the parse-time risk is low.
godbus API calls (ConnectSystemBus, WithMatchInterface, WithMatchMember,
WithMatchObjectPath) verified against v5.2.2 (the version in go.mod).
* feat(application): expose Common.SystemWillSleep / Common.SystemDidWake
Adds two cross-platform power events so apps can listen for sleep/wake
once instead of branching on OS:
app.OnApplicationEvent(events.Common.SystemDidWake, …)
Each platform's events_common_<os>.go map gains an entry that
forwards the platform-specific event into the common one (same
pattern as Common.ThemeChanged and Common.ApplicationStarted):
darwin: Mac.ApplicationWillSleep → Common.SystemWillSleep
Mac.ApplicationDidWake → Common.SystemDidWake
windows: Windows.APMSuspend → Common.SystemWillSleep
Windows.APMResumeAutomatic → Common.SystemDidWake
linux: Linux.SystemWillSleep → Common.SystemWillSleep
Linux.SystemDidWake → Common.SystemDidWake
Notes:
- Windows APMResumeSuspend is deliberately not mapped — it fires
*after* APMResumeAutomatic when the wake was triggered by user
input, so mapping both would double-fire Common.SystemDidWake.
- Mac.ApplicationScreensDidWake / ScreensDidSleep are kept platform-
specific; they fire on display power state (separate from system
sleep) and have no Linux/Windows equivalent.
Verified: darwin build + tests pass locally; Linux build + tests
pass on lin-node1; windows cross-vet of pkg/events clean.
* docs(events): document system sleep/wake events
Adds documentation for the new Common.SystemWillSleep / SystemDidWake
events plus the macOS Mac.ApplicationWillSleep / DidWake / ScreensDidSleep
/ ScreensDidWake and Linux SystemWillSleep / SystemDidWake variants.
- features/events/system.mdx: code examples in the Common block + the
macOS and Linux platform tabs; clarifies the Windows resume choice
(APMResumeAutomatic vs APMResumeSuspend).
- reference/events.mdx: adds the two common events to the application-
events table.
- guides/events-reference.mdx: adds rows to the Common, macOS, and
Linux tables; notes the logind dependency on Linux and the screen-
vs-system sleep distinction on macOS.
* Apply suggestions from code review
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
* fix(events): address PR review feedback
Three review issues from #5425:
1. Common-event forwarding mutated the incoming *ApplicationEvent's Id
before re-queuing. Because application listeners run concurrently
(see EventProcessor.dispatchEventToListeners), this could race with
other listeners observing the source event. Fix: emit a fresh
ApplicationEvent for the common copy, sharing only ctx. Applied
uniformly to all five setupCommonEvents implementations
(darwin / linux / windows / ios / android) since they all had the
same pattern.
2. Linux monitorPowerEvents claimed to warn and exit on systems without
logind/elogind, but AddMatchSignal would succeed on any system bus
and the goroutine would block forever on a channel that never
receives. Probe org.freedesktop.DBus.NameHasOwner for
org.freedesktop.login1 up front and warn + return if absent. Also
constrain the match rule with WithMatchSender so a hostile bus
name can't spoof PrepareForSleep signals (Copilot).
3. docs/guides/events-reference.mdx Windows table omitted
APMResumeAutomatic. Added it with a note distinguishing it from
APMResumeSuspend (CodeRabbit).
Verified: darwin + linux build/test pass; dbus-send NameHasOwner
returns true for org.freedesktop.login1 on the Linux test node.
---------
Co-authored-by: taliesin-ai <bot@taliesin.ai>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>1 parent 6944537 commit ba1291c
19 files changed
Lines changed: 891 additions & 645 deletions
File tree
- docs/src/content/docs
- features/events
- guides
- reference
- v3
- internal
- generator/collect
- runtime/desktop/@wailsio/runtime/src
- pkg
- application
- events
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
224 | 224 | | |
225 | 225 | | |
226 | 226 | | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
227 | 238 | | |
228 | 239 | | |
229 | 240 | | |
| |||
245 | 256 | | |
246 | 257 | | |
247 | 258 | | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
248 | 275 | | |
249 | 276 | | |
250 | 277 | | |
| |||
254 | 281 | | |
255 | 282 | | |
256 | 283 | | |
257 | | - | |
| 284 | + | |
258 | 285 | | |
259 | 286 | | |
260 | 287 | | |
261 | 288 | | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
262 | 297 | | |
263 | 298 | | |
264 | 299 | | |
| |||
268 | 303 | | |
269 | 304 | | |
270 | 305 | | |
271 | | - | |
| 306 | + | |
272 | 307 | | |
273 | 308 | | |
274 | 309 | | |
275 | 310 | | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
276 | 322 | | |
277 | 323 | | |
278 | 324 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
423 | 423 | | |
424 | 424 | | |
425 | 425 | | |
| 426 | + | |
| 427 | + | |
426 | 428 | | |
427 | 429 | | |
428 | 430 | | |
| |||
443 | 445 | | |
444 | 446 | | |
445 | 447 | | |
446 | | - | |
| 448 | + | |
| 449 | + | |
447 | 450 | | |
448 | 451 | | |
449 | 452 | | |
| |||
455 | 458 | | |
456 | 459 | | |
457 | 460 | | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
458 | 465 | | |
459 | 466 | | |
460 | 467 | | |
| |||
465 | 472 | | |
466 | 473 | | |
467 | 474 | | |
| 475 | + | |
| 476 | + | |
468 | 477 | | |
469 | 478 | | |
470 | 479 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
667 | 667 | | |
668 | 668 | | |
669 | 669 | | |
| 670 | + | |
| 671 | + | |
670 | 672 | | |
671 | 673 | | |
672 | 674 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| 13 | + | |
| 14 | + | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
| |||
33 | 35 | | |
34 | 36 | | |
35 | 37 | | |
| 38 | + | |
36 | 39 | | |
| 40 | + | |
37 | 41 | | |
38 | 42 | | |
39 | 43 | | |
| |||
57 | 61 | | |
58 | 62 | | |
59 | 63 | | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
60 | 67 | | |
61 | 68 | | |
62 | 69 | | |
63 | 70 | | |
64 | 71 | | |
| 72 | + | |
65 | 73 | | |
66 | 74 | | |
67 | 75 | | |
| |||
Lines changed: 8 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
76 | 79 | | |
77 | 80 | | |
78 | 81 | | |
79 | 82 | | |
80 | 83 | | |
| 84 | + | |
81 | 85 | | |
82 | 86 | | |
83 | 87 | | |
| |||
194 | 198 | | |
195 | 199 | | |
196 | 200 | | |
| 201 | + | |
197 | 202 | | |
| 203 | + | |
198 | 204 | | |
199 | 205 | | |
200 | 206 | | |
| |||
234 | 240 | | |
235 | 241 | | |
236 | 242 | | |
| 243 | + | |
| 244 | + | |
237 | 245 | | |
238 | 246 | | |
239 | 247 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
60 | 69 | | |
61 | 70 | | |
62 | 71 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
40 | 60 | | |
41 | 61 | | |
42 | 62 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
180 | 180 | | |
181 | 181 | | |
182 | 182 | | |
| 183 | + | |
183 | 184 | | |
184 | 185 | | |
185 | 186 | | |
| |||
275 | 276 | | |
276 | 277 | | |
277 | 278 | | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
278 | 360 | | |
279 | 361 | | |
280 | 362 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
19 | 18 | | |
20 | | - | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
21 | 23 | | |
22 | 24 | | |
23 | 25 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
16 | 18 | | |
17 | | - | |
18 | | - | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
19 | 23 | | |
20 | 24 | | |
21 | 25 | | |
| |||
0 commit comments