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

Position a popup window next to the caret (was: Get Caret position In Terms of Screen X and Y) #30

Closed
ghost opened this issue Apr 5, 2014 · 11 comments

Comments

@ghost
Copy link

ghost commented Apr 5, 2014

Hi Tomas, Would you be able to add a method to get the caret position in terms of screen X and Y? I have been trying to find a way to do this and came across only two examples, which kind of work, but not quite well. This is my post on Stack overflow.

http://stackoverflow.com/questions/22880996/caret-position-relative-to-screen-coordinates-in-javafx

It would be great if RichTextFX has such a method.

@TomasMikula TomasMikula changed the title Get Caret position In Terms of Screen X and Y: Enhancement. Get Caret position In Terms of Screen X and Y. Apr 5, 2014
@TomasMikula
Copy link
Member

Hi Maher,
I see the usefulness of being able to display a popup window next to the caret. I think this should be done a little differently than obtaining the x,y coordinates of the caret. The reason is that UI controls in JavaFX follow the MVC pattern, where the Control instance itself is the model and a Skin is the view. The visual representation of the control (of which the caret shape is a part) is defined by the skin, not the control itself. You could theoretically implement and use your own skin for TextArea. Then the TextArea will not know how to get the x,y coordinates of the caret from your custom skin.

What I might do is to add a property

ObjectProperty<PopupWindow> popupAtCaretProperty();

which you could set to a popup window you want to position. The skin will observe the property and position the popup window next to the caret, by setting its anchorX and anchorY. You would still be responsible for showing and hiding the popup, all the skin would do is adjust the coordinates. What do you think?

@ghost
Copy link
Author

ghost commented Apr 5, 2014

Hi Tomas, yes that would work. To confirm, it will allow me to load other
things? I am currently using controlsfx popover.

Thanks for looking into This!
Maher
On Apr 5, 2014 10:55 AM, "TomasMikula" [email protected] wrote:

Hi Maher,
I see the usefulness of being able to display a popup window next to the
caret. I think this should be done a little differently than obtaining the
x,y coordinates of the caret. The reason is that UI controls in JavaFX
follow the MVC pattern, where the Controlhttp://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Control.htmlinstance itself is the model and a
Skinhttp://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Skin.htmlis the view. The visual representation of the control (of which the caret
shape is a part) is defined by the skin, not the control itself. You could
theoretically implement and use your own skin for TextArea. Then the
TextArea will not know how to get the x,y coordinates of the caret from
your custom skin.

What I might do is to add a property

ObjectProperty popupAtCaretProperty();

which you could set to a popup window you want to position. The skin will
observe the property and position the popup window next to the caret, by
setting its anchorX and anchorY. You would still be responsible for showing
and hiding the popup, all the skin would do is adjust the coordinates. What
do you think?

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

@TomasMikula
Copy link
Member

Yes, it would allow you to use any subclass of PopupWindow, which PopOver is.

Hopefully I will get to this in the coming week.

@ghost
Copy link
Author

ghost commented Apr 5, 2014

Awesome!
On Apr 5, 2014 12:10 PM, "TomasMikula" [email protected] wrote:

Yes, it would allow you to use any subclass of PopupWindowhttp://docs.oracle.com/javase/8/javafx/api/javafx/stage/PopupWindow.html,
which PopOver is.

Hopefully I will get to this in the coming week.

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

@Ambior
Copy link

Ambior commented Apr 10, 2014

Hi Tomas,
i ve been looking for such method to. I've got some workaround , using this sample
https://community.oracle.com/thread/2534556

Point2D getCaretLocation() {
    final Path caret = findCaret(this, getParagraphs().get(getCurrentParagraph()));
    return (null != caret) ? findScreenLocation(caret) : new Point2D(0.0, 0.0);
}

Path findCaret(final Parent parent, final Paragraph<StyleInfo> paragraph) {
    for (final Node n : parent.getChildrenUnmodifiable()) {
        if (n instanceof ParagraphGraphic && paragraph.equals(((ParagraphGraphic<StyleInfo>) n).getParagraph())) {
            return (Path) ((Parent) n).getChildrenUnmodifiable().get(1);
        } else if (n instanceof Parent) {
            final Path p = findCaret((Parent) n, paragraph);
            if (p != null) {
                return p;
            }
        }
    }
    return null;
}

It somehow works, but your solution sound better :-).

Greetings , Roel

@TomasMikula
Copy link
Member

Thanks for the workaround.

Again, this solution is dependent on the skin implementation (ParagraphGraphic etc.). Although it is unlikely that anyone will write their custom skin for StyledTextArea et al., we should get it right anyway.

@ghost
Copy link
Author

ghost commented Apr 10, 2014

Hi Roel, I found that work around too. But it does not seem to work
properly. For example.
I have to show/ invoke the pop up twice so that gets to the right location.
This is only fr the first time. The second issue. If you happen to have
more than one text area. It starts to act strange.
Best,
Maher
On Apr 10, 2014 4:23 AM, "TomasMikula" [email protected] wrote:

Thanks for the workaround.

Again, this solution is dependent on the skin implementation
(ParagraphGraphic etc.). Although it is unlikely that anyone will write
their custom skin for StyledTextArea et al., we should get it right anyway.

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

@ghost
Copy link
Author

ghost commented Apr 12, 2014

any news :) ? 🎱

@TomasMikula
Copy link
Member

Yes!

popup-demo

See the source code of the above demo.

@TomasMikula TomasMikula changed the title Get Caret position In Terms of Screen X and Y. Position a popup window next to the caret (was: Get Caret position In Terms of Screen X and Y) Apr 12, 2014
@ghost
Copy link
Author

ghost commented Apr 12, 2014

Thanks! Will update the JAR right away!

@ghost
Copy link
Author

ghost commented Apr 12, 2014

Yes! Work perfectly. Thanks!

@ghost ghost mentioned this issue May 31, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants