Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: How to scroll to selection in CodeArea? #932

Closed
prat-man opened this issue Jun 23, 2020 · 2 comments · Fixed by #933
Closed

Question: How to scroll to selection in CodeArea? #932

prat-man opened this issue Jun 23, 2020 · 2 comments · Fixed by #933

Comments

@prat-man
Copy link
Contributor

Currently I am aware that we need to use requestFollowCaret() to scroll to caret.

However, I am not finding a solution for displaying the selected text (entirely if fits in viewport, otherwise partially).
For most cases it is working fine. However, sometimes, the selected text is just to the left of the viewport and scrolling a bit to the left reveals the highlighted text.

Is there some way I can make sure that the selected text gets displayed inside the viewport?

Here is a sample code of what I am doing right now:

codeArea.selectRange(start, end);
codeArea.requestFollowCaret();

I tried doing the following, but to no avail:

codeArea.moveTo(start);
codeArea.requestFollowCaret();

codeArea.selectRange(start, end);

The use of these selections, for me, is for a find (and replace) option as found in most text editors.

@Jugen
Copy link
Collaborator

Jugen commented Jun 25, 2020

Thanks, I'll submit a PR to address this.

BTW your codeArea.moveTo(start); .... attempt can work but it requires a scene graph pulse to occur between it and codeArea.selectRange(start, end); .... So like a 20ms delay needs to happen between the two, which is a pain.

An alternative which I haven't tried is to change the anchor position of the selection when needed. So if the next selection is to the left of the last selection [use getParagraphSelection( parIdx ) to get the selection start & end relative to the paragraph] then select the word/phrase backwards so that the caret is at the start of the selection.

The above are just suggestions if you can't use the PR or wait until the next release.

@prat-man
Copy link
Contributor Author

I found a simple solution to my problem.

// select the text
codeArea.selectRange(start, end);

// scroll to selection
codeArea.scrollXToPixel(0);
codeArea.requestFollowCaret();

This works perfectly, by first scrolling to the very left, and then scrolling to the caret, making it always display the selection inside the viewport.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants