Minor tweaks

This commit is contained in:
Sindre Sorhus 2022-09-05 14:00:54 +07:00
parent 5e674337c1
commit d2fc65f9c3
14 changed files with 53 additions and 67 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<Scheme <Scheme
LastUpgradeVersion = "1250" LastUpgradeVersion = "1410"
version = "1.3"> version = "1.3">
<BuildAction <BuildAction
parallelizeBuildables = "YES" parallelizeBuildables = "YES"

View File

@ -3,7 +3,7 @@
archiveVersion = 1; archiveVersion = 1;
classes = { classes = {
}; };
objectVersion = 55; objectVersion = 56;
objects = { objects = {
/* Begin PBXBuildFile section */ /* Begin PBXBuildFile section */
@ -119,7 +119,7 @@
isa = PBXProject; isa = PBXProject;
attributes = { attributes = {
LastSwiftUpdateCheck = 1240; LastSwiftUpdateCheck = 1240;
LastUpgradeCheck = 1250; LastUpgradeCheck = 1410;
TargetAttributes = { TargetAttributes = {
E36FB9452609BA43004272D9 = { E36FB9452609BA43004272D9 = {
CreatedOnToolsVersion = 12.4; CreatedOnToolsVersion = 12.4;
@ -127,7 +127,7 @@
}; };
}; };
buildConfigurationList = E36FB9412609BA43004272D9 /* Build configuration list for PBXProject "KeyboardShortcutsExample" */; buildConfigurationList = E36FB9412609BA43004272D9 /* Build configuration list for PBXProject "KeyboardShortcutsExample" */;
compatibilityVersion = "Xcode 13.0"; compatibilityVersion = "Xcode 14.0";
developmentRegion = en; developmentRegion = en;
hasScannedForEncodings = 0; hasScannedForEncodings = 0;
knownRegions = ( knownRegions = (
@ -205,6 +205,7 @@
CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO; COPY_PHASE_STRIP = NO;
DEAD_CODE_STRIPPING = YES;
DEBUG_INFORMATION_FORMAT = dwarf; DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES; ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES; ENABLE_TESTABILITY = YES;
@ -266,6 +267,7 @@
CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO; COPY_PHASE_STRIP = NO;
DEAD_CODE_STRIPPING = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO; ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES; ENABLE_STRICT_OBJC_MSGSEND = YES;
@ -296,6 +298,7 @@
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES; COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1; CURRENT_PROJECT_VERSION = 1;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_ASSET_PATHS = "\"KeyboardShortcutsExample/Preview Content\""; DEVELOPMENT_ASSET_PATHS = "\"KeyboardShortcutsExample/Preview Content\"";
DEVELOPMENT_TEAM = ""; DEVELOPMENT_TEAM = "";
ENABLE_HARDENED_RUNTIME = YES; ENABLE_HARDENED_RUNTIME = YES;
@ -322,6 +325,7 @@
CODE_SIGN_STYLE = Automatic; CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES; COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1; CURRENT_PROJECT_VERSION = 1;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_ASSET_PATHS = "\"KeyboardShortcutsExample/Preview Content\""; DEVELOPMENT_ASSET_PATHS = "\"KeyboardShortcutsExample/Preview Content\"";
DEVELOPMENT_TEAM = ""; DEVELOPMENT_TEAM = "";
ENABLE_HARDENED_RUNTIME = YES; ENABLE_HARDENED_RUNTIME = YES;

View File

@ -26,7 +26,7 @@ final class CallbackMenuItem: NSMenuItem {
self.isChecked = isChecked self.isChecked = isChecked
self.isHidden = isHidden self.isHidden = isHidden
if let keyModifiers = keyModifiers { if let keyModifiers {
self.keyEquivalentModifierMask = keyModifiers self.keyEquivalentModifierMask = keyModifiers
} }
} }
@ -67,7 +67,7 @@ extension NSMenuItem {
self.isChecked = isChecked self.isChecked = isChecked
self.isHidden = isHidden self.isHidden = isHidden
if let keyModifiers = keyModifiers { if let keyModifiers {
self.keyEquivalentModifierMask = keyModifiers self.keyEquivalentModifierMask = keyModifiers
} }
} }

View File

@ -1,11 +1,11 @@
// swift-tools-version:5.6 // swift-tools-version:5.7
import PackageDescription import PackageDescription
let package = Package( let package = Package(
name: "KeyboardShortcuts", name: "KeyboardShortcuts",
defaultLocalization: "en", defaultLocalization: "en",
platforms: [ platforms: [
.macOS(.v10_11) .macOS(.v10_13)
], ],
products: [ products: [
.library( .library(

View File

@ -113,7 +113,7 @@ enum CarbonKeyboardShortcuts {
} }
fileprivate static func handleEvent(_ event: EventRef?) -> OSStatus { fileprivate static func handleEvent(_ event: EventRef?) -> OSStatus {
guard let event = event else { guard let event else {
return OSStatus(eventNotHandledErr) return OSStatus(eventNotHandledErr)
} }

View File

@ -146,7 +146,7 @@ public enum KeyboardShortcuts {
import SwiftUI import SwiftUI
import KeyboardShortcuts import KeyboardShortcuts
struct PreferencesView: View { struct SettingsScreen: View {
var body: some View { var body: some View {
VStack { VStack {
// //
@ -176,7 +176,7 @@ public enum KeyboardShortcuts {
import SwiftUI import SwiftUI
import KeyboardShortcuts import KeyboardShortcuts
struct PreferencesView: View { struct SettingsScreen: View {
var body: some View { var body: some View {
VStack { 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. 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) { public static func setShortcut(_ shortcut: Shortcut?, for name: Name) {
guard let shortcut = shortcut else { guard let shortcut else {
userDefaultsRemove(name: name) userDefaultsRemove(name: name)
return return
} }

View File

@ -2,4 +2,4 @@
"press_shortcut" = "Press Shortcut"; "press_shortcut" = "Press Shortcut";
"keyboard_shortcut_used_by_menu_item" = "This keyboard shortcut cannot be used as its already used by the “%@” menu item."; "keyboard_shortcut_used_by_menu_item" = "This keyboard shortcut cannot be used as its already used by the “%@” menu item.";
"keyboard_shortcut_used_by_system" = "This keyboard shortcut cannot be used as its already a system-wide keyboard shortcut."; "keyboard_shortcut_used_by_system" = "This keyboard shortcut cannot be used as its 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”.";

View File

@ -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`. - 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?) { public func setShortcut(for name: KeyboardShortcuts.Name?) {
guard let name = name else { guard let name else {
clearShortcut() clearShortcut()
AssociatedKeys.observer[self] = nil AssociatedKeys.observer[self] = nil
return return
@ -84,7 +84,7 @@ extension NSMenuItem {
@_disfavoredOverload @_disfavoredOverload
public func setShortcut(_ shortcut: KeyboardShortcuts.Shortcut?) { public func setShortcut(_ shortcut: KeyboardShortcuts.Shortcut?) {
func set() { func set() {
guard let shortcut = shortcut else { guard let shortcut else {
clearShortcut() clearShortcut()
return return
} }

View File

@ -34,7 +34,7 @@ extension KeyboardShortcuts {
self.defaultShortcut = defaultShortcut self.defaultShortcut = defaultShortcut
if if
let defaultShortcut = defaultShortcut, let defaultShortcut,
!userDefaultsContains(name: self) !userDefaultsContains(name: self)
{ {
setShortcut(defaultShortcut, for: self) setShortcut(defaultShortcut, for: self)

View File

@ -20,7 +20,7 @@ extension KeyboardShortcuts {
/** /**
A SwiftUI `View` that lets the user record a keyboard shortcut. 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. 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,6 +59,16 @@ extension KeyboardShortcuts {
public var body: some View { public var body: some View {
if hasLabel { if hasLabel {
if #available(macOS 13, *) {
LabeledContent {
_Recorder(
name: name,
onChange: onChange
)
} label: {
label
}
} else {
_Recorder( _Recorder(
name: name, name: name,
onChange: onChange onChange: onChange
@ -66,6 +76,7 @@ extension KeyboardShortcuts {
.formLabel { .formLabel {
label label
} }
}
} else { } else {
_Recorder( _Recorder(
name: name, name: name,
@ -77,7 +88,7 @@ extension KeyboardShortcuts {
} }
@available(macOS 10.15, *) @available(macOS 10.15, *)
extension KeyboardShortcuts.Recorder where Label == EmptyView { extension KeyboardShortcuts.Recorder<EmptyView> {
/** /**
- Parameter name: Strongly-typed keyboard shortcut name. - 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. - 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, *) @available(macOS 10.15, *)
extension KeyboardShortcuts.Recorder where Label == Text { extension KeyboardShortcuts.Recorder<Text> {
/** /**
- Parameter title: The title of the keyboard shortcut recorder, describing its purpose. - Parameter title: The title of the keyboard shortcut recorder, describing its purpose.
- Parameter name: Strongly-typed keyboard shortcut name. - Parameter name: Strongly-typed keyboard shortcut name.

View File

@ -5,7 +5,7 @@ extension KeyboardShortcuts {
/** /**
A `NSView` that lets the user record a keyboard shortcut. 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. 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 Cocoa
import KeyboardShortcuts import KeyboardShortcuts
final class PreferencesViewController: NSViewController { final class SettingsViewController: NSViewController {
override func loadView() { override func loadView() {
view = NSView() view = NSView()
@ -120,7 +120,7 @@ extension KeyboardShortcuts {
private func setUpEvents() { private func setUpEvents() {
observer = NotificationCenter.default.addObserver(forName: .shortcutByNameDidChange, object: nil, queue: nil) { [weak self] notification in observer = NotificationCenter.default.addObserver(forName: .shortcutByNameDidChange, object: nil, queue: nil) { [weak self] notification in
guard guard
let self = self, let self,
let nameInNotification = notification.userInfo?["name"] as? KeyboardShortcuts.Name, let nameInNotification = notification.userInfo?["name"] as? KeyboardShortcuts.Name,
nameInNotification == self.shortcutName nameInNotification == self.shortcutName
else { else {
@ -177,7 +177,7 @@ extension KeyboardShortcuts {
KeyboardShortcuts.isPaused = true // The position here matters. KeyboardShortcuts.isPaused = true // The position here matters.
eventMonitor = LocalEventMonitor(events: [.keyDown, .leftMouseUp, .rightMouseUp]) { [weak self] event in eventMonitor = LocalEventMonitor(events: [.keyDown, .leftMouseUp, .rightMouseUp]) { [weak self] event in
guard let self = self else { guard let self else {
return nil return nil
} }
@ -254,7 +254,7 @@ extension KeyboardShortcuts {
NSAlert.showModal( NSAlert.showModal(
for: self.window, for: self.window,
title: "keyboard_shortcut_used_by_system".localized, 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 message: "keyboard_shortcuts_can_be_changed".localized
) )

View File

@ -223,7 +223,7 @@ extension KeyboardShortcuts.Shortcut {
// Some characters cannot be automatically translated. // Some characters cannot be automatically translated.
if if
let key = key, let key,
let character = keyToCharacterMapping[key] let character = keyToCharacterMapping[key]
{ {
return character return character
@ -274,7 +274,7 @@ extension KeyboardShortcuts.Shortcut {
guard keyString.count <= 1 else { guard keyString.count <= 1 else {
guard guard
let key = key, let key,
let string = keyToKeyEquivalentString[key] let string = keyToKeyEquivalentString[key]
else { else {
return "" return ""

View File

@ -74,7 +74,7 @@ final class LocalEventMonitor {
} }
func stop() { func stop() {
guard let monitor = monitor else { guard let monitor else {
return return
} }
@ -159,7 +159,7 @@ extension NSAlert {
self.alertStyle = style self.alertStyle = style
self.icon = icon self.icon = icon
if let message = message { if let message {
self.informativeText = message self.informativeText = message
} }
} }
@ -169,7 +169,7 @@ extension NSAlert {
*/ */
@discardableResult @discardableResult
func runModal(for window: NSWindow? = nil) -> NSApplication.ModalResponse { func runModal(for window: NSWindow? = nil) -> NSApplication.ModalResponse {
guard let window = window else { guard let window else {
return runModal() return runModal()
} }
@ -391,7 +391,7 @@ extension HorizontalAlignment {
@available(macOS 10.15, *) @available(macOS 10.15, *)
extension View { extension View {
func formLabel<Label: View>(@ViewBuilder _ label: () -> Label) -> some View { func formLabel(@ViewBuilder _ label: () -> some View) -> some View {
HStack(alignment: .firstTextBaseline) { HStack(alignment: .firstTextBaseline) {
label() label()
labelsHidden() labelsHidden()

View File

@ -10,35 +10,6 @@ I'm happy to accept more configurability and features. PR welcome! What you see
<img src="https://github.com/sindresorhus/KeyboardShortcuts/raw/main/screenshot.png" width="532"> <img src="https://github.com/sindresorhus/KeyboardShortcuts/raw/main/screenshot.png" width="532">
<br>
---
<div align="center">
<p>
<p>
<sup>
<a href="https://github.com/sponsors/sindresorhus">Sindre's open source work is supported by the community</a>
</sup>
</p>
<sup>Special thanks to:</sup>
<br>
<br>
<a href="https://keygen.sh">
<div>
<img src="https://sindresorhus.com/assets/thanks/keygen-logo.svg" width="210" alt="Keygen">
</div>
<b>A dead-simple software licensing and distribution API built for developers</b>
</a>
<br>
<br>
</p>
</div>
---
<br>
## Requirements ## Requirements
macOS 10.11+ macOS 10.11+
@ -132,7 +103,7 @@ Using [`KeyboardShortcuts.RecorderCocoa`](Sources/KeyboardShortcuts/RecorderCoco
import Cocoa import Cocoa
import KeyboardShortcuts import KeyboardShortcuts
final class PreferencesViewController: NSViewController { final class SettingsViewController: NSViewController {
override func loadView() { override func loadView() {
view = NSView() 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 - [Defaults](https://github.com/sindresorhus/Defaults) - Swifty and modern UserDefaults
- [Regex](https://github.com/sindresorhus/Regex) - Swifty regular expressions - [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 - [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) - [More…](https://github.com/search?q=user%3Asindresorhus+language%3Aswift)