Compare commits

..

11 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
6b645dedf0 refactor: 移除指引窗口的动画效果以简化代码 2025-12-27 20:15:27 +08:00
3cb14be4bc feat(UI): 添加动画效果和优化界面布局
为菜单内容和指引界面添加平滑的动画过渡效果
优化权限管理区域的布局和视觉层次
调整窗口打开/关闭时的渐变动画
2025-12-27 20:09:29 +08:00
be65e275b3 feat(权限): 添加权限检查和管理功能
添加权限检查功能,包括自动启动和系统命令执行权限
在菜单中添加打开系统设置和登录项管理的按钮
2025-12-27 19:57:52 +08:00
21bb3131a0 chore: 清理构建脚本中的用户数据和缓存
删除xcuserdata相关文件并更新构建脚本,增加清理Xcode缓存和用户数据的步骤
2025-12-27 19:52:04 +08:00
693e4966d4 feat: 添加打包脚本并优化指引窗口管理
添加 build_and_package.sh 脚本用于自动化构建和打包
在 MenuContent 中增加对指引窗口的引用管理,避免重复创建
更新 .gitignore 忽略构建产物
添加 ExportOptions.plist 文件用于构建配置
2025-12-27 19:43:00 +08:00
37d73db4e5 feat: 优化应用启动和指引窗口逻辑
添加 LSUIElement 配置使应用不显示在 Dock 中
重构指引窗口显示逻辑,改为直接打开新窗口
移除自动显示的主窗口,改为按需打开
更新菜单图标和背景颜色以匹配系统风格
2025-12-27 19:35:30 +08:00
18 changed files with 296 additions and 112 deletions

3
.gitignore vendored
View File

@@ -94,5 +94,6 @@ Temporary Items
# PowerBar specific
PowerBar.xcarchive/
ExportedApp/
PowerBar.app
PowerBar-*.dmg
PowerBar.dmg
ExportOptions.plist

20
ExportOptions.plist Normal file
View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>method</key>
<string>development</string>
<key>signingStyle</key>
<string>automatic</string>
<key>stripSwiftSymbols</key>
<true/>
<key>teamID</key>
<string>W9MA4VFH3F</string>
<key>destination</key>
<string>export</string>
<key>compileBitcode</key>
<false/>
<key>thinning</key>
<string>&lt;none&gt;</string>
</dict>
</plist>

View File

@@ -250,6 +250,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
@@ -259,6 +260,7 @@
ENABLE_PREVIEWS = YES;
ENABLE_USER_SELECTED_FILES = readonly;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_LSUIElement = YES;
INFOPLIST_KEY_NSHumanReadableCopyright = "";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
@@ -282,6 +284,7 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
"CODE_SIGN_IDENTITY[sdk=macosx*]" = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
@@ -291,6 +294,7 @@
ENABLE_PREVIEWS = YES;
ENABLE_USER_SELECTED_FILES = readonly;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_LSUIElement = YES;
INFOPLIST_KEY_NSHumanReadableCopyright = "";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",

View File

@@ -1,14 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>PowerBar.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
</dict>
</plist>

View File

@@ -2,6 +2,7 @@ import Foundation
import Combine
import IOKit.pwr_mgt
import ServiceManagement
import AppKit
// MARK: - App State Manager
class AppState: ObservableObject {
@@ -10,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() {
//
@@ -36,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()
}
}
}
@@ -124,7 +76,7 @@ class AppState: ObservableObject {
// caffeinate
caffeinateProcess = Process()
caffeinateProcess?.executableURL = URL(fileURLWithPath: "/usr/bin/caffeinate")
caffeinateProcess?.arguments = ["-d"] // -d
caffeinateProcess?.arguments = ["-i", "-s", "-d"] // -i -s -d
//
let outputPipe = Pipe()
@@ -154,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
@@ -191,11 +136,66 @@ class AppState: ObservableObject {
}
}
// MARK: - Permission Management
///
func checkPermissions() {
print("检查应用程序权限...")
//
checkLaunchAtLoginStatus()
print("自动启动权限: \(isLaunchAtLogin ? "已启用" : "已禁用")")
//
let canExecuteSystemCommands = checkSystemCommandPermission()
print("系统命令执行权限: \(canExecuteSystemCommands ? "已允许" : "已拒绝")")
}
///
private func checkSystemCommandPermission() -> Bool {
//
let process = Process()
process.executableURL = URL(fileURLWithPath: "/bin/echo")
process.arguments = ["test"]
do {
try process.run()
process.waitUntilExit()
return process.terminationStatus == 0
} catch {
print("执行系统命令失败: \(error)")
return false
}
}
///
func openSecuritySettings() {
print("打开系统设置的隐私与安全性页面...")
let settingsUrl = URL(string: "x-apple.systempreferences:com.apple.preference.security")!
if NSWorkspace.shared.open(settingsUrl) {
print("已打开系统设置页面")
} else {
print("打开系统设置页面失败")
}
}
///
func openLoginItemsSettings() {
print("打开系统设置的登录项页面...")
let settingsUrl = URL(string: "x-apple.systempreferences:com.apple.preference.usersandgroups?LoginItems")!
if NSWorkspace.shared.open(settingsUrl) {
print("已打开登录项设置页面")
} else {
print("打开登录项设置页面失败")
}
}
// MARK: - Deinitializer
deinit {
// App 退
stopMonitoringTimer()
stopCaffeinate()
}
}

