[10362] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[12682] | 2 | package org.openstreetmap.josm.tools;
|
---|
[10358] | 3 |
|
---|
| 4 | import java.awt.Dimension;
|
---|
[10409] | 5 | import java.awt.GraphicsEnvironment;
|
---|
[10358] | 6 | import java.awt.Toolkit;
|
---|
| 7 |
|
---|
[12846] | 8 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
[10392] | 9 |
|
---|
[10358] | 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.
|
---|
[12682] | 14 | * @since 12682 (moved from {@code gui.util} package)
|
---|
[10358] | 15 | * @since 10358
|
---|
| 16 | */
|
---|
[10368] | 17 | public final class GuiSizesHelper {
|
---|
[10362] | 18 |
|
---|
| 19 | private GuiSizesHelper() {
|
---|
| 20 | // Hide default constructor for utils classes
|
---|
| 21 | }
|
---|
| 22 |
|
---|
[10358] | 23 | /** cache value for screen resolution */
|
---|
[10392] | 24 | private static float screenDPI = -1;
|
---|
[10358] | 25 |
|
---|
[10409] | 26 | /**
|
---|
| 27 | * Request the screen resolution (cached)
|
---|
[10358] | 28 | * @return screen resolution in DPI
|
---|
| 29 | */
|
---|
[10392] | 30 | private static float getScreenDPI() {
|
---|
[10358] | 31 | if (screenDPI == -1) {
|
---|
[12682] | 32 | synchronized (GuiSizesHelper.class) {
|
---|
[10358] | 33 | if (screenDPI == -1) {
|
---|
[12846] | 34 | float scalePref = (float) Config.getPref().getDouble("gui.scale", 1.0);
|
---|
[10392] | 35 | if (scalePref != 0) {
|
---|
| 36 | screenDPI = 96f * scalePref;
|
---|
| 37 | } else {
|
---|
[10409] | 38 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
[10392] | 39 | screenDPI = Toolkit.getDefaultToolkit().getScreenResolution();
|
---|
[10409] | 40 | } else {
|
---|
[10392] | 41 | screenDPI = 96;
|
---|
| 42 | }
|
---|
[10360] | 43 | }
|
---|
[10358] | 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() {
|
---|
[10409] | 56 | return getScreenDPI() / 96f;
|
---|
[10358] | 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
|
---|
[10362] | 69 | * @param size Size value to adapt (base size is a low DPI screen)
|
---|
[10358] | 70 | * @return adapted size (may be unmodified)
|
---|
| 71 | */
|
---|
| 72 | public static int getSizeDpiAdjusted(int size) {
|
---|
| 73 | if (size <= 0) return size;
|
---|
[10409] | 74 | return Math.round(size * getScreenDPI() / 96);
|
---|
[10358] | 75 | }
|
---|
| 76 |
|
---|
| 77 | /**
|
---|
| 78 | * Returns a resolution adapted size
|
---|
[10362] | 79 | * @param size Size value to adapt (base size is a low DPI screen)
|
---|
[10358] | 80 | * @return adapted size (may be unmodified)
|
---|
| 81 | */
|
---|
| 82 | public static float getSizeDpiAdjusted(float size) {
|
---|
| 83 | if (size <= 0f) return size;
|
---|
[10409] | 84 | return size * getScreenDPI() / 96;
|
---|
[10358] | 85 | }
|
---|
| 86 |
|
---|
| 87 | /**
|
---|
| 88 | * Returns a resolution adapted size
|
---|
[10362] | 89 | * @param size Size value to adapt (base size is a low DPI screen)
|
---|
[10358] | 90 | * @return adapted size (may be unmodified)
|
---|
| 91 | */
|
---|
| 92 | public static double getSizeDpiAdjusted(double size) {
|
---|
| 93 | if (size <= 0d) return size;
|
---|
[10409] | 94 | return size * getScreenDPI() / 96;
|
---|
[10358] | 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) {
|
---|
[10392] | 103 | float pixelPerInch = getScreenDPI();
|
---|
[10409] | 104 | int width = dim.width;
|
---|
| 105 | int height = dim.height;
|
---|
[10358] | 106 | if (dim.width > 0) {
|
---|
[10392] | 107 | width = Math.round(dim.width * pixelPerInch / 96);
|
---|
[10358] | 108 | }
|
---|
| 109 |
|
---|
| 110 | if (dim.height > 0) {
|
---|
[10392] | 111 | height = Math.round(dim.height * pixelPerInch / 96);
|
---|
[10358] | 112 | }
|
---|
| 113 |
|
---|
| 114 | return new Dimension(width, height);
|
---|
| 115 | }
|
---|
| 116 | }
|
---|