source: josm/trunk/src/org/openstreetmap/josm/tools/GuiSizesHelper.java@ 16486

Last change on this file since 16486 was 16486, checked in by simon04, 4 years ago

fix #18694 - Wrongly rendered cursors on HiDPI screen (patch by johsin18)

File size: 3.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.awt.Dimension;
5import java.awt.GraphicsEnvironment;
6import java.awt.Toolkit;
7
8import 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 */
17public 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 * Sets coefficient of monitor pixel density.
61 * @param pixelDensity coefficient of monitor pixel density to be set.
62 */
63 public static void setPixelDensity(float pixelDensity) {
64 screenDPI = pixelDensity * 96f;
65 }
66
67 /**
68 * Check if a high DPI resolution is used
69 * @return <code>true</code> for HIDPI screens
70 */
71 public static boolean isHiDPI() {
72 return getPixelDensity() >= 2f;
73 }
74
75 /**
76 * Returns a resolution adapted size
77 * @param size Size value to adapt (base size is a low DPI screen)
78 * @return adapted size (may be unmodified)
79 */
80 public static int getSizeDpiAdjusted(int size) {
81 if (size <= 0) return size;
82 return Math.round(size * getScreenDPI() / 96);
83 }
84
85 /**
86 * Returns a resolution adapted size
87 * @param size Size value to adapt (base size is a low DPI screen)
88 * @return adapted size (may be unmodified)
89 */
90 public static float getSizeDpiAdjusted(float size) {
91 if (size <= 0f) return size;
92 return size * getScreenDPI() / 96;
93 }
94
95 /**
96 * Returns a resolution adapted size
97 * @param size Size value to adapt (base size is a low DPI screen)
98 * @return adapted size (may be unmodified)
99 */
100 public static double getSizeDpiAdjusted(double size) {
101 if (size <= 0d) return size;
102 return size * getScreenDPI() / 96;
103 }
104
105 /**
106 * Returns a resolution adapted Dimension
107 * @param dim Dimension value to adapt (base size is a low DPI screen)
108 * @return adapted dimension (may be unmodified)
109 */
110 public static Dimension getDimensionDpiAdjusted(Dimension dim) {
111 float pixelPerInch = getScreenDPI();
112 int width = dim.width;
113 int height = dim.height;
114 if (dim.width > 0) {
115 width = Math.round(dim.width * pixelPerInch / 96);
116 }
117
118 if (dim.height > 0) {
119 height = Math.round(dim.height * pixelPerInch / 96);
120 }
121
122 return new Dimension(width, height);
123 }
124}
Note: See TracBrowser for help on using the repository browser.