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

In a multi-line paragraph how to highlight the line the caret is on? #841

Closed
garybentley opened this issue Aug 15, 2019 · 4 comments
Closed

Comments

@garybentley
Copy link

I'm wanting to have the line the caret is currently on be highlighted. I've asked this before #611 but a lot has changed since I first asked and now I'm thinking of doing:

  1. Add a listener to the caret position.
  2. In that listener get the line the caret is current on.
  3. Adding a selection with a custom style for the start/end positions of the line.

Would this approach work?

The main issue I can see is that there doesn't seem to be any way to get the text offsets of the start and end of the line. Is there any way to get those offsets?

@garybentley
Copy link
Author

I've now implemented this but using a different method. I'm listening to the caret bounds and then drawing and positioning a Pane behind the area for the bounds position.

Thus:

Pane background = new Pane();
Pane highlight = new Pane();
background.getChildren().add(highlight);
GenericStyledArea<?, ?, ?> area = new GenericStyledArea(...);
StackPane sp = new StackPane();
sp.getChildren().addAll(background, area);
area.caretBoundsProperty().addListener((pr, oldv, newv) ->
{
   Bounds b = background.screenToLocal(newv.get());
   highlight.relocate(0, b.getMinY());
   highlight.setPrefHeight(b.getHeight());
   highlight.setPrefWidth(area.getWidth());
});

This produces something like the following:

text-line-highlight

Note: sometimes the caret bounds aren't present.

While this works, it would be nice to have an "in area" method of doing this. This type of feature is common in writing applications and code editors.

@Jugen
Copy link
Collaborator

Jugen commented Aug 16, 2019

That's a good hack :-)

I've had a look at this and I'm planning on adding the following API to GenericStyledArea:

setLineHighlighterFill( Color highlight )
setLineHighlighterOn( boolean show )
isLineHighlighterOn()
getCurrentLineStartInParargraph()
getCurrentLineEndInParargraph()

Any thoughts or comments welcome.

@garybentley
Copy link
Author

That would be some great additions. It might be better to have a Paint rather than a Color though, for those who want to do something a little fancier.

The only problem with my workaround is that I'm now seeing this problem #842.

@Jugen
Copy link
Collaborator

Jugen commented Aug 29, 2019

RichTextFX 0.10.2 has been released with this feature.

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

No branches or pull requests

2 participants