717ef02155
One IOPM PreventUserIdleDisplaySleep assertion (released automatically if BroV quits); the choice is remembered across BroV restarts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
60 lines
2.2 KiB
Swift
60 lines
2.2 KiB
Swift
import SwiftUI
|
|
import IOKit.pwr_mgt
|
|
|
|
// MARK: - Coffee mode (header ☕)
|
|
//
|
|
// Keeps the Mac and its display awake, like `caffeinate -d`: one IOPM assertion that
|
|
// macOS drops by itself if BroV quits. The choice survives BroV restarts.
|
|
|
|
@MainActor
|
|
final class Coffee: ObservableObject {
|
|
static let shared = Coffee()
|
|
private static let key = "coffeeMode"
|
|
|
|
@Published private(set) var on = false
|
|
private var assertion: IOPMAssertionID = 0
|
|
|
|
private init() {
|
|
if UserDefaults.standard.bool(forKey: Self.key) { set(true) }
|
|
}
|
|
|
|
func toggle() { set(!on) }
|
|
|
|
private func set(_ value: Bool) {
|
|
if value, assertion == 0 {
|
|
var id: IOPMAssertionID = 0
|
|
let ok = IOPMAssertionCreateWithName(kIOPMAssertionTypePreventUserIdleDisplaySleep as CFString,
|
|
IOPMAssertionLevel(kIOPMAssertionLevelOn),
|
|
"BroV: кофе-режим" as CFString, &id)
|
|
guard ok == kIOReturnSuccess else { return }
|
|
assertion = id
|
|
} else if !value, assertion != 0 {
|
|
IOPMAssertionRelease(assertion)
|
|
assertion = 0
|
|
}
|
|
on = value
|
|
UserDefaults.standard.set(value, forKey: Self.key)
|
|
}
|
|
}
|
|
|
|
struct CoffeeButton: View {
|
|
@ObservedObject private var coffee = Coffee.shared
|
|
|
|
var body: some View {
|
|
Button {
|
|
coffee.toggle()
|
|
SoundEngine.shared.play(coffee.on ? "pop" : "close")
|
|
Haptic.tap()
|
|
} label: {
|
|
Image(systemName: coffee.on ? "cup.and.saucer.fill" : "cup.and.saucer")
|
|
.font(.system(size: 13, weight: .semibold))
|
|
.foregroundColor(coffee.on ? Color(hex: "#D97757") : Color(hex: "#8E939C"))
|
|
.frame(width: 22, height: 22)
|
|
.background(Circle().fill(Color(hex: "#D97757").opacity(coffee.on ? 0.16 : 0)))
|
|
}
|
|
.buttonStyle(.plain)
|
|
.help(coffee.on ? "Кофе-режим включён: Мак и экран не засыпают. Нажми, чтобы выключить"
|
|
: "Кофе-режим: не давать Маку и экрану засыпать")
|
|
}
|
|
}
|