From aef0f746163513b6b220431ecf31798f2db054e6 Mon Sep 17 00:00:00 2001 From: leo12025 Date: Sat, 27 Dec 2025 22:01:35 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=AE=80=E5=8C=96=E7=A9=BA?= =?UTF-8?q?=E9=97=B2=E7=8A=B6=E6=80=81=E6=A3=80=E6=B5=8B=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E7=9B=B4=E6=8E=A5=E6=98=BE=E7=A4=BA=E4=BF=9D=E6=8A=A4?= =?UTF-8?q?=E7=8A=B6=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 移除空闲状态检测及相关UI显示,直接显示"保护中"状态 --- PowerBar/AppState.swift | 65 +++----------------------------------- PowerBar/PowerBarApp.swift | 12 ++----- 2 files changed, 6 insertions(+), 71 deletions(-) diff --git a/PowerBar/AppState.swift b/PowerBar/AppState.swift index 8b5f787..4902c27 100644 --- a/PowerBar/AppState.swift +++ b/PowerBar/AppState.swift @@ -11,20 +11,12 @@ class AppState: ObservableObject { // 功能的总开关 @Published var isFunctionEnabled: Bool = false - // 用户当前是否空闲 - @Published var isUserIdle: Bool = false - // 自动启动设置 @Published var isLaunchAtLogin: Bool = false // 内部属性 - private var monitorTimer: Timer? private var caffeinateProcess: Process? // 直接使用Process对象,但简化实现 - // 配置参数 - private let idleThreshold: TimeInterval = 300 // 5分钟无操作视为空闲 - private let checkInterval: TimeInterval = 10 // 每10秒检查一次 - // 初始化方法 init() { // 检查并设置初始的自动启动状态 @@ -37,52 +29,11 @@ class AppState: ObservableObject { func toggleFunction() { isFunctionEnabled.toggle() if isFunctionEnabled { - // 开启功能,立即开始一轮检查,并设置定时器 - performCheck() - startMonitoringTimer() + // 开启功能,直接启动caffeinate,不检查用户活动 + startCaffeinate() } else { - // 关闭功能,停止一切 - stopMonitoringTimer() + // 关闭功能,停止caffeinate 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() - } } } @@ -155,14 +106,7 @@ class AppState: ObservableObject { print("已重置 caffeinate 进程引用") } - // MARK: - UI State Update - - /// 在主线程更新用户空闲状态 - private func updateUserIdleStatus(_ isIdle: Bool) { - DispatchQueue.main.async { - self.isUserIdle = isIdle - } - } + // MARK: - Launch at Login Management @@ -252,7 +196,6 @@ class AppState: ObservableObject { deinit { // 确保 App 退出时清理资源 - stopMonitoringTimer() stopCaffeinate() } } diff --git a/PowerBar/PowerBarApp.swift b/PowerBar/PowerBarApp.swift index 7e319e1..e2c6b26 100644 --- a/PowerBar/PowerBarApp.swift +++ b/PowerBar/PowerBarApp.swift @@ -93,18 +93,10 @@ struct MenuContent: View { if appState.isFunctionEnabled { HStack(spacing: 6) { Circle() - .fill(appState.isUserIdle ? Color.orange : Color.green) + .fill(Color.green) .frame(width: 8, height: 8) - .transition(.scale(scale: 0.5, anchor: .center)) - Text(appState.isUserIdle ? "空闲" : "活动中") + Text("保护中") .font(.body) - - // 附加显示 caffeinate 状态 - if !appState.isUserIdle { - Text("(保护中)") - .font(.caption2) - .foregroundColor(.secondary) - } } } else { HStack(spacing: 6) {