Compare commits

...

2 Commits

Author SHA1 Message Date
aef0f74616 refactor: 简化空闲状态检测逻辑,直接显示保护状态
移除空闲状态检测及相关UI显示,直接显示"保护中"状态
2025-12-27 22:01:35 +08:00
886b460d5e fix: 添加更多caffeinate参数以防止系统睡眠
添加-i和-s参数以全面防止系统睡眠,包括用户不活动和系统睡眠的情况
2025-12-27 21:52:39 +08:00
2 changed files with 7 additions and 72 deletions

View File

@@ -11,20 +11,12 @@ class AppState: ObservableObject {
// //
@Published var isFunctionEnabled: Bool = false @Published var isFunctionEnabled: Bool = false
//
@Published var isUserIdle: Bool = false
// //
@Published var isLaunchAtLogin: Bool = false @Published var isLaunchAtLogin: Bool = false
// //
private var monitorTimer: Timer?
private var caffeinateProcess: Process? // 使Process private var caffeinateProcess: Process? // 使Process
//
private let idleThreshold: TimeInterval = 300 // 5
private let checkInterval: TimeInterval = 10 // 10
// //
init() { init() {
// //
@@ -37,52 +29,11 @@ class AppState: ObservableObject {
func toggleFunction() { func toggleFunction() {
isFunctionEnabled.toggle() isFunctionEnabled.toggle()
if isFunctionEnabled { if isFunctionEnabled {
// // caffeinate
performCheck() startCaffeinate()
startMonitoringTimer()
} else { } else {
// // caffeinate
stopMonitoringTimer()
stopCaffeinate() stopCaffeinate()
updateUserIdleStatus(false) //
}
}
// MARK: - Monitoring Logic
private func startMonitoringTimer() {
guard monitorTimer == nil else { return }
print("开始监控...")
// 使 weak self
monitorTimer = Timer.scheduledTimer(withTimeInterval: checkInterval, repeats: true) { [weak self] _ in
self?.performCheck()
}
}
private func stopMonitoringTimer() {
monitorTimer?.invalidate()
monitorTimer = nil
print("监控已停止。")
}
private func performCheck() {
// 1.
let idleTime = getSystemIdleTime()
// 2.
let isIdleNow = (idleTime >= idleThreshold)
// 3.
if isIdleNow != isUserIdle {
updateUserIdleStatus(isIdleNow)
if isIdleNow {
//
stopCaffeinate()
} else {
//
startCaffeinate()
}
} }
} }
@@ -125,7 +76,7 @@ class AppState: ObservableObject {
// caffeinate // caffeinate
caffeinateProcess = Process() caffeinateProcess = Process()
caffeinateProcess?.executableURL = URL(fileURLWithPath: "/usr/bin/caffeinate") caffeinateProcess?.executableURL = URL(fileURLWithPath: "/usr/bin/caffeinate")
caffeinateProcess?.arguments = ["-d"] // -d caffeinateProcess?.arguments = ["-i", "-s", "-d"] // -i -s -d
// //
let outputPipe = Pipe() let outputPipe = Pipe()
@@ -155,14 +106,7 @@ class AppState: ObservableObject {
print("已重置 caffeinate 进程引用") print("已重置 caffeinate 进程引用")
} }
// MARK: - UI State Update
/// 线
private func updateUserIdleStatus(_ isIdle: Bool) {
DispatchQueue.main.async {
self.isUserIdle = isIdle
}
}
// MARK: - Launch at Login Management // MARK: - Launch at Login Management
@@ -252,7 +196,6 @@ class AppState: ObservableObject {
deinit { deinit {
// App 退 // App 退
stopMonitoringTimer()
stopCaffeinate() stopCaffeinate()
} }
} }

View File

@@ -93,18 +93,10 @@ struct MenuContent: View {
if appState.isFunctionEnabled { if appState.isFunctionEnabled {
HStack(spacing: 6) { HStack(spacing: 6) {
Circle() Circle()
.fill(appState.isUserIdle ? Color.orange : Color.green) .fill(Color.green)
.frame(width: 8, height: 8) .frame(width: 8, height: 8)
.transition(.scale(scale: 0.5, anchor: .center)) Text("保护中")
Text(appState.isUserIdle ? "空闲" : "活动中")
.font(.body) .font(.body)
// caffeinate
if !appState.isUserIdle {
Text("(保护中)")
.font(.caption2)
.foregroundColor(.secondary)
}
} }
} else { } else {
HStack(spacing: 6) { HStack(spacing: 6) {