feat(updates): 使用 Sparkle 从发布 ZIP 下载并重启更新
This commit is contained in:
@@ -19,6 +19,7 @@ import notify
|
||||
private let awake = KeepAwakeService()
|
||||
private let presentation = SettingsPresentation()
|
||||
private let updates = ReleaseUpdates()
|
||||
private lazy var updater = SparkleUpdater(updates: updates)
|
||||
private let appearance = AppearancePreferences()
|
||||
private let menuBarPreferences = MenuBarPreferences()
|
||||
private let language = LanguagePreferences()
|
||||
@@ -75,7 +76,7 @@ import notify
|
||||
MenuPanelView(metrics: self.metrics, preferences: self.appearance, keepAwake: self.awake, updates: self.updates,
|
||||
presentation: panel, onOpenSettings: { [weak self] in
|
||||
self?.settingsCommand()
|
||||
}, onOpenAbout: { [weak self] in self?.aboutCommand() }, onPreferredHeight: { [weak self] height in
|
||||
}, onPreferredHeight: { [weak self] height in
|
||||
self?.menuBar?.setPreferredContentHeight(height)
|
||||
})
|
||||
}
|
||||
@@ -110,7 +111,7 @@ import notify
|
||||
NSApp.mainMenu = root
|
||||
}
|
||||
|
||||
func applicationDidFinishLaunching(_ notification: Notification) { updates.start() }
|
||||
func applicationDidFinishLaunching(_ notification: Notification) { updater.start() }
|
||||
|
||||
@objc private func settingsCommand() { showSettings() }
|
||||
@objc private func aboutCommand() { showSettings(.about) }
|
||||
@@ -127,7 +128,6 @@ import notify
|
||||
workspaceTokens.forEach(NSWorkspace.shared.notificationCenter.removeObserver)
|
||||
workspaceTokens.removeAll()
|
||||
menuBar?.shutdown()
|
||||
updates.stop()
|
||||
awake.disable()
|
||||
menuBar = nil
|
||||
openSettingsAction = nil
|
||||
|
||||
@@ -1,147 +1,74 @@
|
||||
import Foundation
|
||||
import Observation
|
||||
|
||||
/// The release tag carries the marketing version. Kymo's ZIP also carries its build number.
|
||||
nonisolated struct PublishedRelease: Decodable, Equatable {
|
||||
struct Asset: Decodable, Equatable { let name: String }
|
||||
let tagName: String
|
||||
let draft: Bool
|
||||
let prerelease: Bool
|
||||
let assets: [Asset]
|
||||
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case tagName = "tag_name"
|
||||
case draft, prerelease, assets
|
||||
}
|
||||
|
||||
static let repository = URL(string: "https://git.airnan.cn/wangairnan/Kymo")!
|
||||
static let endpoint = URL(string: "https://git.airnan.cn/api/v1/repos/wangairnan/Kymo/releases/latest")!
|
||||
|
||||
var page: URL { Self.repository.appending(path: "releases/tag").appending(component: tagName) }
|
||||
var version: String { tagName.hasPrefix("v") ? String(tagName.dropFirst()) : tagName }
|
||||
|
||||
private static func components(_ value: String) throws -> [Int] {
|
||||
var numbers: [Int] = []
|
||||
for part in value.components(separatedBy: ".") {
|
||||
guard !part.isEmpty, part.utf8.allSatisfy({ (48...57).contains($0) }), let n = Int(part) else {
|
||||
throw URLError(.cannotParseResponse)
|
||||
}
|
||||
numbers.append(n)
|
||||
}
|
||||
while numbers.last == 0 { numbers.removeLast() }
|
||||
return numbers
|
||||
}
|
||||
|
||||
var build: Int? {
|
||||
let prefix = "kymo-" + version + "-"
|
||||
return assets.compactMap { asset -> Int? in
|
||||
let name = asset.name
|
||||
guard name.hasPrefix(prefix), name.hasSuffix(".zip") else { return nil }
|
||||
let remainder = name.dropFirst(prefix.count)
|
||||
guard let separator = remainder.firstIndex(of: "-") else { return nil }
|
||||
let build = remainder[..<separator]
|
||||
guard build.allSatisfy({ $0.isASCII && $0.isNumber }) else { return nil }
|
||||
return Int(build)
|
||||
}.max()
|
||||
}
|
||||
|
||||
func isNewer(than localVersion: String, build localBuild: Int) throws -> Bool {
|
||||
guard !draft, !prerelease else { throw URLError(.cannotParseResponse) }
|
||||
let remote = try Self.components(version)
|
||||
let local = try Self.components(localVersion)
|
||||
if remote != local { return local.lexicographicallyPrecedes(remote) }
|
||||
return build.map { $0 > localBuild } ?? false
|
||||
}
|
||||
|
||||
static func latest() async throws -> Self {
|
||||
let config = URLSessionConfiguration.ephemeral
|
||||
config.urlCache = nil
|
||||
config.httpCookieStorage = nil
|
||||
config.urlCredentialStorage = nil
|
||||
config.timeoutIntervalForRequest = 15
|
||||
let session = URLSession(configuration: config)
|
||||
defer { session.finishTasksAndInvalidate() }
|
||||
let (data, response) = try await session.data(from: endpoint)
|
||||
guard (response as? HTTPURLResponse)?.statusCode == 200 else { throw URLError(.badServerResponse) }
|
||||
return try JSONDecoder().decode(Self.self, from: data)
|
||||
}
|
||||
}
|
||||
|
||||
/// Shared by the panel badge and About page. Sparkle owns the actual update session.
|
||||
@MainActor @Observable final class ReleaseUpdates {
|
||||
enum Phase { case idle, checking, checked, failed }
|
||||
enum Phase { case idle, checking, current, available, downloading, extracting, ready, restarting, failed }
|
||||
struct Version: Equatable {
|
||||
let display: String
|
||||
let build: String
|
||||
let infoURL: URL?
|
||||
var label: String { "v\(display) (\(build))" }
|
||||
}
|
||||
|
||||
private(set) var phase = Phase.idle
|
||||
private(set) var available: PublishedRelease?
|
||||
private(set) var version: Version?
|
||||
private(set) var errorMessage: String?
|
||||
private(set) var fraction: Double?
|
||||
var automatic: Bool {
|
||||
didSet {
|
||||
guard automatic != oldValue else { return }
|
||||
guard oldValue != automatic else { return }
|
||||
defaults.set(automatic, forKey: Self.preferenceKey)
|
||||
if automatic { check(manual: false) }
|
||||
else {
|
||||
timer?.cancel()
|
||||
if automaticRequest { cancelRequest() }
|
||||
}
|
||||
changeAutomatic?(automatic)
|
||||
}
|
||||
}
|
||||
|
||||
private static let preferenceKey = "updates.automatic"
|
||||
private let defaults: UserDefaults
|
||||
private let version: String
|
||||
private let build: Int
|
||||
private let fetch: () async throws -> PublishedRelease
|
||||
@ObservationIgnored private var timer: Task<Void, Never>?
|
||||
@ObservationIgnored private var request: Task<Void, Never>?
|
||||
@ObservationIgnored private var automaticRequest = false
|
||||
@ObservationIgnored var changeAutomatic: ((Bool) -> Void)?
|
||||
@ObservationIgnored var beginCheck: (() -> Void)?
|
||||
@ObservationIgnored var beginDownload: ((Version) -> Void)?
|
||||
@ObservationIgnored private var restart: (() -> Void)?
|
||||
|
||||
init(defaults: UserDefaults = .standard,
|
||||
version: String = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "",
|
||||
build: Int = Int(Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String ?? "") ?? 0,
|
||||
fetch: @escaping () async throws -> PublishedRelease = PublishedRelease.latest) {
|
||||
init(defaults: UserDefaults = .standard) {
|
||||
self.defaults = defaults
|
||||
self.version = version
|
||||
self.build = build
|
||||
self.fetch = fetch
|
||||
automatic = defaults.object(forKey: Self.preferenceKey) as? Bool ?? true
|
||||
}
|
||||
|
||||
func start() { if automatic { check(manual: false) } }
|
||||
func check() {
|
||||
guard [.idle, .available, .current, .failed].contains(phase) else { return }
|
||||
beginCheck?()
|
||||
}
|
||||
|
||||
func check(manual: Bool = true) {
|
||||
guard request == nil else { return }
|
||||
timer?.cancel()
|
||||
phase = .checking
|
||||
automaticRequest = !manual
|
||||
request = Task { [weak self, fetch] in
|
||||
guard !Task.isCancelled else { return }
|
||||
do {
|
||||
let release = try await fetch()
|
||||
guard !Task.isCancelled, let self else { return }
|
||||
available = try release.isNewer(than: version, build: build) ? release : nil
|
||||
phase = .checked
|
||||
} catch {
|
||||
guard !Task.isCancelled else { return }
|
||||
self?.phase = .failed
|
||||
}
|
||||
guard let self else { return }
|
||||
request = nil
|
||||
automaticRequest = false
|
||||
if automatic {
|
||||
let delay: Duration = phase == .failed ? .seconds(3600) : .seconds(86400)
|
||||
timer = Task { [weak self] in
|
||||
do { try await Task.sleep(for: delay) } catch { return }
|
||||
self?.check(manual: false)
|
||||
}
|
||||
}
|
||||
func performAction() {
|
||||
switch phase {
|
||||
case .available:
|
||||
if let version, version.infoURL == nil { beginDownload?(version) }
|
||||
case .ready:
|
||||
guard let restart else { return }
|
||||
self.restart = nil
|
||||
transition(to: .restarting)
|
||||
restart()
|
||||
case .failed, .idle, .current: check()
|
||||
default: break
|
||||
}
|
||||
}
|
||||
|
||||
private func cancelRequest() {
|
||||
request?.cancel()
|
||||
request = nil
|
||||
automaticRequest = false
|
||||
if phase == .checking { phase = .idle }
|
||||
func found(_ version: Version) {
|
||||
self.version = version
|
||||
transition(to: .available)
|
||||
}
|
||||
|
||||
func stop() {
|
||||
timer?.cancel()
|
||||
cancelRequest()
|
||||
func ready(restart: @escaping () -> Void) {
|
||||
transition(to: .ready)
|
||||
self.restart = restart
|
||||
}
|
||||
|
||||
func transition(to phase: Phase, fraction: Double? = nil, error: String? = nil) {
|
||||
self.phase = phase
|
||||
self.fraction = fraction
|
||||
errorMessage = error
|
||||
restart = nil
|
||||
if phase == .current || phase == .idle { version = nil }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
import Foundation
|
||||
import Sparkle
|
||||
|
||||
@MainActor final class SparkleUpdater: NSObject, SPUUserDriver, SPUUpdaterDelegate {
|
||||
private let updates: ReleaseUpdates
|
||||
private lazy var updater = SPUUpdater(hostBundle: .main, applicationBundle: .main, userDriver: self, delegate: self)
|
||||
private var started = false
|
||||
private var requestedBuild: String?
|
||||
private var expectedBytes: UInt64 = 0
|
||||
private var receivedBytes: UInt64 = 0
|
||||
|
||||
init(updates: ReleaseUpdates) {
|
||||
self.updates = updates
|
||||
super.init()
|
||||
updates.changeAutomatic = { [weak self] enabled in self?.updater.automaticallyChecksForUpdates = enabled }
|
||||
updates.beginCheck = { [weak self] in self?.check() }
|
||||
updates.beginDownload = { [weak self] in self?.check(download: $0) }
|
||||
}
|
||||
|
||||
func start() {
|
||||
guard prepare() else { return }
|
||||
if updates.automatic { updater.checkForUpdatesInBackground() }
|
||||
}
|
||||
|
||||
private func prepare() -> Bool {
|
||||
if started { return true }
|
||||
do {
|
||||
if updater.automaticallyChecksForUpdates != updates.automatic {
|
||||
updater.automaticallyChecksForUpdates = updates.automatic
|
||||
}
|
||||
try updater.start()
|
||||
started = true
|
||||
return true
|
||||
} catch {
|
||||
updates.transition(to: .failed, error: error.localizedDescription)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
private func check(download: ReleaseUpdates.Version? = nil) {
|
||||
guard prepare(), !updater.sessionInProgress else { return }
|
||||
requestedBuild = download?.build
|
||||
updates.transition(to: .checking)
|
||||
updater.checkForUpdates()
|
||||
}
|
||||
|
||||
func show(_ request: SPUUpdatePermissionRequest, reply: @escaping (SUUpdatePermissionResponse) -> Void) {
|
||||
reply(SUUpdatePermissionResponse(automaticUpdateChecks: updates.automatic, sendSystemProfile: false))
|
||||
}
|
||||
|
||||
func showUserInitiatedUpdateCheck(cancellation: @escaping () -> Void) {}
|
||||
|
||||
func showUpdateFound(with appcastItem: SUAppcastItem, state: SPUUserUpdateState,
|
||||
reply: @escaping (SPUUserUpdateChoice) -> Void) {
|
||||
guard state.userInitiated || updates.automatic else { reply(.dismiss); return }
|
||||
let version = ReleaseUpdates.Version(display: appcastItem.displayVersionString, build: appcastItem.versionString,
|
||||
infoURL: appcastItem.isInformationOnlyUpdate ? appcastItem.infoURL : nil)
|
||||
updates.found(version)
|
||||
if state.stage == .installing {
|
||||
updates.ready { reply(.install) }
|
||||
} else if state.userInitiated, requestedBuild == version.build, !appcastItem.isInformationOnlyUpdate {
|
||||
updates.transition(to: .downloading)
|
||||
reply(.install)
|
||||
} else {
|
||||
// Keep the badge, release the session so Sparkle can check again in six hours.
|
||||
reply(.dismiss)
|
||||
}
|
||||
}
|
||||
|
||||
func showUpdateReleaseNotes(with downloadData: SPUDownloadData) {}
|
||||
func showUpdateReleaseNotesFailedToDownloadWithError(_ error: Error) {}
|
||||
|
||||
func showUpdateNotFoundWithError(_ error: Error, acknowledgement: @escaping () -> Void) {
|
||||
updates.transition(to: .current)
|
||||
acknowledgement()
|
||||
}
|
||||
|
||||
func showUpdaterError(_ error: Error, acknowledgement: @escaping () -> Void) {
|
||||
updates.transition(to: .failed, error: error.localizedDescription)
|
||||
acknowledgement()
|
||||
}
|
||||
|
||||
func showDownloadInitiated(cancellation: @escaping () -> Void) {
|
||||
expectedBytes = 0
|
||||
receivedBytes = 0
|
||||
updates.transition(to: .downloading)
|
||||
}
|
||||
|
||||
func showDownloadDidReceiveExpectedContentLength(_ expectedContentLength: UInt64) {
|
||||
expectedBytes = expectedContentLength
|
||||
}
|
||||
|
||||
func showDownloadDidReceiveData(ofLength length: UInt64) {
|
||||
receivedBytes += length
|
||||
let fraction = expectedBytes > 0 ? min(1, Double(receivedBytes) / Double(expectedBytes)) : nil
|
||||
updates.transition(to: .downloading, fraction: fraction)
|
||||
}
|
||||
|
||||
func showDownloadDidStartExtractingUpdate() { updates.transition(to: .extracting) }
|
||||
func showExtractionReceivedProgress(_ progress: Double) { updates.transition(to: .extracting, fraction: progress) }
|
||||
|
||||
func showReady(toInstallAndRelaunch reply: @escaping (SPUUserUpdateChoice) -> Void) {
|
||||
updates.ready { reply(.install) }
|
||||
}
|
||||
|
||||
func showInstallingUpdate(withApplicationTerminated terminated: Bool, retryTerminatingApplication: @escaping () -> Void) {
|
||||
if !terminated { updates.ready(restart: retryTerminatingApplication) }
|
||||
}
|
||||
|
||||
func showUpdateInstalledAndRelaunched(_ relaunched: Bool, acknowledgement: @escaping () -> Void) {
|
||||
updates.transition(to: .current)
|
||||
acknowledgement()
|
||||
}
|
||||
|
||||
func dismissUpdateInstallation() {}
|
||||
|
||||
func updater(_ updater: SPUUpdater, didFinishUpdateCycleFor updateCheck: SPUUpdateCheck, error: Error?) {
|
||||
requestedBuild = nil
|
||||
guard let error = error as NSError? else { return }
|
||||
if error.domain == SUSparkleErrorDomain, error.code == SUError.noUpdateError.rawValue {
|
||||
updates.transition(to: .current)
|
||||
} else {
|
||||
updates.transition(to: .failed, error: error.localizedDescription)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,6 @@ struct MenuPanelView: View {
|
||||
let updates: ReleaseUpdates
|
||||
var presentation = PanelPresentation()
|
||||
var onOpenSettings: () -> Void = {}
|
||||
var onOpenAbout: () -> Void = {}
|
||||
var onPreferredHeight: (CGFloat) -> Void = { _ in }
|
||||
@Environment(\.locale) var locale
|
||||
@State private var panelSize = PanelLayout.contentSize(visible: NSScreen.main?.visibleFrame ?? CGRect(x: 0, y: 0, width: 1000, height: 760))
|
||||
@@ -137,7 +136,15 @@ struct MenuPanelView: View {
|
||||
.frame(width: 22, height: 22)
|
||||
HStack(alignment: .top, spacing: 3) {
|
||||
Text("Kymo").font(.system(size: 17, weight: .semibold))
|
||||
if let version = Self.version {
|
||||
if updates.version != nil {
|
||||
UpdateAction(phase: updates.phase, version: updates.version, compact: true, action: updates.performAction)
|
||||
.buttonStyle(.plain)
|
||||
.font(.system(size: 10, weight: .semibold))
|
||||
.foregroundStyle(Color.accentColor)
|
||||
.padding(.horizontal, 5).padding(.vertical, 2)
|
||||
.background(Color.accentColor.opacity(0.12), in: Capsule())
|
||||
.offset(y: -2)
|
||||
} else if let version = Self.version {
|
||||
Text(verbatim: "v" + version)
|
||||
.font(.system(size: 9, weight: .semibold)).monospacedDigit()
|
||||
.padding(.horizontal, 5).padding(.vertical, 1)
|
||||
@@ -206,13 +213,6 @@ struct MenuPanelView: View {
|
||||
}
|
||||
.help(metrics.isPaused ? Text("action.resume") : Text("action.pause"))
|
||||
.accessibilityLabel(metrics.isPaused ? Text("action.resume") : Text("action.pause"))
|
||||
if updates.available != nil {
|
||||
Button(action: onOpenAbout) {
|
||||
Image(systemName: "arrow.down.circle.fill").frame(width: 20, height: 20)
|
||||
.foregroundStyle(Color.accentColor)
|
||||
}
|
||||
.help(Text("updates.available")).accessibilityLabel(Text("updates.available"))
|
||||
}
|
||||
Button(action: onOpenSettings) {
|
||||
Image(systemName: "gearshape").frame(width: 20, height: 20)
|
||||
}
|
||||
|
||||
@@ -100,24 +100,22 @@ struct AppearanceSettingsView: View {
|
||||
Section {
|
||||
Toggle("updates.automatic", isOn: $updates.automatic)
|
||||
.toggleStyle(.checkbox)
|
||||
Text("updates.schedule").font(.callout).foregroundStyle(.secondary)
|
||||
HStack {
|
||||
Button("updates.check") { updates.check() }
|
||||
.disabled(updates.phase == .checking)
|
||||
switch updates.phase {
|
||||
case .idle: EmptyView()
|
||||
case .checking: Text("updates.checking").foregroundStyle(.secondary)
|
||||
case .failed: Text("updates.failed").foregroundStyle(.secondary)
|
||||
case .checked:
|
||||
if updates.available == nil { Text("updates.current").foregroundStyle(.secondary) }
|
||||
if let version = updates.version {
|
||||
Text("updates.new \(version.label)")
|
||||
} else if updates.phase == .current {
|
||||
Text("updates.current").foregroundStyle(.secondary)
|
||||
}
|
||||
Spacer()
|
||||
UpdateAction(phase: updates.phase, version: updates.version, action: updates.performAction)
|
||||
}
|
||||
if let release = updates.available {
|
||||
let display = release.build.map { "\(release.tagName) (\($0))" } ?? release.tagName
|
||||
LabeledContent {
|
||||
Link("updates.releasePage", destination: release.page)
|
||||
} label: {
|
||||
Text("updates.new \(display)")
|
||||
}
|
||||
if updates.phase == .downloading || updates.phase == .extracting {
|
||||
ProgressView(value: updates.fraction).progressViewStyle(.linear)
|
||||
}
|
||||
if let error = updates.errorMessage {
|
||||
Text(verbatim: error).font(.callout).foregroundStyle(.secondary)
|
||||
.fixedSize(horizontal: false, vertical: true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,6 +68,167 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"updates.schedule": {
|
||||
"extractionState": "manual",
|
||||
"localizations": {
|
||||
"en": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "Check at launch and every 6 hours while Kymo is running."
|
||||
}
|
||||
},
|
||||
"zh-Hans": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "开启后,每次启动和运行期间每 6 小时检查一次。"
|
||||
}
|
||||
},
|
||||
"zh-Hant": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "開啟後,每次啟動和執行期間每 6 小時檢查一次。"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"updates.download": {
|
||||
"extractionState": "manual",
|
||||
"localizations": {
|
||||
"en": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "Download update"
|
||||
}
|
||||
},
|
||||
"zh-Hans": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "下载更新"
|
||||
}
|
||||
},
|
||||
"zh-Hant": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "下載更新"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"updates.restart": {
|
||||
"extractionState": "manual",
|
||||
"localizations": {
|
||||
"en": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "Restart and update"
|
||||
}
|
||||
},
|
||||
"zh-Hans": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "重启并更新"
|
||||
}
|
||||
},
|
||||
"zh-Hant": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "重新啟動並更新"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"updates.downloading": {
|
||||
"extractionState": "manual",
|
||||
"localizations": {
|
||||
"en": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "Downloading…"
|
||||
}
|
||||
},
|
||||
"zh-Hans": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "正在下载…"
|
||||
}
|
||||
},
|
||||
"zh-Hant": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "正在下載…"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"updates.preparing": {
|
||||
"extractionState": "manual",
|
||||
"localizations": {
|
||||
"en": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "Preparing update…"
|
||||
}
|
||||
},
|
||||
"zh-Hans": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "正在准备更新…"
|
||||
}
|
||||
},
|
||||
"zh-Hant": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "正在準備更新…"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"updates.restarting": {
|
||||
"extractionState": "manual",
|
||||
"localizations": {
|
||||
"en": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "Restarting…"
|
||||
}
|
||||
},
|
||||
"zh-Hans": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "正在重启…"
|
||||
}
|
||||
},
|
||||
"zh-Hant": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "正在重新啟動…"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"updates.retry": {
|
||||
"extractionState": "manual",
|
||||
"localizations": {
|
||||
"en": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "Retry"
|
||||
}
|
||||
},
|
||||
"zh-Hans": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "重试"
|
||||
}
|
||||
},
|
||||
"zh-Hant": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "重試"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"updates.automatic": {
|
||||
"extractionState": "manual",
|
||||
"localizations": {
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import SwiftUI
|
||||
|
||||
/// The same single action is presented as a compact version badge or a settings button.
|
||||
struct UpdateAction: View {
|
||||
let phase: ReleaseUpdates.Phase
|
||||
let version: ReleaseUpdates.Version?
|
||||
var compact = false
|
||||
let action: () -> Void
|
||||
|
||||
var body: some View {
|
||||
if let url = version?.infoURL, phase == .available {
|
||||
Link(destination: url) { content("updates.releasePage", symbol: "arrow.up.right") }
|
||||
} else {
|
||||
Button(action: action) {
|
||||
switch phase {
|
||||
case .available: content("updates.download", symbol: "arrow.down.circle.fill")
|
||||
case .ready: content("updates.restart", symbol: "arrow.clockwise.circle.fill")
|
||||
case .checking: content("updates.checking", symbol: "magnifyingglass")
|
||||
case .downloading: content("updates.downloading", symbol: "arrow.down")
|
||||
case .extracting: content("updates.preparing", symbol: "shippingbox")
|
||||
case .restarting: content("updates.restarting", symbol: "arrow.clockwise")
|
||||
case .failed: content("updates.retry", symbol: "arrow.clockwise")
|
||||
case .idle, .current: content("updates.check", symbol: "arrow.clockwise")
|
||||
}
|
||||
}
|
||||
.disabled([.checking, .downloading, .extracting, .restarting].contains(phase))
|
||||
}
|
||||
}
|
||||
|
||||
private func content(_ title: LocalizedStringKey, symbol: String) -> some View {
|
||||
HStack(spacing: 3) {
|
||||
Image(systemName: symbol)
|
||||
if compact, let version {
|
||||
Text(verbatim: "v" + version.display)
|
||||
} else {
|
||||
Text(title)
|
||||
}
|
||||
}
|
||||
.accessibilityElement(children: .ignore)
|
||||
.accessibilityLabel(Text(title))
|
||||
.accessibilityValue(Text(verbatim: version?.label ?? ""))
|
||||
.help(Text(title))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user