Compare commits

...

5 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
27e98a527f feat(assets): 添加应用图标资源文件
添加不同尺寸的Mac应用图标文件并更新Contents.json配置
2025-12-27 21:32:33 +08:00
230a2836a6 chore: 删除临时目录中的Applications符号链接 2025-12-27 20:21:17 +08:00
3514f450c9 refactor: 移除菜单内容中多余的过渡动画效果 2025-12-27 20:17:03 +08:00
13 changed files with 18 additions and 76 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()
startMonitoringTimer()
} else {
//
stopMonitoringTimer()
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() startCaffeinate()
} } else {
// caffeinate
stopCaffeinate()
} }
} }
@@ -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

@@ -1,51 +1,61 @@
{ {
"images" : [ "images" : [
{ {
"filename" : "icon_16x16.png",
"idiom" : "mac", "idiom" : "mac",
"scale" : "1x", "scale" : "1x",
"size" : "16x16" "size" : "16x16"
}, },
{ {
"filename" : "icon_16x16@2x.png",
"idiom" : "mac", "idiom" : "mac",
"scale" : "2x", "scale" : "2x",
"size" : "16x16" "size" : "16x16"
}, },
{ {
"filename" : "icon_32x32.png",
"idiom" : "mac", "idiom" : "mac",
"scale" : "1x", "scale" : "1x",
"size" : "32x32" "size" : "32x32"
}, },
{ {
"filename" : "icon_32x32@2x.png",
"idiom" : "mac", "idiom" : "mac",
"scale" : "2x", "scale" : "2x",
"size" : "32x32" "size" : "32x32"
}, },
{ {
"filename" : "icon_128x128.png",
"idiom" : "mac", "idiom" : "mac",
"scale" : "1x", "scale" : "1x",
"size" : "128x128" "size" : "128x128"
}, },
{ {
"filename" : "icon_128x128@2x.png",
"idiom" : "mac", "idiom" : "mac",
"scale" : "2x", "scale" : "2x",
"size" : "128x128" "size" : "128x128"
}, },
{ {
"filename" : "icon_256x256.png",
"idiom" : "mac", "idiom" : "mac",
"scale" : "1x", "scale" : "1x",
"size" : "256x256" "size" : "256x256"
}, },
{ {
"filename" : "icon_256x256@2x.png",
"idiom" : "mac", "idiom" : "mac",
"scale" : "2x", "scale" : "2x",
"size" : "256x256" "size" : "256x256"
}, },
{ {
"filename" : "icon_512x512.png",
"idiom" : "mac", "idiom" : "mac",
"scale" : "1x", "scale" : "1x",
"size" : "512x512" "size" : "512x512"
}, },
{ {
"filename" : "icon_512x512@2x.png",
"idiom" : "mac", "idiom" : "mac",
"scale" : "2x", "scale" : "2x",
"size" : "512x512" "size" : "512x512"

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 465 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 159 KiB

View File

@@ -83,8 +83,6 @@ struct MenuContent: View {
.buttonStyle(.plain) .buttonStyle(.plain)
Divider() Divider()
.transition(.opacity)
.animation(.easeInOut(duration: 0.3), value: appState.isFunctionEnabled)
// //
VStack(alignment: .leading, spacing: 4) { VStack(alignment: .leading, spacing: 4) {
@@ -95,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) {
@@ -119,8 +109,7 @@ struct MenuContent: View {
} }
Spacer() Spacer()
} }
.transition(.opacity)
.animation(.easeInOut(duration: 0.3), value: appState.isFunctionEnabled || appState.isUserIdle)
} }
.padding(8) .padding(8)
.background( .background(