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

caret and anchor stale after selectRange with listeners #874

Closed
Groostav opened this issue Nov 22, 2019 · 7 comments · Fixed by #875 or #877
Closed

caret and anchor stale after selectRange with listeners #874

Groostav opened this issue Nov 22, 2019 · 7 comments · Fixed by #875 or #877

Comments

@Groostav
Copy link

Groostav commented Nov 22, 2019

If you attempt to use selectRange to update caret and anchor positions that have attached listeners, the caret and anchor potitions will be stale.

@Test fun `sscce`(){
    val underTest = StyleClassedTextArea()

    //comment out this line of code and the test passes
    val dontCare = Bindings.createStringBinding(Callable { "binded" }, underTest.caretPositionProperty(), underTest.anchorProperty())
    underTest.appendText("asdf")
    
    //act
    underTest.selectRange(1, 2)

    //assert
    assertThat(underTest.selectedText).isEqualTo("s") //OK
    assertThat(listOf(underTest.anchor, underTest.caretPosition)).isEqualTo(listOf(1, 2)) //fail
    //actual: 0, 2
}
@Groostav
Copy link
Author

Groostav commented Nov 22, 2019

In my production code i have both the caret and anchor positions as 0, 0 after calling selectRange, but in this SSCCE only the anchor is stale; the caret is correct. I'll see if I can add another example here.

@Jugen
Copy link
Collaborator

Jugen commented Nov 24, 2019

I've had a look and it appears that this happens when a listener is added to the anchorProperty ?!
So it works correctly if we have:

val dontCare = Bindings.createStringBinding
(
   () -> "binded", underTest.caretPositionProperty()
);

Will look further into this .....

@Groostav
Copy link
Author

Thanks @Jugen , I can confirm this fixes the SSCCE and my problem in production. Any idea when this will make it into maven?

thanks again for the fix!

@Jugen
Copy link
Collaborator

Jugen commented Nov 29, 2019

I published to maven on Wed so hopefully today or tomorrow ....

@Groostav
Copy link
Author

@Jugen Unfortunatly while this now makes it through our tests we discovered the problem is better but still bad in production.

Is it possible the same or a similar problem is happening to the anchor position?

@Jugen
Copy link
Collaborator

Jugen commented Dec 1, 2019

Umm, did you maybe mean caret position and not anchor position ?
Otherwise can you provide more information.

@Groostav
Copy link
Author

Groostav commented Dec 4, 2019

@Jugen No I did mean anchor position. The caret appears to be working correctly.

Firstly: the use case is to have a richTextFX text area, with a couple of labels bound to metadata about that text areas selection --a pretty common thing if you look at gedit or notepad++.

I've found that if I leave my selection-listeners on the text area it changes the behaviour of the text area. Text selection with the mouse becomes stunted and heavily prone to artificially moving the caret to the end of the document or the anchor to the beginning of the document. This is what lead to the original report. Since your fix the behaviour got better but it is not corrected. If I leave my selection-listeners on the text area, I still find it selecting the wrong ranges frequently.

I have tried to get this down to an SSCCE but have had difficulty.

But I have found a workaround: rather than bind to the anchorProperty and the caretPositionProperty, I'm simply binding to the selectedTextProperty.

I still need to do some work here, I'll let you know when I've got something reasonably actionable for you, but this one is tricky to run down.

probably what an "SSCCE" is going to look like is a live UI with some console output, and then a statement that "this console output should not be here".

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