question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add feature to InlineCssTextArea to dynamically change highlight color

See original GitHub issue

Desired Feature

It’s currently possible to change dynamically/programmatically the text coloring with the InlineCssTextArea using for example:

setStyle(startRange, endRange, "-fx-fill:red");

Using “-fx-highlight-fill” does not seem to work and I have seen this property discussed elsewhere. It would be great to be able to also change the coloring of the (mouse) selection highlight on the fly via code, without a dependency on css stylesheets. Ideally you would be able to specify this per range (as you can for the the text fill color in the example above), however just being able to change the single color (from the default DODGERBLUE in the SelectionPath property) for all mouse selections on the fly would also be very useful. I can see that that Selection and CaretSelectionBind provide a:

    void configureSelectionPath(SelectionPath path);

method, but it does not seem possible to hook into this (and perhaps that would be the wrong approach anyway). It may already be possible to achieve this dynamically. If so, any directions would be much appreciated.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11

github_iconTop GitHub Comments

1reaction
Yossi-77commented, Aug 31, 2021

Thanks again for the suggestion. I’ve tried that, but no luck. I’ve also tried putting the addSelection in a runLater (still no luck).

I have got something that does work though… It’s pretty desperate:

public class TestTextArea extends InlineCssTextArea {

	public TestTextArea() {

		final Selection underlyingSelection = getCaretSelectionBind().getUnderlyingSelection();

		Selection overlaySelection = new SelectionImpl("overlay", this, path -> ((Path) path).setFill(Color.GREEN)) {

			@Override
			public int hashCode() {

				return underlyingSelection.hashCode();
			}


			@Override
			public boolean equals(Object o) {

				return underlyingSelection.equals(o);
			}

		};

		selectionProperty().addListener((obs, oldVal, newVal) -> {

			removeSelection(overlaySelection);
			overlaySelection.selectRange(underlyingSelection.getStartParagraphIndex(), underlyingSelection.getStartColumnPosition(), underlyingSelection.getEndParagraphIndex(), underlyingSelection.getEndColumnPosition());
			addSelection(overlaySelection);
		});
	}
}

I think I could see in the code that the Selections end up in a set. There is a check in the code that should prevent the main underlying selection being removed, but given it’s going into a set, that check can be circumvented by hacking the equals method of any new Selection. My theory was that the underlying Selection was being rendered over the overlay Selection (I don’t know if that is actually true) and getting rid of the underlying Selection might leave the desired behavior.

If this gives you any clue as to how to put in place a cleaner solution it would be great to know. For now, I guess I will keep this in place, given it appears to function robustly, even if pretty terrible code.

0reactions
Yossi-77commented, Aug 31, 2021

Solution found - link a custom Selection’s range to the underlying selection attached to the CaretSelectionBind and make the underlying selection transparent to prevent it rendering over the custom selection.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Changing Font/Size/Color/Format of highlighted text in ...
Assuming you are talking about InlineCssTextArea from RichTextFX, use the combination of getSelection() and setStyle(from, to, style) :
Read more >
Dynamically change color / highlight. - SAP Community
Hi, I have 3 radio button, and was group as radio button group with FctCode 'RG'. When select a radio button, the text...
Read more >
RichTextFX/CHANGELOG.md at master - GitHub
Add a setFont method to CodeArea #754; Feature: Set padding between lines in ... I get IndexOutOfBoundsException #741; CSS selection color from 0.7...
Read more >
Adding dynamic color to your app - Google Codelabs
In this codelab, you'll migrate the theming in an app to Material 3 and later apply dynamic color.
Read more >
Dynamically controlling cell background color — OpenText
There is a feature in BIRT called a Highlight. Highlights are conditional styles. With them, you can control cell background colors. So, select...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found