Selection is not visible when editor is loaded

SyntaxEditor for WPF Forum

Posted 4 months ago by Tobias Lingemann - Software Devolpment Engineer, Vector Informatik GmbH
Version: 21.1.0
Platform: .NET 4.7
Environment: Windows 10 (64-bit)
Avatar

Hi,

when we open an editor instance with a specific selection, the view is not scrolled. So the selection might not be visible when the document is opened. When we change the selection after the view is loaded, the view is scrolled to make the selection visible.

As a workaround we reset the selection after the view is loaded, but I think the SyntaxEditor component should automatically scroll the view to make sure the selection is visible.

private void OnViewLoaded()
{
  MakeSureSelectionIsVisible();
}

private void MakeSureSelectionIsVisible()
{
  var selection = SyntaxEditor.ActiveView.Selection;
  var selectedRange = selection.TextRange;

  if (SyntaxEditor.ActiveView.VisibleViewLines.SnapshotRange.Contains(selectedRange))
  {
    return;
  }

  // To force that selection becomes visible, we have to change the selection first. Otherwise nothing happens.
  selection.MoveToDocumentStart();
  selection.SelectRange(selectedRange);
}


Best regards, Tobias Lingemann.

Comments (3)

Posted 4 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Tobias,

I worry about if we would add code to scroll it automatically on load, that could break other customer functionality where they may be persisting and restoring the previous usage scenario's scroll state.

You could change your OnViewLoaded method to simply be this instead:

SyntaxEditor.ActiveView.Scroller.ScrollToCaret();


Actipro Software Support

Posted 4 months ago by Tobias Lingemann - Software Devolpment Engineer, Vector Informatik GmbH
Avatar

I just think its weird that the control behaves differently whether it is already loaded or not. Maybe there is a way to handle both cases. When you first restore the caret position and then restore the scroll state, it should work either way. But when the scroll position is restored first, you could loose the state if the caret is not visible.

Another way would be to make the restoring of caret and scroll state a unified operation. But that would require clients to update their code, if they don't want the caret to become visible.


Best regards, Tobias Lingemann.

Posted 4 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Tobias,

I did some tracing through the scroller logic.  It actually is already calling ScrollToCaret on any selection change, even before the control is loaded.  The problem is that if the SyntaxEditor hasn't been measured/arranged yet at the time you set the selection, the text area of it is still zero-sized.  Thus the scrolling logic at that point doesn't know how to arrange the lines and quits without doing anything.

Related to your other question about restoring caret position and scroll state, I would generally recommend restoring the scroll state first, then the selection.  If you do your selection restore with a batch, then you can specify EditorViewSelectionBatchOptions.NoScrollToCaret to prevent it from scrolling for that selection change.


Actipro Software Support

The latest build of this product (v21.1.3) was released 12 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.