Windows: made scaling compatible with Windows OS scaling, which distinguish between "screen scaling" and "text scaling" (issue #175)

This commit is contained in:
Karl Tauber
2020-10-05 13:14:44 +02:00
parent 8db05f47b5
commit 62ffd57108
3 changed files with 59 additions and 3 deletions

View File

@@ -14,6 +14,8 @@ FlatLaf Change Log
- CheckBoxMenuItem and RadioButtonMenuItem: Improved checkmark background colors - CheckBoxMenuItem and RadioButtonMenuItem: Improved checkmark background colors
of selected menu items that have also an icon. This makes it is easier to of selected menu items that have also an icon. This makes it is easier to
recognize selected menu items. recognize selected menu items.
- Windows: Made scaling compatible with Windows OS scaling, which distinguish
between "screen scaling" and "text scaling". (issue #175)
#### Fixed bugs #### Fixed bugs

View File

@@ -467,9 +467,11 @@ public abstract class FlatLaf
uiFont = (font instanceof FontUIResource) ? (FontUIResource) font : new FontUIResource( font ); uiFont = (font instanceof FontUIResource) ? (FontUIResource) font : new FontUIResource( font );
} }
// fallback
if( uiFont == null ) if( uiFont == null )
uiFont = createCompositeFont( Font.SANS_SERIF, Font.PLAIN, 12 ); uiFont = createCompositeFont( Font.SANS_SERIF, Font.PLAIN, 12 );
// increase font size if system property "flatlaf.uiScale" is set
uiFont = UIScale.applyCustomScaleFactor( uiFont ); uiFont = UIScale.applyCustomScaleFactor( uiFont );
// use active value for all fonts to allow changing fonts in all components // use active value for all fonts to allow changing fonts in all components

View File

@@ -22,6 +22,7 @@ import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration; import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment; import java.awt.GraphicsEnvironment;
import java.awt.Insets; import java.awt.Insets;
import java.awt.Toolkit;
import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener; import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport; import java.beans.PropertyChangeSupport;
@@ -162,6 +163,13 @@ public class UIScale
if( !isUserScalingEnabled() ) if( !isUserScalingEnabled() )
return; return;
// apply custom scale factor specified in system property "flatlaf.uiScale"
float customScaleFactor = getCustomScaleFactor();
if( customScaleFactor > 0 ) {
setUserScaleFactor( customScaleFactor );
return;
}
// use font size to calculate scale factor (instead of DPI) // use font size to calculate scale factor (instead of DPI)
// because even if we are on a HiDPI display it is not sure // because even if we are on a HiDPI display it is not sure
// that a larger font size is set by the current LaF // that a larger font size is set by the current LaF
@@ -170,7 +178,45 @@ public class UIScale
if( font == null ) if( font == null )
font = UIManager.getFont( "Label.font" ); font = UIManager.getFont( "Label.font" );
setUserScaleFactor( computeScaleFactor( font ) ); float newScaleFactor;
if( SystemInfo.isWindows ) {
// Special handling for Windows to be compatible with OS scaling,
// which distinguish between "screen scaling" and "text scaling".
// - Windows "screen scaling" scales everything (text, icon, gaps, etc)
// and may have different scaling factors for each screen.
// - Windows "text scaling" increases only the font size, but on all screens.
//
// Both can be changed by the user in the Windows 10 Settings:
// - Settings > Display > Scale and layout
// - Settings > Ease of Access > Display > Make text bigger (100% - 225%)
if( font instanceof UIResource ) {
if( isSystemScalingEnabled() ) {
// Do not apply own scaling if the JRE scales using Windows screen scale factor.
// If user increases font size in Windows 10 settings, desktop property
// "win.messagebox.font" is changed and FlatLaf uses the larger font.
newScaleFactor = 1;
} else {
// If the JRE does not scale (Java 8), the size of the UI font
// (usually from desktop property "win.messagebox.font")
// combines the Windows screen and text scale factors.
// But the font in desktop property "win.defaultGUI.font" is only
// scaled with the Windows screen scale factor. So use it to compute
// our scale factor that is equal to Windows screen scale factor.
Font winFont = (Font) Toolkit.getDefaultToolkit().getDesktopProperty( "win.defaultGUI.font" );
newScaleFactor = computeScaleFactor( (winFont != null) ? winFont : font );
}
} else {
// If font was explicitly set from outside (is not a UIResource)
// use it to compute scale factor. This allows applications to
// use custom fonts (e.g. that the user can change in UI) and
// get scaling if a larger font size is used.
// E.g. FlatLaf Demo supports increasing font size in "Font" menu and UI scales.
newScaleFactor = computeScaleFactor( font );
}
} else
newScaleFactor = computeScaleFactor( font );
setUserScaleFactor( newScaleFactor );
} }
private static float computeScaleFactor( Font font ) { private static float computeScaleFactor( Font font ) {
@@ -206,8 +252,7 @@ public class UIScale
if( !isUserScalingEnabled() ) if( !isUserScalingEnabled() )
return font; return font;
String uiScale = System.getProperty( FlatSystemProperties.UI_SCALE ); float scaleFactor = getCustomScaleFactor();
float scaleFactor = parseScaleFactor( uiScale );
if( scaleFactor <= 0 ) if( scaleFactor <= 0 )
return font; return font;
@@ -219,6 +264,13 @@ public class UIScale
return new FontUIResource( font.deriveFont( (float) newFontSize ) ); return new FontUIResource( font.deriveFont( (float) newFontSize ) );
} }
/**
* Get custom scale factor specified in system property "flatlaf.uiScale".
*/
private static float getCustomScaleFactor() {
return parseScaleFactor( System.getProperty( FlatSystemProperties.UI_SCALE ) );
}
/** /**
* Similar to sun.java2d.SunGraphicsEnvironment.getScaleFactor(String) * Similar to sun.java2d.SunGraphicsEnvironment.getScaleFactor(String)
*/ */