1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.awt.Dimension;
|
---|
5 | import java.awt.GraphicsEnvironment;
|
---|
6 | import java.awt.Toolkit;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Support class to handle size information of Gui elements
|
---|
12 | * This is needed, because display resolution may vary a lot and a common set
|
---|
13 | * of sizes wont work for all users alike.
|
---|
14 | * @since 12682 (moved from {@code gui.util} package)
|
---|
15 | * @since 10358
|
---|
16 | */
|
---|
17 | public final class GuiSizesHelper {
|
---|
18 |
|
---|
19 | private GuiSizesHelper() {
|
---|
20 | // Hide default constructor for utils classes
|
---|
21 | }
|
---|
22 |
|
---|
23 | /** cache value for screen resolution */
|
---|
24 | private static float screenDPI = -1;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Request the screen resolution (cached)
|
---|
28 | * @return screen resolution in DPI
|
---|
29 | */
|
---|
30 | private static float getScreenDPI() {
|
---|
31 | if (screenDPI == -1) {
|
---|
32 | synchronized (GuiSizesHelper.class) {
|
---|
33 | if (screenDPI == -1) {
|
---|
34 | float scalePref = (float) Config.getPref().getDouble("gui.scale", 1.0);
|
---|
35 | if (scalePref != 0) {
|
---|
36 | screenDPI = 96f * scalePref;
|
---|
37 | } else {
|
---|
38 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
39 | screenDPI = Toolkit.getDefaultToolkit().getScreenResolution();
|
---|
40 | } else {
|
---|
41 | screenDPI = 96;
|
---|
42 | }
|
---|
43 | }
|
---|
44 | }
|
---|
45 | }
|
---|
46 | }
|
---|
47 | return screenDPI;
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Returns coefficient of monitor pixel density. All hardcoded sizes must be multiplied by this value.
|
---|
52 | *
|
---|
53 | * @return float value. 1 - means standard monitor, 2 and high - "retina" display.
|
---|
54 | */
|
---|
55 | public static float getPixelDensity() {
|
---|
56 | return getScreenDPI() / 96f;
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Check if a high DPI resolution is used
|
---|
61 | * @return <code>true</code> for HIDPI screens
|
---|
62 | */
|
---|
63 | public static boolean isHiDPI() {
|
---|
64 | return getPixelDensity() >= 2f;
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Returns a resolution adapted size
|
---|
69 | * @param size Size value to adapt (base size is a low DPI screen)
|
---|
70 | * @return adapted size (may be unmodified)
|
---|
71 | */
|
---|
72 | public static int getSizeDpiAdjusted(int size) {
|
---|
73 | if (size <= 0) return size;
|
---|
74 | return Math.round(size * getScreenDPI() / 96);
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Returns a resolution adapted size
|
---|
79 | * @param size Size value to adapt (base size is a low DPI screen)
|
---|
80 | * @return adapted size (may be unmodified)
|
---|
81 | */
|
---|
82 | public static float getSizeDpiAdjusted(float size) {
|
---|
83 | if (size <= 0f) return size;
|
---|
84 | return size * getScreenDPI() / 96;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Returns a resolution adapted size
|
---|
89 | * @param size Size value to adapt (base size is a low DPI screen)
|
---|
90 | * @return adapted size (may be unmodified)
|
---|
91 | */
|
---|
92 | public static double getSizeDpiAdjusted(double size) {
|
---|
93 | if (size <= 0d) return size;
|
---|
94 | return size * getScreenDPI() / 96;
|
---|
95 | }
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * Returns a resolution adapted Dimension
|
---|
99 | * @param dim Dimension value to adapt (base size is a low DPI screen)
|
---|
100 | * @return adapted dimension (may be unmodified)
|
---|
101 | */
|
---|
102 | public static Dimension getDimensionDpiAdjusted(Dimension dim) {
|
---|
103 | float pixelPerInch = getScreenDPI();
|
---|
104 | int width = dim.width;
|
---|
105 | int height = dim.height;
|
---|
106 | if (dim.width > 0) {
|
---|
107 | width = Math.round(dim.width * pixelPerInch / 96);
|
---|
108 | }
|
---|
109 |
|
---|
110 | if (dim.height > 0) {
|
---|
111 | height = Math.round(dim.height * pixelPerInch / 96);
|
---|
112 | }
|
---|
113 |
|
---|
114 | return new Dimension(width, height);
|
---|
115 | }
|
---|
116 | }
|
---|