source: josm/trunk/src/org/openstreetmap/josm/gui/util/GuiSizesHelper.java@ 11098

Last change on this file since 11098 was 10505, checked in by bastiK, 8 years ago

see #9995 - change default gui scale from 'auto' (0) to 1.0 for stable release

File size: 3.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.util;
3
4import java.awt.Dimension;
5import java.awt.GraphicsEnvironment;
6import java.awt.Toolkit;
7
8import 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 */
16public final class GuiSizesHelper {
17
18 private GuiSizesHelper() {
19 // Hide default constructor for utils classes
20 }
21
22 /** cache value for screen resolution */
23 private static float screenDPI = -1;
24
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", 1.0);
34 if (scalePref != 0) {
35 screenDPI = 96f * scalePref;
36 } else {
37 if (!GraphicsEnvironment.isHeadless()) {
38 screenDPI = Toolkit.getDefaultToolkit().getScreenResolution();
39 } else {
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 return getScreenDPI() / 96f;
56 }
57
58 /**
59 * Check if a high DPI resolution is used
60 * @return <code>true</code> for HIDPI screens
61 */
62 public static boolean isHiDPI() {
63 return getPixelDensity() >= 2f;
64 }
65
66 /**
67 * Returns a resolution adapted size
68 * @param size Size value to adapt (base size is a low DPI screen)
69 * @return adapted size (may be unmodified)
70 */
71 public static int getSizeDpiAdjusted(int size) {
72 if (size <= 0) return size;
73 return Math.round(size * getScreenDPI() / 96);
74 }
75
76 /**
77 * Returns a resolution adapted size
78 * @param size Size value to adapt (base size is a low DPI screen)
79 * @return adapted size (may be unmodified)
80 */
81 public static float getSizeDpiAdjusted(float size) {
82 if (size <= 0f) return size;
83 return size * getScreenDPI() / 96;
84 }
85
86 /**
87 * Returns a resolution adapted size
88 * @param size Size value to adapt (base size is a low DPI screen)
89 * @return adapted size (may be unmodified)
90 */
91 public static double getSizeDpiAdjusted(double size) {
92 if (size <= 0d) return size;
93 return size * getScreenDPI() / 96;
94 }
95
96 /**
97 * Returns a resolution adapted Dimension
98 * @param dim Dimension value to adapt (base size is a low DPI screen)
99 * @return adapted dimension (may be unmodified)
100 */
101 public static Dimension getDimensionDpiAdjusted(Dimension dim) {
102 float pixelPerInch = getScreenDPI();
103 int width = dim.width;
104 int height = dim.height;
105 if (dim.width > 0) {
106 width = Math.round(dim.width * pixelPerInch / 96);
107 }
108
109 if (dim.height > 0) {
110 height = Math.round(dim.height * pixelPerInch / 96);
111 }
112
113 return new Dimension(width, height);
114 }
115}
Note: See TracBrowser for help on using the repository browser.