Git Product home page Git Product logo

indsequentialtextselectionmanager's Introduction

INDSequentialTextSelectionManager

Easy WebView-like text selection in multiple text views

When building Flamingo, I wanted to find a way to select text through multiple NSTextViews in a single drag, similar to how it works in a WebView. After finding out that there's nothing in AppKit that would do this for me, I developed this project to replicate that functionality.

Here's a GIF demonstrating how it works:

INDSequentialTextSelectionManager

WARNING: This is alpha quality code at the moment and has not been tested in production.

How to use

The API is extremely simple. These two methods are all you'll need:

- (void)registerTextView:(NSTextView *)textView withUniqueIdentifier:(NSString *)identifier;
- (void)unregisterTextView:(NSTextView *)textView;

Allocate an instance of INDSequentialTextSelectionManager for every group of NSTextViews that you want to participate in sequential selection. When a text view goes on screen, call -registerTextView:withUniqueIdentifier:. The unique identifier should be something relevant to the content of the text view and not the text view instance itself. Since INDSequentialTextSelectionManager is designed to support things like cell recycling (where a single NSTextView instance is shared among multiple cells), the unique identifier is used instead of the text view instance itself to keep track of the selection state. The unique identifier must be unique for each text view being tracked by the manager.

If a text view's frame, bounds, or location in the view hierarchy changes, -registerTextView:withUniqueIdentifier: must be called again to let the manager know to update its cached layout information.

When a text view goes off-screen, call -unregisterTextView: to ensure that it's no longer tracked by the manager.

Example: NSTableView

Implementing INDSequentialTextManager support with an NSTableView is as easy as implementing these 2 delegate methods:

- (void)tableView:(NSTableView *)tableView didRemoveRowView:(NSTableRowView *)rowView forRow:(NSInteger)row
{
	INDTableCellView *cellView = [self cellViewForRowView:rowView];
	[self.selectionManager unregisterTextView:cellView.textView];
}

- (void)tableView:(NSTableView *)tableView didAddRowView:(NSTableRowView *)rowView forRow:(NSInteger)row
{
	INDTableCellView *cellView = [self cellViewForRowView:rowView];
	[self.selectionManager registerTextView:cellView.textView withUniqueIdentifier:@(row).stringValue];
}

(Full code can be found in the Example project)

TODO

  • Support for other NSTextView contextual menu items like Services, Text to Speech, etc.
  • Autoscroll when dragging
  • Proper inactive state for selection

Contact

License

INDSequentialTextSelectionManager is licensed under the MIT License.

indsequentialtextselectionmanager's People

Contributors

indragiek avatar thomasdao avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.