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

8222210: JFXPanel popups open at wrong coordinates when using multiple hidpi monitors #924

Closed
wants to merge 4 commits into from

Conversation

johanvos
Copy link
Collaborator

@johanvos johanvos commented Oct 20, 2022

The root problem is actually broader than stated in the JBS issue. This PR now translates screencoordinates from absolute coordinates into coordinates that take the platformScale into account.
The whole process is complicated by the fact that throughout our code, we use e.g. x and y without clearly stating if those are absolute, logical, screen or rendering coordinates.
I believe the most consistent approach is to have the different entry points (e.g. a Glass Window or a JFXPanel) to deal with platformScale before passing screen coordinates. This is already done in the Glass approach, and this PR does the same in JFXPanel. That means some code is duplicated, but since this is only about 12 lines, and said code lives in 2 different modules, I think it's not worth the hassle of moving that into e.g. the base module.


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed (2 reviews required, with at least 1 Reviewer, 1 Author)

Issue

  • JDK-8222210: JFXPanel popups open at wrong coordinates when using multiple hidpi monitors

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jfx pull/924/head:pull/924
$ git checkout pull/924

Update a local copy of the PR:
$ git checkout pull/924
$ git pull https://git.openjdk.org/jfx pull/924/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 924

View PR using the GUI difftool:
$ git pr show -t 924

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jfx/pull/924.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Oct 20, 2022

👋 Welcome back jvos! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk openjdk bot added the rfr Ready for review label Oct 20, 2022
@mlbridge
Copy link

mlbridge bot commented Oct 20, 2022

Webrevs

