How can I tell NSTextView which side to expand the selection on?

If I have NSTextView

one that is in this state:

NSTextView with partial text selected

How can I show the text view that if the user clicks shift+right, then instead of going straight to "o", he cancels "e" instead? Although it is related to affinity , but I tried to set it both for NSSelectionAffinityUpstream

and NSSelectionAffinityDownstream

with the following code:

[self setSelectionRange: NSMakeRange(9,6)
               affinity: x
         stillSelecting: NO];

      

But that hasn't changed. Pressing shift+rightstill selected 'o'.

NSTextView

knows how to do it OUTSIDE, because if you position the cursor between 'w' and 'o' then press shift+leftuntil it matches the screenshot and then press shift+right, this matches the behavior I mentioned.

I'm fine to override the code shift+arrowand flip my own, but I'd rather let NSTextView

my own work. Does anyone know if I am missing something?

+3


source to share


1 answer


I'm not sure what you are trying to do as this is the default text / selection behavior. Are you trying to override this to always do this, or are you trying to add another keyboard shortcut that overrides this behavior? Anyway, some background:

Distinctive closeness doesn't quite work the way it sounds (which came as a surprise to me after studying it now). In fact, there is a gap between affinity and inherited actions NSResponder

corresponding to changes in movement and choice. I'll get to this point.

What you want to watch is the responder's actions like -moveBackwardAndModifySelection:

, -moveWordBackwardAndModifySelection:

etc. In the documentation, the first call to such actions that change the selection action defines the "end" of the selection, which will be modified, followed by calls to change the selection in either direction. This means that if your first action was to select forward ( -moveForwardAndModifySelection:

), the "front end" (right end in left-right languages, left end right to left, automatically) is what will be changed from this point forward. whether you call -moveForward...

or -moveBackward...

.



I found a gap between affinity and -move...AndModifySelection:

by looking at the open source version of Cocoatron NSTextView . Apparently the intrinsic property _affinity is not covered in any of the move and select methods. It determines whether the change in the selection range should be "upstream" or "downstream" based on a _selectionOrigin

private property that is set when the selection ranges are changed (using a -setSelectedRange(s)...

down / drag method or mouse). I think the intimacy property is just for you to consult. Overriding it to always return this or that value doesn't change any behavior, it just misrepresents the outsiders. _selectionOrigin

only seems changed with the method-setSelectedRanges...

if the selection is of zero length (i.e. Just cursor placement).

So with all this in mind, you may need to add one step to modifying the selection manually. First set an empty selection with the place where you want the original selection to be (forward if you want backward affinity, backward if you want direct affinity), then set a nonzero choice with the desired affinity.

Circular and funny, I know, but I think it should be given the source code for CocoaTron.

+1


source







All Articles