-
-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename
.on()
to .events()
and add SwiftUI view modifier for liste…
…ning to events (#79) Co-authored-by: Sindre Sorhus <[email protected]>
- Loading branch information
1 parent
ee60fcf
commit 8a2cc91
Showing
3 changed files
with
108 additions
and
14 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import SwiftUI | ||
|
||
@available(macOS 12, *) | ||
extension View { | ||
/** | ||
Register a listener for keyboard shortcut events with the given name. | ||
|
||
You can safely call this even if the user has not yet set a keyboard shortcut. It will just be inactive until they do. | ||
|
||
The listener will stop automatically when the view disappears. | ||
|
||
- Note: This method is not affected by `.removeAllHandlers()`. | ||
*/ | ||
public func onKeyboardShortcut(_ shortcut: KeyboardShortcuts.Name, perform: @escaping (KeyboardShortcuts.EventType) -> Void) -> some View { | ||
task { | ||
for await eventType in KeyboardShortcuts.events(for: shortcut) { | ||
perform(eventType) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
Register a listener for keyboard shortcut events with the given name and type. | ||
|
||
You can safely call this even if the user has not yet set a keyboard shortcut. It will just be inactive until they do. | ||
|
||
The listener will stop automatically when the view disappears. | ||
|
||
- Note: This method is not affected by `.removeAllHandlers()`. | ||
*/ | ||
public func onKeyboardShortcut(_ shortcut: KeyboardShortcuts.Name, type: KeyboardShortcuts.EventType, perform: @escaping () -> Void) -> some View { | ||
task { | ||
for await _ in KeyboardShortcuts.events(type, for: shortcut) { | ||
perform() | ||
} | ||
} | ||
} | ||
} |