Skip to content

Commit

Permalink
Refactored the win32 swt graphic classes and DPIUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
amartya4256 committed Feb 13, 2024
1 parent 1c99833 commit 8132e56
Show file tree
Hide file tree
Showing 9 changed files with 177 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*******************************************************************************/
package org.eclipse.swt.internal;

import java.util.*;
import java.util.function.*;

import org.eclipse.swt.*;
Expand All @@ -39,7 +40,7 @@ public class DPIUtil {

private static final int DPI_ZOOM_100 = 96;

private static int deviceZoom = 100;
private static int deviceZoom = 125;
private static int nativeDeviceZoom = 100;

private static enum AutoScaleMethod { AUTO, NEAREST, SMOOTH }
Expand Down Expand Up @@ -147,14 +148,24 @@ public static float[] autoScaleDown (Drawable drawable, float size[]) {
* Auto-scale down int dimensions.
*/
public static int autoScaleDown (int size) {
if (deviceZoom == 100 || size == SWT.DEFAULT) return size;
float scaleFactor = getScalingFactor ();
return autoScaleDown(size, null);
}

public static int autoScaleDown (int size, Shell shell) {
int zoom = Optional.ofNullable(shell).map(Shell::getCurrentDeviceZoom).orElse(deviceZoom);
if (zoom == 100 || size == SWT.DEFAULT) return size;
float scaleFactor = getScalingFactor (shell);
return Math.round (size / scaleFactor);
}

/**
* Auto-scale down int dimensions if enabled for Drawable class.
*/
public static int autoScaleDown (Drawable drawable, int size) {
return autoScaleDown(drawable, size, null);
}

public static int autoScaleDown (Drawable drawable, int size, Shell shell) {
if (drawable != null && !drawable.isAutoScalable ()) return size;
return autoScaleDown (size);
}
Expand All @@ -163,13 +174,12 @@ public static int autoScaleDown (Drawable drawable, int size) {
* Auto-scale down float dimensions.
*/
public static float autoScaleDown (float size) {
if (deviceZoom == 100 || size == SWT.DEFAULT) return size;
float scaleFactor = getScalingFactor ();
return (size / scaleFactor);
return autoScaleDown(size, null);
}

public static float autoScaleDown (float size, Shell shell) {
if (shell.getCurrentDeviceZoom() == 100 || size == SWT.DEFAULT) return size;
int zoom = Optional.ofNullable(shell).map(Shell::getCurrentDeviceZoom).orElse(deviceZoom);
if (zoom == 100 || size == SWT.DEFAULT) return size;
float scaleFactor = getScalingFactor (shell);
return (size / scaleFactor);
}
Expand All @@ -178,8 +188,7 @@ public static float autoScaleDown (float size, Shell shell) {
* Auto-scale down float dimensions if enabled for Drawable class.
*/
public static float autoScaleDown (Drawable drawable, float size) {
if (drawable != null && !drawable.isAutoScalable ()) return size;
return autoScaleDown (size);
return autoScaleDown (drawable, size, null);
}

public static float autoScaleDown (Drawable drawable, float size, Shell shell) {
Expand All @@ -191,8 +200,13 @@ public static float autoScaleDown (Drawable drawable, float size, Shell shell) {
* Returns a new scaled down Point.
*/
public static Point autoScaleDown (Point point) {
if (deviceZoom == 100 || point == null) return point;
float scaleFactor = getScalingFactor ();
return autoScaleDown(point, null);
}

public static Point autoScaleDown (Point point, Shell shell) {
int zoom = Optional.ofNullable(shell).map(Shell::getCurrentDeviceZoom).orElse(deviceZoom);
if (zoom == 100 || point == null) return point;
float scaleFactor = getScalingFactor (shell);
Point scaledPoint = new Point (0,0);
scaledPoint.x = Math.round (point.x / scaleFactor);
scaledPoint.y = Math.round (point.y / scaleFactor);
Expand All @@ -203,18 +217,27 @@ public static Point autoScaleDown (Point point) {
* Returns a new scaled down Point if enabled for Drawable class.
*/
public static Point autoScaleDown (Drawable drawable, Point point) {
return autoScaleDown(drawable, point, null);
}

public static Point autoScaleDown (Drawable drawable, Point point, Shell shell) {
if (drawable != null && !drawable.isAutoScalable ()) return point;
return autoScaleDown (point);
return autoScaleDown (point, shell);
}

/**
* Returns a new scaled down Rectangle.
*/
public static Rectangle autoScaleDown (Rectangle rect) {
if (deviceZoom == 100 || rect == null) return rect;
return autoScaleDown(rect, null);
}

public static Rectangle autoScaleDown (Rectangle rect, Shell shell) {
int zoom = Optional.ofNullable(shell).map(Shell::getCurrentDeviceZoom).orElse(deviceZoom);
if (zoom == 100 || rect == null) return rect;
Rectangle scaledRect = new Rectangle (0,0,0,0);
Point scaledTopLeft = DPIUtil.autoScaleDown (new Point (rect.x, rect.y));
Point scaledBottomRight = DPIUtil.autoScaleDown (new Point (rect.x + rect.width, rect.y + rect.height));
Point scaledTopLeft = DPIUtil.autoScaleDown (new Point (rect.x, rect.y), shell);
Point scaledBottomRight = DPIUtil.autoScaleDown (new Point (rect.x + rect.width, rect.y + rect.height), shell);

scaledRect.x = scaledTopLeft.x;
scaledRect.y = scaledTopLeft.y;
Expand Down Expand Up @@ -318,13 +341,12 @@ public static int[] autoScaleUp(Drawable drawable, int[] pointArray) {
* Auto-scale up int dimensions.
*/
public static int autoScaleUp (int size) {
if (deviceZoom == 100 || size == SWT.DEFAULT) return size;
float scaleFactor = getScalingFactor ();
return Math.round (size * scaleFactor);
return autoScaleUp(size, null);
}

public static int autoScaleUp (int size, Shell shell) {
if (shell.getCurrentDeviceZoom() == 100 || size == SWT.DEFAULT) return size;
int zoom = Optional.ofNullable(shell).map(Shell::getCurrentDeviceZoom).orElse(deviceZoom);
if (zoom == 100 || size == SWT.DEFAULT) return size;
float scaleFactor = getScalingFactor (shell);
return Math.round (size * scaleFactor);
}
Expand All @@ -342,8 +364,7 @@ public static int autoScaleUpUsingNativeDPI (int size) {
* Auto-scale up int dimensions if enabled for Drawable class.
*/
public static int autoScaleUp (Drawable drawable, int size) {
if (drawable != null && !drawable.isAutoScalable ()) return size;
return autoScaleUp (size);
return autoScaleUp(drawable, size, null);
}

public static int autoScaleUp (Drawable drawable, int size, Shell shell) {
Expand All @@ -352,20 +373,23 @@ public static int autoScaleUp (Drawable drawable, int size, Shell shell) {
}

public static float autoScaleUp(float size) {
if (deviceZoom == 100 || size == SWT.DEFAULT) return size;
float scaleFactor = getScalingFactor ();
return (size * scaleFactor);
return autoScaleUp(size, null);
}

public static float autoScaleUp(float size, Shell shell) {
if (deviceZoom == 100 || size == SWT.DEFAULT) return size;
int zoom = Optional.ofNullable(shell).map(Shell::getCurrentDeviceZoom).orElse(deviceZoom);
if (zoom == 100 || size == SWT.DEFAULT) return size;
float scaleFactor = getScalingFactor(shell);
return (size * scaleFactor);
}

public static float autoScaleUp(Drawable drawable, float size) {
return autoScaleUp(drawable, size, null);
}

public static float autoScaleUp(Drawable drawable, float size, Shell shell) {
if (drawable != null && !drawable.isAutoScalable ()) return size;
return autoScaleUp (size);
return autoScaleUp (size, shell);
}

/**
Expand Down Expand Up @@ -417,17 +441,15 @@ public static Rectangle autoScaleUp (Drawable drawable, Rectangle rect) {
* @return float scaling factor
*/
private static float getScalingFactor () {
if (useCairoAutoScale) {
return 1;
}
return deviceZoom / 100f;
return getScalingFactor(null);
}

private static float getScalingFactor (Shell shell) {
if (useCairoAutoScale) {
return 1;
}
return shell.getCurrentDeviceZoom() / 100f;
int zoom = Optional.ofNullable(shell).map(Shell::getCurrentDeviceZoom).orElse(deviceZoom);
return zoom / 100f;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ public void drawImage (Image image, int srcX, int srcY, int srcWidth, int srcHei

void drawImage(Image srcImage, int srcX, int srcY, int srcWidth, int srcHeight, int destX, int destY, int destWidth, int destHeight, boolean simple) {
/* Refresh Image as per zoom level, if required. */
srcImage.refreshImageForZoom ();
srcImage.refreshImageForZoom (data.shell.getCurrentDeviceZoom());

if (data.gdipGraphics != 0) {
//TODO - cache bitmap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,11 @@ public Image(Device device, ImageDataProvider imageDataProvider) {
* @return true if image is refreshed
*/
boolean refreshImageForZoom () {
return refreshImageForZoom(DPIUtil.getDeviceZoom());
}

boolean refreshImageForZoom (int deviceZoomLevel) {
boolean refreshed = false;
int deviceZoomLevel = DPIUtil.getDeviceZoom();
if (imageFileNameProvider != null) {
if (deviceZoomLevel != currentDeviceZoom) {
ElementAtZoom<String> filename = DPIUtil.validateAndGetImagePathAtZoom (imageFileNameProvider, deviceZoomLevel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,8 @@ public void close() {
*/
public boolean contains (float x, float y, GC gc, boolean outline) {
Drawable drawable = getDevice();
x = DPIUtil.autoScaleUp(drawable, x);
y = DPIUtil.autoScaleUp(drawable, y);
x = DPIUtil.autoScaleUp(drawable, x, gc.data.shell);
y = DPIUtil.autoScaleUp(drawable, y, gc.data.shell);
return containsInPixels(x, y, gc, outline);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public class Pattern extends Resource {
*/
public Pattern(Device device, Image image) {
super(device);
isImagePattern = true;
if (image == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
if (image.isDisposed()) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
this.device.checkGDIP();
Expand Down Expand Up @@ -198,6 +199,7 @@ public Pattern(Device device, float x1, float y1, float x2, float y2, Color colo
this.color2 = color2;
this.alpha1 = alpha1;
this.alpha2 = alpha2;
this.isImagePattern = false;
initializeSize(null);
}

Expand All @@ -211,13 +213,14 @@ private Pattern(Shell shell, float x1, float y1, float x2, float y2, Color color
this.color2 = color2;
this.alpha1 = alpha1;
this.alpha2 = alpha2;
this.isImagePattern = false;
initializeSize(shell);
}

private HashMap<Integer, Pattern> scaledpattern = new HashMap<>();

Pattern getScaledPattern(Shell shell) {
if(shell.getCurrentDeviceZoom() == this.device.getDeviceZoom()) {
if(shell.getCurrentDeviceZoom() == this.device.getDeviceZoom() || this.isImagePattern) {
return this;
}
if(this.scaledpattern.get(shell.getCurrentDeviceZoom()) == null) {
Expand All @@ -230,6 +233,7 @@ Pattern getScaledPattern(Shell shell) {
private float x1, y1, x2, y2;
private Color color1, color2;
private int alpha1, alpha2;
private final boolean isImagePattern;

//void initializeSize(float x1, float y1, float x2, float y2, Color color1, int alpha1, Color color2, int alpha2) {
/**
Expand Down
Loading

0 comments on commit 8132e56

Please sign in to comment.