diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatSystemProperties.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatSystemProperties.java new file mode 100644 index 00000000..b6cc16e0 --- /dev/null +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatSystemProperties.java @@ -0,0 +1,66 @@ +/* + * Copyright 2020 FormDev Software GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.formdev.flatlaf; + +/** + * Defines/documents own system properties used in FlatLaf. + * + * @author Karl Tauber + */ +public interface FlatSystemProperties +{ + /** + * Specifies a custom scale factor used to scale the UI. + *

+ * If Java runtime scales (Java 9 or later), this scale factor is applied on top + * of the Java system scale factor. Java 8 does not scale and this scale factor + * replaces the user scale factor that FlatLaf computes based on the font. + * To replace the Java 9+ system scale factor, use system property "sun.java2d.uiScale", + * which has the same syntax as this one. + *

+ * Allowed Values e.g. {@code 1.5}, {@code 1.5x}, {@code 150%} or {@code 144dpi} (96dpi is 100%)
+ */ + String UI_SCALE = "flatlaf.uiScale"; + + /** + * Specifies whether Ubuntu font should be used on Ubuntu Linux. + * By default, if not running in a JetBrains Runtime, the Liberation Sans font + * is used because there are rendering issues (in Java) with Ubuntu fonts. + *

+ * Allowed Values {@code false} and {@code true}
+ * Default {@code false} + */ + String USE_UBUNTU_FONT = "flatlaf.useUbuntuFont"; + + /** + * Specifies whether vertical text position is corrected when UI is scaled on HiDPI screens. + *

+ * Allowed Values {@code false} and {@code true}
+ * Default {@code true} + */ + String USE_TEXT_Y_CORRECTION = "flatlaf.useTextYCorrection"; + + /** + * Checks whether a system property is set and returns {@code true} if its value + * is {@code "true"} (case-insensitive), otherwise it returns {@code false}. + * If the system property is not set, {@code defaultValue} is returned. + */ + static boolean getBoolean( String key, boolean defaultValue ) { + String value = System.getProperty( key ); + return (value != null) ? Boolean.parseBoolean( value ) : defaultValue; + } +} diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/LinuxFontPolicy.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/LinuxFontPolicy.java index 1ba7e683..8642b4ff 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/LinuxFontPolicy.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/LinuxFontPolicy.java @@ -78,7 +78,7 @@ class LinuxFontPolicy // --> use Liberation Sans font if( family.startsWith( "Ubuntu" ) && !SystemInfo.IS_JETBRAINS_JVM && - !Boolean.parseBoolean( System.getProperty( "flatlaf.useUbuntuFont" ) ) ) + !FlatSystemProperties.getBoolean( FlatSystemProperties.USE_UBUNTU_FONT, false ) ) family = "Liberation Sans"; // scale font size diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/HiDPIUtils.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/HiDPIUtils.java index 93b417a6..12b2f441 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/util/HiDPIUtils.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/util/HiDPIUtils.java @@ -23,6 +23,7 @@ import java.awt.geom.AffineTransform; import java.awt.geom.Rectangle2D; import java.text.AttributedCharacterIterator; import javax.swing.JComponent; +import com.formdev.flatlaf.FlatSystemProperties; /** * @author Karl Tauber @@ -104,7 +105,7 @@ public class HiDPIUtils private static boolean useTextYCorrection() { if( useTextYCorrection == null ) - useTextYCorrection = Boolean.valueOf( System.getProperty( "flatlaf.useTextYCorrection", "true" ) ); + useTextYCorrection = FlatSystemProperties.getBoolean( FlatSystemProperties.USE_TEXT_Y_CORRECTION, true ); return useTextYCorrection; } 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 2338f25e..b1ded6bd 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 @@ -32,6 +32,7 @@ import javax.swing.plaf.DimensionUIResource; import javax.swing.plaf.FontUIResource; import javax.swing.plaf.InsetsUIResource; import javax.swing.plaf.UIResource; +import com.formdev.flatlaf.FlatSystemProperties; /** * Two scaling modes are supported for HiDPI displays: @@ -195,8 +196,7 @@ public class UIScale private static boolean isUserScalingEnabled() { // same as in IntelliJ IDEA - String hidpi = System.getProperty( "hidpi" ); - return (hidpi != null) ? Boolean.parseBoolean( hidpi ) : true; + return FlatSystemProperties.getBoolean( "hidpi", true ); } /** @@ -204,7 +204,7 @@ public class UIScale * to the given font. */ public static FontUIResource applyCustomScaleFactor( FontUIResource font ) { - String uiScale = System.getProperty( "flatlaf.uiScale" ); + String uiScale = System.getProperty( FlatSystemProperties.UI_SCALE ); float scaleFactor = parseScaleFactor( uiScale ); if( scaleFactor <= 0 ) return font; diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatPaintingStringTest.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatPaintingStringTest.java index 60c6246b..3a9ab3cd 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatPaintingStringTest.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatPaintingStringTest.java @@ -26,6 +26,7 @@ import java.awt.Insets; import java.awt.geom.AffineTransform; import javax.swing.*; import javax.swing.border.EmptyBorder; +import com.formdev.flatlaf.FlatSystemProperties; import com.formdev.flatlaf.ui.FlatUIUtils; import com.formdev.flatlaf.util.Graphics2DProxy; import com.formdev.flatlaf.util.HiDPIUtils; @@ -40,7 +41,7 @@ public class FlatPaintingStringTest extends JPanel { public static void main( String[] args ) { - System.setProperty( "flatlaf.uiScale", "1x" ); + System.setProperty( FlatSystemProperties.UI_SCALE, "1x" ); System.setProperty( "sun.java2d.uiScale", "1x" ); SwingUtilities.invokeLater( () -> { diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestFrame.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestFrame.java index 8998eedc..eae07aea 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestFrame.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/FlatTestFrame.java @@ -35,6 +35,7 @@ import com.formdev.flatlaf.FlatDarkLaf; import com.formdev.flatlaf.FlatIntelliJLaf; import com.formdev.flatlaf.FlatLaf; import com.formdev.flatlaf.FlatLightLaf; +import com.formdev.flatlaf.FlatSystemProperties; import com.formdev.flatlaf.IntelliJTheme; import com.formdev.flatlaf.demo.LookAndFeelsComboBox; import com.formdev.flatlaf.demo.DemoPrefs; @@ -66,10 +67,10 @@ public class FlatTestFrame DemoPrefs.init( PREFS_ROOT_PATH ); // set scale factor - if( System.getProperty( "flatlaf.uiScale", System.getProperty( "sun.java2d.uiScale" ) ) == null ) { + if( System.getProperty( FlatSystemProperties.UI_SCALE, System.getProperty( "sun.java2d.uiScale" ) ) == null ) { String scaleFactor = DemoPrefs.getState().get( KEY_SCALE_FACTOR, null ); if( scaleFactor != null ) - System.setProperty( "flatlaf.uiScale", scaleFactor ); + System.setProperty( FlatSystemProperties.UI_SCALE, scaleFactor ); } // set look and feel @@ -145,7 +146,7 @@ public class FlatTestFrame lookAndFeelComboBox.setModel( lafModel ); updateScaleFactorComboBox(); - String scaleFactor = System.getProperty( "flatlaf.uiScale", System.getProperty( "sun.java2d.uiScale" ) ); + String scaleFactor = System.getProperty( FlatSystemProperties.UI_SCALE, System.getProperty( "sun.java2d.uiScale" ) ); if( scaleFactor != null ) scaleFactorComboBox.setSelectedItem( scaleFactor ); @@ -472,10 +473,10 @@ public class FlatTestFrame scaleFactorComboBox.setPopupVisible( false ); if( scaleFactor != null ) { - System.setProperty( "flatlaf.uiScale", scaleFactor ); + System.setProperty( FlatSystemProperties.UI_SCALE, scaleFactor ); DemoPrefs.getState().put( KEY_SCALE_FACTOR, scaleFactor ); } else { - System.clearProperty( "flatlaf.uiScale" ); + System.clearProperty( FlatSystemProperties.UI_SCALE ); DemoPrefs.getState().remove( KEY_SCALE_FACTOR ); } diff --git a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/uidefaults/UIDefaultsDump.java b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/uidefaults/UIDefaultsDump.java index 53a01ecf..b552165f 100644 --- a/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/uidefaults/UIDefaultsDump.java +++ b/flatlaf-testing/src/main/java/com/formdev/flatlaf/testing/uidefaults/UIDefaultsDump.java @@ -84,7 +84,7 @@ public class UIDefaultsDump public static void main( String[] args ) { Locale.setDefault( Locale.ENGLISH ); System.setProperty( "sun.java2d.uiScale", "1x" ); - System.setProperty( "flatlaf.uiScale", "1x" ); + System.setProperty( FlatSystemProperties.UI_SCALE, "1x" ); File dir = new File( "src/main/resources/com/formdev/flatlaf/testing/uidefaults" );