diff --git a/Kit/helpers.swift b/Kit/helpers.swift index b5c4e558..b8b0de8c 100644 --- a/Kit/helpers.swift +++ b/Kit/helpers.swift @@ -18,6 +18,23 @@ public struct LaunchAtLogin { private static let id = "\(Bundle.main.bundleIdentifier!).LaunchAtLogin" public static var isEnabled: Bool { + get { + if #available(macOS 13, *) { + return isEnabledNext + } else { + return isEnabledLegacy + } + } + set { + if #available(macOS 13, *) { + isEnabledNext = newValue + } else { + isEnabledLegacy = newValue + } + } + } + + private static var isEnabledLegacy: Bool { get { guard let jobs = (LaunchAtLogin.self as DeprecationWarningWorkaround.Type).jobsDict else { return false @@ -29,6 +46,36 @@ public struct LaunchAtLogin { SMLoginItemSetEnabled(id as CFString, newValue) } } + + @available(macOS 13, *) + private static var isEnabledNext: Bool { + get { SMAppService.mainApp.status == .enabled } + set { + do { + if newValue { + if SMAppService.mainApp.status == .enabled { + try? SMAppService.mainApp.unregister() + } + try SMAppService.mainApp.register() + } else { + try SMAppService.mainApp.unregister() + } + } catch { + print("failed to \(newValue ? "enable" : "disable") launch at login: \(error.localizedDescription)") + } + } + } + + public static func migrate() { + guard #available(macOS 13, *), !Store.shared.exist(key: "LaunchAtLoginNext") else { + return + } + + Store.shared.set(key: "LaunchAtLoginNext", value: true) + isEnabledNext = isEnabledLegacy + isEnabledLegacy = false + try? SMAppService.loginItem(identifier: id).unregister() + } } private protocol DeprecationWarningWorkaround { diff --git a/Stats/helpers.swift b/Stats/helpers.swift index d8f6878f..941cb985 100644 --- a/Stats/helpers.swift +++ b/Stats/helpers.swift @@ -103,6 +103,8 @@ extension AppDelegate { if !Store.shared.exist(key: "runAtLoginInitialized") { Store.shared.set(key: "runAtLoginInitialized", value: true) LaunchAtLogin.isEnabled = true + } else { + LaunchAtLogin.migrate() } if Store.shared.exist(key: "dockIcon") {