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

Style At Caret Vs that of Insert(StyledDocument). #46

Closed
ghost opened this issue Apr 22, 2014 · 6 comments
Closed

Style At Caret Vs that of Insert(StyledDocument). #46

ghost opened this issue Apr 22, 2014 · 6 comments
Labels

Comments

@ghost
Copy link

ghost commented Apr 22, 2014

Hi Tomas,
I have the following scenario:

I create a text, style it, then save it to a clip board as a styled document.

Then i apply a different style at the caret. using:

    richTextArea.plainTextChanges().subscribe(change -> {
            int from = change.getPosition();
            int length = change.getInserted().length();
            richTextArea.setStyle(from, from + length, style);
    });

After this step, if I try to insert the styledDocument that I saved previously, it will come in using the last style applied at the caret, not its own style. So this is similar to inserting plain text.

I guess this is as expected, since the text area is subscribing to change, and any change will take in the style last applied.
I tried to do something like

        StyleSpans spans = styledDoc.getStyleSpans(0);
        richTextArea.plainTextChanges().subscribe(change -> {
            int from = change.getPosition();
            int length = change.getInserted().length();
            richTextArea.setStyleSpans(richTextArea.getCaretPosition(), spans);
        });
        richTextArea.insert(richTextArea.getCaretPosition(), styledDoc);

but with no luck. I also tried to set the subscribe to null.. but I gt an error.
is there a way to override that subscription or stop it in the event when i am inserting a styled document?

@ghost
Copy link
Author

ghost commented Apr 22, 2014

(update: cleaned up comments)
if I apply a style using the method below, which relies on applying a style to a rang, I do not run into the problem above.

I apply a style to a range. Range is is defined by adding white space when needed ( if the caret happens to be at the end) or by coloring the character before the caret. (not the best behavior I admit).

       //if caret is at the end.. then add a white space
       if (richTextArea.getCaretPosition() == richTextArea.getLength()) {
           richTextArea.appendText(" ");
        }
       richTextArea.setStyle(richTextArea.getCaretPosition(), richTextArea.getCaretPosition()+1, style); 

Because this does not utilize the subscribe to change method, I can append styled documents and have them come in with their styles. The problem with it is that it is not as clean.

Would it be possible to add a method that will unsubscribe, or override caret style, append styled document, then revert caret style. Not sure what the best behavior is. What do you suggest? :/

@TomasMikula
Copy link
Member

Hi Maher,

subscribe returns a Subscription which you can use to unsubscribe (forever):

Subscription sub = area.plainTextChanges().subscribe(...);

// when no longer interested in changes
sub.unsubscribe();

To ignore changes temporarily, you can do this

InterceptableEventStream<PlainTextChange> changes =
        richTextArea.plainTextChanges().interceptable();

changes.subscribe(change -> {
    // handle the change
});

// ignore changes while inserting styled document
changes.muteWhile(() -> {
    richTextArea.insert(index, document);
});

or this

Indicator ignoreChanges = new Indicator();

richTextArea.plainTextChanges().subscribe(change -> {
    if(ignoreChanges.isOff()) {
        // handle the change
    }
});

// ignore changes while inserting styled document
ignoreChanges.onWhile(() -> {
    richTextArea.insert(index, document);
});

@ghost
Copy link
Author

ghost commented Apr 23, 2014

Thanks!

On Tue, Apr 22, 2014 at 7:11 PM, TomasMikula notifications@github.comwrote:

Hi Maher,

subscribe returns a Subscription which you can use to unsubscribe
(forever):

Subscription sub = area.plainTextChanges().subscribe(...);
// when no longer interested in changessub.unsubscribe();

To ignore changes temporarily, you can do this

InterceptableEventStream changes =
richTextArea.plainTextChanges().interceptable();
changes.subscribe(change -> {
// handle the change});
// ignore changes while inserting styled documentchanges.muteWhile(() -> {
richTextArea.insert(index, document);});

or this

Indicator ignoreChanges = new Indicator();
richTextArea.plainTextChanges().subscribe(change -> {
if(ignoreChanges.isOff()) {
// handle the change
}});
// ignore changes while inserting styled documentignoreChanges.onWhile(() -> {
richTextArea.insert(index, document);});


Reply to this email directly or view it on GitHubhttps://github.com//issues/46#issuecomment-41117645
.

@ghost
Copy link
Author

ghost commented Apr 23, 2014

I tried the :

InterceptableEventStream changes =
richTextArea.plainTextChanges().interceptable();

changes.subscribe(change -> {
// handle the change
});

it worked, but the unsubscribe didn't. I can;t seem to unsubscribe.

The process is this:

I create a view port, which as a text area, and declare subscription
object: subtoChange.

