Quantcast
Channel: SharpDevelop Community
Viewing all articles
Browse latest Browse all 1764

Scrolling to last line not always working in AvalonEdit

$
0
0

I wanted to add an "auto-scroll" behaviour to my application which uses AvalonEdit as text editor. The idea is that when text is programmatically added to the end, and the user has currently scrolled to the end, then scroll so that the new text is visible (ex. the behaviour in the output windows of Visual Studio).

When text is added, I check if the scroll viewer is currently scrolled to the end, and if the added text is added to the end. If so, after the text was added, I call TextEditor.ScrollToLine(Document.LineCount).

However, while this initially worked well, I hit some unexpected bugs in AvalonEdit. After some time, ScrollToLine() doesn't scroll there anymore when adding a single line.

I created a gist with the relevant files: https://gist.github.com/DanielRose/c94f11f1a972a453902e Run it and first click on "Add text (many lines)", then often on "Add text (single lines)". After several lines, the auto-scrolling stops. In the code, note the commented section where you can see what I tried (didn't make any difference).

 

From my debugging so far it seems the following happens: The scroll bars are set to Auto. After a while, the underlying ScrollViewer gets in a mode where it re-measures itself and the children, and wants to check if the scroll bars are still needed. It does this by measuring without any scroll bars (i.e. the available size is the size including the area of the scroll bars), and then later with the scroll bars (if needed, and the available size is again without the scroll bars). The scroll bars are normally the height of one text line.

So when the measure pass without the scroll bars happens, the text is already scrolled to the end. So no scrolling needs to be done. Then, when the scroll bars are added again, they cover the last line. However, the scrolling has already happened, so the last line stays covered.

Since now the ScrollViewer is not scrolled to the end anymore, the auto-scrolling stops.

For info, check the debug output in CreateAndMeasureVisualLines() (you can see the different sizes there) and IScrollInfo.ScrollOwner (esp. the ComputedVerticalScrollBarVisibility and ComputedHorizontalScrollBarVisibility).

 

So far, I have found no easy way to fix this besides not using Auto as scroll bar visibility.


Viewing all articles
Browse latest Browse all 1764

Trending Articles