Commit 34152a1a3b
Verified · cmc
Layout: unified · split
Sources/Orgstar/OrgstarApp.swift +15 −1
| @@ -162,9 +162,11 @@ struct PromptRequest: Identifiable { | |||
| 162 | final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDelegate { | 162 | final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDelegate { |
| 163 | var session: DocumentSession? | 163 | var session: DocumentSession? |
| 164 | var commands: AppCommands? | 164 | var commands: AppCommands? |
| 165 | let updates = Updates() | ||
| 165 | 166 | ||
| 166 | func applicationDidFinishLaunching(_ notification: Notification) { | 167 | func applicationDidFinishLaunching(_ notification: Notification) { |
| 167 | if SystemReminderCenter.isAvailable { UNUserNotificationCenter.current().delegate = self } | 168 | if SystemReminderCenter.isAvailable { UNUserNotificationCenter.current().delegate = self } |
| 169 | updates.start() | ||
| 168 | } | 170 | } |
| 169 | 171 | ||
| 170 | /// `:session` interpreters end with the app. | 172 | /// `:session` interpreters end with the app. |
| @@ -462,9 +464,12 @@ struct OrgstarApp: App { | |||
| 462 | Button("Edit Config File") { commands.jump(path: UserConfig.configFile.path, offset: 0) } | 464 | Button("Edit Config File") { commands.jump(path: UserConfig.configFile.path, offset: 0) } |
| 463 | .keyboardShortcut(",", modifiers: [.command, .option]) | 465 | .keyboardShortcut(",", modifiers: [.command, .option]) |
| 464 | } | 466 | } |
| 467 | CommandGroup(after: .appInfo) { | ||
| 468 | CheckForUpdatesItem(updates: delegate.updates) | ||
| 469 | } | ||
| 465 | } | 470 | } |
| 466 | Settings { | 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 | struct SettingsView: View { | 542 | struct SettingsView: View { |
| 538 | let commands: AppCommands | 543 | let commands: AppCommands |
| 539 | let workspace: WorkspaceModel? | 544 | let workspace: WorkspaceModel? |
| 545 | let updates: Updates | ||
| 540 | @State private var importing = false | 546 | @State private var importing = false |
| 541 | @Environment(\.openWindow) private var openWindow | 547 | @Environment(\.openWindow) private var openWindow |
| 542 | @AppStorage("saveMode") private var saveMode = SaveMode.automatic | 548 | @AppStorage("saveMode") private var saveMode = SaveMode.automatic |
| @@ -612,6 +618,14 @@ struct SettingsView: View { | |||
| 612 | 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.") | 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 | .settingsFooter() | 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 | Section { | 629 | Section { |
| 616 | Picker("Save files", selection: $saveMode) { | 630 | Picker("Save files", selection: $saveMode) { |
| 617 | Text("Automatically, when typing stops").tag(SaveMode.automatic) | 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 | } | ||