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

Any efficient way to setStyle for multiple positions? #1177

Open
cyfung1031 opened this issue Mar 9, 2023 · 2 comments
Open

Any efficient way to setStyle for multiple positions? #1177

cyfung1031 opened this issue Mar 9, 2023 · 2 comments

Comments

@cyfung1031
Copy link

cyfung1031 commented Mar 9, 2023

I am using setStyleClass of StyleClassedTextArea to make colors in a long text paragraph. I have different colors to apply to different words.

Say classNames style-1 is red, style-2 is blue, style-3 is green.

If i want to apply style-1 to multiple sections of the textarea, I need to run them as a loop.

area.setStyleClass(2, 5, "style-1");
area.setStyleClass(6, 11, "style-1");
area.setStyleClass(23, 24, "style-1");
area.setStyleClass(52, 65, "style-1");
area.setStyleClass(76, 121, "style-1");
area.setStyleClass(150, 154, "style-1");
...

I find it is slow. I guess it is due to it renders every time I applied the styleclass. I tried to set it invisible first but in vain.
Any way to make the rendering more efficient?

or any batchEditStart(), batchEditEnd() for styling? So it will not immediately render before I set all style-1, style-2, style-3

@cyfung1031
Copy link
Author

cyfung1031 commented Mar 10, 2023

I think I have found the suitable demo for this question.

https://github.com/FXMisc/RichTextFX/blob/master/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/JavaKeywordsDemo.java

https://github.com/FXMisc/RichTextFX/blob/master/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/SpellCheckingDemo.java

I should have set a kind of observer to observe the changes before setting the text to the textarea.

I would suggest that it is better to add some explanation for StyleSpans how it works.
I did not know that it requires reactfx (org.reactfx.collection.ListModification).
As a beginner to this library, I thought org.fxmisc.richtext is just enough.

And from the word StyleSpans suggested, I did not know it can work with style name using Collections.singleton(styleClass), and even it is for a continuous segment of text, we can use Collections.emptyList() to by pass those segments we don't need to apply any formatting.


However, I do not understand the differences between these two implementations.

        textArea.multiPlainChanges()
                .successionEnds(Duration.ofMillis(500))
                .subscribe(change -> {
                    textArea.setStyleSpans(0, computeHighlighting(textArea.getText()));
                });
    codeArea.getVisibleParagraphs().addModificationObserver
        (
            new VisibleParagraphStyler<>( codeArea, this::computeHighlighting )
        );

Any pros & cons for these two implementations? How to choose?

@Jugen
Copy link
Collaborator

Jugen commented Mar 10, 2023

When I was looking at StyleSpans this morning I also thought: this needs better documentation. :-)

With regards to the two implementations:
The first implementation passes the textArea's entire text to computeHighlighting, but only 500ms after the last edit.
The second implementation does an an immediate computeHighlighting, but only on any changed visible paragraphs.

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