1 year ago

#367878

test-img

Tritonal

NSTextView scroll adjustment when typing at end of document

I have a NSTextView with a pretty big bottom indentation. I'm using textDidChange delegate method to adjust some attributes in the text whenever it is changed.

When typing at the edge of the screen and the end of the document, the scroll correctly gets adjusted at the bottom. However, when typing in another character after that, the scroll jumps up a bit.

Looking at the call stack, I see different behavior between typing at a position which will cause the line to wrap, as compared to a normal line.

When typing causes wrapping, the call stack looks like this:

[NSTextView adjustScroll:]
[NSClipView _scrollTo:animateScroll:flashScrollerKnobs:]
[NSTextView keyDown:]

Normally, textDidChange: is called between keyDown: and _scrollTo:, but not here.

However, when typing on the line which doesn't cause a wrap, textDidChange is called as usual:

[NSTextView adjustScroll:]
[NSClipView _scrollTo:animateScroll:flashScrollerKnobs:]
// some attribute changes here
[Document textDidChange:]
[NSTextView keyDown:]

There are multiple questions on SO which state that changing attributes on NSTextView causes all sorts of scrolling trouble, but it's surprising that the scroll values are completely off between these two events. I'd rather have two equal results, but I'm confused what to do here. _scrollTo is a private method and can't be overridden, and I can't really override keyDown either.

Is there a way to hook into the call order of scrolling and the textDidChange event, or to change the control flow of text and clip views?

Further investigation

To reproduce the issue, you'll have to meet at least these conditions:
ā€¢ You are setting attributes in delegate method
ā€¢ Text view is scaled (using scaleUnitSquareToSize)
ā€¢ You might need to make changes to both temporary attributes and the text view's attributed string
ā€¢ Non-contiguous layout is on.

Based on this thread from 2016 it seems to be a non-contiguous layout bug, which is yet to be fixed. A blog post from 2017 goes through similar issues regarding the order in which text and attributes are processed.

objective-c

macos

cocoa

appkit

nstextview

0 Answers

Your Answer

Accepted video resources