Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
d5ccfd9651
|
|||
|
57d605674e
|
|||
|
b133d8a4e9
|
@@ -266,7 +266,7 @@
|
|||||||
"$(inherited)",
|
"$(inherited)",
|
||||||
"@executable_path/../Frameworks",
|
"@executable_path/../Frameworks",
|
||||||
);
|
);
|
||||||
MARKETING_VERSION = 1.0;
|
MARKETING_VERSION = 1.0.1;
|
||||||
PRODUCT_BUNDLE_IDENTIFIER = me.leo12025.PowerBar;
|
PRODUCT_BUNDLE_IDENTIFIER = me.leo12025.PowerBar;
|
||||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||||
REGISTER_APP_GROUPS = YES;
|
REGISTER_APP_GROUPS = YES;
|
||||||
@@ -300,7 +300,7 @@
|
|||||||
"$(inherited)",
|
"$(inherited)",
|
||||||
"@executable_path/../Frameworks",
|
"@executable_path/../Frameworks",
|
||||||
);
|
);
|
||||||
MARKETING_VERSION = 1.0;
|
MARKETING_VERSION = 1.0.1;
|
||||||
PRODUCT_BUNDLE_IDENTIFIER = me.leo12025.PowerBar;
|
PRODUCT_BUNDLE_IDENTIFIER = me.leo12025.PowerBar;
|
||||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||||
REGISTER_APP_GROUPS = YES;
|
REGISTER_APP_GROUPS = YES;
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import Combine
|
|||||||
import IOKit.pwr_mgt
|
import IOKit.pwr_mgt
|
||||||
import ServiceManagement
|
import ServiceManagement
|
||||||
import AppKit
|
import AppKit
|
||||||
|
import UserNotifications
|
||||||
|
|
||||||
// MARK: - App State Manager
|
// MARK: - App State Manager
|
||||||
class AppState: ObservableObject {
|
class AppState: ObservableObject {
|
||||||
@@ -16,11 +17,20 @@ class AppState: ObservableObject {
|
|||||||
|
|
||||||
// 内部属性
|
// 内部属性
|
||||||
private var caffeinateProcess: Process? // 直接使用Process对象,但简化实现
|
private var caffeinateProcess: Process? // 直接使用Process对象,但简化实现
|
||||||
|
private var displayCountObserver: AnyCancellable? // 监听显示器数量变化
|
||||||
|
private var wasAutomaticallyEnabled: Bool = false // 记录是否是自动启用的
|
||||||
|
private var currentDisplayCount: Int = NSScreen.screens.count // 当前显示器数量
|
||||||
|
|
||||||
// 初始化方法
|
// 初始化方法
|
||||||
init() {
|
init() {
|
||||||
// 检查并设置初始的自动启动状态
|
// 检查并设置初始的自动启动状态
|
||||||
checkLaunchAtLoginStatus()
|
checkLaunchAtLoginStatus()
|
||||||
|
|
||||||
|
// 请求通知权限
|
||||||
|
requestNotificationPermission()
|
||||||
|
|
||||||
|
// 设置显示器数量变化监听
|
||||||
|
setupDisplayCountObserver()
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: - Public Methods
|
// MARK: - Public Methods
|
||||||
@@ -192,10 +202,122 @@ class AppState: ObservableObject {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: - Display Monitoring and Auto-enable
|
||||||
|
|
||||||
|
/// 请求通知权限
|
||||||
|
private func requestNotificationPermission() {
|
||||||
|
let center = UNUserNotificationCenter.current()
|
||||||
|
center.requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
|
||||||
|
if granted {
|
||||||
|
print("通知权限已授予")
|
||||||
|
} else if let error = error {
|
||||||
|
print("请求通知权限失败: \(error)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 设置显示器数量变化监听
|
||||||
|
private func setupDisplayCountObserver() {
|
||||||
|
// 使用 Combine 监听 NSScreen.screens.count 的变化
|
||||||
|
// 这里使用定时器定期检查,因为没有直接的通知可以监听
|
||||||
|
displayCountObserver = Timer.publish(every: 5, on: .main, in: .common)
|
||||||
|
.autoconnect()
|
||||||
|
.sink { [weak self] _ in
|
||||||
|
self?.checkDisplayCount()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 检查显示器数量变化
|
||||||
|
private func checkDisplayCount() {
|
||||||
|
let newDisplayCount = NSScreen.screens.count
|
||||||
|
|
||||||
|
// 如果显示器数量变化
|
||||||
|
if newDisplayCount != currentDisplayCount {
|
||||||
|
if newDisplayCount > 1 {
|
||||||
|
// 外部显示器连接
|
||||||
|
handleExternalDisplayConnected()
|
||||||
|
} else {
|
||||||
|
// 外部显示器断开
|
||||||
|
handleExternalDisplayDisconnected()
|
||||||
|
}
|
||||||
|
currentDisplayCount = newDisplayCount
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 处理外部显示器连接
|
||||||
|
private func handleExternalDisplayConnected() {
|
||||||
|
if !isFunctionEnabled {
|
||||||
|
// 自动启用保持亮屏
|
||||||
|
wasAutomaticallyEnabled = true
|
||||||
|
toggleFunction()
|
||||||
|
|
||||||
|
// 发送通知
|
||||||
|
sendNotification(title: "PowerBar", body: "检测到外部显示器,已自动打开保持亮屏功能")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 处理外部显示器断开
|
||||||
|
private func handleExternalDisplayDisconnected() {
|
||||||
|
if wasAutomaticallyEnabled && isFunctionEnabled {
|
||||||
|
// 发送通知询问是否关闭保持亮屏
|
||||||
|
sendNotificationWithAction(title: "PowerBar", body: "外部显示器已断开,是否关闭保持亮屏功能?")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 发送普通通知
|
||||||
|
private func sendNotification(title: String, body: String) {
|
||||||
|
let content = UNMutableNotificationContent()
|
||||||
|
content.title = title
|
||||||
|
content.body = body
|
||||||
|
content.sound = UNNotificationSound.default
|
||||||
|
|
||||||
|
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)
|
||||||
|
UNUserNotificationCenter.current().add(request) { error in
|
||||||
|
if let error = error {
|
||||||
|
print("发送通知失败: \(error)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 发送带操作的通知
|
||||||
|
private func sendNotificationWithAction(title: String, body: String) {
|
||||||
|
let content = UNMutableNotificationContent()
|
||||||
|
content.title = title
|
||||||
|
content.body = body
|
||||||
|
content.sound = UNNotificationSound.default
|
||||||
|
content.categoryIdentifier = "displayDisconnected"
|
||||||
|
|
||||||
|
// 注册通知类别和操作
|
||||||
|
let cancelAction = UNNotificationAction(identifier: "cancel", title: "取消", options: [])
|
||||||
|
let disableAction = UNNotificationAction(identifier: "disable", title: "关闭", options: [])
|
||||||
|
let category = UNNotificationCategory(identifier: "displayDisconnected", actions: [disableAction, cancelAction], intentIdentifiers: [])
|
||||||
|
UNUserNotificationCenter.current().setNotificationCategories([category])
|
||||||
|
|
||||||
|
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)
|
||||||
|
UNUserNotificationCenter.current().add(request) { error in
|
||||||
|
if let error = error {
|
||||||
|
print("发送通知失败: \(error)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 处理通知操作
|
||||||
|
func handleNotificationResponse(identifier: String) {
|
||||||
|
if identifier == "disable" {
|
||||||
|
// 用户选择关闭保持亮屏
|
||||||
|
wasAutomaticallyEnabled = false
|
||||||
|
toggleFunction()
|
||||||
|
} else if identifier == "cancel" {
|
||||||
|
// 用户选择取消,保持当前状态
|
||||||
|
wasAutomaticallyEnabled = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: - Deinitializer
|
// MARK: - Deinitializer
|
||||||
|
|
||||||
deinit {
|
deinit {
|
||||||
// 确保 App 退出时清理资源
|
// 确保 App 退出时清理资源
|
||||||
stopCaffeinate()
|
stopCaffeinate()
|
||||||
|
displayCountObserver?.cancel()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
import AppKit
|
import AppKit
|
||||||
|
import UserNotifications
|
||||||
|
|
||||||
@main
|
@main
|
||||||
struct PowerBarApp: App {
|
struct PowerBarApp: App {
|
||||||
@@ -26,6 +27,30 @@ struct PowerBarApp: App {
|
|||||||
.foregroundColor(appState.isFunctionEnabled ? .green : .gray)
|
.foregroundColor(appState.isFunctionEnabled ? .green : .gray)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.onChange(of: ScenePhase.active) {
|
||||||
|
// 当应用变为活动状态时,设置通知中心代理
|
||||||
|
UNUserNotificationCenter.current().delegate = NotificationDelegate.shared
|
||||||
|
NotificationDelegate.shared.appState = appState
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 通知代理,处理通知响应
|
||||||
|
class NotificationDelegate: NSObject, UNUserNotificationCenterDelegate {
|
||||||
|
static let shared = NotificationDelegate()
|
||||||
|
weak var appState: AppState?
|
||||||
|
|
||||||
|
private override init() { super.init() }
|
||||||
|
|
||||||
|
// 当通知在前台收到时调用
|
||||||
|
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification) async -> UNNotificationPresentationOptions {
|
||||||
|
return [.banner, .sound]
|
||||||
|
}
|
||||||
|
|
||||||
|
// 当用户点击通知操作时调用
|
||||||
|
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse) async {
|
||||||
|
let actionIdentifier = response.actionIdentifier
|
||||||
|
appState?.handleNotificationResponse(identifier: actionIdentifier)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
10
README.md
10
README.md
@@ -1,11 +1,17 @@
|
|||||||
# PowerBar
|
# PowerBar
|
||||||
|
|
||||||
一个智能的macOS菜单栏应用,根据用户活动状态自动管理系统睡眠。
|
一个智能的macOS菜单栏应用,根据用户活动状态和外部显示器连接状态自动管理系统睡眠。
|
||||||
|
|
||||||
|
## 版本
|
||||||
|
|
||||||
|
1.0.1
|
||||||
|
|
||||||
## 功能特性
|
## 功能特性
|
||||||
|
|
||||||
- 📊 **智能睡眠管理**:根据用户活动状态自动控制系统睡眠
|
- 📊 **智能睡眠管理**:根据用户活动状态自动控制系统睡眠
|
||||||
- 🔍 **空闲检测**:5分钟无操作自动视为空闲,停止阻止睡眠
|
- 🖥️ **外部显示器检测**:自动检测外部显示器连接状态
|
||||||
|
- 🚀 **自动控制**:外部显示器连接时自动开启保持亮屏
|
||||||
|
- 📢 **通知提醒**:外部显示器连接/断开时发送通知
|
||||||
- 🎯 **清晰的状态指示**:通过菜单栏图标直观显示当前状态
|
- 🎯 **清晰的状态指示**:通过菜单栏图标直观显示当前状态
|
||||||
- 🎨 **现代化UI**:简洁美观的菜单界面,易于操作
|
- 🎨 **现代化UI**:简洁美观的菜单界面,易于操作
|
||||||
- ⚡ **低资源消耗**:轻量级设计,几乎不占用系统资源
|
- ⚡ **低资源消耗**:轻量级设计,几乎不占用系统资源
|
||||||
|
|||||||
49
archive_export.sh
Executable file
49
archive_export.sh
Executable file
@@ -0,0 +1,49 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# PowerBar 构建脚本
|
||||||
|
# 使用 archive + export 方式构建应用
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
echo "=== PowerBar 构建脚本 ==="
|
||||||
|
|
||||||
|
echo "1. 清理之前的构建产物..."
|
||||||
|
# 清理之前的构建产物
|
||||||
|
rm -rf build
|
||||||
|
rm -rf archive
|
||||||
|
rm -rf PowerBar.app
|
||||||
|
rm -rf PowerBar-*.dmg
|
||||||
|
|
||||||
|
echo "2. 创建构建目录..."
|
||||||
|
mkdir -p build
|
||||||
|
mkdir -p archive
|
||||||
|
|
||||||
|
echo "3. 归档项目..."
|
||||||
|
xcodebuild archive \
|
||||||
|
-project PowerBar.xcodeproj \
|
||||||
|
-scheme PowerBar \
|
||||||
|
-archivePath archive/PowerBar.xcarchive \
|
||||||
|
-destination 'generic/platform=macOS' \
|
||||||
|
-configuration Release
|
||||||
|
|
||||||
|
echo "4. 导出归档..."
|
||||||
|
xcodebuild -exportArchive -archivePath archive/PowerBar.xcarchive -exportPath build -exportOptionsPlist ExportOptions.plist
|
||||||
|
|
||||||
|
echo "5. 复制应用到当前目录..."
|
||||||
|
cp -R build/PowerBar.app .
|
||||||
|
|
||||||
|
echo "6. 创建DMG安装包..."
|
||||||
|
# 获取当前版本号
|
||||||
|
VERSION=$(xcodebuild -project PowerBar.xcodeproj -showBuildSettings | grep MARKETING_VERSION | head -1 | awk -F'=' '{print $2}' | xargs)
|
||||||
|
|
||||||
|
# 创建DMG
|
||||||
|
hdiutil create -volname "PowerBar" \
|
||||||
|
-srcfolder PowerBar.app \
|
||||||
|
-ov \
|
||||||
|
-format UDZO \
|
||||||
|
PowerBar-$VERSION.dmg
|
||||||
|
|
||||||
|
echo "7. 构建完成!"
|
||||||
|
echo "应用:$(pwd)/PowerBar.app"
|
||||||
|
echo "DMG:$(pwd)/PowerBar-$VERSION.dmg"
|
||||||
|
echo "=== 构建脚本执行结束 ==="
|
||||||
@@ -7,7 +7,7 @@
|
|||||||
PROJECT_NAME="PowerBar"
|
PROJECT_NAME="PowerBar"
|
||||||
SCHEME="PowerBar"
|
SCHEME="PowerBar"
|
||||||
CONFIGURATION="Release"
|
CONFIGURATION="Release"
|
||||||
VERSION="1.0.0"
|
VERSION="1.0.1"
|
||||||
|
|
||||||
echo "=== PowerBar 打包脚本 v$VERSION ==="
|
echo "=== PowerBar 打包脚本 v$VERSION ==="
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user