AvaloniaEdit
AvaloniaEdit copied to clipboard
ScrollTo() - when First loading a Document, has difficulty scrolling to
I have this gnarly little bit of code in Blitz Search with a Todo Note to bug AvaloniaEdit about it..
I have noticed ScrollTo not resolving if I set the text document, and immediately follow up with calling scroll to.
//I don't like this, Maybe we can work it into AvaloniaEdit itself "Load a document and center it on this line when things are finished"
for (int i = 0; i < 2; i++)
{
AvaloniaTextEditor.ScrollTo(lineNumer, 1);
try
{
var line = AvaloniaTextEditor.Document.GetLineByNumber(lineNumer);
AvaloniaTextEditor.CaretOffset = line.Offset + column;
}
catch (ArgumentOutOfRangeException e)
{
Console.WriteLine(e);
break;
}
try
{
await Task.Delay(50,_currentToken.Token);
}
catch (OperationCanceledException)
{
return;
}
if (_currentToken.IsCancellationRequested)
{
break;
}
}
AvaloniaTextEditor.TextArea.TextView.Redraw();
}
Probably you need to post setting the caret offset I to the Avalonia dispatcher to wait until the layout has finished and visual lines have been generated.
I'm somewhat familiar with the inner workings, but I am hoping to get some way to have to be less smart about that on the Editor itself. Can it do that automatically?
This should do the trick:
var line = AvaloniaTextEditor.Document.GetLineByNumber(lineNumer);
AvaloniaTextEditor.CaretOffset = line.Offset + column;
AvaloniaTextEditor.TextArea.Caret.BringCaretToView(); // ← Try this call.