From a8b15c6a12781c930593d97eba8320a5a4bebe2f Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Sat, 2 Jul 2022 22:38:37 +0200 Subject: [PATCH] MenuItem: fixed sometimes wrapped HTML text on HiDPI screens on Windows --- CHANGELOG.md | 1 + .../flatlaf/ui/FlatMenuItemRenderer.java | 13 +++++++++++ .../flatlaf/testing/FlatMenusTest.java | 22 +++++++++++++++++++ .../formdev/flatlaf/testing/FlatMenusTest.jfd | 16 ++++++++++++++ 4 files changed, 52 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 579d0cc6..63f3c27c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ FlatLaf Change Log - ComboBox: Fixed vertical alignment of text in popup list with text in combo box in IntelliJ/Darcula themes. +- MenuItem: Fixed sometimes wrapped HTML text on HiDPI screens on Windows. - TableHeader: Fixed exception when changing table structure (e.g. removing column) from a table header popup menu action. (issue #532) - `HiDPIUtils.paintAtScale1x()` now supports rotated graphics. (issue #557) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuItemRenderer.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuItemRenderer.java index a995be72..153bc16c 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuItemRenderer.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuItemRenderer.java @@ -446,6 +446,19 @@ debug*/ protected static void paintHTMLText( Graphics g, JMenuItem menuItem, Rectangle textRect, View htmlView, Color selectionForeground ) { + // On Windows, using Segoe UI font, Java 15 or older and scaling greater than 1, + // the width of the HTML view may be initially too small (because component + // is not connected to a GraphicsConfiguration when getPreferredSize() is invoked). + // Since Java 16, this seems to be fixed somehow by doing popup menu layout twice. + // + // If using a too small width for htmlView.paint(), the view would rearrange + // its children and wrap them to two lines. To avoid this, use view preferred X span + // for painting. Core Lafs actually do the same in class MenuItemLayoutHelper. + // + // Example HTML text that causes the problem: "some HTML text" + textRect = new Rectangle( textRect ); + textRect.width = (int) htmlView.getPreferredSpan( View.X_AXIS ); + if( isArmedOrSelected( menuItem ) && selectionForeground != null ) g = new GraphicsProxyWithTextColor( (Graphics2D) g, selectionForeground ); diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatMenusTest.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatMenusTest.java index 848700e7..bab13f82 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatMenusTest.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatMenusTest.java @@ -181,6 +181,10 @@ public class FlatMenusTest JMenuItem menuItem45 = new JMenuItem(); JMenuItem menuItem46 = new JMenuItem(); JMenuItem menuItem47 = new JMenuItem(); + JMenu menu13 = new JMenu(); + JMenuItem menuItem48 = new JMenuItem(); + JMenuItem menuItem49 = new JMenuItem(); + JMenuItem menuItem50 = new JMenuItem(); menuBar2 = new JMenuBar(); JMenu menu8 = new JMenu(); FlatMenusTest.LargerMenuItem menuItem13 = new FlatMenusTest.LargerMenuItem(); @@ -442,6 +446,24 @@ public class FlatMenusTest menu12.add(menuItem47); } menuBar1.add(menu12); + + //======== menu13 ======== + { + menu13.setText("HTML"); + + //---- menuItem48 ---- + menuItem48.setText("some HTML text"); + menu13.add(menuItem48); + + //---- menuItem49 ---- + menuItem49.setText("some longer HTML text"); + menu13.add(menuItem49); + + //---- menuItem50 ---- + menuItem50.setText("another HTML text"); + menu13.add(menuItem50); + } + menuBar1.add(menu13); } add(menuBar1, "cell 1 0 2 1,growx"); diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatMenusTest.jfd b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatMenusTest.jfd index 6897ecce..fe73123b 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatMenusTest.jfd +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatMenusTest.jfd @@ -194,6 +194,22 @@ new FormModel { "icon": new com.jformdesigner.model.SwingIcon( 0, "/com/formdev/flatlaf/testing/test64.png" ) } ) } ) + add( new FormContainer( "javax.swing.JMenu", new FormLayoutManager( class javax.swing.JMenu ) ) { + name: "menu13" + "text": "HTML" + add( new FormComponent( "javax.swing.JMenuItem" ) { + name: "menuItem48" + "text": "some HTML text" + } ) + add( new FormComponent( "javax.swing.JMenuItem" ) { + name: "menuItem49" + "text": "some longer HTML text" + } ) + add( new FormComponent( "javax.swing.JMenuItem" ) { + name: "menuItem50" + "text": "another HTML text" + } ) + } ) }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 1 0 2 1,growx" } )