-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathApp.swift
More file actions
705 lines (647 loc) · 35.3 KB
/
Copy pathApp.swift
File metadata and controls
705 lines (647 loc) · 35.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
// App.swift. Sleepless: a standalone menu-bar toggle that keeps the Mac running
// with the lid closed (on battery, no external display) via `pmset disablesleep`.
//
// Mechanism (verified live on this machine; disablesleep is UNDOCUMENTED in
// pmset(1) but real. It sets IORegistry "SleepDisabled" = Yes and disables
// idle + Apple-menu + lid-close clamshell sleep):
// ON : sudo pmset -a disablesleep 1
// OFF: sudo pmset -a disablesleep 0
// READ (no root): pmset -g | grep -i SleepDisabled (value 1 = ON; 0/absent = OFF)
// The OFF/ON commands run passwordless via a tightly-scoped /etc/sudoers.d drop-in.
// disablesleep is runtime-only and resets to 0 on reboot, and that reset is a
// deliberate safety feature; the app does NOT auto re-arm.
//
// UI: clicking the menu-bar coffee cup opens a small native popover with an NSSwitch
// toggle (the System-Settings control), a state caption, an auto-off timer, the
// battery-floor slider, a Launch-at-login switch, and Quit. The menu-bar glyph also
// shows state at a glance.
//
// The coffee-cup metaphor is literal: an EMPTY cup means the Mac sleeps normally, a
// FULL cup means it is being kept awake (caffeinated), and a full cup with a small
// dot means it is awake on battery with the auto-off safety net live.
//
// Three small, fail-safe features layer on top, none of which adds a daemon or
// persists OS state (so "reboot resets it" still holds):
// 1. Auto-off timer (1h / 2h) — a one-shot in-memory Timer that flips sleep back
// on when it fires. Dies on quit; nothing survives a reboot.
// 2. Launch at login (SMAppService.mainApp) — OFF by default. The app always
// launches reading the TRUE system state, so a login launch can never
// re-enable disablesleep on its own.
// 3. Low-Power-Mode auto-off — on battery, if Low Power Mode is on, Sleepless
// turns itself off. Same shape as the battery floor, evaluated on the same tick.
//
// Build (mirrors Nexus.app): Command Line Tools `swiftc`, NO Xcode project.
// swiftc -O -parse-as-library -target arm64-apple-macos26.0 -framework AppKit \
// -framework ServiceManagement
// File MUST be named App.swift and compiled -parse-as-library so the
// @main enum + @MainActor static main() entry is Swift-6 isolation-safe.
import AppKit
import ServiceManagement
// MARK: - Tunables
private let pollInterval: TimeInterval = 60
// Battery-floor config (user-adjustable via the popover slider; persisted in UserDefaults).
private let floorKey = "batteryFloorPercent"
private let floorDefault = 15
private let floorMin = 5
private let floorMax = 50
// MARK: - Menu-bar coffee glyph (native SF Symbols, MONOCHROME template — state by SHAPE)
// macOS convention: a menu-bar extra is a template image (no colour) so it adapts to light/dark
// bars and inverts on highlight. State is read from the SILHOUETTE, not colour. The old
// empty-vs-filled cups looked near-identical at 16 px, so we switch the silhouette dramatically
// with steam (a hot cup = awake):
// OFF (sleeps normally) = cup.and.saucer cup resting on its saucer, NO steam (cold/asleep)
// ON (kept awake, on power) = cup.and.heat.waves.fill hot cup with rising steam (awake)
// ARMED (kept awake, on battery) = cup.and.heat.waves.fill + a small dot (awake, safety net live)
// The no-steam → steam change reads instantly even at 16 px; the armed dot is the only extra
// mark. All template (monochrome) — SF Symbols only, no hand-drawn paths.
enum SleepGlyph {
case off
case on
case armed
}
private func makeCupGlyph(_ glyph: SleepGlyph) -> NSImage {
let cfg = NSImage.SymbolConfiguration(pointSize: 15, weight: .regular).applying(.init(scale: .medium))
let name = (glyph == .off) ? "cup.and.saucer" : "cup.and.heat.waves.fill"
let base = NSImage(systemSymbolName: name, accessibilityDescription: "Sleepless")?
.withSymbolConfiguration(cfg)
?? NSImage(systemSymbolName: "cup.and.saucer.fill", accessibilityDescription: "Sleepless")
?? NSImage()
guard glyph == .armed else {
base.isTemplate = true
return base
}
// ARMED: full steaming cup + a small filled dot top-right (the "auto-off safety net is live"
// mark). Drawn in template black so it tints + inverts with the menu bar exactly like the cup.
let size = base.size
guard size.width > 0, size.height > 0 else { base.isTemplate = true; return base }
let composed = NSImage(size: size)
composed.lockFocus()
base.draw(in: NSRect(origin: .zero, size: size))
let d = max(size.height * 0.26, 4)
let dot = NSBezierPath(ovalIn: NSRect(x: size.width - d, y: size.height - d, width: d, height: d))
NSColor.black.setFill()
dot.fill()
composed.unlockFocus()
composed.isTemplate = true
return composed
}
// Flipped container so popover content lays out top-down with simple frames.
private final class FlippedView: NSView { override var isFlipped: Bool { true } }
// Brand accent (2026 "Liquid Glass" redesign): indigo -> violet -> fuchsia. The
// violet mid-tone is the single accent the popover uses to communicate the
// privileged "awake" state, matching the app icon's gradient mid-stop. These are
// the only hard-coded colours; everything else stays on system semantic colours so
// the panel still reads as a first-party control.
private let brandAccent = NSColor(srgbRed: 139/255.0, green: 92/255.0, blue: 246/255.0, alpha: 1) // #8B5CF6 violet
private let brandAccentSoft = NSColor(srgbRed: 167/255.0, green: 139/255.0, blue: 250/255.0, alpha: 1) // #A78BFA
// Frosted-glass popover backing: a flipped NSVisualEffectView so content still
// lays out top-down while the panel gets a translucent, blurred material that
// samples the desktop/windows behind it (system light/dark aware). On macOS 26 the
// .popover material renders as the system Liquid Glass automatically; we deliberately
// keep this native (no hand-rolled tint on the surface) so a sudo-touching panel
// stays visually first-party. Colour lives on the controls, never the surface.
private final class GlassView: NSVisualEffectView { override var isFlipped: Bool { true } }
// Inset grouping "card" (System Settings rhythm): a flipped, layer-backed container
// with a subtle, appearance-adaptive fill, a hairline border, and continuous-corner
// rounding. When `active`, the card carries a faint brand-violet wash + a violet
// hairline so the privileged "kept awake" state is unmistakable at a glance in the
// accent colour (Apple's "tint elements, not surfaces" model). Re-resolved on
// light/dark changes and on state changes via updateLayer.
private final class CardView: NSView {
var active = false { didSet { if active != oldValue { needsDisplay = true } } }
override var isFlipped: Bool { true }
override var wantsUpdateLayer: Bool { true }
override func updateLayer() {
let dark = effectiveAppearance.bestMatch(from: [.darkAqua, .aqua]) == .darkAqua
if active {
layer?.backgroundColor = brandAccent.withAlphaComponent(dark ? 0.18 : 0.10).cgColor
layer?.borderColor = brandAccent.withAlphaComponent(dark ? 0.60 : 0.45).cgColor
layer?.borderWidth = 1
} else {
layer?.backgroundColor = (dark ? NSColor.white.withAlphaComponent(0.06)
: NSColor.black.withAlphaComponent(0.045)).cgColor
layer?.borderColor = (dark ? NSColor.white.withAlphaComponent(0.08)
: NSColor.black.withAlphaComponent(0.06)).cgColor
layer?.borderWidth = 1
}
layer?.cornerRadius = 11
layer?.cornerCurve = .continuous
}
}
@MainActor
final class AppDelegate: NSObject, NSApplicationDelegate {
private var statusItem: NSStatusItem!
private var timer: Timer?
private let onGlyph = makeCupGlyph(.on)
private let offGlyph = makeCupGlyph(.off)
private let armedGlyph = makeCupGlyph(.armed)
// Popover UI
private let popover = NSPopover()
private var toggleSwitch: NSSwitch!
private var mainCard: CardView! // group-1 card; gets the brand-violet wash when awake
private var headerMark: NSImageView! // header coffee mark; tints violet when awake
private var captionLabel: NSTextField!
private var floorValueLabel: NSTextField!
private var floorSlider: NSSlider!
private var autoOffControl: NSSegmentedControl!
private var countdownLabel: NSTextField!
private var loginSwitch: NSSwitch!
private var clickMonitor: Any?
private var batteryFloorPercent = floorDefault
private var isOn = false
private var userForcedOn = false // user deliberately turned it on; honor over the Low Power Mode auto-off (the hard battery floor still wins)
// Auto-off timer (in-memory; dies on quit, never survives a reboot)
private var autoOffMinutes = 0 // 0 = none (stay on until off), 60, or 120
private var keepAwakeTimer: Timer? // one-shot: flips sleep back on when it fires
private var countdownTicker: Timer? // 1 Hz label refresh, only while the popover is open
private var timerEndDate: Date?
private let popoverWidth: CGFloat = 320
private let popoverHeight: CGFloat = 432
func applicationDidFinishLaunching(_ notification: Notification) {
NSApp.setActivationPolicy(.accessory)
batteryFloorPercent = min(max((UserDefaults.standard.object(forKey: floorKey) as? Int) ?? floorDefault, floorMin), floorMax)
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
if let button = statusItem.button {
button.image = offGlyph
button.action = #selector(statusClicked)
button.target = self
}
popover.behavior = .applicationDefined // app-managed dismissal (no transient close/reopen flicker)
popover.animates = true
popover.contentSize = NSSize(width: popoverWidth, height: popoverHeight)
popover.contentViewController = makeContentController()
refresh() // reflect TRUE system state on launch (never a stale assumption)
timer = Timer.scheduledTimer(timeInterval: pollInterval, target: self,
selector: #selector(poll), userInfo: nil, repeats: true)
}
// MARK: - Popover content (native NSSwitch toggle, macOS-aligned)
private func makeContentController() -> NSViewController {
let W = popoverWidth, pad: CGFloat = 16
let contentW = W - pad * 2
let ci: CGFloat = 12 // card inner padding
let cw = contentW - ci * 2 // card inner content width
// Standard system popover material: untinted, no forced emphasis, so it reads
// as a first-party control (like the Wi-Fi / Sound / Battery popovers), not a
// themed panel. NSPopover supplies its own corner, shadow, and arrow.
let root = GlassView(frame: NSRect(x: 0, y: 0, width: W, height: popoverHeight))
root.material = .popover
root.blendingMode = .behindWindow
root.state = .followsWindowActiveState
// Header: small coffee mark + "Sleepless" (quiet system glyph, not a branded logo).
// The mark tints to the brand violet while the Mac is kept awake.
let mark = NSImageView(frame: NSRect(x: pad, y: 14, width: 18, height: 18))
let headerCup = makeCupGlyph(.on); headerCup.isTemplate = true
mark.image = headerCup
mark.contentTintColor = .labelColor
root.addSubview(mark)
headerMark = mark
let title = makeLabel("Sleepless", font: .systemFont(ofSize: 14, weight: .semibold), color: .labelColor)
title.frame = NSRect(x: pad + 24, y: 14, width: contentW - 24, height: 20)
root.addSubview(title)
// Grouped inset cards (System Settings rhythm) replace per-row hairline separators.
func makeCard(_ rect: NSRect) -> CardView {
let c = CardView(frame: rect)
c.wantsLayer = true
root.addSubview(c)
return c
}
let swProto = NSSwitch().intrinsicContentSize
let swW = swProto.width > 0 ? swProto.width : 38
let swH = swProto.height > 0 ? swProto.height : 21
// GROUP 1 — main switch + state caption
let g1y: CGFloat = 46, g1h: CGFloat = 84
let g1 = makeCard(NSRect(x: pad, y: g1y, width: contentW, height: g1h))
mainCard = g1
let rowLabel = makeLabel("Keep awake with lid closed", font: .systemFont(ofSize: 13), color: .labelColor)
rowLabel.frame = NSRect(x: ci, y: ci, width: cw - swW - 8, height: 22)
g1.addSubview(rowLabel)
toggleSwitch = NSSwitch()
toggleSwitch.target = self
toggleSwitch.action = #selector(switchToggled(_:))
toggleSwitch.frame = NSRect(x: contentW - ci - swW, y: ci + (22 - swH) / 2, width: swW, height: swH)
g1.addSubview(toggleSwitch)
captionLabel = makeLabel("", font: .systemFont(ofSize: 12), color: .secondaryLabelColor)
captionLabel.frame = NSRect(x: ci, y: ci + 30, width: cw, height: 32)
captionLabel.usesSingleLineMode = false
captionLabel.lineBreakMode = .byWordWrapping
captionLabel.maximumNumberOfLines = 2
captionLabel.cell?.wraps = true
g1.addSubview(captionLabel)
// GROUP 2 — auto-off timer (label + segmented [Off | 1h | 2h] + countdown)
let g2y = g1y + g1h + 12, g2h: CGFloat = 78
let g2 = makeCard(NSRect(x: pad, y: g2y, width: contentW, height: g2h))
let timerLabel = makeLabel("Auto-off timer", font: .systemFont(ofSize: 13), color: .labelColor)
timerLabel.frame = NSRect(x: ci, y: ci + 3, width: 110, height: 22)
g2.addSubview(timerLabel)
autoOffControl = NSSegmentedControl(labels: ["Off", "1h", "2h"],
trackingMode: .selectOne,
target: self, action: #selector(autoOffChanged(_:)))
autoOffControl.selectedSegment = 0
autoOffControl.controlSize = .regular
autoOffControl.segmentStyle = .automatic
autoOffControl.sizeToFit()
let segSize = autoOffControl.frame.size
let segW = segSize.width > 0 ? segSize.width : 150
autoOffControl.frame = NSRect(x: contentW - ci - segW, y: ci, width: segW, height: max(segSize.height, 24))
g2.addSubview(autoOffControl)
countdownLabel = makeLabel("", font: .systemFont(ofSize: 12), color: .secondaryLabelColor)
countdownLabel.frame = NSRect(x: ci, y: ci + 36, width: cw, height: 16)
g2.addSubview(countdownLabel)
// GROUP 3 — battery-floor (label + value + slider + min/max hints)
let g3y = g2y + g2h + 12, g3h: CGFloat = 92
let g3 = makeCard(NSRect(x: pad, y: g3y, width: contentW, height: g3h))
let floorLabel = makeLabel("Auto-off at low battery", font: .systemFont(ofSize: 13), color: .labelColor)
floorLabel.frame = NSRect(x: ci, y: ci, width: cw - 54, height: 18)
g3.addSubview(floorLabel)
floorValueLabel = makeLabel("\(batteryFloorPercent)%", font: .systemFont(ofSize: 13, weight: .semibold), color: .secondaryLabelColor)
floorValueLabel.alignment = .right
floorValueLabel.frame = NSRect(x: contentW - ci - 54, y: ci, width: 54, height: 18)
g3.addSubview(floorValueLabel)
floorSlider = NSSlider(value: Double(batteryFloorPercent), minValue: Double(floorMin), maxValue: Double(floorMax),
target: self, action: #selector(floorSliderChanged(_:)))
floorSlider.isContinuous = true // live update while dragging
floorSlider.controlSize = .regular
floorSlider.frame = NSRect(x: ci, y: ci + 26, width: cw, height: 20)
g3.addSubview(floorSlider)
let minHint = makeLabel("\(floorMin)%", font: .systemFont(ofSize: 10), color: .tertiaryLabelColor)
minHint.frame = NSRect(x: ci, y: ci + 50, width: 34, height: 13)
g3.addSubview(minHint)
let maxHint = makeLabel("\(floorMax)%", font: .systemFont(ofSize: 10), color: .tertiaryLabelColor)
maxHint.alignment = .right
maxHint.frame = NSRect(x: contentW - ci - 34, y: ci + 50, width: 34, height: 13)
g3.addSubview(maxHint)
// GROUP 4 — launch at login (off by default; never auto-enables sleep prevention)
let g4y = g3y + g3h + 12, g4h: CGFloat = 46
let g4 = makeCard(NSRect(x: pad, y: g4y, width: contentW, height: g4h))
let loginLabel = makeLabel("Launch at login", font: .systemFont(ofSize: 13), color: .labelColor)
loginLabel.frame = NSRect(x: ci, y: ci, width: cw - swW - 8, height: 22)
g4.addSubview(loginLabel)
loginSwitch = NSSwitch()
loginSwitch.target = self
loginSwitch.action = #selector(loginToggled(_:))
loginSwitch.state = loginItemEnabled() ? .on : .off
loginSwitch.frame = NSRect(x: contentW - ci - swW, y: ci + (22 - swH) / 2, width: swW, height: swH)
g4.addSubview(loginSwitch)
// Footer — Quit (separated by space, not a hairline)
let quit = NSButton(title: "Quit Sleepless", target: self, action: #selector(quit))
quit.controlSize = .regular
quit.bezelStyle = .rounded
quit.sizeToFit()
let qs = quit.frame.size
quit.frame = NSRect(x: W - pad - qs.width, y: g4y + g4h + 12, width: qs.width, height: qs.height)
root.addSubview(quit)
let vc = NSViewController()
vc.view = root
return vc
}
private func makeLabel(_ s: String, font: NSFont, color: NSColor) -> NSTextField {
let t = NSTextField(labelWithString: s)
t.font = font
t.textColor = color
t.isEditable = false
t.isBordered = false
t.drawsBackground = false
return t
}
// MARK: - Click the menu-bar cup to open/close the popover
@objc private func statusClicked() {
if popover.isShown { closePopover() } else { openPopover() }
}
private func openPopover() {
refresh() // sync switch/caption to TRUE state before showing
loginSwitch?.state = loginItemEnabled() ? .on : .off
guard let button = statusItem.button else { return }
NSApp.activate(ignoringOtherApps: true)
popover.show(relativeTo: button.bounds, of: button, preferredEdge: .minY)
popover.contentViewController?.view.window?.makeKey()
if keepAwakeTimer != nil { startCountdownTicker() }
updateCountdownLabel()
// Close when the user clicks anywhere outside the app (status bar, another app, desktop).
clickMonitor = NSEvent.addGlobalMonitorForEvents(matching: [.leftMouseDown, .rightMouseDown]) { [weak self] _ in
self?.closePopover()
}
}
private func closePopover() {
popover.performClose(nil)
countdownTicker?.invalidate(); countdownTicker = nil // stop the 1 Hz label refresh (keep-awake timer keeps running)
if let monitor = clickMonitor { NSEvent.removeMonitor(monitor); clickMonitor = nil }
}
@objc private func switchToggled(_ sender: NSSwitch) {
if performToggle(wantOn: sender.state == .on) {
sender.state = .off // setup needed / failed: reflect reality (performToggle notified)
}
}
// Core keep-awake toggle, decoupled from the UI sender. Returns true ONLY when the user
// must act (the passwordless grant is missing and setup did not complete) so the caller can
// reflect OFF. The decision to prompt is made on the REAL sudo result (see setDisableSleep),
// never by re-reading SleepDisabled: a successful sudo means the command ran, even if a
// safety net (Low Power Mode / battery floor) legitimately turns sleep back on afterwards —
// which must NOT be mistaken for "permission missing" and trigger a password prompt. This
// unobservable, state-proxy decision is what made earlier releases re-prompt spuriously.
@discardableResult
private func performToggle(wantOn: Bool) -> Bool {
var result = setDisableSleep(wantOn)
// Only a genuinely MISSING grant warrants the one-time native-auth setup. A successful
// sudo (.ok) — or any other failure — never re-prompts here.
if wantOn, result == .grantMissing {
if installGrantViaAuth() { result = setDisableSleep(true) }
if result != .ok {
notify("Couldn't keep awake. The permission isn't set up yet.")
return true
}
}
// A deliberate, successful turn-on wins over the Low Power Mode auto-off (hard floor still wins).
userForcedOn = wantOn && result == .ok
refresh() // applies UI + safety nets; switch reflects reality
if isOn, autoOffMinutes > 0 { startKeepAwakeTimer(minutes: autoOffMinutes) }
return false
}
// Install the one-time scoped grant via a SINGLE native macOS authorization (the
// standard Touch ID / password sheet) — no Terminal. Runs the bundled, audited
// grant.sh as root through osascript's "with administrator privileges"; grant.sh is
// root-aware so it writes the sudoers drop-in directly with no inner sudo prompt.
// Returns true once the passwordless grant is in place; after that the app never asks again.
@discardableResult
private func installGrantViaAuth() -> Bool {
let intro = NSAlert()
intro.alertStyle = .informational
intro.messageText = "Enable keeping your Mac awake"
intro.informativeText = "Sleepless flips a protected macOS setting (pmset disablesleep), so it needs your permission once. macOS will ask you to authenticate (Touch ID or your password). After that the switch works instantly, with no more prompts."
intro.addButton(withTitle: "Enable")
intro.addButton(withTitle: "Not now")
NSApp.activate(ignoringOtherApps: true)
guard intro.runModal() == .alertFirstButtonReturn else { return false }
guard let res = Bundle.main.resourcePath else { return false }
let grant = res + "/grant.sh"
// Pass the REAL user: under the native auth sheet grant.sh runs as root with
// SUDO_USER unset, so without this the grant would be written for "root" (useless).
let shellCmd = "SLEEPLESS_USER='\(NSUserName())' /bin/bash '\(grant)' --yes"
// escape for an AppleScript string literal, then run with one native auth sheet
let escaped = shellCmd.replacingOccurrences(of: "\\", with: "\\\\").replacingOccurrences(of: "\"", with: "\\\"")
let osa = "do shell script \"\(escaped)\" with administrator privileges"
let proc = Process()
proc.executableURL = URL(fileURLWithPath: "/usr/bin/osascript")
proc.arguments = ["-e", osa]
proc.standardOutput = Pipe(); proc.standardError = Pipe()
do { try proc.run(); proc.waitUntilExit() }
catch { notify("Couldn't start the one-time setup."); return false }
if proc.terminationStatus == 0 { return true } // grant.sh installed the rule successfully
if proc.terminationStatus != 128 { // 128 = user cancelled the auth sheet
notify("Setup didn't complete. Try again, or run grant.sh from the app bundle.")
}
return false
}
// A brief, subtle pulse on the menu-bar glyph whenever the state (and thus the cup
// shape) changes, so the change is noticeable. Opacity-only: no layer geometry is
// mutated, so it can't shift the status item on any macOS version.
private func pulseStatusItem() {
guard let b = statusItem.button else { return }
b.wantsLayer = true
let pulse = CABasicAnimation(keyPath: "opacity")
pulse.fromValue = 0.3
pulse.toValue = 1.0
pulse.duration = 0.34
pulse.timingFunction = CAMediaTimingFunction(name: .easeOut)
b.layer?.add(pulse, forKey: "statePulse")
}
@objc private func poll() { refresh() }
// MARK: - Auto-off timer (Feature 1)
@objc private func autoOffChanged(_ sender: NSSegmentedControl) {
switch sender.selectedSegment {
case 1: autoOffMinutes = 60
case 2: autoOffMinutes = 120
default: autoOffMinutes = 0
}
if isOn, autoOffMinutes > 0 {
startKeepAwakeTimer(minutes: autoOffMinutes)
} else {
cancelKeepAwakeTimer()
updateCountdownLabel()
}
}
private func startKeepAwakeTimer(minutes: Int) {
cancelKeepAwakeTimer()
guard minutes > 0, isOn else { updateCountdownLabel(); return }
let seconds = TimeInterval(minutes * 60)
timerEndDate = Date().addingTimeInterval(seconds)
keepAwakeTimer = Timer.scheduledTimer(timeInterval: seconds, target: self,
selector: #selector(keepAwakeTimerFired), userInfo: nil, repeats: false)
if popover.isShown { startCountdownTicker() }
updateCountdownLabel()
}
private func cancelKeepAwakeTimer() {
keepAwakeTimer?.invalidate(); keepAwakeTimer = nil
countdownTicker?.invalidate(); countdownTicker = nil
timerEndDate = nil
}
@objc private func keepAwakeTimerFired() {
setDisableSleep(false)
cancelKeepAwakeTimer()
autoOffMinutes = 0
autoOffControl?.selectedSegment = 0
applyUI(on: readSleepDisabled())
notify("Auto-off timer ended. Sleepless turned off.")
}
private func startCountdownTicker() {
countdownTicker?.invalidate()
countdownTicker = Timer.scheduledTimer(timeInterval: 1, target: self,
selector: #selector(countdownTick), userInfo: nil, repeats: true)
}
@objc private func countdownTick() { updateCountdownLabel() }
private func updateCountdownLabel() {
guard let end = timerEndDate, isOn else { countdownLabel?.stringValue = ""; return }
let remaining = Int(end.timeIntervalSinceNow.rounded())
guard remaining > 0 else { countdownLabel?.stringValue = ""; return }
let h = remaining / 3600, m = (remaining % 3600) / 60, s = remaining % 60
let t = h > 0 ? String(format: "%d:%02d:%02d", h, m, s) : String(format: "%d:%02d", m, s)
countdownLabel?.stringValue = "Auto-off in \(t)"
}
// MARK: - Launch at login (Feature 2) — OFF by default; never re-enables sleep prevention
@objc private func loginToggled(_ sender: NSSwitch) {
do {
if sender.state == .on { try SMAppService.mainApp.register() }
else { try SMAppService.mainApp.unregister() }
} catch {
NSLog("Sleepless: login item update failed: %@", error.localizedDescription)
notify("Couldn't update Launch at login.")
}
sender.state = loginItemEnabled() ? .on : .off
}
private func loginItemEnabled() -> Bool { SMAppService.mainApp.status == .enabled }
// MARK: - Core state sync
@objc private func refresh() {
let on = readSleepDisabled()
applyUI(on: on)
if on { enforceSafetyNets() }
}
private func applyUI(on: Bool) {
isOn = on
if !on { cancelKeepAwakeTimer() } // going OFF clears any countdown/timer
// ARMED = kept awake while actively discharging on battery, so the
// auto-off safety net is live. Distinct menu-bar glyph (cup + dot).
var armed = false
if on {
let (onBattery, discharging, _) = batteryStatus()
armed = onBattery && discharging
}
if let button = statusItem.button {
let newImage = on ? (armed ? armedGlyph : onGlyph) : offGlyph
if button.image !== newImage { // state (cup shape) changed -> swap + pulse
button.image = newImage
pulseStatusItem()
}
button.toolTip = on
? (armed
? "Sleepless: on (battery). Auto-off at \(batteryFloorPercent)% or in Low Power Mode."
: "Sleepless: on. Stays awake with the lid closed.")
: "Sleepless: off. Sleeps normally."
}
toggleSwitch?.state = on ? .on : .off
// Brand-violet accent communicates the privileged "awake" state at a glance.
mainCard?.active = on
headerMark?.contentTintColor = on ? brandAccentSoft : .labelColor
renderText()
updateCountdownLabel()
}
// Update text labels only (no pmset subprocess; safe to call on every slider tick).
private func renderText() {
floorValueLabel?.stringValue = "\(batteryFloorPercent)%"
captionLabel?.stringValue = isOn
? "Stays awake when the lid is closed. Turns off at \(batteryFloorPercent)% battery or in Low Power Mode."
: "Sleeps normally when you close the lid."
}
@objc private func floorSliderChanged(_ sender: NSSlider) {
let v = min(max(Int(sender.doubleValue.rounded()), floorMin), floorMax)
if v != batteryFloorPercent {
batteryFloorPercent = v
UserDefaults.standard.set(v, forKey: floorKey)
}
renderText()
}
// Result of the privileged keep-awake toggle, based on sudo's REAL exit status — not on a
// second, independent state read. `.ok` = the command ran; `.grantMissing` = the passwordless
// sudoers grant isn't installed (sudo -n refused), the one case that warrants setup; `.failed`
// = any other error. Using sudo's own result (instead of re-reading SleepDisabled) is the fix:
// a safety net flipping sleep back on must never look like "permission missing" and re-prompt.
private enum ToggleResult: Equatable { case ok, grantMissing, failed(String) }
@discardableResult
private func setDisableSleep(_ on: Bool) -> ToggleResult {
// sudo -n: never prompt (GUI app has no TTY). The exact argument vector matches the
// NOPASSWD sudoers grant, so this runs without a password.
let (exit, _, err) = runPrivileged(["-n", "/usr/bin/pmset", "-a", "disablesleep", on ? "1" : "0"])
let result: ToggleResult
if exit == 0 {
result = .ok
} else if err.range(of: "a password is required", options: .caseInsensitive) != nil
|| err.range(of: "not allowed", options: .caseInsensitive) != nil
|| err.range(of: "may not run", options: .caseInsensitive) != nil {
result = .grantMissing // grant absent/removed -> sudo -n refused to run passwordless
} else {
result = .failed(err.isEmpty ? "exit \(exit)" : err.trimmingCharacters(in: .whitespacesAndNewlines))
}
return result
}
// Run a privileged command via sudo, capturing exit status + stderr (which the generic
// runCapture discards). stdin is /dev/null so a GUI process with no controlling TTY can
// never block on a prompt. This is what lets the app KNOW whether its own toggle worked.
private func runPrivileged(_ args: [String]) -> (exit: Int32, out: String, err: String) {
let process = Process()
process.executableURL = URL(fileURLWithPath: "/usr/bin/sudo")
process.arguments = args
var env = ProcessInfo.processInfo.environment
env["PATH"] = "/usr/bin:/bin:/usr/sbin:/sbin:/opt/homebrew/bin"
env["HOME"] = FileManager.default.homeDirectoryForCurrentUser.path
process.environment = env
let outPipe = Pipe(), errPipe = Pipe()
process.standardOutput = outPipe
process.standardError = errPipe
process.standardInput = FileHandle.nullDevice
do { try process.run() }
catch {
NSLog("Sleepless: failed to launch sudo: %@", error.localizedDescription)
return (-1, "", "launch failed: \(error.localizedDescription)")
}
let outData = outPipe.fileHandleForReading.readDataToEndOfFile()
let errData = errPipe.fileHandleForReading.readDataToEndOfFile()
process.waitUntilExit()
return (process.terminationStatus,
String(data: outData, encoding: .utf8) ?? "",
String(data: errData, encoding: .utf8) ?? "")
}
// MARK: - Battery + Low-Power-Mode safety nets (silent; no extra UI) — Feature 3
private func enforceSafetyNets() {
let (onBattery, discharging, percent) = batteryStatus()
guard onBattery, discharging else { return }
// Hard battery floor ALWAYS wins, even over a deliberate turn-on: never drain to empty.
if percent <= batteryFloorPercent {
setDisableSleep(false); userForcedOn = false
applyUI(on: readSleepDisabled())
notify("Battery low (\(percent)%). Sleepless turned off.")
return
}
// Low Power Mode auto-off, UNLESS the user deliberately chose to keep awake this session.
if ProcessInfo.processInfo.isLowPowerModeEnabled && !userForcedOn {
setDisableSleep(false)
applyUI(on: readSleepDisabled())
notify("Low Power Mode on. Sleepless turned off.")
}
}
// MARK: - Readers (no root needed)
private func readSleepDisabled() -> Bool {
let out = runCapture("/usr/bin/pmset", ["-g"])
for line in out.split(whereSeparator: { $0 == "\n" }) {
if line.range(of: "SleepDisabled", options: .caseInsensitive) != nil {
let toks = line.split(whereSeparator: { $0 == " " || $0 == "\t" })
if let last = toks.last { return last == "1" }
}
}
return false // line absent -> OFF
}
private func batteryStatus() -> (onBattery: Bool, discharging: Bool, percent: Int) {
let out = runCapture("/usr/bin/pmset", ["-g", "batt"])
let onBattery = out.contains("Battery Power")
let discharging = out.range(of: "discharging", options: .caseInsensitive) != nil
var percent = 100
for tok in out.split(whereSeparator: { " \t\n;".contains($0) }) {
if tok.hasSuffix("%"), let v = Int(tok.dropLast()) { percent = v; break }
}
return (onBattery, discharging, percent)
}
// MARK: - Notification (mirrors Nexus' osascript approach)
private func notify(_ message: String) {
let script = "display notification \"\(message)\" with title \"Sleepless\" sound name \"Tink\""
_ = runCapture("/usr/bin/osascript", ["-e", script])
}
// MARK: - Process runner (explicit PATH/HOME; captures stdout)
@discardableResult
private func runCapture(_ launchPath: String, _ args: [String]) -> String {
let process = Process()
process.executableURL = URL(fileURLWithPath: launchPath)
process.arguments = args
var env = ProcessInfo.processInfo.environment
env["PATH"] = "/usr/bin:/bin:/usr/sbin:/sbin:/opt/homebrew/bin"
env["HOME"] = FileManager.default.homeDirectoryForCurrentUser.path
process.environment = env
let pipe = Pipe()
process.standardOutput = pipe
process.standardError = Pipe()
do { try process.run() }
catch { NSLog("Sleepless: failed to launch %@: %@", launchPath, error.localizedDescription); return "" }
let data = pipe.fileHandleForReading.readDataToEndOfFile()
process.waitUntilExit()
return String(data: data, encoding: .utf8) ?? ""
}
@objc private func quit() { NSApp.terminate(nil) }
}
@main
enum SleeplessApp {
@MainActor
static func main() {
let app = NSApplication.shared
let delegate = AppDelegate()
app.delegate = delegate
objc_setAssociatedObject(app, &delegateKey, delegate, .OBJC_ASSOCIATION_RETAIN)
app.run()
}
}
nonisolated(unsafe) private var delegateKey = 0