Also a bunch of styling buttons are created.

When style parameter button is pressed. the subscription object gets
initialized.

So it happens the first time, and continues to be reset again, every time a
styling button is pressed.

   editor.subtoChange

= editor.richTextArea.plainTextChanges().subscribe(change -> {
int from = change.getPosition();
int length = change.getInserted().length();
editor.richTextArea.setStyle(from, from + length, style);
});

and when I want to insert a styled document, I do this

        editor.subtoChange.unsubscribe();

editor.richTextArea.insert(editor.richTextArea.getCaretPosition(),
styledDoc);

do you see anything incorrect in the process?

On Tue, Apr 22, 2014 at 7:14 PM, Maher Elkhaldi m.s.khaldi@gmail.comwrote:

Thanks!

On Tue, Apr 22, 2014 at 7:11 PM, TomasMikula notifications@github.comwrote:

Hi Maher,

subscribe returns a Subscription which you can use to unsubscribe
(forever):

Subscription sub = area.plainTextChanges().subscribe(...);
// when no longer interested in changessub.unsubscribe();

To ignore changes temporarily, you can do this

InterceptableEventStream changes =
richTextArea.plainTextChanges().interceptable();
changes.subscribe(change -> {
// handle the change});
// ignore changes while inserting styled documentchanges.muteWhile(() -> {
richTextArea.insert(index, document);});

or this

Indicator ignoreChanges = new Indicator();
richTextArea.plainTextChanges().subscribe(change -> {
if(ignoreChanges.isOff()) {
// handle the change
}});
// ignore changes while inserting styled documentignoreChanges.onWhile(() -> {
richTextArea.insert(index, document);});


Reply to this email directly or view it on GitHubhttps://github.com//issues/46#issuecomment-41117645
.

@ghost
Copy link
Author

ghost commented Apr 23, 2014

on a second thought i think I know why.

I am inserting at the end of a document. so if my guess is correct, when
inserting, it pushes the caret (which has an older style) and inserts
inside. so when done inserting.. and I type again after the inserted
document, i will be typing within the old style.

Is my guess correct?

On Tue, Apr 22, 2014 at 8:32 PM, Maher Elkhaldi m.s.khaldi@gmail.comwrote:

I tried the :

InterceptableEventStream changes =
richTextArea.plainTextChanges().interceptable();

changes.subscribe(change -> {
// handle the change
});

it worked, but the unsubscribe didn't. I can;t seem to unsubscribe.

The process is this:

I create a view port, which as a text area, and declare subscription
object: subtoChange.

Also a bunch of styling buttons are created.

When style parameter button is pressed. the subscription object gets
initialized.

So it happens the first time, and continues to be reset again, every time
a styling button is pressed.

   editor.subtoChange

= editor.richTextArea.plainTextChanges().subscribe(change -> {
int from = change.getPosition();
int length = change.getInserted().length();
editor.richTextArea.setStyle(from, from + length, style);
});

and when I want to insert a styled document, I do this

        editor.subtoChange.unsubscribe();

editor.richTextArea.insert(editor.richTextArea.getCaretPosition(),
styledDoc);

do you see anything incorrect in the process?

On Tue, Apr 22, 2014 at 7:14 PM, Maher Elkhaldi m.s.khaldi@gmail.comwrote:

Thanks!

On Tue, Apr 22, 2014 at 7:11 PM, TomasMikula notifications@github.comwrote:

Hi Maher,

subscribe returns a Subscription which you can use to unsubscribe
(forever):

Subscription sub = area.plainTextChanges().subscribe(...);
// when no longer interested in changessub.unsubscribe();

To ignore changes temporarily, you can do this

InterceptableEventStream changes =
richTextArea.plainTextChanges().interceptable();
changes.subscribe(change -> {
// handle the change});
// ignore changes while inserting styled documentchanges.muteWhile(() -> {
richTextArea.insert(index, document);});

or this

Indicator ignoreChanges = new Indicator();
richTextArea.plainTextChanges().subscribe(change -> {
if(ignoreChanges.isOff()) {
// handle the change
}});
// ignore changes while inserting styled documentignoreChanges.onWhile(() -> {
richTextArea.insert(index, document);});


Reply to this email directly or view it on GitHubhttps://github.com//issues/46#issuecomment-41117645
.

@TomasMikula
Copy link
Member

Calling subscribe multiple times does not reset the subscription, but registers a new one every time. You only unsubscribe the latest one.

You shouldn't call subscribe multiple times, once is enough, when you set up the scene. If you subscribe on every button click, it means the subscription code will be executed many times on every change. This will eventually consume too much CPU as well as memory.

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

No branches or pull requests

1 participant