Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add methods for removing handlers for specific Names #194

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions Sources/KeyboardShortcuts/KeyboardShortcuts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Global keyboard shortcuts for your macOS app.
public enum KeyboardShortcuts {
private static var registeredShortcuts = Set<Shortcut>()

private static var legacyKeyDownHandlers = [Name: [() -> Void]]()
private static var legacyKeyUpHandlers = [Name: [() -> Void]]()
internal static var legacyKeyDownHandlers = [Name: [() -> Void]]()
internal static var legacyKeyUpHandlers = [Name: [() -> Void]]()

private static var streamKeyDownHandlers = [Name: [UUID: () -> Void]]()
private static var streamKeyUpHandlers = [Name: [UUID: () -> Void]]()
Expand Down Expand Up @@ -181,6 +181,34 @@ public enum KeyboardShortcuts {
legacyKeyUpHandlers = [:]
}

/**
Remove all handlers for one or more names. Includes both "key down" and "key up" handlers.
*/
public static func removeHandlers(for names: [Name]) {
for name in names {
legacyKeyDownHandlers[name, default: []].removeAll()
legacyKeyUpHandlers[name, default: []].removeAll()
}
}

/**
Remove all "key down" handlers for one or more names.
*/
public static func removeKeyDownHandlers(for names: [Name]) {
for name in names {
legacyKeyDownHandlers[name, default: []].removeAll()
}
}

/**
Remove all "key up" handlers for one or more names.
*/
public static func removeKeyUpHandlers(for names: [Name]) {
for name in names {
legacyKeyUpHandlers[name, default: []].removeAll()
}
}

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

/**
Expand Down
131 changes: 130 additions & 1 deletion Tests/KeyboardShortcutsTests/KeyboardShortcutsTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import XCTest
import KeyboardShortcuts
@testable import KeyboardShortcuts

final class KeyboardShortcutsTests: XCTestCase {
// TODO: Add more tests.
Expand Down Expand Up @@ -27,4 +27,133 @@ final class KeyboardShortcutsTests: XCTestCase {
XCTAssertNil(KeyboardShortcuts.getShortcut(for: shortcutName1))
XCTAssertEqual(KeyboardShortcuts.getShortcut(for: shortcutName2), defaultShortcut)
}

func testRemoveHandlersForNames() throws {
let shortcut1 = KeyboardShortcuts.Shortcut(.a)
let shortcut2 = KeyboardShortcuts.Shortcut(.b)

let shortcutName1 = KeyboardShortcuts.Name("testRemoveHandlersForNames1")
let shortcutName2 = KeyboardShortcuts.Name("testRemoveHandlersForNames2")

KeyboardShortcuts.setShortcut(shortcut1, for: shortcutName1)
KeyboardShortcuts.setShortcut(shortcut2, for: shortcutName2)

KeyboardShortcuts.onKeyDown(for: shortcutName1) {
print("Shortcut 1 is down")
}
KeyboardShortcuts.onKeyUp(for: shortcutName1) {
print("Shortcut 1 is up")
}
KeyboardShortcuts.onKeyDown(for: shortcutName2) {
print("Shortcut 2 is down")
}
KeyboardShortcuts.onKeyUp(for: shortcutName2) {
print("Shortcut 2 is up")
}

XCTAssertEqual(KeyboardShortcuts.legacyKeyDownHandlers[shortcutName1]?.count, 1)
XCTAssertEqual(KeyboardShortcuts.legacyKeyDownHandlers[shortcutName2]?.count, 1)
XCTAssertEqual(KeyboardShortcuts.legacyKeyUpHandlers[shortcutName1]?.count, 1)
XCTAssertEqual(KeyboardShortcuts.legacyKeyUpHandlers[shortcutName2]?.count, 1)

KeyboardShortcuts.removeHandlers(for: [shortcutName1])

XCTAssertEqual(KeyboardShortcuts.legacyKeyDownHandlers[shortcutName1]?.count, 0)
XCTAssertEqual(KeyboardShortcuts.legacyKeyDownHandlers[shortcutName2]?.count, 1)
XCTAssertEqual(KeyboardShortcuts.legacyKeyUpHandlers[shortcutName1]?.count, 0)
XCTAssertEqual(KeyboardShortcuts.legacyKeyUpHandlers[shortcutName2]?.count, 1)

KeyboardShortcuts.removeHandlers(for: [shortcutName2])

XCTAssertEqual(KeyboardShortcuts.legacyKeyDownHandlers[shortcutName1]?.count, 0)
XCTAssertEqual(KeyboardShortcuts.legacyKeyDownHandlers[shortcutName2]?.count, 0)
XCTAssertEqual(KeyboardShortcuts.legacyKeyUpHandlers[shortcutName1]?.count, 0)
XCTAssertEqual(KeyboardShortcuts.legacyKeyUpHandlers[shortcutName2]?.count, 0)
}

func testRemoveKeyDownHandlersForNames() throws {
let shortcut1 = KeyboardShortcuts.Shortcut(.a)
let shortcut2 = KeyboardShortcuts.Shortcut(.b)

let shortcutName1 = KeyboardShortcuts.Name("testRemoveKeyDownHandlersForNames1")
let shortcutName2 = KeyboardShortcuts.Name("testRemoveKeyDownHandlersForNames2")

KeyboardShortcuts.setShortcut(shortcut1, for: shortcutName1)
KeyboardShortcuts.setShortcut(shortcut2, for: shortcutName2)

KeyboardShortcuts.onKeyDown(for: shortcutName1) {
print("Shortcut 1 is down")
}
KeyboardShortcuts.onKeyUp(for: shortcutName1) {
print("Shortcut 1 is up")
}
KeyboardShortcuts.onKeyDown(for: shortcutName2) {
print("Shortcut 2 is down")
}
KeyboardShortcuts.onKeyUp(for: shortcutName2) {
print("Shortcut 2 is up")
}

XCTAssertEqual(KeyboardShortcuts.legacyKeyDownHandlers[shortcutName1]?.count, 1)
XCTAssertEqual(KeyboardShortcuts.legacyKeyDownHandlers[shortcutName2]?.count, 1)
XCTAssertEqual(KeyboardShortcuts.legacyKeyUpHandlers[shortcutName1]?.count, 1)
XCTAssertEqual(KeyboardShortcuts.legacyKeyUpHandlers[shortcutName2]?.count, 1)

KeyboardShortcuts.removeKeyDownHandlers(for: [shortcutName1])

XCTAssertEqual(KeyboardShortcuts.legacyKeyDownHandlers[shortcutName1]?.count, 0)
XCTAssertEqual(KeyboardShortcuts.legacyKeyDownHandlers[shortcutName2]?.count, 1)
XCTAssertEqual(KeyboardShortcuts.legacyKeyUpHandlers[shortcutName1]?.count, 1)
XCTAssertEqual(KeyboardShortcuts.legacyKeyUpHandlers[shortcutName2]?.count, 1)

KeyboardShortcuts.removeKeyDownHandlers(for: [shortcutName2])

XCTAssertEqual(KeyboardShortcuts.legacyKeyDownHandlers[shortcutName1]?.count, 0)
XCTAssertEqual(KeyboardShortcuts.legacyKeyDownHandlers[shortcutName2]?.count, 0)
XCTAssertEqual(KeyboardShortcuts.legacyKeyUpHandlers[shortcutName1]?.count, 1)
XCTAssertEqual(KeyboardShortcuts.legacyKeyUpHandlers[shortcutName2]?.count, 1)
}

func testRemoveKeyUpHandlersForNames() throws {
let shortcut1 = KeyboardShortcuts.Shortcut(.a)
let shortcut2 = KeyboardShortcuts.Shortcut(.b)

let shortcutName1 = KeyboardShortcuts.Name("testRemoveKeyUpHandlersForNames1")
let shortcutName2 = KeyboardShortcuts.Name("testRemoveKeyUpHandlersForNames2")

KeyboardShortcuts.setShortcut(shortcut1, for: shortcutName1)
KeyboardShortcuts.setShortcut(shortcut2, for: shortcutName2)

KeyboardShortcuts.onKeyDown(for: shortcutName1) {
print("Shortcut 1 is down")
}
KeyboardShortcuts.onKeyUp(for: shortcutName1) {
print("Shortcut 1 is up")
}
KeyboardShortcuts.onKeyDown(for: shortcutName2) {
print("Shortcut 2 is down")
}
KeyboardShortcuts.onKeyUp(for: shortcutName2) {
print("Shortcut 2 is up")
}

XCTAssertEqual(KeyboardShortcuts.legacyKeyDownHandlers[shortcutName1]?.count, 1)
XCTAssertEqual(KeyboardShortcuts.legacyKeyDownHandlers[shortcutName2]?.count, 1)
XCTAssertEqual(KeyboardShortcuts.legacyKeyUpHandlers[shortcutName1]?.count, 1)
XCTAssertEqual(KeyboardShortcuts.legacyKeyUpHandlers[shortcutName2]?.count, 1)

KeyboardShortcuts.removeKeyUpHandlers(for: [shortcutName1])

XCTAssertEqual(KeyboardShortcuts.legacyKeyDownHandlers[shortcutName1]?.count, 1)
XCTAssertEqual(KeyboardShortcuts.legacyKeyDownHandlers[shortcutName2]?.count, 1)
XCTAssertEqual(KeyboardShortcuts.legacyKeyUpHandlers[shortcutName1]?.count, 0)
XCTAssertEqual(KeyboardShortcuts.legacyKeyUpHandlers[shortcutName2]?.count, 1)

KeyboardShortcuts.removeKeyUpHandlers(for: [shortcutName2])

XCTAssertEqual(KeyboardShortcuts.legacyKeyDownHandlers[shortcutName1]?.count, 1)
XCTAssertEqual(KeyboardShortcuts.legacyKeyDownHandlers[shortcutName2]?.count, 1)
XCTAssertEqual(KeyboardShortcuts.legacyKeyUpHandlers[shortcutName1]?.count, 0)
XCTAssertEqual(KeyboardShortcuts.legacyKeyUpHandlers[shortcutName2]?.count, 0)
}
}