From 82e9ca93b615efb1cf27482db1ccd71bcd4d79c8 Mon Sep 17 00:00:00 2001 From: Louis Pontoise Date: Sat, 28 May 2022 19:12:50 +0900 Subject: [PATCH] feat: add a button to reset the preferences (#1275) --- alt-tab-macos.xcodeproj/project.pbxproj | 4 ++++ src/logic/Preferences.swift | 6 +++++- src/ui/generic-components/Button.swift | 10 ++++++++++ src/ui/permission-window/PermissionView.swift | 5 +---- src/ui/preferences-window/tabs/GeneralTab.swift | 9 ++++++++- 5 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 src/ui/generic-components/Button.swift diff --git a/alt-tab-macos.xcodeproj/project.pbxproj b/alt-tab-macos.xcodeproj/project.pbxproj index 291d5154f..ac8681249 100644 --- a/alt-tab-macos.xcodeproj/project.pbxproj +++ b/alt-tab-macos.xcodeproj/project.pbxproj @@ -37,6 +37,7 @@ BF0C88D7E209C788558BD943 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = BF0C8CC9692E6E4A32846D72 /* InfoPlist.strings */; }; BF0C894199BB262BDB2D8D37 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = BF0C8221101CD179DB1121D5 /* InfoPlist.strings */; }; BF0C894325A54CF0B6B53464 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = BF0C8B8B31D2F421AC258351 /* Localizable.strings */; }; + BF0C898511686611E4D7D81E /* Button.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF0C8B2C057099EDABE46779 /* Button.swift */; }; BF0C89C49CFBAAB4646BB8CB /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = BF0C82A8848224E76B880A83 /* InfoPlist.strings */; }; BF0C8A739A74695E60F16369 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = BF0C8062E78677398A4A217B /* InfoPlist.strings */; }; BF0C8A95AAD9DADC95887A2D /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = BF0C895A9AF79A869EE6B108 /* Localizable.strings */; }; @@ -203,6 +204,7 @@ BF0C89A541FBEA23E7CB1D94 /* lb */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = lb; path = Localizable.strings; sourceTree = ""; }; BF0C8A50151A4EF88E494357 /* da */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = da; path = InfoPlist.strings; sourceTree = ""; }; BF0C8B15FFB262ECC54B5AA4 /* sl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = sl; path = InfoPlist.strings; sourceTree = ""; }; + BF0C8B2C057099EDABE46779 /* Button.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Button.swift; sourceTree = ""; }; BF0C8B2EA9269BA0B79A525F /* sk */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = sk; path = Localizable.strings; sourceTree = ""; }; BF0C8BA452332236D972C60E /* ATShortcut.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ATShortcut.swift; sourceTree = ""; }; BF0C8C4E74DCA631A049492A /* preferences-policies.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = "preferences-policies.jpg"; sourceTree = ""; }; @@ -799,6 +801,7 @@ D04BAE333F7170E87C5AC0EF /* CustomRecorderControl.swift */, D04BA8AC77465E1B2BC947CF /* StackView.swift */, BF0C8DF400A1AF64EB0CD0B3 /* TrafficLightButton.swift */, + BF0C8B2C057099EDABE46779 /* Button.swift */, ); path = "generic-components"; sourceTree = ""; @@ -1585,6 +1588,7 @@ BF0C855CF8BA047E180425F9 /* KeyRepeatTimer.swift in Sources */, BF0C8CC5057406014FD612CC /* ATShortcut.swift in Sources */, BF0C8C7E96DB48120462DA00 /* TrafficLightButton.swift in Sources */, + BF0C898511686611E4D7D81E /* Button.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/src/logic/Preferences.swift b/src/logic/Preferences.swift index c71bde7b3..87095c6d4 100644 --- a/src/logic/Preferences.swift +++ b/src/logic/Preferences.swift @@ -148,6 +148,10 @@ class Preferences { } } + static func resetAll() { + defaults.removePersistentDomain(forName: App.id) + } + static func registerDefaults() { defaults.register(defaults: defaultValues) } @@ -161,7 +165,7 @@ class Preferences { UserDefaults.cache.removeValue(forKey: key) } - static var all: [String: Any] { defaults.persistentDomain(forName: NSRunningApplication.current.bundleIdentifier!)! } + static var all: [String: Any] { defaults.persistentDomain(forName: App.id)! } static func migratePreferences() { let preferencesKey = "preferencesVersion" diff --git a/src/ui/generic-components/Button.swift b/src/ui/generic-components/Button.swift new file mode 100644 index 000000000..1eaeba3c4 --- /dev/null +++ b/src/ui/generic-components/Button.swift @@ -0,0 +1,10 @@ +import Cocoa + +class Button: NSButton { + convenience init(_ title: String, _ action: ActionClosure?) { + self.init(title: title, target: nil, action: nil) + onAction = action + translatesAutoresizingMaskIntoConstraints = false + fit() + } +} diff --git a/src/ui/permission-window/PermissionView.swift b/src/ui/permission-window/PermissionView.swift index 24008e2f2..332933432 100644 --- a/src/ui/permission-window/PermissionView.swift +++ b/src/ui/permission-window/PermissionView.swift @@ -25,10 +25,7 @@ class PermissionView: StackView { justification.translatesAutoresizingMaskIntoConstraints = false justification.preferredMaxLayoutWidth = 500 justification.widthAnchor.constraint(equalToConstant: justification.fittingSize.width + 5).isActive = true - let button = NSButton(title: buttonText, target: nil, action: nil) - button.translatesAutoresizingMaskIntoConstraints = false - button.onAction = { _ in NSWorkspace.shared.open(URL(string: buttonUrl)!) } - button.fit() + let button = Button(buttonText) { _ in NSWorkspace.shared.open(URL(string: buttonUrl)!) } let status = NSTextField(wrappingLabelWithString: "") status.translatesAutoresizingMaskIntoConstraints = false let buttonStack = NSStackView(views: [button, status]) diff --git a/src/ui/preferences-window/tabs/GeneralTab.swift b/src/ui/preferences-window/tabs/GeneralTab.swift index 8209c4f7e..f0ac21342 100644 --- a/src/ui/preferences-window/tabs/GeneralTab.swift +++ b/src/ui/preferences-window/tabs/GeneralTab.swift @@ -4,6 +4,8 @@ class GeneralTab { static func initTab() -> NSView { let startAtLogin = LabelAndControl.makeLabelWithCheckbox(NSLocalizedString("Start at login:", comment: ""), "startAtLogin", extraAction: startAtLoginCallback) let menubarIcon = LabelAndControl.makeLabelWithDropdown(NSLocalizedString("Menubar icon:", comment: ""), "menubarIcon", MenubarIconPreference.allCases, extraAction: Menubar.menubarIconCallback) + let resetPreferences = Button(NSLocalizedString("Reset preferences and restart", comment: "")) { _ in GeneralTab.resetPreferences() } + if #available(OSX 11, *) { resetPreferences.hasDestructiveAction = true } let menubarIconDropdown = menubarIcon[1] as! NSPopUpButton for i in 0...2 { let image = NSImage.initCopy("menubar-" + String(i + 1)) @@ -25,7 +27,12 @@ class GeneralTab { startAtLoginCallback(startAtLogin[1] as! NSControl) - return grid + return StackView([grid, resetPreferences], .vertical, bottom: GridView.padding) + } + + static func resetPreferences() { + Preferences.resetAll() + App.app.restart() } // add/remove plist file in ~/Library/LaunchAgents/ depending on the checkbox state