Skip to content

Commit

Permalink
fix: sleepmanager not working
Browse files Browse the repository at this point in the history
Signed-off-by: samzong <[email protected]>
  • Loading branch information
samzong committed Dec 30, 2024
1 parent 42f64c3 commit bb97419
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 22 deletions.
12 changes: 11 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,19 @@ jobs:
# Create a temporary directory for mounting
TEMP_DIR=$(mktemp -d)
# Copy app to temp directory
mkdir -p $TEMP_DIR
cp -r "./build/MacMusicPlayer.xcarchive/Products/Applications/MacMusicPlayer.app" "$TEMP_DIR/"
# Create symbolic link to Applications folder
ln -s /Applications "$TEMP_DIR/Applications"
# Create a DMG
hdiutil create -volname "MacMusicPlayer" -srcfolder "./build/MacMusicPlayer.xcarchive/Products/Applications/MacMusicPlayer.app" \
hdiutil create -volname "MacMusicPlayer" -srcfolder "$TEMP_DIR" \
-ov -format UDZO "MacMusicPlayer-${{ env.VERSION }}.dmg"
# Clean up
rm -rf "$TEMP_DIR"
- name: Generate Release Notes
id: release_notes
Expand Down
12 changes: 12 additions & 0 deletions AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ class AppDelegate: NSObject, NSApplicationDelegate {

menu.addItem(NSMenuItem.separator())

// Version info
let versionString = getVersionString()
let versionItem = NSMenuItem(title: versionString, action: nil, keyEquivalent: "")
versionItem.isEnabled = false
menu.addItem(versionItem)

// Quit
menu.addItem(NSMenuItem(title: NSLocalizedString("Quit", comment: ""), action: #selector(quit), keyEquivalent: ""))

Expand Down Expand Up @@ -214,4 +220,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func applicationWillTerminate(_ aNotification: Notification) {
sleepManager.preventSleep = false
}

private func getVersionString() -> String {
let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "1.0"
let buildNumber = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String ?? "1"
return String(format: NSLocalizedString("Version %@ (%@)", comment: ""), appVersion, buildNumber)
}
}
9 changes: 9 additions & 0 deletions MacMusicPlayer/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,14 @@
<true/>
<key>CFBundleIdentifier</key>
<string>com.seimotech.MacMusicPlayer</string>
<key>SMPrivilegedExecutables</key>
<dict>
<key>com.seimotech.MacMusicPlayer</key>
<string>identifier "com.seimotech.MacMusicPlayer" and anchor apple generic</string>
</dict>
<key>CFBundleShortVersionString</key>
<string>1.0.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
3 changes: 2 additions & 1 deletion MacMusicPlayer/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@
// System
"Enable launch at login" = "Start at Login Enabled";
"Disable launch at login" = "Start at Login Disabled";
"MacMusicPlayer needs permission to prevent system sleep to ensure uninterrupted music playback." = "MacMusicPlayer needs permission to keep your Mac awake for uninterrupted music playback.";
"MacMusicPlayer needs permission to prevent system sleep to ensure uninterrupted music playback." = "MacMusicPlayer needs permission to keep your Mac awake for uninterrupted music playback.";
"Version %@ (%@)" = "Version %@ (%@)";
3 changes: 2 additions & 1 deletion MacMusicPlayer/ja.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@
// System
"Enable launch at login" = "ログイン時の起動を有効にしました";
"Disable launch at login" = "ログイン時の起動を無効にしました";
"MacMusicPlayer needs permission to prevent system sleep to ensure uninterrupted music playback." = "途切れのない音楽再生のため、システムのスリープを防止する権限が必要です";
"MacMusicPlayer needs permission to prevent system sleep to ensure uninterrupted music playback." = "途切れのない音楽再生のため、システムのスリープを防止する権限が必要です";
"Version %@ (%@)" = "バージョン %@ (%@)";
3 changes: 2 additions & 1 deletion MacMusicPlayer/ko.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@
// System
"Enable launch at login" = "로그인 시 실행이 활성화되었습니다";
"Disable launch at login" = "로그인 시 실행이 비활성화되었습니다";
"MacMusicPlayer needs permission to prevent system sleep to ensure uninterrupted music playback." = "중단 없는 음악 재생을 위해 시스템 절전 방지 권한이 필요합니다";
"MacMusicPlayer needs permission to prevent system sleep to ensure uninterrupted music playback." = "중단 없는 음악 재생을 위해 시스템 절전 방지 권한이 필요합니다";
"Version %@ (%@)" = "버전 %@ (%@)";
3 changes: 2 additions & 1 deletion MacMusicPlayer/zh-Hans.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@
// System
"Enable launch at login" = "已开启开机启动";
"Disable launch at login" = "已关闭开机启动";
"MacMusicPlayer needs permission to prevent system sleep to ensure uninterrupted music playback." = "需要权限保持系统唤醒,以确保音乐不被中断";
"MacMusicPlayer needs permission to prevent system sleep to ensure uninterrupted music playback." = "需要权限保持系统唤醒,以确保音乐不被中断";
"Version %@ (%@)" = "版本 %@ (%@)";
3 changes: 2 additions & 1 deletion MacMusicPlayer/zh-Hant.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@
// System
"Enable launch at login" = "已開啟開機啟動";
"Disable launch at login" = "已關閉開機啟動";
"MacMusicPlayer needs permission to prevent system sleep to ensure uninterrupted music playback." = "需要權限保持系統喚醒,以確保音樂不被中斷";
"MacMusicPlayer needs permission to prevent system sleep to ensure uninterrupted music playback." = "需要權限保持系統喚醒,以確保音樂不被中斷";
"Version %@ (%@)" = "版本 %@ (%@)";
26 changes: 17 additions & 9 deletions Managers/LaunchManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,27 @@ class LaunchManager: ObservableObject {
}

init() {
// 从 UserDefaults 读取设置,默认为 false
self.launchAtLogin = UserDefaults.standard.bool(forKey: "LaunchAtLogin")
// 确保设置与实际状态一致
self.launchAtLogin = UserDefaults.standard.object(forKey: "LaunchAtLogin") == nil ? true : UserDefaults.standard.bool(forKey: "LaunchAtLogin")
setLaunchAtLogin(launchAtLogin)
}

private func setLaunchAtLogin(_ enable: Bool) {
if enable {
let success = SMLoginItemSetEnabled(bundleIdentifier as CFString, true)
print("Enable launch at login: \(success)")
if #available(macOS 13.0, *) {
do {
if enable {
if SMAppService.mainApp.status == .notRegistered {
try SMAppService.mainApp.register()
}
} else {
if SMAppService.mainApp.status == .enabled {
try SMAppService.mainApp.unregister()
}
}
} catch {
print("Failed to \(enable ? "enable" : "disable") launch at login: \(error)")
}
} else {
let success = SMLoginItemSetEnabled(bundleIdentifier as CFString, false)
print("Disable launch at login: \(success)")
_ = SMLoginItemSetEnabled(bundleIdentifier as CFString, enable)
}
}
}
}
35 changes: 28 additions & 7 deletions Managers/SleepManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,31 @@ import Foundation
import IOKit.pwr_mgt

