From 5dda3b423b950d49b0a1ce4d1d939a7494a19a89 Mon Sep 17 00:00:00 2001 From: Michel Storms Date: Sat, 5 Oct 2024 09:43:54 +0200 Subject: [PATCH] - add comments for unregisterAll() - relocate unregisterAll() to below public static func reset() --- .../KeyboardShortcuts/KeyboardShortcuts.swift | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/Sources/KeyboardShortcuts/KeyboardShortcuts.swift b/Sources/KeyboardShortcuts/KeyboardShortcuts.swift index 395a467..3f8aa4e 100644 --- a/Sources/KeyboardShortcuts/KeyboardShortcuts.swift +++ b/Sources/KeyboardShortcuts/KeyboardShortcuts.swift @@ -127,18 +127,6 @@ public enum KeyboardShortcuts { unregisterIfNeeded(shortcut) } - public static func unregisterAll() { - CarbonKeyboardShortcuts.unregisterAll() - registeredShortcuts.removeAll() - - // remove user defaults too - let userDefaults = UserDefaults.standard - - for key in userDefaults.dictionaryRepresentation().keys where key.hasPrefix("KeyboardShortcuts_") { - userDefaults.removeObject(forKey: key) - } - } - static func initialize() { guard !isInitialized else { return @@ -246,7 +234,7 @@ public enum KeyboardShortcuts { setShortcut(name.defaultShortcut, for: name) } } - + /** Reset the keyboard shortcut for one or more names. @@ -275,6 +263,29 @@ public enum KeyboardShortcuts { reset(names) } + /** + Unregister all keyboard shortcuts and remove their stored values from `UserDefaults`. + + This method unregisters all keyboard shortcuts registered via `KeyboardShortcuts` and removes any associated data stored in `UserDefaults`. + + Use this method to completely reset the state of all keyboard shortcuts in your app, including removing any saved user-defined shortcuts and default shortcuts. + + - Note: This action cannot be undone. All shortcuts will be removed, including any default shortcuts defined in `Name`. If you want to reset shortcuts back to their default values, consider using `.reset(_:)` instead. + + - Important: This method affects all shortcuts registered with `KeyboardShortcuts` and should be used with caution. + */ + public static func unregisterAll() { + CarbonKeyboardShortcuts.unregisterAll() + registeredShortcuts.removeAll() + + // remove user defaults too + let userDefaults = UserDefaults.standard + + for key in userDefaults.dictionaryRepresentation().keys where key.hasPrefix("KeyboardShortcuts_") { + userDefaults.removeObject(forKey: key) + } + } + /** Set the keyboard shortcut for a name.