Rectangle awtBounds = graphicsConfiguration.getBounds();
AffineTransform awtScales = graphicsConfiguration.getDefaultTransform();
for (Screen screen : Screen.getScreens()) {
if ((Math.abs(screen.getPlatformX() - awtBounds.getX() * awtScales.getScaleX()) < 0.001) &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: would it be better to create a standard method?

isNearZero(double)?

although the value of the constant might depend on a situation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a good question, and I don't know the "best" answer. What we want to achieve here is to get the FX Screen that matches the AWT graphicsConfiguration. I assume that the screen.getPlatformX() being equal to the bounds.getX() etc, but I have no evidence for it. I could not find clear definitions of either the JavaFX Screen parameters nor the AWT concepts. In practice, the approach above (which I tweaked because at least on my Ubuntu, there are differences between the JavaFX platformScale and the AWT scales) works correctly. However, as I don't see any clear definition, I think it is safe to account for rounding errors (given the mixture of int, float, doubles that are used in the different areas).

@@ -407,14 +446,17 @@ private void sendMouseEventToFX(MouseEvent e) {
if (e.getID() == MouseEvent.MOUSE_PRESSED || e.getID() == MouseEvent.MOUSE_RELEASED) {
popupTrigger = e.isPopupTrigger();
}
Dimension2D onScreen = getSwingToFxPixel(getGraphicsConfiguration(), e.getXOnScreen(), e.getYOnScreen());
int fxXOnScreen = (int) onScreen.getWidth();
int fxYOnScreen = (int) onScreen.getHeight();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: should it be (int) or Math.round()?
also, coordinates can be negative - will it work then?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe these values should be int in the first place. Otherwise, we have issues in JavaFX where we process them as ints.
About negative coordinates: yes, that works. Is the question about using Dimension2D (width/height) for something that is actually a Point2D? We can use a Point2D here as well, but that class seems to have a bit more overhead than Dimension2D for this goal -- but I'm open to change it to Point2D.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, what I meant is this:

 System.out.println("(int)-1.9999=" + (int)-1.9999 + " (int)Math.round(-1.9999)=" + (int)(Math.round(-1.9999)));
prints
(int)-1.9999=-1 (int)Math.round(-1.9999)=-2

i.e. typecast to int is equivalent to Math.ceil for negative values and Math.floor for positive. We probably should use Math.round().

This code should be better tested on Windows with
a) second monitor positioned to the left of the main one, making its coordinates negative
b) using fractional HiDPI scaling which is only available on Windows it seems

And yes, I did not see Dimension2D - we probably should remove confusion and use Point2D (why does it have more overhead?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on the desired result, we use Math.floor, Math.ceil, or Math.round -- it's important to pick the right one.

But yes, using one of those is (almost always) better than just casting to (int) if the value might be negative.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in this case, Math.floor() is what you want. I wonder, though, whether it needs to be converted to an int at all? It looks like everywhere it is used, it can be kept as a double.

Btw, I almost wrote a comment asking a question as to whether you were using the values of x, y, width, and height correctly, because I got confused that you were using a Dimension to hold an x,y point. If you decide to keep it as a Dimension, I might suggest adding a clarifying comment.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need the int values, as they are used to invoke mouseEvent, scrollEvent and menuEvent on com.sun.javafx.embed.EmbeddedSceneInterface .
For some reason, scrollEvent takes doubles, but mouseEvent and menuEvent take ints.
I'll use Math.floor() instead of the int cast. The key thing here is that the strategy for the mouse/scroll/menu events should be the same as the strategy for the window location changes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And yes, I did not see Dimension2D - we probably should remove confusion and use Point2D (why does it have more overhead?)

Point2D provides much more functionality -- but has 2 fields only. So it should not be an overhead at all, hence I'll switch to Point2D. Thanks for Pointing that out :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect Math.floor() would be incorrect - we should use Math.round() for coordinates and Math.floor() for sizes (sizes will always be positive, I hope, and the rounded size will be slightly smaller than the original value).

But for coordinates we should use (int)Math.round()

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll change it to Math.round, although I doubt it doesn't improve anything (but it won't make it worse either).
The reason we need the transformation on those coordinates is to position new windows, relative against the location of the root window that we are given by Swing. Hence, if the operation applied to the window location coordinates (in sendMoveEventToFX) is the same as the operation applied to the mouse point (in sendMouseEventToFX), we are good.
A bigger problem imho is that mouseEvents, scrollEvents and menuEvents are dealing with different precisions in the EmbeddedSceneInterface -- but that is a different problem.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also expect that it won't make much of a difference here, but Andy is right that, for snapping positions, Math.round is preferred. That's what layout panes do in Region::snapPositionX/Y

@kevinrushforth
Copy link
Member

/reviewers 2

@openjdk
Copy link

openjdk bot commented Oct 22, 2022

@kevinrushforth
The total number of required reviews for this PR (including the jcheck configuration and the last /reviewers command) is now set to 2 (with at least 1 Reviewer, 1 Author).

Fix detection on screen, based on awtScale factors
Copy link
Member

@kevinrushforth kevinrushforth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks basically correct, although I did leave a couple questions / comments inline. I'll test it next week.

@@ -407,14 +446,17 @@ private void sendMouseEventToFX(MouseEvent e) {
if (e.getID() == MouseEvent.MOUSE_PRESSED || e.getID() == MouseEvent.MOUSE_RELEASED) {
popupTrigger = e.isPopupTrigger();
}
Dimension2D onScreen = getSwingToFxPixel(getGraphicsConfiguration(), e.getXOnScreen(), e.getYOnScreen());
int fxXOnScreen = (int) onScreen.getWidth();
int fxYOnScreen = (int) onScreen.getHeight();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in this case, Math.floor() is what you want. I wonder, though, whether it needs to be converted to an int at all? It looks like everywhere it is used, it can be kept as a double.

Btw, I almost wrote a comment asking a question as to whether you were using the values of x, y, width, and height correctly, because I got confused that you were using a Dimension to hold an x,y point. If you decide to keep it as a Dimension, I might suggest adding a clarifying comment.


private Dimension2D convertSwingToFxPixel(GraphicsConfiguration g, float wx, float wy) {
float newx, newy;
Screen screen = findScreen(getGraphicsConfiguration());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to use the passed in graphics config, g? If not, you might remove the g parameter, since it is otherwise unused.

Comment on lines +399 to +400
newx = sx + (wx - px) * awtScaleX / pScaleX;
newy = sy + (wy - py) * awtScaleY / pScaleY;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

px and py are already (scaled) "FX" coordinates, so I'm not sure AWT scale should be applied to that. I might be missing something here.

Copy link
Collaborator Author

@johanvos johanvos Nov 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this correction, there is an offset on Linux (when awtScaleX = 1 and pScaleX =2 for example, versus 1.5 for both on Windows).
TBH, I think the relations between the different scale factors should be written out clearly somewhere, as I think there are a number of places where we face platform-specific differences.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree this should be more clearly documented. I've tested it on macOS and Windows and it works fine, so I expect that this is correct as you have it.

@kevinrushforth kevinrushforth self-requested a review November 2, 2022 21:26
Copy link
Member

@kevinrushforth kevinrushforth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. If you want to switch to using Math.round I'll reapprove.

Comment on lines +399 to +400
newx = sx + (wx - px) * awtScaleX / pScaleX;
newy = sy + (wy - py) * awtScaleY / pScaleY;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree this should be more clearly documented. I've tested it on macOS and Windows and it works fine, so I expect that this is correct as you have it.

@@ -407,14 +446,17 @@ private void sendMouseEventToFX(MouseEvent e) {
if (e.getID() == MouseEvent.MOUSE_PRESSED || e.getID() == MouseEvent.MOUSE_RELEASED) {
popupTrigger = e.isPopupTrigger();
}
Dimension2D onScreen = getSwingToFxPixel(getGraphicsConfiguration(), e.getXOnScreen(), e.getYOnScreen());
int fxXOnScreen = (int) onScreen.getWidth();
int fxYOnScreen = (int) onScreen.getHeight();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also expect that it won't make much of a difference here, but Andy is right that, for snapping positions, Math.round is preferred. That's what layout panes do in Region::snapPositionX/Y

@openjdk
Copy link

openjdk bot commented Nov 8, 2022

@johanvos This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8222210: JFXPanel popups open at wrong coordinates when using multiple hidpi monitors

Reviewed-by: kcr, angorya

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 13 new commits pushed to the master branch:

  • b08f135: 8296283: JUnit5 tests using Params API fails to compile
  • 7f17447: 8290844: Add Skin.install() method
  • 01735b3: 8265835: Exception in Quantum due to null platformWindow
  • 748107a: 8295327: JavaFX - IllegalArgumentException when printing with margins equal to 0
  • ac8382b: 8294400: Provide media support for libavcodec version 59
  • 4a2afb4: 8295962: Reference to State in Task.java is ambiguous when building with JDK 19
  • b3eec1d: 8295725: Update copyright header for files modified in 2022
  • 91e1a27: 8278426: ImagePool uses terminally deprecated System.runFinalization method
  • 60a7915: 8295236: Update JavaDoc in javafx.geometry.Point3D
  • f448e9a: 8294722: FX: Update copyright year in docs, readme files to 2023
  • ... and 3 more: https://git.openjdk.org/jfx/compare/35675c8d27d54a26059b182614e18152794dbcec...master

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added the ready Ready to be integrated label Nov 8, 2022
@johanvos
Copy link
Collaborator Author

/integrate

@openjdk
Copy link

openjdk bot commented Nov 10, 2022

Going to push as commit f4e27e9.
Since your change was applied there have been 14 commits pushed to the master branch:

  • 706261b: 8296592: Skip failing test StraightLineTest on Linux
  • b08f135: 8296283: JUnit5 tests using Params API fails to compile
  • 7f17447: 8290844: Add Skin.install() method
  • 01735b3: 8265835: Exception in Quantum due to null platformWindow
  • 748107a: 8295327: JavaFX - IllegalArgumentException when printing with margins equal to 0
  • ac8382b: 8294400: Provide media support for libavcodec version 59
  • 4a2afb4: 8295962: Reference to State in Task.java is ambiguous when building with JDK 19
  • b3eec1d: 8295725: Update copyright header for files modified in 2022
  • 91e1a27: 8278426: ImagePool uses terminally deprecated System.runFinalization method
  • 60a7915: 8295236: Update JavaDoc in javafx.geometry.Point3D
  • ... and 4 more: https://git.openjdk.org/jfx/compare/35675c8d27d54a26059b182614e18152794dbcec...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Nov 10, 2022
@openjdk openjdk bot closed this Nov 10, 2022
@openjdk openjdk bot removed ready Ready to be integrated rfr Ready for review labels Nov 10, 2022
@openjdk
Copy link

openjdk bot commented Nov 10, 2022

@johanvos Pushed as commit f4e27e9.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

@johanvos johanvos deleted the 8222210-jfxpanel branch November 14, 2022 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

3 participants