| 1 | import SwiftUI |
| 2 | import Sparkle |
| 3 | |
| 4 | // Sparkle's own documented SwiftUI idiom for a menu item that's disabled while |
| 5 | // a check is already in flight (canCheckForUpdates), rather than a plain |
| 6 | // always-enabled button. |
| 7 | final class CheckForUpdatesViewModel: ObservableObject { |
| 8 | @Published var canCheckForUpdates = false |
| 9 | |
| 10 | init(updater: SPUUpdater) { |
| 11 | updater.publisher(for: \.canCheckForUpdates).assign(to: &$canCheckForUpdates) |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | struct CheckForUpdatesView: View { |
| 16 | @ObservedObject private var model: CheckForUpdatesViewModel |
| 17 | private let updater: SPUUpdater |
| 18 | |
| 19 | init(updater: SPUUpdater) { |
| 20 | self.updater = updater |
| 21 | self.model = CheckForUpdatesViewModel(updater: updater) |
| 22 | } |
| 23 | |
| 24 | var body: some View { |
| 25 | Button("Check for Updates…", action: updater.checkForUpdates) |
| 26 | .disabled(!model.canCheckForUpdates) |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // The Settings window's General pane: a single on/off switch for background |
| 31 | // updates (rather than separate "check" and "download" toggles) — "Check for |
| 32 | // Updates…" in the app menu always works for a manual kick-off regardless of |
| 33 | // this setting. |
| 34 | // The Updates pane of Settings. (Formerly misnamed GeneralSettingsView — the |
| 35 | // General pane is MacSettingsView.) |
| 36 | struct UpdatesSettingsView: View { |
| 37 | let updater: SPUUpdater |
| 38 | @State private var automaticUpdates: Bool |
| 39 | |
| 40 | init(updater: SPUUpdater) { |
| 41 | self.updater = updater |
| 42 | _automaticUpdates = State(initialValue: updater.automaticallyDownloadsUpdates) |
| 43 | } |
| 44 | |
| 45 | var body: some View { |
| 46 | // A bare checkbox + clickable label, per Mac preference conventions — |
| 47 | // a grouped-form box around a single control is chrome with nothing |
| 48 | // to group. (The General pane keeps .grouped for now.) The TabView in |
| 49 | // LChatMacApp owns the Settings window's one fixed frame, so no size |
| 50 | // is pinned here. |
| 51 | Toggle("Automatically download and install updates", isOn: $automaticUpdates) |
| 52 | .toggleStyle(.checkbox) // Settings-window convention — see MacSettingsView |
| 53 | .onChange(of: automaticUpdates) { _, enabled in |
| 54 | // Downloading automatically requires checking automatically; |
| 55 | // turning the checkbox off falls back to fully manual |
| 56 | // (menu-triggered) checks only. |
| 57 | updater.automaticallyChecksForUpdates = enabled |
| 58 | updater.automaticallyDownloadsUpdates = enabled |
| 59 | } |
| 60 | .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) |
| 61 | .padding(20) |
| 62 | } |
| 63 | } |
| 64 | |