diff options
author | Niraj Modi | 2015-03-06 16:10:02 +0000 |
---|---|---|
committer | Niraj Modi | 2015-03-06 16:10:02 +0000 |
commit | 07b16d95913fef3678e2e1f0b2d8f749c4f63127 (patch) | |
tree | 9e94e7944fc8c244fa4f0f609cf4eecf08d69136 | |
parent | 986c70568f7fe9726e008a7cf7dcb3250426901d (diff) | |
download | eclipse.platform.swt-07b16d95913fef3678e2e1f0b2d8f749c4f63127.tar.gz eclipse.platform.swt-07b16d95913fef3678e2e1f0b2d8f749c4f63127.tar.xz eclipse.platform.swt-07b16d95913fef3678e2e1f0b2d8f749c4f63127.zip |
Bug 421383 - [Graphics] Scaling issues on high DPI displays
- added image filename array in Image class
- Refactoring for DpiUtil class.
- Test Snippet updated to switch zoom on Tab switching
Change-Id: I4ea9df332cf463f5300902210fe2529d41c01283
Signed-off-by: Niraj Modi <niraj.modi@in.ibm.com>
-rw-r--r-- | bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/DpiUtil.java (renamed from bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/DpiUtil.java) | 26 | ||||
-rw-r--r-- | bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/DpiUtil.java | 100 | ||||
-rw-r--r-- | bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java | 9 | ||||
-rw-r--r-- | bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/DpiUtil.java | 87 | ||||
-rw-r--r-- | bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java | 21 | ||||
-rw-r--r-- | examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/hiDpiWork/Snippet1.java | 29 |
6 files changed, 65 insertions, 207 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/DpiUtil.java b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/DpiUtil.java index f5d84e133e..be1cb517c6 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/cocoa/org/eclipse/swt/graphics/DpiUtil.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/graphics/DpiUtil.java @@ -10,6 +10,8 @@ *******************************************************************************/
package org.eclipse.swt.graphics;
+import java.io.*;
+
/**
* This class hold common constants and utility functions w.r.t. to SWT high DPI
* functionality.
@@ -22,6 +24,9 @@ public class DpiUtil { public static final String FILE_DOUBLE_IDENTIFIER = "@2x";
public static final int SIZE = 3;
+ /* DPI Constants */
+ public static final int DPI_ZOOM_200 = 192;
+ public static final int DPI_ZOOM_150 = 144;
/**
* Prepare an array of Image filenames for various DPI levels.
*
@@ -53,9 +58,9 @@ public class DpiUtil { */
public static int mapDpiToImageSelectorIndex (int dpi) {
int imageSelectorIndex;
- if (dpi >= 144) {
+ if (dpi >= DPI_ZOOM_200) {
imageSelectorIndex = 2;
- } else if (dpi >= 108) {
+ } else if (dpi >= DPI_ZOOM_150) {
imageSelectorIndex = 1;
} else {
imageSelectorIndex = 0;
@@ -84,4 +89,21 @@ public class DpiUtil { }
return imageSelectorIndex;
}
+
+ /**
+ * Checks if filename is a valid file.
+ *
+ * @param filename String name of the file
+ * @return true if file exits
+ */
+ public static boolean fileExists (String filename) {
+ if (filename == null) {
+ return false;
+ }
+ File file = new File (filename);
+ if (( file.exists() ) && (!file.isDirectory())) {
+ return true;
+ }
+ return false;
+ }
}
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/DpiUtil.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/DpiUtil.java deleted file mode 100644 index 18e20aa84f..0000000000 --- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/DpiUtil.java +++ /dev/null @@ -1,100 +0,0 @@ -/*******************************************************************************
- * Copyright (c) 2015 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-package org.eclipse.swt.graphics;
-
-import java.io.*;
-
-/**
- * This class hold common constants and utility functions w.r.t. to SWT high DPI
- * functionality.
- *
- * @since 3.104
- */
-public class DpiUtil {
- public static final char FILE_EXTENSION_SEPARATOR = '.';
- public static final String FILE_ONE_HALF_IDENTIFIER = "@1.5x";
- public static final String FILE_DOUBLE_IDENTIFIER = "@2x";
- public static final int SIZE = 3;
-
- /**
- * Prepare an array of Image filenames for various DPI levels.
- *
- * @return String[] image filenames
- */
- static String[] getImageNames (String fileName) {
- if (fileName == null || fileName.trim().length() == 0)
- return new String[0];
-
- String[] fileNames = new String[SIZE];
- /* Adding the default fileName as-is */
- fileNames[0] = fileName;
-
- /* Creating high DPI fileNames as per pattern */
- int separatorLoc = fileName.lastIndexOf (FILE_EXTENSION_SEPARATOR);
- if (separatorLoc != -1) {
- String name = fileName.substring (0, separatorLoc);
- String ext = fileName.substring (separatorLoc + 1);
- fileNames[1] = name + FILE_ONE_HALF_IDENTIFIER + FILE_EXTENSION_SEPARATOR + ext;
- fileNames[2] = name + FILE_DOUBLE_IDENTIFIER + FILE_EXTENSION_SEPARATOR + ext;
- }
- return fileNames;
- }
-
- /**
- * Compute the imageSelector index based on the DPI value.
- *
- * @return imageSelector index
- */
- static int mapDpiToImageSelectorIndex (int dpi) {
- int imageSelectorIndex;
- if (dpi >= 192) {
- imageSelectorIndex = 2;
- } else if (dpi >= 144) {
- imageSelectorIndex = 1;
- } else {
- imageSelectorIndex = 0;
- }
- return imageSelectorIndex;
- }
-
- /**
- * Compute the imageSelector index based on the zoom value.
- *
- * @return imageSelector index
- */
- static int mapZoomToImageSelectorIndex (int zoom) {
- int imageSelectorIndex = 0;
- switch (zoom) {
- case 200:
- imageSelectorIndex = 2;
- break;
- case 150:
- imageSelectorIndex = 1;
- break;
- case 100:
- default:
- imageSelectorIndex = 0;
- break;
- }
- return imageSelectorIndex;
- }
-
- static boolean fileExists (String filename) {
- if (filename == null) {
- return false;
- }
- File f = new File (filename);
- if (( f.exists() ) && (!f.isDirectory())) {
- return true;
- }
- return false;
- }
-}
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java index 820a2cb533..c27c000f3d 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/graphics/Image.java @@ -1141,7 +1141,7 @@ public Rectangle getBounds() { * * @see ImageData */ -public ImageData getDefaultImageData() { +public ImageData getImageData() { if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); if (OS.USE_CAIRO) { @@ -1813,13 +1813,6 @@ public void addRepresentation (String filename, int zoom) { copyImageDataFromDpiImageStorage(device.getImageSelector()); } -public ImageData getImageData () { - copyImageDataFromDpiImageStorage(0); - ImageData returnVal = getDefaultImageData (); - copyImageDataFromDpiImageStorage(device.getImageSelector()); - return returnVal; -} - public ImageData getImageData (int zoom) { copyImageDataFromDpiImageStorage(DpiUtil.mapZoomToImageSelectorIndex(zoom)); ImageData returnVal = getDefaultImageData (); diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/DpiUtil.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/DpiUtil.java deleted file mode 100644 index 70e266c4d6..0000000000 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/DpiUtil.java +++ /dev/null @@ -1,87 +0,0 @@ -/*******************************************************************************
- * Copyright (c) 2015 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-package org.eclipse.swt.graphics;
-
-/**
- * This class hold common constants and utility functions w.r.t. to SWT high DPI
- * functionality.
- *
- * @since 3.104
- */
-public class DpiUtil {
- public static final char FILE_EXTENSION_SEPARATOR = '.';
- public static final String FILE_ONE_HALF_IDENTIFIER = "@1.5x";
- public static final String FILE_DOUBLE_IDENTIFIER = "@2x";
- public static final int SIZE = 3;
-
- /**
- * Prepare an array of Image filenames for various DPI levels.
- *
- * @return String[] image filenames
- */
- public static String[] getImageNames (String fileName) {
- if (fileName == null || fileName.trim().length() == 0)
- return new String[0];
-
- String[] fileNames = new String[SIZE];
- /* Adding the default fileName as-is */
- fileNames[0] = fileName;
-
- /* Creating high DPI fileNames as per pattern */
- int separatorLoc = fileName.lastIndexOf (FILE_EXTENSION_SEPARATOR);
- if (separatorLoc != -1) {
- String name = fileName.substring (0, separatorLoc);
- String ext = fileName.substring (separatorLoc + 1);
- fileNames[1] = name + FILE_ONE_HALF_IDENTIFIER + FILE_EXTENSION_SEPARATOR + ext;
- fileNames[2] = name + FILE_DOUBLE_IDENTIFIER + FILE_EXTENSION_SEPARATOR + ext;
- }
- return fileNames;
- }
-
- /**
- * Compute the imageSelector index based on the DPI value.
- *
- * @return imageSelector index
- */
- public static int mapDpiToImageSelectorIndex (int dpi) {
- int imageSelectorIndex;
- if (dpi >= 192) {
- imageSelectorIndex = 2;
- } else if (dpi >= 144) {
- imageSelectorIndex = 1;
- } else {
- imageSelectorIndex = 0;
- }
- return imageSelectorIndex;
- }
-
- /**
- * Compute the imageSelector index based on the zoom value.
- *
- * @return imageSelector index
- */
- public static int mapZoomToImageSelectorIndex (int zoom) {
- int imageSelectorIndex = 0;
- switch (zoom) {
- case 200:
- imageSelectorIndex = 2;
- break;
- case 150:
- imageSelectorIndex = 1;
- break;
- case 100:
- default:
- imageSelectorIndex = 0;
- break;
- }
- return imageSelectorIndex;
- }
-}
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java index e84f15a6e6..d6155823aa 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/Image.java @@ -122,7 +122,9 @@ public final class Image extends Resource implements Drawable { * icon. Used only in WinCE */ ImageData data[] = new ImageData [DpiUtil.SIZE]; - + + String dpiFilename[] = new String [DpiUtil.SIZE]; + /** * width of the image */ @@ -645,9 +647,10 @@ public Image (Device device, String filename) { public Image(Device device, String[] filenames) { super(device); if (filenames == null || filenames.length == 0) SWT.error(SWT.ERROR_NULL_ARGUMENT); + dpiFilename = filenames; + int imageSelectorIndex = device.getImageSelector (); - File file = new File (filenames [imageSelectorIndex]); - if (imageSelectorIndex > 0 && (!file.exists () || file.isDirectory ())) { + if (imageSelectorIndex > 0 && !DpiUtil.fileExists (filenames [imageSelectorIndex])) { imageSelectorIndex = 0; } initNative (filenames [imageSelectorIndex]); @@ -890,11 +893,9 @@ public void addRepresentation (String fileName, int zoom) { int imageSelectorIndex = DpiUtil.mapZoomToImageSelectorIndex(zoom); if (imageSelectorIndex == device.getImageSelector ()) { initNative (fileName); - addRepresentation (new ImageData(fileName), zoom); - } - else { - data [imageSelectorIndex] = new ImageData(fileName); } + dpiFilename [imageSelectorIndex] = fileName; + addRepresentation (new ImageData(fileName), zoom); } /** @@ -915,7 +916,11 @@ public void addRepresentation (InputStream stream, int zoom) { * @since 3.104 */ public ImageData getImageData (int zoom) { - return data[DpiUtil.mapZoomToImageSelectorIndex(zoom)]; + int imageSelectorIndex = DpiUtil.mapZoomToImageSelectorIndex(zoom); + if (data[imageSelectorIndex] == null && dpiFilename[imageSelectorIndex] != null) { + addRepresentation(dpiFilename[imageSelectorIndex], zoom); + } + return data[imageSelectorIndex]; } /** diff --git a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/hiDpiWork/Snippet1.java b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/hiDpiWork/Snippet1.java index 888f8384b5..32e2c78b18 100644 --- a/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/hiDpiWork/Snippet1.java +++ b/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/hiDpiWork/Snippet1.java @@ -23,6 +23,7 @@ import java.io.*; */ import org.eclipse.swt.*; import org.eclipse.swt.custom.*; +import org.eclipse.swt.events.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.layout.*; import org.eclipse.swt.widgets.*; @@ -32,7 +33,7 @@ public class Snippet1 { public static void main (String [] args) { Display display = new Display (); File f = new File("collapseall.png"); - Image image = new Image(display, f.getAbsolutePath()); + final Image image = new Image(display, f.getAbsolutePath()); final Shell shell = new Shell (display); shell.setLayout(new GridLayout()); final CTabFolder folder = new CTabFolder(shell, SWT.BORDER); @@ -45,7 +46,7 @@ public static void main (String [] args) { item.setText("Item "+i); item.setImage(image); Text text = new Text(folder, SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL); - text.setText("Text for item "+i+"\n\none, two, three\n\nabcdefghijklmnop"); + text.setText("Switch\nTabs:\n Image\nZooms...\n"); item.setControl(text); } folder.setMinimizeVisible(true); @@ -71,6 +72,30 @@ public static void main (String [] args) { shell.layout(true); } }); + + folder.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + super.widgetSelected(e); + int index = 0; + for (CTabItem item : folder.getItems()) { + if (item == e.item) break; + index++; + } + index = index % 3; + switch (index) { + case 2: + image.addRepresentation("./collapseall@2x.png"); + break; + case 1: + image.addRepresentation("./collapseall@1.5x.png"); + break; + case 0: + image.addRepresentation("./collapseall.png"); + break; + } + } + }); shell.setSize(300, 300); shell.open (); while (!shell.isDisposed ()) { |