Skip to content

Commit

Permalink
fix: auto-update preferences sync with os from launch
Browse files Browse the repository at this point in the history
  • Loading branch information
lwouis committed Mar 10, 2020
1 parent f950083 commit b3fb222
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
9 changes: 4 additions & 5 deletions src/ui/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class App: NSApplication, NSApplicationDelegate {

func applicationDidFinishLaunching(_ aNotification: Notification) {
#if !DEBUG
PFMoveToApplicationsFolderIfNecessary()
PFMoveToApplicationsFolderIfNecessary()
#endif
SystemPermissions.ensureAccessibilityCheckboxIsChecked()
SystemPermissions.ensureScreenRecordingCheckboxIsChecked()
Expand All @@ -39,14 +39,16 @@ class App: NSApplication, NSApplicationDelegate {
Spaces.initialDiscovery()
Applications.initialDiscovery()
Keyboard.listenToGlobalEvents(self)
preferencesWindow = PreferencesWindow()
UpdatesTab.observeUserDefaults()
}

// keyboard shortcuts are broken without a menu. We generated the default menu from XCode and load it
// see https://stackoverflow.com/a/3746058/2249756
private func loadMainMenuXib() {
var menuObjects: NSArray?
Bundle.main.loadNibNamed("MainMenu", owner: self, topLevelObjects: &menuObjects)
menu = menuObjects?.first(where: {$0 is NSMenu }) as? NSMenu
menu = menuObjects?.first(where: { $0 is NSMenu }) as? NSMenu
}

// we put application code here which should be executed on init() and Preferences change
Expand Down Expand Up @@ -77,9 +79,6 @@ class App: NSApplication, NSApplicationDelegate {

@objc
func showPreferencesPanel() {
if preferencesWindow == nil {
preferencesWindow = PreferencesWindow()
}
Screen.repositionPanel(preferencesWindow!, Screen.preferred(), .appleCentered)
preferencesWindow?.show()
}
Expand Down
13 changes: 6 additions & 7 deletions src/ui/preferences-window/tabs/UpdatesTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ class UpdatesTab: NSObject {
return TabViewItem.make(NSLocalizedString("Updates", comment: ""), NSImage.refreshTemplateName, makeView())
}

static func makeView() -> NSGridView {
static func observeUserDefaults() {
UserDefaults.standard.addObserver(UpdatesTab.policyObserver, forKeyPath: "SUAutomaticallyUpdate", options: [.initial, .new], context: nil)
UserDefaults.standard.addObserver(UpdatesTab.policyObserver, forKeyPath: "SUEnableAutomaticChecks", options: [.initial, .new], context: nil)
}

static private func makeView() -> NSGridView {
dontPeriodicallyCheck = NSButton(radioButtonWithTitle: NSLocalizedString("Don't check for updates periodically", comment: ""), target: self, action: #selector(updatePolicyCallback))
dontPeriodicallyCheck.fit()
periodicallyCheck = NSButton(radioButtonWithTitle: NSLocalizedString("Check for updates periodically", comment: ""), target: self, action: #selector(updatePolicyCallback))
Expand All @@ -25,7 +30,6 @@ class UpdatesTab: NSObject {
policies.alignment = .left
policies.orientation = .vertical
policies.spacing = GridView.interPadding / 2
observePolicy()
let view = GridView.make([
[policyLabel, policies],
[NSButton(title: NSLocalizedString("Check for updates now…", comment: ""), target: self, action: #selector(checkForUpdatesNow))],
Expand All @@ -39,11 +43,6 @@ class UpdatesTab: NSObject {
return view
}

private static func observePolicy() {
UserDefaults.standard.addObserver(UpdatesTab.policyObserver, forKeyPath: "SUAutomaticallyUpdate", options: [.initial, .new], context: nil)
UserDefaults.standard.addObserver(UpdatesTab.policyObserver, forKeyPath: "SUEnableAutomaticChecks", options: [.initial, .new], context: nil)
}

@objc
static func checkForUpdatesNow(_ sender: Any) {
SUUpdater.shared().checkForUpdates(sender)
Expand Down

0 comments on commit b3fb222

Please sign in to comment.