Reusable components for implementing NSStandardKeyBindingResponding in custom text views.
- SelectionNavigator - Create, modify and extend selections from keyboard and mouse events. Analogous to
NSTextSelectionNavigation
, but more Swifty. - Transposer - Logic for
transpose:
andtransposeWords:
- If you're storing your text in a custom data structure, conform it to
TextContent
.String
,AttributedString.CharacterView
andNSAttributedString
already conform toTextContent
. This is all you need to useTransposer
. - If your text storage has a method of finding paragraph boundaries that's better than O(n), implement
index(ofParagraphBoundaryBefore:)
andindex(ofParagraphBoundaryAfter:)
. - Implement
TextLayoutDataSource
. - Implement
InitializableFromAffinity
,InitializableFromGranularity
andNavigableSelection
for your affinity, granularity, and selection types. - Use the methods on
SelectionNavigation
to derive new selections from your existing selections.
If you're adding StandardKeyBindingResponder to your project KeyBinding Inspector might be helpful for testing.
Note: StandardKeyBindingResponder is very young and its API is not final.
The goal is to have a single struct KeyBindingResponder
that manages all state related to NSStandardKeyBindingResponding. For a simple text view your key binding overrides and mouse event handlers (mouseDown(_:)
, mouseDragged(_:)
, mouseUp(_:)
) should each be a single line of code.
- Multiple selections
- Kill buffer (mark/yank)
- Writing directions (i.e. RTL support)
- CR and CRLF line endings
- An adapter to use
NSTextStorage
asTextContent
- Make
NSLayoutManager
andNSTextLayoutManager
conform toTextLayoutDataSource
KeyBindingResponder
- Scroll handling – scroll to the appropriate location after modifying a selection and scroll key bindings (
scrollPageUp:
etc.). - Insertion and indentation key bindings (
insertNewline(_:)
etc.) - Case changes (
changeCaseOfLetter(_:)
, etc.) - Allow
KeyBindingResponder
to use eitherNSTextSelectionNavigation
orSelectionNavigator
- Scroll handling – scroll to the appropriate location after modifying a selection and scroll key bindings (
- Documentation
Contributions are welcome! If you want to help out, I can walk you through the code, answer questions, etc. Just email me. You can also just open an issue or pull request. For large features, consider opening an issue to discuss before you get started. Issues are tagged with StandardKeyBindingResponder.
StandardKeyBindingResponder is copyright David Albert and released under the terms of the MIT License. See LICENSE.txt for details.