Created
July 13, 2021 09:00
-
-
Save Fiser12/b14c0a5006b26d33036298e389585c6d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Combine | |
import ComposableArchitecture | |
import KeyboardShortcuts | |
public struct KeyboardShortcutManager { | |
public enum Action: Equatable { | |
case onKeyDown(UUID) | |
case onKeyUp(UUID) | |
} | |
var bind: ([UUID]) -> Effect<Action, Never> = { _ in _unimplemented("bind") } | |
public func bind(uuids: [UUID]) -> Effect<Action, Never> { | |
self.bind(uuids) | |
} | |
} | |
extension KeyboardShortcutManager { | |
public struct Properties: Equatable { | |
var uuid: UUID? = nil | |
public init(uuid: UUID? = nil) { | |
self.uuid = uuid | |
} | |
} | |
} | |
extension KeyboardShortcutManager { | |
private static var subscriber: Effect<KeyboardShortcutManager.Action, Never>.Subscriber? = nil | |
public static let live: () -> KeyboardShortcutManager = { | |
var manager = KeyboardShortcutManager() | |
manager.bind = { uuids in | |
Effect.run { subscriber in | |
KeyboardShortcutManager.subscriber = subscriber | |
uuids.forEach{ uuid in | |
KeyboardShortcuts.onKeyDown(for: KeyboardShortcuts.Name(uuid.uuidString)) { | |
subscriber.send(.onKeyDown(uuid)) | |
} | |
KeyboardShortcuts.onKeyUp(for: KeyboardShortcuts.Name(uuid.uuidString)) { | |
subscriber.send(.onKeyUp(uuid)) | |
} | |
} | |
return AnyCancellable { | |
KeyboardShortcutManager.subscriber = nil | |
} | |
} | |
} | |
return manager | |
} | |
} | |
public func _unimplemented( | |
_ function: StaticString, file: StaticString = #file, line: UInt = #line | |
) -> Never { | |
fatalError( | |
""" | |
`\(function)` was called but is not implemented. Be sure to provide an implementation for | |
this endpoint when creating the mock. | |
""", | |
file: file, | |
line: line | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here an example of how to bind one UUID for listening when they are pressing buttons. I am using UUIDs as strings to avoid possible collisions. You should register the key previously with the library KeyboardShortcuts from SwiftUI allow the user to create customizable keys for custom elements.