UltravioletFramework / ultraviolet

The Ultraviolet Framework is a .NET game development framework written in C#.
https://github.com/UltravioletFramework/ultraviolet/wiki
MIT License
541 stars 46 forks source link

UPF Textbox ScrollToEnd scrolls to one line above end. #129

Closed fireball87 closed 4 years ago

fireball87 commented 4 years ago

I have a read only textbox in UPF that is created via

<TextBox Name="chatTextBox" Grid.Row="0" Grid.Column="0" VerticalScrollBarVisibility="Visible" IsReadOnly="True"></TextBox>

I add to it using the code

private void addChatText(string text)
            {
                bool scrolledToBottom = chatTextBox?.VerticalOffset > chatTextBox?.ExtentHeight - chatTextBox?.ViewportHeight - 50;
                chatTextBox?.AppendText(text + "\n");
                if (scrolledToBottom)
                {
                    chatTextBox?.ScrollToEnd();
                }
            }

The detection of the bottom works well enough, but when scroll to end is fired it ends up scrolling to one line above the end. In my code you can see that I end with a new line, this is what I am currently doing as a workaround (where otherwise i would add new lines at the beginning of all but the first line)

For stuff I have tried making sure all of this runs in the UV event loop (doesn't seem to affect things either way/doesn't fix the bug), I have also tried sending only the scroll to the event loop only as a method of delaying the call, but this still gives the same effect.

For my system info, Manjaro Linux running KDE Dotnet Core 3.1

image An image of where scroll to end will scroll to.

image An image of the actual end of the line, with no newline at the end this would be cutting a line of text.

tlgkccampbell commented 4 years ago

This was a result of the call to ScrollToEnd() happening before the TextBox's layout was updated. Commit e3c8cfe1defc0e2f54be5858cf78f307c5e8419b addresses this issue by forcibly updating the control's layout whenever any of the scrolling methods are called.