fix: show sidebar update banner from background checks (#1543)
This commit is contained in:
parent
3b507d361f
commit
971b2b4e77
7 changed files with 167 additions and 20 deletions
|
|
@ -27,10 +27,10 @@ class UpdateController {
|
|||
}
|
||||
|
||||
init() {
|
||||
// Default to manual update checks. This also prevents Sparkle from prompting at startup.
|
||||
// cmux checks for updates in the background, but keeps automatic download and
|
||||
// profile submission disabled so all install intent stays user-driven.
|
||||
let defaults = UserDefaults.standard
|
||||
defaults.register(defaults: [
|
||||
"SUEnableAutomaticChecks": false,
|
||||
"SUSendProfileInfo": false,
|
||||
"SUAutomaticallyUpdate": false,
|
||||
])
|
||||
|
|
@ -59,8 +59,8 @@ class UpdateController {
|
|||
guard !didStartUpdater else { return }
|
||||
ensureSparkleInstallationCache()
|
||||
#if DEBUG
|
||||
// UI tests need to exercise Sparkle's permission request deterministically.
|
||||
// Clearing these defaults causes Sparkle to re-request permission on next start.
|
||||
// Keep the permission-related defaults resettable for UI tests even though the
|
||||
// delegate now suppresses Sparkle's permission UI entirely.
|
||||
if ProcessInfo.processInfo.environment["CMUX_UI_TEST_RESET_SPARKLE_PERMISSION"] == "1" {
|
||||
let defaults = UserDefaults.standard
|
||||
defaults.removeObject(forKey: "SUEnableAutomaticChecks")
|
||||
|
|
@ -71,13 +71,9 @@ class UpdateController {
|
|||
}
|
||||
#endif
|
||||
do {
|
||||
// cmux never enables automatic update checks; we rely on the in-app update pill.
|
||||
// Sparkle reads these from defaults, but set them explicitly before starting.
|
||||
let defaults = UserDefaults.standard
|
||||
defaults.set(false, forKey: "SUEnableAutomaticChecks")
|
||||
defaults.set(false, forKey: "SUSendProfileInfo")
|
||||
defaults.set(false, forKey: "SUAutomaticallyUpdate")
|
||||
|
||||
updater.automaticallyChecksForUpdates = true
|
||||
updater.automaticallyDownloadsUpdates = false
|
||||
updater.sendsSystemProfile = false
|
||||
try updater.start()
|
||||
didStartUpdater = true
|
||||
} catch {
|
||||
|
|
@ -201,7 +197,7 @@ class UpdateController {
|
|||
/// Validate the check for updates menu item.
|
||||
func validateMenuItem(_ item: NSMenuItem) -> Bool {
|
||||
if item.action == #selector(checkForUpdates) {
|
||||
// Always allow user-initiated checks; we start Sparkle lazily on first use.
|
||||
// Always allow user-initiated checks; Sparkle can safely surface current progress.
|
||||
return true
|
||||
}
|
||||
return true
|
||||
|
|
|
|||
|
|
@ -13,6 +13,10 @@ enum UpdateFeedResolver {
|
|||
}
|
||||
|
||||
extension UpdateDriver: SPUUpdaterDelegate {
|
||||
func updaterShouldPromptForPermissionToCheck(forUpdates _: SPUUpdater) -> Bool {
|
||||
false
|
||||
}
|
||||
|
||||
func feedURLString(for updater: SPUUpdater) -> String? {
|
||||
#if DEBUG
|
||||
let env = ProcessInfo.processInfo.environment
|
||||
|
|
@ -35,6 +39,7 @@ extension UpdateDriver: SPUUpdaterDelegate {
|
|||
/// Called when an update is scheduled to install silently,
|
||||
/// which occurs when automatic download is enabled.
|
||||
func updater(_ updater: SPUUpdater, willInstallUpdateOnQuit item: SUAppcastItem, immediateInstallationBlock immediateInstallHandler: @escaping () -> Void) -> Bool {
|
||||
viewModel.clearDetectedUpdate()
|
||||
viewModel.state = .installing(.init(
|
||||
isAutoUpdate: true,
|
||||
retryTerminatingApplication: immediateInstallHandler,
|
||||
|
|
@ -56,6 +61,7 @@ extension UpdateDriver: SPUUpdaterDelegate {
|
|||
}
|
||||
|
||||
func updater(_ updater: SPUUpdater, didFindValidUpdate item: SUAppcastItem) {
|
||||
viewModel.recordDetectedUpdate(item)
|
||||
let version = item.displayVersionString
|
||||
let fileURL = item.fileURL?.absoluteString ?? ""
|
||||
if fileURL.isEmpty {
|
||||
|
|
@ -66,6 +72,7 @@ extension UpdateDriver: SPUUpdaterDelegate {
|
|||
}
|
||||
|
||||
func updaterDidNotFindUpdate(_ updater: SPUUpdater, error: Error) {
|
||||
viewModel.clearDetectedUpdate()
|
||||
let nsError = error as NSError
|
||||
let reasonValue = (nsError.userInfo[SPUNoUpdateFoundReasonKey] as? NSNumber)?.intValue
|
||||
let reason = reasonValue.map { SPUNoUpdateFoundReason(rawValue: OSStatus($0)) } ?? nil
|
||||
|
|
@ -80,13 +87,18 @@ extension UpdateDriver: SPUUpdaterDelegate {
|
|||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
func updater(_ updater: SPUUpdater, userDidMake _: SPUUserUpdateChoice, forUpdate _: SUAppcastItem, state _: SPUUserUpdateState) {
|
||||
viewModel.clearDetectedUpdate()
|
||||
}
|
||||
|
||||
func updaterWillRelaunchApplication(_ updater: SPUUpdater) {
|
||||
AppDelegate.shared?.persistSessionForUpdateRelaunch()
|
||||
TerminalController.shared.stop()
|
||||
NSApp.invalidateRestorableState()
|
||||
for window in NSApp.windows {
|
||||
window.invalidateRestorableState()
|
||||
Task { @MainActor in
|
||||
AppDelegate.shared?.persistSessionForUpdateRelaunch()
|
||||
TerminalController.shared.stop()
|
||||
NSApp.invalidateRestorableState()
|
||||
for window in NSApp.windows {
|
||||
window.invalidateRestorableState()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,14 @@ enum UpdateTestSupport {
|
|||
static func applyIfNeeded(to viewModel: UpdateViewModel) {
|
||||
let env = ProcessInfo.processInfo.environment
|
||||
guard env["CMUX_UI_TEST_MODE"] == "1" else { return }
|
||||
|
||||
if let detectedVersion = env["CMUX_UI_TEST_DETECTED_UPDATE_VERSION"],
|
||||
!detectedVersion.isEmpty {
|
||||
DispatchQueue.main.async {
|
||||
viewModel.detectedUpdateVersion = UpdateViewModel.normalizedDetectedUpdateVersion(from: detectedVersion)
|
||||
}
|
||||
}
|
||||
|
||||
guard let state = env["CMUX_UI_TEST_UPDATE_STATE"] else { return }
|
||||
|
||||
DispatchQueue.main.async {
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import Sparkle
|
|||
class UpdateViewModel: ObservableObject {
|
||||
@Published var state: UpdateState = .idle
|
||||
@Published var overrideState: UpdateState?
|
||||
@Published var detectedUpdateVersion: String?
|
||||
#if DEBUG
|
||||
@Published var debugOverrideText: String?
|
||||
#endif
|
||||
|
|
@ -14,6 +15,14 @@ class UpdateViewModel: ObservableObject {
|
|||
overrideState ?? state
|
||||
}
|
||||
|
||||
func recordDetectedUpdate(_ item: SUAppcastItem) {
|
||||
detectedUpdateVersion = Self.normalizedDetectedUpdateVersion(from: item.displayVersionString)
|
||||
}
|
||||
|
||||
func clearDetectedUpdate() {
|
||||
detectedUpdateVersion = nil
|
||||
}
|
||||
|
||||
var text: String {
|
||||
#if DEBUG
|
||||
if let debugOverrideText { return debugOverrideText }
|
||||
|
|
@ -334,6 +343,11 @@ class UpdateViewModel: ObservableObject {
|
|||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
static func normalizedDetectedUpdateVersion(from version: String) -> String? {
|
||||
let trimmed = version.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
return trimmed.isEmpty ? nil : trimmed
|
||||
}
|
||||
}
|
||||
|
||||
enum UpdateState: Equatable {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue