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