Package org.vanted.scaling
Class DPIHelper
java.lang.Object
org.vanted.scaling.DPIHelper
public class DPIHelper extends Object
An utility helper that takes care of processing the DPI from a provided value
(Slider value) and is responsible for managing the scaling preferences among
some other useful functions in hand.
- Author:
- D. Garkov
-
Field Summary
Fields Modifier and Type Field Description static boolean
PREFERENCES_GET
Specify when you get Preferences value.static boolean
PREFERENCES_SET
Specify when you set Preferences value.static int
STANDARD_VALUE
Standard value, when no scaling is to be performed.static int
VALUE_DEFAULT
Default value, when not setting any particular value.static int
VALUE_UNSET_INTERNAL
Internal value for checking if any values are stored. -
Constructor Summary
Constructors Constructor Description DPIHelper()
-
Method Summary
Modifier and Type Method Description static void
addScalingComponents(AbstractOptionPane pane, Container main, int min, int max)
Insert the preferencing Components into the specified pane.static void
adjustWindowDecoratations()
By transferring all Window decorations from OS to LookAndFeel, those could be now scaled too.void
displayLifesaver()
Displays a reset dialog that prompts the user to reset the scaling or not, when the previous setting has gone out of reasonable scaling bounds and proper viewing has become impossible.static void
flushPreferences()
Write preferences to disk.static DPIHelper
getInstance()
static boolean
getLifesaverBoolean()
static void
initWindowResizer()
Attaches a WindowResizer listener that will resize any newly opened windows.static boolean
isAvoidable()
Test whether scaling is currently necessary.static void
loadPane()
Load the Preferences Pane.static Preferences
loadPreferences(Class<?> clazz)
Fetches the respective Preferences forclazz
.static URL
loadResource(Class<?> clazz, String filename)
A resource loading utility method for resources placed in the resource package.static int
managePreferences(int val, boolean get)
This is a two-way internal method, used for all scaling-only preferences-related procedures.static float
processEmulatedDPIValue(int sValue)
Converts a slider value into an Emulated DPI value.static void
putLifesaverValue(boolean value)
static int
scaleCssPixels(int pixels)
The CSS pixel px tries to match the reference pixel, which depends on screen DPI and viewer distance from screen.
-
Field Details
-
STANDARD_VALUE
public static final int STANDARD_VALUEStandard value, when no scaling is to be performed. Useful for resetting. -
VALUE_DEFAULT
public static final int VALUE_DEFAULTDefault value, when not setting any particular value.- See Also:
- Constant Field Values
-
VALUE_UNSET_INTERNAL
public static final int VALUE_UNSET_INTERNALInternal value for checking if any values are stored.- See Also:
- Constant Field Values
-
PREFERENCES_GET
public static final boolean PREFERENCES_GETSpecify when you get Preferences value.- See Also:
- Constant Field Values
-
PREFERENCES_SET
public static final boolean PREFERENCES_SETSpecify when you set Preferences value.- See Also:
- Constant Field Values
-
-
Constructor Details
-
DPIHelper
public DPIHelper()
-
-
Method Details
-
getInstance
-
processEmulatedDPIValue
public static float processEmulatedDPIValue(int sValue)Converts a slider value into an Emulated DPI value. We firstly determine standard constants and we use in the process the default slider values. For different than default, one should initialize a ScalingSlider with such new values.- Parameters:
sValue
- sliderValue (get it from Preferences)- Returns:
- float DPI value
-
isAvoidable
public static boolean isAvoidable()Test whether scaling is currently necessary.- Returns:
- true if Scaling could be skipped.
-
addScalingComponents
Insert the preferencing Components into the specified pane.- Parameters:
pane
- to add into
-
scaleCssPixels
public static int scaleCssPixels(int pixels)The CSS pixel px tries to match the reference pixel, which depends on screen DPI and viewer distance from screen. Here we obtain DPPX - the device pixel ratio. Thus we map pixels to provided number of pixels.- Parameters:
pixels
-- Returns:
-
initWindowResizer
public static void initWindowResizer()Attaches a WindowResizer listener that will resize any newly opened windows. This does not resize any already visible windows. -
displayLifesaver
public void displayLifesaver()Displays a reset dialog that prompts the user to reset the scaling or not, when the previous setting has gone out of reasonable scaling bounds and proper viewing has become impossible. There is also a 'Disable' option. -
getLifesaverBoolean
public static boolean getLifesaverBoolean() -
putLifesaverValue
public static void putLifesaverValue(boolean value) -
managePreferences
public static int managePreferences(int val, boolean get)This is a two-way internal method, used for all scaling-only preferences-related procedures. It handles both setting/putting and getting of values. To set values useDPIHelper.PREFERENCES_SET
asget
parameter. To get:DPIHelper.PREFERENCES_GET
, respectively.- Parameters:
val
- Our new ScalingSlider value. Used only, when putting into (i.e. get is false). Meaning when querying values, you could just useDPIHelper.VALUE_DEFAULT
.get
- When true, it returns the previously stored value.- Returns:
- Stored value under 'Scaling Preferences' or
DPIHelper.VALUE_DEFAULT
for potential error checking, if get is false.
-
flushPreferences
Write preferences to disk.- Throws:
BackingStoreException
-
loadPane
public static void loadPane()Load the Preferences Pane. -
adjustWindowDecoratations
public static void adjustWindowDecoratations()By transferring all Window decorations from OS to LookAndFeel, those could be now scaled too. Should be called before initialization of any JFrames or JDialogs, since it affects only subsequently created ones. -
loadResource
A resource loading utility method for resources placed in the resource package.- Parameters:
clazz
- delegates to the respective class loaderfilename
- the resource name- Returns:
- the loaded resource
-
loadPreferences
Fetches the respective Preferences forclazz
.- Parameters:
clazz
-- Returns:
- preferences instance
-