From d2fc65f9c3322d3e9d5ba8c510e204be2a134578 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Mon, 5 Sep 2022 14:00:54 +0700 Subject: [PATCH] Minor tweaks --- .../xcschemes/KeyboardShortcuts.xcscheme | 2 +- .../project.pbxproj | 10 ++++-- .../KeyboardShortcutsExample/Utilities.swift | 4 +-- Package.swift | 4 +-- .../CarbonKeyboardShortcuts.swift | 2 +- .../KeyboardShortcuts/KeyboardShortcuts.swift | 8 ++--- .../Localization/en.lproj/Localizable.strings | 2 +- Sources/KeyboardShortcuts/NSMenuItem++.swift | 4 +-- Sources/KeyboardShortcuts/Name.swift | 2 +- Sources/KeyboardShortcuts/Recorder.swift | 27 ++++++++++----- Sources/KeyboardShortcuts/RecorderCocoa.swift | 10 +++--- Sources/KeyboardShortcuts/Shortcut.swift | 4 +-- Sources/KeyboardShortcuts/Utilities.swift | 8 ++--- readme.md | 33 ++----------------- 14 files changed, 53 insertions(+), 67 deletions(-) diff --git a/.swiftpm/xcode/xcshareddata/xcschemes/KeyboardShortcuts.xcscheme b/.swiftpm/xcode/xcshareddata/xcschemes/KeyboardShortcuts.xcscheme index e65d815..8c33c55 100644 --- a/.swiftpm/xcode/xcshareddata/xcschemes/KeyboardShortcuts.xcscheme +++ b/.swiftpm/xcode/xcshareddata/xcschemes/KeyboardShortcuts.xcscheme @@ -1,6 +1,6 @@ OSStatus { - guard let event = event else { + guard let event else { return OSStatus(eventNotHandledErr) } diff --git a/Sources/KeyboardShortcuts/KeyboardShortcuts.swift b/Sources/KeyboardShortcuts/KeyboardShortcuts.swift index b4b00fd..8eb8a6d 100644 --- a/Sources/KeyboardShortcuts/KeyboardShortcuts.swift +++ b/Sources/KeyboardShortcuts/KeyboardShortcuts.swift @@ -146,7 +146,7 @@ public enum KeyboardShortcuts { import SwiftUI import KeyboardShortcuts - struct PreferencesView: View { + struct SettingsScreen: View { var body: some View { VStack { // … @@ -176,7 +176,7 @@ public enum KeyboardShortcuts { import SwiftUI import KeyboardShortcuts - struct PreferencesView: View { + struct SettingsScreen: View { var body: some View { VStack { // … @@ -202,10 +202,10 @@ public enum KeyboardShortcuts { Setting it to `nil` removes the shortcut, even if the `Name` has a default shortcut defined. Use `.reset()` if you want it to respect the default shortcut. - You would usually not need this as the user would be the one setting the shortcut in a preferences user-interface, but it can be useful when, for example, migrating from a different keyboard shortcuts package. + You would usually not need this as the user would be the one setting the shortcut in a settings user-interface, but it can be useful when, for example, migrating from a different keyboard shortcuts package. */ public static func setShortcut(_ shortcut: Shortcut?, for name: Name) { - guard let shortcut = shortcut else { + guard let shortcut else { userDefaultsRemove(name: name) return } diff --git a/Sources/KeyboardShortcuts/Localization/en.lproj/Localizable.strings b/Sources/KeyboardShortcuts/Localization/en.lproj/Localizable.strings index e1eb03b..af9437d 100644 --- a/Sources/KeyboardShortcuts/Localization/en.lproj/Localizable.strings +++ b/Sources/KeyboardShortcuts/Localization/en.lproj/Localizable.strings @@ -2,4 +2,4 @@ "press_shortcut" = "Press Shortcut"; "keyboard_shortcut_used_by_menu_item" = "This keyboard shortcut cannot be used as it’s already used by the “%@” menu item."; "keyboard_shortcut_used_by_system" = "This keyboard shortcut cannot be used as it’s already a system-wide keyboard shortcut."; -"keyboard_shortcuts_can_be_changed" = "Most system-wide keyboard shortcuts can be changed in “System Preferences › Keyboard › Shortcuts”."; +"keyboard_shortcuts_can_be_changed" = "Most system-wide keyboard shortcuts can be changed in “System Settings › Keyboard › Keyboard Shortcuts”."; diff --git a/Sources/KeyboardShortcuts/NSMenuItem++.swift b/Sources/KeyboardShortcuts/NSMenuItem++.swift index 8106fc2..03e6517 100644 --- a/Sources/KeyboardShortcuts/NSMenuItem++.swift +++ b/Sources/KeyboardShortcuts/NSMenuItem++.swift @@ -44,7 +44,7 @@ extension NSMenuItem { - Important: You will have to disable the global keyboard shortcut while the menu is open, as otherwise, the keyboard events will be buffered up and triggered when the menu closes. This is because `NSMenu` puts the thread in tracking-mode, which prevents the keyboard events from being received. You can listen to whether a menu is open by implementing `NSMenuDelegate#menuWillOpen` and `NSMenuDelegate#menuDidClose`. You then use `KeyboardShortcuts.disable` and `KeyboardShortcuts.enable`. */ public func setShortcut(for name: KeyboardShortcuts.Name?) { - guard let name = name else { + guard let name else { clearShortcut() AssociatedKeys.observer[self] = nil return @@ -84,7 +84,7 @@ extension NSMenuItem { @_disfavoredOverload public func setShortcut(_ shortcut: KeyboardShortcuts.Shortcut?) { func set() { - guard let shortcut = shortcut else { + guard let shortcut else { clearShortcut() return } diff --git a/Sources/KeyboardShortcuts/Name.swift b/Sources/KeyboardShortcuts/Name.swift index 4bacd7f..6d1aa28 100644 --- a/Sources/KeyboardShortcuts/Name.swift +++ b/Sources/KeyboardShortcuts/Name.swift @@ -34,7 +34,7 @@ extension KeyboardShortcuts { self.defaultShortcut = defaultShortcut if - let defaultShortcut = defaultShortcut, + let defaultShortcut, !userDefaultsContains(name: self) { setShortcut(defaultShortcut, for: self) diff --git a/Sources/KeyboardShortcuts/Recorder.swift b/Sources/KeyboardShortcuts/Recorder.swift index c219bc0..6650a20 100644 --- a/Sources/KeyboardShortcuts/Recorder.swift +++ b/Sources/KeyboardShortcuts/Recorder.swift @@ -20,7 +20,7 @@ extension KeyboardShortcuts { /** A SwiftUI `View` that lets the user record a keyboard shortcut. - You would usually put this in your preferences window. + You would usually put this in your settings window. It automatically prevents choosing a keyboard shortcut that is already taken by the system or by the app's main menu by showing a user-friendly alert to the user. @@ -59,13 +59,24 @@ extension KeyboardShortcuts { public var body: some View { if hasLabel { - _Recorder( - name: name, - onChange: onChange - ) - .formLabel { + if #available(macOS 13, *) { + LabeledContent { + _Recorder( + name: name, + onChange: onChange + ) + } label: { label } + } else { + _Recorder( + name: name, + onChange: onChange + ) + .formLabel { + label + } + } } else { _Recorder( name: name, @@ -77,7 +88,7 @@ extension KeyboardShortcuts { } @available(macOS 10.15, *) -extension KeyboardShortcuts.Recorder where Label == EmptyView { +extension KeyboardShortcuts.Recorder { /** - Parameter name: Strongly-typed keyboard shortcut name. - Parameter onChange: Callback which will be called when the keyboard shortcut is changed/removed by the user. This can be useful when you need more control. For example, when migrating from a different keyboard shortcut solution and you need to store the keyboard shortcut somewhere yourself instead of relying on the built-in storage. However, it's strongly recommended to just rely on the built-in storage when possible. @@ -95,7 +106,7 @@ extension KeyboardShortcuts.Recorder where Label == EmptyView { } @available(macOS 10.15, *) -extension KeyboardShortcuts.Recorder where Label == Text { +extension KeyboardShortcuts.Recorder { /** - Parameter title: The title of the keyboard shortcut recorder, describing its purpose. - Parameter name: Strongly-typed keyboard shortcut name. diff --git a/Sources/KeyboardShortcuts/RecorderCocoa.swift b/Sources/KeyboardShortcuts/RecorderCocoa.swift index b0fb4ce..4f341a9 100644 --- a/Sources/KeyboardShortcuts/RecorderCocoa.swift +++ b/Sources/KeyboardShortcuts/RecorderCocoa.swift @@ -5,7 +5,7 @@ extension KeyboardShortcuts { /** A `NSView` that lets the user record a keyboard shortcut. - You would usually put this in your preferences window. + You would usually put this in your settings window. It automatically prevents choosing a keyboard shortcut that is already taken by the system or by the app's main menu by showing a user-friendly alert to the user. @@ -15,7 +15,7 @@ extension KeyboardShortcuts { import Cocoa import KeyboardShortcuts - final class PreferencesViewController: NSViewController { + final class SettingsViewController: NSViewController { override func loadView() { view = NSView() @@ -120,7 +120,7 @@ extension KeyboardShortcuts { private func setUpEvents() { observer = NotificationCenter.default.addObserver(forName: .shortcutByNameDidChange, object: nil, queue: nil) { [weak self] notification in guard - let self = self, + let self, let nameInNotification = notification.userInfo?["name"] as? KeyboardShortcuts.Name, nameInNotification == self.shortcutName else { @@ -177,7 +177,7 @@ extension KeyboardShortcuts { KeyboardShortcuts.isPaused = true // The position here matters. eventMonitor = LocalEventMonitor(events: [.keyDown, .leftMouseUp, .rightMouseUp]) { [weak self] event in - guard let self = self else { + guard let self else { return nil } @@ -254,7 +254,7 @@ extension KeyboardShortcuts { NSAlert.showModal( for: self.window, title: "keyboard_shortcut_used_by_system".localized, - // TODO: Add button to offer to open the relevant system preference pane for the user. + // TODO: Add button to offer to open the relevant system settings pane for the user. message: "keyboard_shortcuts_can_be_changed".localized ) diff --git a/Sources/KeyboardShortcuts/Shortcut.swift b/Sources/KeyboardShortcuts/Shortcut.swift index 6a5b7ce..136d53d 100644 --- a/Sources/KeyboardShortcuts/Shortcut.swift +++ b/Sources/KeyboardShortcuts/Shortcut.swift @@ -223,7 +223,7 @@ extension KeyboardShortcuts.Shortcut { // Some characters cannot be automatically translated. if - let key = key, + let key, let character = keyToCharacterMapping[key] { return character @@ -274,7 +274,7 @@ extension KeyboardShortcuts.Shortcut { guard keyString.count <= 1 else { guard - let key = key, + let key, let string = keyToKeyEquivalentString[key] else { return "" diff --git a/Sources/KeyboardShortcuts/Utilities.swift b/Sources/KeyboardShortcuts/Utilities.swift index 5e706da..cdd96db 100644 --- a/Sources/KeyboardShortcuts/Utilities.swift +++ b/Sources/KeyboardShortcuts/Utilities.swift @@ -74,7 +74,7 @@ final class LocalEventMonitor { } func stop() { - guard let monitor = monitor else { + guard let monitor else { return } @@ -159,7 +159,7 @@ extension NSAlert { self.alertStyle = style self.icon = icon - if let message = message { + if let message { self.informativeText = message } } @@ -169,7 +169,7 @@ extension NSAlert { */ @discardableResult func runModal(for window: NSWindow? = nil) -> NSApplication.ModalResponse { - guard let window = window else { + guard let window else { return runModal() } @@ -391,7 +391,7 @@ extension HorizontalAlignment { @available(macOS 10.15, *) extension View { - func formLabel(@ViewBuilder _ label: () -> Label) -> some View { + func formLabel(@ViewBuilder _ label: () -> some View) -> some View { HStack(alignment: .firstTextBaseline) { label() labelsHidden() diff --git a/readme.md b/readme.md index 714e2d4..a506ff1 100644 --- a/readme.md +++ b/readme.md @@ -10,35 +10,6 @@ I'm happy to accept more configurability and features. PR welcome! What you see -
- ---- - - - ---- - -
- ## Requirements macOS 10.11+ @@ -132,7 +103,7 @@ Using [`KeyboardShortcuts.RecorderCocoa`](Sources/KeyboardShortcuts/RecorderCoco import Cocoa import KeyboardShortcuts -final class PreferencesViewController: NSViewController { +final class SettingsViewController: NSViewController { override func loadView() { view = NSView() @@ -255,6 +226,6 @@ No. However, there is nothing stopping you from using Swift Package Manager for - [Defaults](https://github.com/sindresorhus/Defaults) - Swifty and modern UserDefaults - [Regex](https://github.com/sindresorhus/Regex) - Swifty regular expressions -- [Preferences](https://github.com/sindresorhus/Preferences) - Add a preferences window to your macOS app in minutes +- [Preferences](https://github.com/sindresorhus/Preferences) - Add a settings window to your macOS app in minutes - [LaunchAtLogin](https://github.com/sindresorhus/LaunchAtLogin) - Add "Launch at Login" functionality to your macOS app - [More…](https://github.com/search?q=user%3Asindresorhus+language%3Aswift)