class SleepManager: ObservableObject {
@Published var preventSleep = true {
@Published var preventSleep: Bool {
didSet {
updateSleepAssertion()
UserDefaults.standard.set(preventSleep, forKey: "PreventSleepEnabled")
}
}

private var assertionID: IOPMAssertionID = 0

func updateSleepAssertion() {
init() {
if UserDefaults.standard.object(forKey: "PreventSleepEnabled") != nil {
self.preventSleep = UserDefaults.standard.bool(forKey: "PreventSleepEnabled")
} else {
self.preventSleep = true
UserDefaults.standard.set(true, forKey: "PreventSleepEnabled")
}

updateSleepAssertion()
}

deinit {
releaseAssertion()
}

private func updateSleepAssertion() {
if preventSleep {
createAssertion()
} else {
Expand All @@ -26,11 +42,16 @@ class SleepManager: ObservableObject {
}

private func createAssertion() {
let reason = "MacMusicPlayer is preventing display sleep"
IOPMAssertionCreateWithName(kIOPMAssertionTypePreventUserIdleDisplaySleep as CFString,
IOPMAssertionLevel(kIOPMAssertionLevelOn),
reason as CFString,
&assertionID)
if assertionID != 0 {
releaseAssertion()
}

_ = IOPMAssertionCreateWithName(
kIOPMAssertionTypePreventUserIdleDisplaySleep as CFString,
IOPMAssertionLevel(kIOPMAssertionLevelOn),
"MacMusicPlayer is preventing display sleep" as CFString,
&assertionID
)
}

private func releaseAssertion() {
Expand Down

0 comments on commit bb97419

Please sign in to comment.