From c69e9e12c6312a1b71f84bcc877114c330ecc594 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Sat, 7 Sep 2019 23:48:24 +0200 Subject: [PATCH] Demo: add info label shows the current system/user scale factor and the java version --- .../com/formdev/flatlaf/util/UIScale.java | 11 ++++++++ .../com/formdev/flatlaf/demo/ControlBar.java | 25 +++++++++++++++++++ .../com/formdev/flatlaf/demo/ControlBar.jfd | 6 +++++ .../com/formdev/flatlaf/demo/FlatLafDemo.java | 1 - 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/UIScale.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/UIScale.java index 1130b829..ed0b4499 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/UIScale.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/UIScale.java @@ -19,6 +19,7 @@ package com.formdev.flatlaf.util; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics2D; +import java.awt.GraphicsConfiguration; import java.awt.GraphicsEnvironment; import java.awt.Insets; import java.beans.PropertyChangeEvent; @@ -42,6 +43,7 @@ import javax.swing.plaf.UIResource; * So the JRE does the scaling itself. * E.g. when you draw a 10px line, a 15px line is drawn on screen. * The scale factor may be different for each connected display. + * The scale factor may change for a window when moving the window from one display to another one. * * 2) user scaling mode * @@ -51,6 +53,7 @@ import javax.swing.plaf.UIResource; * The JRE does not scale anything. * So we have to invoke {@link #scale(float)} where necessary. * There is only one user scale factor for all displays. + * The user scale factor may change if the active LaF or "Label.font" has changed. * * @author Karl Tauber */ @@ -89,6 +92,14 @@ public class UIScale return jreHiDPI; } + public static double getSystemScaleFactor( Graphics2D g ) { + return isJreHiDPIEnabled() ? g.getDeviceConfiguration().getDefaultTransform().getScaleX() : 1; + } + + public static double getSystemScaleFactor( GraphicsConfiguration gc ) { + return isJreHiDPIEnabled() ? gc.getDefaultTransform().getScaleX() : 1; + } + //---- user scaling (Java 8) ---------------------------------------------- private static float scaleFactor = 1; diff --git a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/ControlBar.java b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/ControlBar.java index 9ed15a1c..ecb38dc0 100644 --- a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/ControlBar.java +++ b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/ControlBar.java @@ -25,6 +25,7 @@ import javax.swing.plaf.metal.MetalLookAndFeel; import javax.swing.plaf.nimbus.NimbusLookAndFeel; import com.formdev.flatlaf.*; import com.formdev.flatlaf.util.SystemInfo; +import com.formdev.flatlaf.util.UIScale; import net.miginfocom.swing.*; /** @@ -102,11 +103,26 @@ class ControlBar frame.addWindowListener( new WindowAdapter() { @Override public void windowOpened( WindowEvent e ) { + updateInfoLabel(); closeButton.requestFocusInWindow(); } + @Override + public void windowActivated( WindowEvent e ) { + updateInfoLabel(); + } } ); } + private void updateInfoLabel() { + double systemScaleFactor = UIScale.getSystemScaleFactor( getGraphicsConfiguration() ); + float userScaleFactor = UIScale.getUserScaleFactor(); + infoLabel.setText( "(Java " + System.getProperty( "java.version" ) + + (systemScaleFactor != 1 ? ("; system scale factor " + systemScaleFactor) : "") + + (userScaleFactor != 1 ? ("; user scale factor " + userScaleFactor) : "") + + (systemScaleFactor == 1 && userScaleFactor == 1 ? "; no scaling" : "") + + ")" ); + } + private void registerSwitchToLookAndFeel( int keyCode, String lafClassName ) { ((JComponent)frame.getContentPane()).registerKeyboardAction( e -> { @@ -138,6 +154,9 @@ class ControlBar // change look and feel UIManager.setLookAndFeel( newLaf.className ); + // update info label because user scale factor may change + updateInfoLabel(); + // update all components SwingUtilities.updateComponentTreeUI( frame ); @@ -209,6 +228,7 @@ class ControlBar lookAndFeelComboBox = new JComboBox<>(); rightToLeftCheckBox = new JCheckBox(); enabledCheckBox = new JCheckBox(); + infoLabel = new JLabel(); closeButton = new JButton(); //======== this ======== @@ -242,6 +262,10 @@ class ControlBar enabledCheckBox.addActionListener(e -> enabledChanged()); add(enabledCheckBox, "cell 2 1"); + //---- infoLabel ---- + infoLabel.setText("text"); + add(infoLabel, "cell 3 1,alignx center,growx 0"); + //---- closeButton ---- closeButton.setText("Close"); closeButton.addActionListener(e -> closePerformed()); @@ -254,6 +278,7 @@ class ControlBar private JComboBox lookAndFeelComboBox; private JCheckBox rightToLeftCheckBox; private JCheckBox enabledCheckBox; + private JLabel infoLabel; private JButton closeButton; // JFormDesigner - End of variables declaration //GEN-END:variables diff --git a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/ControlBar.jfd b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/ControlBar.jfd index 8430f592..ded674b4 100644 --- a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/ControlBar.jfd +++ b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/ControlBar.jfd @@ -41,6 +41,12 @@ new FormModel { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 2 1" } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "infoLabel" + "text": "text" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 3 1,alignx center,growx 0" + } ) add( new FormComponent( "javax.swing.JButton" ) { name: "closeButton" "text": "Close" diff --git a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/FlatLafDemo.java b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/FlatLafDemo.java index f97e3bc8..b2e64741 100644 --- a/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/FlatLafDemo.java +++ b/flatlaf-demo/src/main/java/com/formdev/flatlaf/demo/FlatLafDemo.java @@ -55,7 +55,6 @@ public class FlatLafDemo // create frame DemoFrame frame = new DemoFrame(); - frame.setTitle( frame.getTitle() + " (Java " + System.getProperty( "java.version" ) + ")" ); // show frame frame.pack();