AvaloniaEdit icon indicating copy to clipboard operation
AvaloniaEdit copied to clipboard

ScrollTo() - when First loading a Document, has difficulty scrolling to

Open Natestah opened this issue 1 year ago • 3 comments

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();
    }

Natestah avatar Nov 11 '24 18:11 Natestah

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.

danipen avatar Nov 11 '24 18:11 danipen

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?

Natestah avatar Nov 11 '24 19:11 Natestah

This should do the trick:

var line = AvaloniaTextEditor.Document.GetLineByNumber(lineNumer);
AvaloniaTextEditor.CaretOffset = line.Offset + column;
AvaloniaTextEditor.TextArea.Caret.BringCaretToView(); // ← Try this call.

mgarstenauer avatar Mar 29 '25 09:03 mgarstenauer