1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.util;
|
---|
3 |
|
---|
4 | import java.awt.Dimension;
|
---|
5 | import java.awt.HeadlessException;
|
---|
6 | import java.awt.Toolkit;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.Main;
|
---|
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 10358
|
---|
15 | */
|
---|
16 | public final class GuiSizesHelper {
|
---|
17 |
|
---|
18 | private GuiSizesHelper() {
|
---|
19 | // Hide default constructor for utils classes
|
---|
20 | }
|
---|
21 |
|
---|
22 |
|
---|
23 | /** cache value for screen resolution */
|
---|
24 | private static float screenDPI = -1;
|
---|
25 |
|
---|
26 | /** Request the screen resolution (cached)
|
---|
27 | * @return screen resolution in DPI
|
---|
28 | */
|
---|
29 | private static float getScreenDPI() {
|
---|
30 | if (screenDPI == -1) {
|
---|
31 | synchronized (GuiHelper.class) {
|
---|
32 | if (screenDPI == -1) {
|
---|
33 | float scalePref = (float) Main.pref.getDouble("gui.scale", 0);
|
---|
34 | if (scalePref != 0) {
|
---|
35 | screenDPI = 96f * scalePref;
|
---|
36 | } else {
|
---|
37 | try {
|
---|
38 | screenDPI = Toolkit.getDefaultToolkit().getScreenResolution();
|
---|
39 | } catch (HeadlessException e) {
|
---|
40 | screenDPI = 96;
|
---|
41 | }
|
---|
42 | }
|
---|
43 | }
|
---|
44 | }
|
---|
45 | }
|
---|
46 | return screenDPI;
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Returns coefficient of monitor pixel density. All hardcoded sizes must be multiplied by this value.
|
---|
51 | *
|
---|
52 | * @return float value. 1 - means standard monitor, 2 and high - "retina" display.
|
---|
53 | */
|
---|
54 | public static float getPixelDensity() {
|
---|
55 | float pixelPerInch = getScreenDPI();
|
---|
56 | return pixelPerInch / 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 | float pixelPerInch = getScreenDPI();
|
---|
75 | return Math.round(size * pixelPerInch / 96);
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Returns a resolution adapted size
|
---|
80 | * @param size Size value to adapt (base size is a low DPI screen)
|
---|
81 | * @return adapted size (may be unmodified)
|
---|
82 | */
|
---|
83 | public static float getSizeDpiAdjusted(float size) {
|
---|
84 | if (size <= 0f) return size;
|
---|
85 | float pixelPerInch = getScreenDPI();
|
---|
86 | return size * pixelPerInch / 96;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Returns a resolution adapted size
|
---|
91 | * @param size Size value to adapt (base size is a low DPI screen)
|
---|
92 | * @return adapted size (may be unmodified)
|
---|
93 | */
|
---|
94 | public static double getSizeDpiAdjusted(double size) {
|
---|
95 | if (size <= 0d) return size;
|
---|
96 | float pixelPerInch = getScreenDPI();
|
---|
97 | return size * pixelPerInch / 96;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Returns a resolution adapted Dimension
|
---|
102 | * @param dim Dimension value to adapt (base size is a low DPI screen)
|
---|
103 | * @return adapted dimension (may be unmodified)
|
---|
104 | */
|
---|
105 | public static Dimension getDimensionDpiAdjusted(Dimension dim) {
|
---|
106 | float pixelPerInch = getScreenDPI();
|
---|
107 | int width = dim.width, height = dim.height;
|
---|
108 | if (dim.width > 0) {
|
---|
109 | width = Math.round(dim.width * pixelPerInch / 96);
|
---|
110 | }
|
---|
111 |
|
---|
112 | if (dim.height > 0) {
|
---|
113 | height = Math.round(dim.height * pixelPerInch / 96);
|
---|
114 | }
|
---|
115 |
|
---|
116 | return new Dimension(width, height);
|
---|
117 | }
|
---|
118 | }
|
---|