View File

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

@@ -1,4 +1,5 @@
import SwiftUI
import AppKit
@main
struct PowerBarApp: App {
@@ -25,24 +26,14 @@ struct PowerBarApp: App {
.foregroundColor(appState.isFunctionEnabled ? .green : .gray)
}
}
//
WindowGroup {
if !hasShownGuide {
GuideView()
.onDisappear {
hasShownGuide = true
}
}
}
.windowStyle(.hiddenTitleBar)
.defaultSize(width: 400, height: 400)
.windowResizability(.contentSize)
}
}
//
struct MenuContent: View {
@EnvironmentObject var appState: AppState
//
@State private var guideWindow: NSWindow?
var body: some View {
VStack(alignment: .leading, spacing: 8) {
@@ -64,12 +55,15 @@ struct MenuContent: View {
//
Button(action: {
//
appState.toggleFunction()
withAnimation(.easeInOut(duration: 0.3)) {
appState.toggleFunction()
}
}) {
HStack(spacing: 12) {
Image(systemName: appState.isFunctionEnabled ? "stop.circle.fill" : "play.circle.fill")
.foregroundColor(appState.isFunctionEnabled ? .red : .green)
.font(.title2)
.transition(.scale(scale: 0.8, anchor: .center))
VStack(alignment: .leading, spacing: 2) {
Text(appState.isFunctionEnabled ? "关闭常亮" : "开启常亮")
.font(.body)
@@ -83,6 +77,7 @@ struct MenuContent: View {
.background(
RoundedRectangle(cornerRadius: 8)
.fill(appState.isFunctionEnabled ? Color.red.opacity(0.1) : Color.green.opacity(0.1))
.transition(.opacity)
)
}
.buttonStyle(.plain)
@@ -98,17 +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)
Text(appState.isUserIdle ? "空闲" : "活动")
Text("保护")
.font(.body)
// caffeinate
if !appState.isUserIdle {
Text("(保护中)")
.font(.caption2)
.foregroundColor(.secondary)
}
}
} else {
HStack(spacing: 6) {
@@ -121,6 +109,7 @@ struct MenuContent: View {
}
Spacer()
}
}
.padding(8)
.background(
@@ -130,36 +119,94 @@ struct MenuContent: View {
Divider()
//
VStack(alignment: .leading, spacing: 8) {
Text("权限管理")
.font(.caption)
.foregroundColor(.secondary)
//
Button(action: {
//
let settingsUrl = URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_Automation")!
if NSWorkspace.shared.open(settingsUrl) {
print("已打开系统设置自动化页面")
} else {
//
let fallbackUrl = URL(string: "x-apple.systempreferences:com.apple.preference.security")!
NSWorkspace.shared.open(fallbackUrl)
}
}) {
HStack {
Image(systemName: "gearshape.fill")
.frame(width: 20)
.foregroundColor(.blue)
Text("打开系统设置")
.font(.caption)
.foregroundColor(.blue)
}
.padding(8)
.frame(maxWidth: .infinity, alignment: .leading)
.background(
RoundedRectangle(cornerRadius: 8)
.fill(Color.blue.opacity(0.05))
)
}
.buttonStyle(.plain)
//
Button(action: {
let settingsUrl = URL(string: "x-apple.systempreferences:com.apple.preference.usersandgroups?LoginItems")!
NSWorkspace.shared.open(settingsUrl)
}) {
HStack {
Image(systemName: "person.2.fill")
.frame(width: 20)
.foregroundColor(.blue)
Text("管理登录项")
.font(.caption)
.foregroundColor(.blue)
}
.padding(8)
.frame(maxWidth: .infinity, alignment: .leading)
.background(
RoundedRectangle(cornerRadius: 8)
.fill(Color.blue.opacity(0.05))
)
}
.buttonStyle(.plain)
}
.padding(8)
Divider()
//
Toggle(isOn: Binding(
get: { appState.isLaunchAtLogin },
set: { _ in appState.toggleLaunchAtLogin() }
)) {
HStack {
Image(systemName: "power.on.circle")
Image(systemName: "power.circle")
.frame(width: 20)
Text("开机自动启动")
}
}
.toggleStyle(.checkbox)
.font(.caption)
.padding(4)
.padding(8)
Divider()
//
Button("查看使用指南") {
//
// 使 AppStorage
UserDefaults.standard.set(false, forKey: "hasShownGuide")
//
// macOS
NSAlert.showAlert(title: "使用指南", message: "请重启应用以查看使用指南,或在下次启动时自动显示。")
//
openGuideWindow()
}
.buttonStyle(.plain)
.font(.caption)
.foregroundColor(.secondary)
.padding(4)
.padding(8)
Divider()
@@ -170,16 +217,47 @@ struct MenuContent: View {
.buttonStyle(.plain)
.font(.caption)
.foregroundColor(.secondary)
.padding(4)
.padding(8)
}
.padding(12)
.frame(minWidth: 280)
.background(
RoundedRectangle(cornerRadius: 12)
.fill(Color.white)
.fill(Color(.windowBackgroundColor))
.shadow(radius: 4)
)
}
//
private func openGuideWindow() {
//
if let existingWindow = guideWindow {
existingWindow.makeKeyAndOrderFront(nil)
return
}
//
let window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 400, height: 500),
styleMask: [.titled, .closable, .resizable],
backing: .buffered,
defer: false
)
//
window.title = "PowerBar 使用指南"
window.contentView = NSHostingView(rootView: GuideView())
window.center()
//
window.makeKeyAndOrderFront(nil)
//
UserDefaults.standard.set(true, forKey: "hasShownGuide")
//
guideWindow = window
}
}
// NSAlert便
@@ -231,7 +309,7 @@ struct GuideView: View {
}
HStack(spacing: 12) {
Image(systemName: "power.on.circle")
Image(systemName: "power.circle")
.font(.title)
.foregroundColor(.orange)
.frame(width: 40)
@@ -338,13 +416,15 @@ struct GuideView: View {
RoundedRectangle(cornerRadius: 8)
.fill(.blue)
)
.transition(.scale(scale: 0.9, anchor: .center))
.animation(.easeInOut(duration: 0.2), value: UUID())
}
.padding(.bottom, 20)
}
.frame(width: 400, height: 500)
.background(
RoundedRectangle(cornerRadius: 12)
.fill(Color.white)
.fill(Color(.windowBackgroundColor))
)
.shadow(radius: 10)
}

