-
Notifications
You must be signed in to change notification settings - Fork 6.2k
8373650: Test "javax/swing/JMenuItem/6458123/ManualBug6458123.java" fails because the check icons are not aligned properly as expected #28889
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
Conversation
…ls because the check icons are not aligned properly as expected
|
👋 Welcome back psadhukhan! A progress list of the required criteria for merging this PR into |
|
@prsadhuk 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: 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 320 new commits pushed to the
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 |
|
/touch |
|
@prsadhuk The pull request is being re-evaluated and the inactivity timeout has been reset. |
Webrevs
|
What is the proper alignment in this context? The “After fix” image shows that the icons still jump left and right. |
DamonGuy
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason why the "After fix" image also has the "Radio button" dot misaligned still?
| x + OFFSET, | ||
| (icon.getIconHeight() <= 16) ? y + OFFSET : (y + icon.getIconHeight() / 2), state); | ||
| } else if (icon.getIconWidth() <= 16) { | ||
| if ((c instanceof JMenuItem mi) && mi.getText().isEmpty()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition (c instanceof JMenuItem mi) is always true according to the assert at line 881:
jdk/src/java.desktop/windows/classes/com/sun/java/swing/plaf/windows/WindowsIconFactory.java
Line 881 in 2daf12e
| assert menuItem == null || c == menuItem; |
where menuItem is a field in VistaMenuItemCheckIcon, the type of the field is JMenuItem.
This means, you can use menuItem and ignore the passed in parameter c.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
| if (icon.getIconWidth() <= 8) { | ||
| skin.paintSkin(g, | ||
| x + OFFSET, | ||
| (icon.getIconHeight() <= 16) ? y + OFFSET : (y + icon.getIconHeight() / 2), state); | ||
| } else if (icon.getIconWidth() <= 16) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why painting an icon and check marks / radio bullets in RTL case depends on the width of the icon but there's no such dependency in LTR case.
Shouldn't the performed menu layout handle these cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I see, MenuItemLayout handler's x position calculation for RTL depends on icon rect width
jdk/src/java.desktop/share/classes/sun/swing/MenuItemLayoutHelper.java
Lines 737 to 741 in 6eaabed
| private void calcXPositionsRTL(int startXPos, int leadingGap, | |
| int gap, Rectangle... rects) { | |
| int curXPos = startXPos - leadingGap; | |
| for (Rectangle rect : rects) { | |
| rect.x = curXPos - rect.width; |
but LTR doesn't depend on rect width
jdk/src/java.desktop/share/classes/sun/swing/MenuItemLayoutHelper.java
Lines 726 to 730 in 6eaabed
| private void calcXPositionsLTR(int startXPos, int leadingGap, | |
| int gap, Rectangle... rects) { | |
| int curXPos = startXPos + leadingGap; | |
| for (Rectangle rect : rects) { | |
| rect.x = curXPos; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, but why do you have tweak the layout produced by MenuItemLayoutHelper?
Do Metal and Nimbus have to tweak the layout too?
Is it because Windows icons used to be special and combined the icon and check mark / bullet mark, or rather replaced the check mark / bullet mark with the icon? Should we get rid of that special treatment?
I'd like to have answers to these questions.
To be clear, I'm not opposing this approach, it seems to work, yet it seems superfluous, the code kind of repeats calculations. Both Metal and Nimbus L&F have similar column layouts where a separate column is allocated for marks and icons, Windows L&F should re-use that code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it because Windows icons used to be special and combined the icon and check mark / bullet mark, or rather replaced the check mark / bullet mark with the icon? Should we get rid of that special treatment?
I am reusing the VistaMenuItemCheckIcon to render the RadioButtonMenuItem and CheckBoxMenuItem icons and getting rid of the working/treatment made in that class (and rewriting) might result in more regressions which I am avoiding..
There are many factors apart from normal that is affecting the icon position like menuItem.setHorizontalTextPosition(LEADING/TRAILING/LEFT/RIGHT etc), menuItem.setHorizontalAlignment(RIGHT/CENTER etc) which can cause issues like what we are seeing in #28210 which atleast is working now for Windows L&F without any rework.
Metal L&F seems to hardcode the icon position to (0,0) or (2,2)
jdk/src/java.desktop/share/classes/javax/swing/plaf/metal/MetalIconFactory.java
Lines 2268 to 2302 in e4474ad
| private static class RadioButtonMenuItemIcon implements Icon, UIResource, Serializable | |
| { | |
| public void paintOceanIcon(Component c, Graphics g, int x, int y) { | |
| ButtonModel model = ((JMenuItem)c).getModel(); | |
| boolean isSelected = model.isSelected(); | |
| boolean isEnabled = model.isEnabled(); | |
| boolean isPressed = model.isPressed(); | |
| boolean isArmed = model.isArmed(); | |
| g.translate( x, y ); | |
| if (isEnabled) { | |
| MetalUtils.drawGradient(c, g, "RadioButtonMenuItem.gradient", | |
| 1, 1, 7, 7, true); | |
| if (isPressed || isArmed) { | |
| g.setColor(MetalLookAndFeel.getPrimaryControl()); | |
| } | |
| else { | |
| g.setColor(MetalLookAndFeel.getControlHighlight()); | |
| } | |
| g.drawArc(-1, -1, 10, 10, 245, 140); | |
| if (isPressed || isArmed) { | |
| g.setColor(MetalLookAndFeel.getControlInfo()); | |
| } | |
| else { | |
| g.setColor(MetalLookAndFeel.getControlDarkShadow()); | |
| } | |
| } | |
| else { | |
| g.setColor( MetalLookAndFeel.getMenuDisabledForeground() ); | |
| } | |
| g.drawOval(0, 0, 8, 8); |
whereas I am reusing the methodology in VistaMenuItemIcon using OFFSET and it seems to work for cases seen till now..
| skin.paintSkin(g, | ||
| (type == JRadioButtonMenuItem.class) ? (x + 4 * OFFSET) : (x + 3 * OFFSET), | ||
| (icon.getIconHeight() <= 16) ? y + OFFSET : (y + icon.getIconHeight() / 2), state); | ||
| } | ||
| } else { | ||
| if ((c instanceof JMenuItem mi) && (mi.getText().isEmpty() || mi.getAccelerator() != null)) { | ||
| skin.paintSkin(g, | ||
| (type == JRadioButtonMenuItem.class) ? (x + 3 * OFFSET) : (x + 4 * OFFSET), | ||
| (icon.getIconHeight() <= 16) ? y + OFFSET : (y + icon.getIconHeight() / 2), state); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder why painting the skin for radio button menu requires such a fix up whereas it's not required for JCheckBoxMenuItem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is taken care via ternary operator, width/spread of check mark is usually more than radio bullet so starting point of checkmark is considered a little ahead compared to radio bullet
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that it's taken care by the ternary operator. Why is it needed, though?
The skin for both check mark and radio bullet have the same size, and the mark is positioned accordingly inside the rectangle. This works well for the LTR layout, both marks are rendered at the same location and appear aligned in the end. Why does RTL layout treat these marks differently?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As told and code shown earlier, the RTL oversees overall rectangle width using all text rect, image icon rect, icon rect etc to calculate icon x position unlike LTR and it seemed a bit off for checkmark as compared to bullet by 1 or 2 pixel or so I catered to it by this logic
DamonGuy
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although the if/else branches look strange, it works. I don't see a clear way to clean up and condense the conditionals so lgtm unless someone else has an idea.
|
Also I guess this Copyright year needs to be updated to 2026 now as well. |
I haven't looked at the updated code yet. I wonder why the logic in As I posted in my previous comment, I'm not against this approach as long as it works and fixes the layout, yet I'd like to clean up the code if possible… in a follow-up issue. |
TejeshR13
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we update the bugid in test too ?
| (type == JRadioButtonMenuItem.class) ? (x + 3 * OFFSET) : (x + 4 * OFFSET), | ||
| (icon.getIconHeight() <= 16) ? y + OFFSET : (y + icon.getIconHeight() / 2), state); | ||
| } else { | ||
| skin.paintSkin(g, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we optimize the code here, since mostly x is changing here based on conditions. It'll help in readability and review too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, y is also changing depending on icon height...
As of now, any more optimization is not possible, it can be done as followup if it is needed..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 926, 930, 936 and 940 looks same to me. I'm referring to these lines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok.. optimized..
|
Its in closed...it shouldn't affect this open review but anyway raised a MR |
TejeshR13
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
/integrate |
|
Going to push as commit e45f565.
Your commit was automatically rebased without conflicts. |

Check/radiobutton icon are not aligned properly in RTL.
WindowsMenuItemUIusesMenuItemLayoutHelper.layoutMenuItemto do the layout which callsdoRTLColumnLayoutwhich calculates x positions incalcXPositionsRTLand then again aligns inalignRects. However, since in Windows historically radiobutton/check icon was not drawn or drawn below the menuitem image icon (since image icon and check icon was drawn in the same layout space and not separately) the aligned x position of check icons returned fromMenuItemLayoutHelperwas not correct but sinceMenuItemLayoutHelperalignment is used in other L&Fs also so we need to realign it in windows specific class i.e in WindowsIconFactory in paintIconBefore fix
After fix
Progress
Issue
Reviewers
Reviewing
Using
gitCheckout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/28889/head:pull/28889$ git checkout pull/28889Update a local copy of the PR:
$ git checkout pull/28889$ git pull https://git.openjdk.org/jdk.git pull/28889/headUsing Skara CLI tools
Checkout this PR locally:
$ git pr checkout 28889View PR using the GUI difftool:
$ git pr show -t 28889Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/28889.diff
Using Webrev
Link to Webrev Comment