Support disabling/enabling multiple keyboard shortcuts in a single call

This commit is contained in:
Sindre Sorhus 2023-04-27 02:14:15 +07:00
parent 921c77e14f
commit 018e445b34
6 changed files with 89 additions and 57 deletions

View File

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

View File

@ -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 */
@ -34,7 +34,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
E33F1EFC26F3B89C00ACEB0F /* KeyboardShortcuts in Frameworks */,
E3A680512A042A0B00715D81 /* KeyboardShortcuts in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@ -55,6 +55,7 @@
E36FB9482609BA43004272D9 /* KeyboardShortcutsExample */,
E36FB9472609BA43004272D9 /* Products */,
E33F1EF926F3B78800ACEB0F /* Packages */,
E3A6804F2A042A0B00715D81 /* Frameworks */,
);
sourceTree = "<group>";
};
@ -89,6 +90,13 @@
path = "Preview Content";
sourceTree = "<group>";
};
E3A6804F2A042A0B00715D81 /* Frameworks */ = {
isa = PBXGroup;
children = (
);
name = Frameworks;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
@ -106,7 +114,7 @@
);
name = KeyboardShortcutsExample;
packageProductDependencies = (
E33F1EFB26F3B89C00ACEB0F /* KeyboardShortcuts */,
E3A680502A042A0B00715D81 /* KeyboardShortcuts */,
);
productName = KeyboardShortcutsExample;
productReference = E36FB9462609BA43004272D9 /* KeyboardShortcutsExample.app */;
@ -118,8 +126,9 @@
E36FB93E2609BA43004272D9 /* Project object */ = {
isa = PBXProject;
attributes = {
BuildIndependentTargetsInParallel = YES;
LastSwiftUpdateCheck = 1240;
LastUpgradeCheck = 1410;
LastUpgradeCheck = 1430;
TargetAttributes = {
E36FB9452609BA43004272D9 = {
CreatedOnToolsVersion = 12.4;
@ -366,7 +375,7 @@
/* End XCConfigurationList section */
/* Begin XCSwiftPackageProductDependency section */
E33F1EFB26F3B89C00ACEB0F /* KeyboardShortcuts */ = {
E3A680502A042A0B00715D81 /* KeyboardShortcuts */ = {
isa = XCSwiftPackageProductDependency;
productName = KeyboardShortcuts;
};

View File

@ -1,4 +1,3 @@
import Cocoa
import Carbon.HIToolbox
extension KeyboardShortcuts {

View File

@ -1,4 +1,4 @@
import Cocoa
import Foundation
/**
Global keyboard shortcuts for your macOS app.
@ -166,54 +166,45 @@ 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) {
public static func disable(_ names: [Name]) {
for name in names {
guard let shortcut = getShortcut(for: name) else {
return
continue
}
unregister(shortcut)
}
}
/**
Enable a disabled keyboard shortcut.
Disable the keyboard shortcut for one or more names.
*/
public static func enable(_ name: Name) {
public static func disable(_ names: Name...) {
disable(names)
}
/**
Enable the keyboard shortcut for one or more names.
*/
public static func enable(_ names: [Name]) {
for name in names {
guard let shortcut = getShortcut(for: name) else {
return
continue
}
register(shortcut)
}
}
/**
Reset the keyboard shortcut for one or more names.
If the `Name` has a default shortcut, it will reset to that.
```swift
import SwiftUI
import KeyboardShortcuts
struct SettingsScreen: View {
var body: some View {
VStack {
//
Button("Reset All") {
KeyboardShortcuts.reset(
.toggleUnicornMode,
.showRainbow
)
}
}
}
}
```
Enable the keyboard shortcut for one or more names.
*/
public static func reset(_ names: Name...) {
reset(names)
public static func enable(_ names: Name...) {
enable(names)
}
/**
@ -248,6 +239,34 @@ public enum KeyboardShortcuts {
}
}
/**
Reset the keyboard shortcut for one or more names.
If the `Name` has a default shortcut, it will reset to that.
```swift
import SwiftUI
import KeyboardShortcuts
struct SettingsScreen: View {
var body: some View {
VStack {
//
Button("Reset All") {
KeyboardShortcuts.reset(
.toggleUnicornMode,
.showRainbow
)
}
}
}
}
```
*/
public static func reset(_ names: Name...) {
reset(names)
}
/**
Set the keyboard shortcut for a name.

View File

@ -1,4 +1,4 @@
import Cocoa
import AppKit
extension NSMenuItem {
private enum AssociatedKeys {

View File

@ -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)
}
}
}