Skip to content

Commit

Permalink
Support disabling/enabling multiple keyboard shortcuts in a single call
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 4, 2023
1 parent 921c77e commit 018e445
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 33 deletions.
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1410"
LastUpgradeVersion = "1430"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
19 changes: 14 additions & 5 deletions Example/KeyboardShortcutsExample.xcodeproj/project.pbxproj
Expand Up @@ -7,13 +7,13 @@
objects = {

/* Begin PBXBuildFile section */
E33F1EFC26F3B89C00ACEB0F /* KeyboardShortcuts in Frameworks */ = {isa = PBXBuildFile; productRef = E33F1EFB26F3B89C00ACEB0F /* KeyboardShortcuts */; };
E36FB94A2609BA43004272D9 /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = E36FB9492609BA43004272D9 /* App.swift */; };
E36FB94C2609BA43004272D9 /* MainScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = E36FB94B2609BA43004272D9 /* MainScreen.swift */; };
E36FB94E2609BA45004272D9 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E36FB94D2609BA45004272D9 /* Assets.xcassets */; };
E36FB9512609BA45004272D9 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E36FB9502609BA45004272D9 /* Preview Assets.xcassets */; };
E36FB9632609BB83004272D9 /* AppState.swift in Sources */ = {isa = PBXBuildFile; fileRef = E36FB9622609BB83004272D9 /* AppState.swift */; };
E36FB9662609BF3D004272D9 /* Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = E36FB9652609BF3D004272D9 /* Utilities.swift */; };
E3A680512A042A0B00715D81 /* KeyboardShortcuts in Frameworks */ = {isa = PBXBuildFile; productRef = E3A680502A042A0B00715D81 /* KeyboardShortcuts */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -34,7 +34,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
E33F1EFC26F3B89C00ACEB0F /* KeyboardShortcuts in Frameworks */,
E3A680512A042A0B00715D81 /* KeyboardShortcuts in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -55,6 +55,7 @@
E36FB9482609BA43004272D9 /* KeyboardShortcutsExample */,
E36FB9472609BA43004272D9 /* Products */,
E33F1EF926F3B78800ACEB0F /* Packages */,
E3A6804F2A042A0B00715D81 /* Frameworks */,
);
sourceTree = "<group>";
};
Expand Down Expand Up @@ -89,6 +90,13 @@
path = "Preview Content";
sourceTree = "<group>";
};
E3A6804F2A042A0B00715D81 /* Frameworks */ = {
isa = PBXGroup;
children = (
);
name = Frameworks;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand All @@ -106,7 +114,7 @@
);
name = KeyboardShortcutsExample;
packageProductDependencies = (
E33F1EFB26F3B89C00ACEB0F /* KeyboardShortcuts */,
E3A680502A042A0B00715D81 /* KeyboardShortcuts */,
);
productName = KeyboardShortcutsExample;
productReference = E36FB9462609BA43004272D9 /* KeyboardShortcutsExample.app */;
Expand All @@ -118,8 +126,9 @@
E36FB93E2609BA43004272D9 /* Project object */ = {
isa = PBXProject;
attributes = {
BuildIndependentTargetsInParallel = YES;
LastSwiftUpdateCheck = 1240;
LastUpgradeCheck = 1410;
LastUpgradeCheck = 1430;
TargetAttributes = {
E36FB9452609BA43004272D9 = {
CreatedOnToolsVersion = 12.4;
Expand Down Expand Up @@ -366,7 +375,7 @@
/* End XCConfigurationList section */

/* Begin XCSwiftPackageProductDependency section */
E33F1EFB26F3B89C00ACEB0F /* KeyboardShortcuts */ = {
E3A680502A042A0B00715D81 /* KeyboardShortcuts */ = {
isa = XCSwiftPackageProductDependency;
productName = KeyboardShortcuts;
};
Expand Down
1 change: 0 additions & 1 deletion Sources/KeyboardShortcuts/Key.swift
@@ -1,4 +1,3 @@
import Cocoa
import Carbon.HIToolbox

extension KeyboardShortcuts {
Expand Down
57 changes: 38 additions & 19 deletions Sources/KeyboardShortcuts/KeyboardShortcuts.swift
@@ -1,4 +1,4 @@
import Cocoa
import Foundation

/**
Global keyboard shortcuts for your macOS app.
Expand Down Expand Up @@ -166,33 +166,54 @@ public enum KeyboardShortcuts {
}

// TODO: Also add `.isEnabled(_ name: Name)`.

/**
Disable a keyboard shortcut.
Disable the keyboard shortcut for one or more names.
*/
public static func disable(_ name: Name) {
guard let shortcut = getShortcut(for: name) else {
return
public static func disable(_ names: [Name]) {
for name in names {
guard let shortcut = getShortcut(for: name) else {
continue
}

unregister(shortcut)
}
}

unregister(shortcut)
/**
Disable the keyboard shortcut for one or more names.
*/
public static func disable(_ names: Name...) {
disable(names)
}

/**
Enable a disabled keyboard shortcut.
Enable the keyboard shortcut for one or more names.
*/
public static func enable(_ name: Name) {
guard let shortcut = getShortcut(for: name) else {
return
public static func enable(_ names: [Name]) {
for name in names {
guard let shortcut = getShortcut(for: name) else {
continue
}

register(shortcut)
}
}

register(shortcut)
/**
Enable the keyboard shortcut for one or more names.
*/
public static func enable(_ names: Name...) {
enable(names)
}

/**
Reset the keyboard shortcut for one or more names.
If the `Name` has a default shortcut, it will reset to that.
- Note: This overload exists as Swift doesn't support splatting.
```swift
import SwiftUI
import KeyboardShortcuts
Expand All @@ -212,17 +233,17 @@ public enum KeyboardShortcuts {
}
```
*/
public static func reset(_ names: Name...) {
reset(names)
public static func reset(_ names: [Name]) {
for name in names {
setShortcut(name.defaultShortcut, for: name)
}
}

/**
Reset the keyboard shortcut for one or more names.
If the `Name` has a default shortcut, it will reset to that.
- Note: This overload exists as Swift doesn't support splatting.
```swift
import SwiftUI
import KeyboardShortcuts
Expand All @@ -242,10 +263,8 @@ public enum KeyboardShortcuts {
}
```
*/
public static func reset(_ names: [Name]) {
for name in names {
setShortcut(name.defaultShortcut, for: name)
}
public static func reset(_ names: Name...) {
reset(names)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion Sources/KeyboardShortcuts/NSMenuItem++.swift
@@ -1,4 +1,4 @@
import Cocoa
import AppKit

extension NSMenuItem {
private enum AssociatedKeys {
Expand Down
17 changes: 11 additions & 6 deletions Sources/KeyboardShortcuts/Name.swift
Expand Up @@ -21,23 +21,28 @@ extension KeyboardShortcuts {
public let defaultShortcut: Shortcut?

/**
Get the keyboard shortcut assigned to the name.
The keyboard shortcut assigned to the name.
*/
public var shortcut: Shortcut? { KeyboardShortcuts.getShortcut(for: self) }
public var shortcut: Shortcut? {
get { KeyboardShortcuts.getShortcut(for: self) }
nonmutating set {
KeyboardShortcuts.setShortcut(newValue, for: self)
}
}

/**
- Parameter name: Name of the shortcut.
- Parameter default: Optional default key combination. Do not set this unless it's essential. Users find it annoying when random apps steal their existing keyboard shortcuts. It's generally better to show a welcome screen on the first app launch that lets the user set the shortcut.
*/
public init(_ name: String, default defaultShortcut: Shortcut? = nil) {
public init(_ name: String, default initialShortcut: Shortcut? = nil) {
self.rawValue = name
self.defaultShortcut = defaultShortcut
self.defaultShortcut = initialShortcut

if
let defaultShortcut,
let initialShortcut,
!userDefaultsContains(name: self)
{
setShortcut(defaultShortcut, for: self)
setShortcut(initialShortcut, for: self)
}
}
}
Expand Down

0 comments on commit 018e445

Please sign in to comment.