Something went wrong. Try again.
An auto tiler for macOS
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184public import AppKitpublic import Foundation
@MainActorpublic protocol UpdateDriver: AnyObject { var canCheckForUpdates: Bool { get } var availableVersion: String? { get } var availabilityDidChange: (@MainActor () -> Void)? { get set } func start() throws func checkForUpdates() var isReadyToRestart: Bool { get } func restartToUpdate() -> Bool}
extension UpdateDriver { public var availableVersion: String? { nil } public var isReadyToRestart: Bool { false } public func restartToUpdate() -> Bool { false }}
@MainActorpublic final class UpdateController { public private(set) var isStarted = false public let isEnabled: Bool public var availabilityDidChange: (@MainActor () -> Void)? private let makeDriver: @MainActor () throws -> any UpdateDriver private var driver: (any UpdateDriver)? private var menuItem: UpdateMenuItem? private weak var menuParent: NSMenuItem?
init(isEnabled: Bool, makeDriver: @escaping @MainActor () throws -> any UpdateDriver) { self.isEnabled = isEnabled self.makeDriver = makeDriver }
public static func live() -> UpdateController { #if DEBUG UpdateController(isEnabled: false) { preconditionFailure("Debug updates are disabled") } #else UpdateController(isEnabled: true) { _ = try UpdateConfiguration(bundle: .main) return SparkleDriver() } #endif }
public var canCheckForUpdates: Bool { isEnabled && isStarted && driver?.canCheckForUpdates == true }
public func start() throws { guard isEnabled, !isStarted else { return } let driver = try makeDriver() driver.availabilityDidChange = { [weak self] in self?.refreshMenuItem() self?.availabilityDidChange?() } try driver.start() self.driver = driver isStarted = true refreshMenuItem() availabilityDidChange?() }
public func installMenuItem(in menu: NSMenu, at index: Int) { menuParent = menu.supermenu?.items.first { $0.submenu === menu && $0.identifier?.rawValue == "menu.more" } if menuItem?.menu === menu { refreshMenuItem() return } let item: UpdateMenuItem if let menuItem { menuItem.menu?.removeItem(menuItem) item = menuItem } else { item = UpdateMenuItem { [weak self] in self?.checkForUpdates() } menuItem = item } menu.insertItem(item, at: index) refreshMenuItem() }
public var availableVersion: String? { driver?.availableVersion } public var isReadyToRestart: Bool { isEnabled && isStarted && driver?.isReadyToRestart == true }
@discardableResult public func restartToUpdate() -> Bool { guard isReadyToRestart, let driver else { return false } return driver.restartToUpdate() }
private func refreshMenuItem() { guard let menuItem else { return } menuItem.isEnabled = canCheckForUpdates menuItem.title = Bundle.module.localizedString(forKey: "Check for Updates…", value: nil, table: "Updates") let hasUpdate = availableVersion != nil || isReadyToRestart let dot = hasUpdate ? NSImage(systemSymbolName: "circle.fill", accessibilityDescription: nil)? .withSymbolConfiguration(.init(pointSize: 6, weight: .regular)) : nil menuItem.image = dot let detail = availableVersion.map { version in let format = Bundle.module.localizedString(forKey: "Update %@ Available…", value: nil, table: "Updates") return String(format: format, locale: .current, version) } menuItem.toolTip = detail menuItem.setAccessibilityHelp(detail) menuItem.setAccessibilityLabel( hasUpdate ? Bundle.module.localizedString( forKey: "Check for Updates…, update ready", value: nil, table: "Updates") : menuItem.title) menuParent?.image = dot menuParent?.setAccessibilityLabel( hasUpdate ? Bundle.module.localizedString(forKey: "More, update ready", value: nil, table: "Updates") : menuParent?.title) }
@discardableResult public func checkForUpdates() -> Bool { guard canCheckForUpdates, let driver else { return false } driver.checkForUpdates() return true }}
@MainActorprivate final class UpdateMenuItem: NSMenuItem { private let check: @MainActor () -> Void
init(check: @escaping @MainActor () -> Void) { self.check = check let title = Bundle.module.localizedString(forKey: "Check for Updates…", value: nil, table: "Updates") super.init(title: title, action: #selector(checkForUpdates), keyEquivalent: "") target = self setAccessibilityLabel(title) }
required init(coder: NSCoder) { fatalError("init(coder:) is unavailable") }
@objc private func checkForUpdates() { check() }}
public struct UpdateConfiguration: Equatable, Sendable { public let feedURL: URL public let publicKey: String
public init(feedURL: String?, publicKey: String?) throws { guard let feedURL, let url = URL(string: feedURL), url.scheme == "https", let host = url.host, !host.isEmpty, url.user == nil, url.password == nil, url.fragment == nil, !feedURL.contains(where: { $0.isWhitespace }) else { throw UpdateConfigurationError.invalidFeedURL } guard let publicKey, let decoded = Data(base64Encoded: publicKey), decoded.count == 32, decoded.base64EncodedString() == publicKey else { throw UpdateConfigurationError.invalidPublicKey } self.feedURL = url self.publicKey = publicKey }
public init(bundle: Bundle) throws { try self.init( feedURL: bundle.object(forInfoDictionaryKey: "SUFeedURL") as? String, publicKey: bundle.object(forInfoDictionaryKey: "SUPublicEDKey") as? String) }}
public enum UpdateConfigurationError: Error, LocalizedError, Equatable { case invalidFeedURL case invalidPublicKey
public var errorDescription: String? { let key: String switch self { case .invalidFeedURL: key = "The update feed is missing or is not a secure HTTPS URL." case .invalidPublicKey: key = "The update signing public key is missing or invalid." } return Bundle.module.localizedString(forKey: key, value: nil, table: "Updates") }}