83
build_and_package.sh Executable file
View File

@@ -0,0 +1,83 @@
#!/bin/bash
# PowerBar 打包脚本
# 用于构建应用程序并创建DMG安装包
# 设置变量
PROJECT_NAME="PowerBar"
SCHEME="PowerBar"
CONFIGURATION="Release"
VERSION="1.0.0"
echo "=== PowerBar 打包脚本 v$VERSION ==="
# 1. 清理之前的构建产物和缓存
echo "\n1. 清理之前的构建产物和缓存..."
rm -rf "$PROJECT_NAME.app"
rm -rf "$PROJECT_NAME-$VERSION.dmg"
rm -rf "build"
rm -rf "~/Library/Developer/Xcode/DerivedData/$PROJECT_NAME-*"
rm -rf "$PROJECT_NAME.xcodeproj/xcuserdata"
rm -rf "$PROJECT_NAME.xcodeproj/project.xcworkspace/xcuserdata"
# 2. 执行Xcode清理
echo "\n2. 执行Xcode清理..."
xcodebuild -scheme "$SCHEME" -configuration "$CONFIGURATION" clean
# 3. 直接构建应用程序
echo "\n3. 构建应用程序..."
xcodebuild -scheme "$SCHEME" -configuration "$CONFIGURATION" -derivedDataPath "build" build
if [ $? -ne 0 ]; then
echo "构建失败!"
exit 1
fi
# 4. 复制应用程序到当前目录
echo "\n4. 复制应用程序到当前目录..."
cp -R "build/Build/Products/$CONFIGURATION/$PROJECT_NAME.app" .
# 5. 验证应用程序
echo "\n5. 验证应用程序..."
if [ -d "$PROJECT_NAME.app" ]; then
echo "应用程序构建成功:$PROJECT_NAME.app"
else
echo "应用程序构建失败!"
exit 1
fi
# 6. 创建DMG安装包
echo "\n6. 创建DMG安装包..."
# 创建临时目录用于构建DMG
temp_dmg_dir="temp_dmg"
rm -rf "$temp_dmg_dir"
mkdir -p "$temp_dmg_dir"
# 复制应用程序到临时目录
cp -R "$PROJECT_NAME.app" "$temp_dmg_dir/"
# 创建Applications快捷方式
ln -s /Applications "$temp_dmg_dir/Applications"
# 创建DMG
hdiutil create -volname "$PROJECT_NAME-$VERSION" -srcfolder "$temp_dmg_dir" -ov -format UDZO "$PROJECT_NAME-$VERSION.dmg"
# 7. 清理临时文件
echo "\n7. 清理临时文件..."
rm -rf "$temp_dmg_dir"
rm -rf "build"
# 8. 验证DMG
echo "\n8. 验证DMG..."
if [ -f "$PROJECT_NAME-$VERSION.dmg" ]; then
echo "DMG创建成功$PROJECT_NAME-$VERSION.dmg"
echo "文件大小:$(du -h "$PROJECT_NAME-$VERSION.dmg" | cut -f1)"
else
echo "DMG创建失败"
exit 1
fi
echo "\n=== 打包完成! ==="
echo "应用程序:$PROJECT_NAME.app"
echo "安装包:$PROJECT_NAME-$VERSION.dmg"