Commit 34152a1a3b
Verified · cmc
Layout: unified · split
Sources/Orgstar/OrgstarApp.swift +15 −1
| @@ -162,9 +162,11 @@ struct PromptRequest: Identifiable { | ||
| 162 | 162 | final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDelegate { |
| 163 | 163 | var session: DocumentSession? |
| 164 | 164 | var commands: AppCommands? |
| 165 | let updates = Updates() | |
| 165 | 166 | |
| 166 | 167 | func applicationDidFinishLaunching(_ notification: Notification) { |
| 167 | 168 | if SystemReminderCenter.isAvailable { UNUserNotificationCenter.current().delegate = self } |
| 169 | updates.start() | |
| 168 | 170 | } |
| 169 | 171 | |
| 170 | 172 | /// `:session` interpreters end with the app. |
| @@ -462,9 +464,12 @@ struct OrgstarApp: App { | ||
| 462 | 464 | Button("Edit Config File") { commands.jump(path: UserConfig.configFile.path, offset: 0) } |
| 463 | 465 | .keyboardShortcut(",", modifiers: [.command, .option]) |
| 464 | 466 | } |
| 467 | CommandGroup(after: .appInfo) { | |
| 468 | CheckForUpdatesItem(updates: delegate.updates) | |
| 469 | } | |
| 465 | 470 | } |
| 466 | 471 | Settings { |
| 467 | SettingsView(commands: commands, workspace: try? workspace.get()) | |
| 472 | SettingsView(commands: commands, workspace: try? workspace.get(), updates: delegate.updates) | |
| 468 | 473 | } |
| 469 | 474 | } |
| 470 | 475 | } |
| @@ -537,6 +542,7 @@ struct AgendaMenuItem: View { | ||
| 537 | 542 | struct SettingsView: View { |
| 538 | 543 | let commands: AppCommands |
| 539 | 544 | let workspace: WorkspaceModel? |
| 545 | let updates: Updates | |
| 540 | 546 | @State private var importing = false |
| 541 | 547 | @Environment(\.openWindow) private var openWindow |
| 542 | 548 | @AppStorage("saveMode") private var saveMode = SaveMode.automatic |
| @@ -612,6 +618,14 @@ struct SettingsView: View { | ||
| 612 | 618 | Text("Every setting here is also in \(UserConfig.configFile.path), which you can edit, share or keep with your dotfiles. Changes in either place show up in the other.") |
| 613 | 619 | .settingsFooter() |
| 614 | 620 | } |
| 621 | if updates.canCheck { | |
| 622 | Section { | |
| 623 | Toggle("Check for updates automatically", isOn: Binding(get: { updates.automatically }, set: { updates.automatically = $0 })) | |
| 624 | } footer: { | |
| 625 | Text("Once a day, from the app's own feed. orgstar ▸ Check for Updates… checks now.") | |
| 626 | .settingsFooter() | |
| 627 | } | |
| 628 | } | |
| 615 | 629 | Section { |
| 616 | 630 | Picker("Save files", selection: $saveMode) { |
| 617 | 631 | Text("Automatically, when typing stops").tag(SaveMode.automatic) |
Sources/Orgstar/Updates.swift added +42
| @@ -0,0 +1,42 @@ | ||
| 1 | import Combine | |
| 2 | import Sparkle | |
| 3 | import SwiftUI | |
| 4 | ||
| 5 | /// Sparkle, for the signed builds: the Info.plist names the appcast and the key the DMGs are | |
| 6 | /// signed with. A dev build has neither, so the updater doesn't start and the menu item stays | |
| 7 | /// disabled. Whether to check automatically is Sparkle's own setting, asked once on the | |
| 8 | /// second launch. | |
| 9 | @MainActor | |
| 10 | @Observable | |
| 11 | final class Updates { | |
| 12 | private(set) var canCheck = false | |
| 13 | @ObservationIgnored private var controller: SPUStandardUpdaterController? | |
| 14 | @ObservationIgnored private var observation: AnyCancellable? | |
| 15 | ||
| 16 | func start() { | |
| 17 | guard controller == nil, Bundle.main.object(forInfoDictionaryKey: "SUFeedURL") != nil else { return } | |
| 18 | let controller = SPUStandardUpdaterController(startingUpdater: true, updaterDelegate: nil, userDriverDelegate: nil) | |
| 19 | self.controller = controller | |
| 20 | observation = controller.updater.publisher(for: \.canCheckForUpdates).sink { [weak self] can in | |
| 21 | MainActor.assumeIsolated { self?.canCheck = can } | |
| 22 | } | |
| 23 | } | |
| 24 | ||
| 25 | func check() { | |
| 26 | controller?.updater.checkForUpdates() | |
| 27 | } | |
| 28 | ||
| 29 | var automatically: Bool { | |
| 30 | get { controller?.updater.automaticallyChecksForUpdates ?? false } | |
| 31 | set { controller?.updater.automaticallyChecksForUpdates = newValue } | |
| 32 | } | |
| 33 | } | |
| 34 | ||
| 35 | struct CheckForUpdatesItem: View { | |
| 36 | let updates: Updates | |
| 37 | ||
| 38 | var body: some View { | |
| 39 | Button("Check for Updates…") { updates.check() } | |
| 40 | .disabled(!updates.canCheck) | |
| 41 | } | |
| 42 | } | |