Skip to content

Commit

Permalink
Add zoom dependent utility methods into DPIUtil and adapt calls from …
Browse files Browse the repository at this point in the history
…widgets to DPIUtil
  • Loading branch information
amartya4256 authored and akoch-yatta committed Apr 12, 2024
1 parent 7c5574c commit 8a77a4e
Show file tree
Hide file tree
Showing 31 changed files with 284 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,12 @@ public static int[] autoScaleDown(Drawable drawable, int[] pointArray) {
* Auto-scale down float array dimensions.
*/
public static float[] autoScaleDown (float size[]) {
if (deviceZoom == 100 || size == null) return size;
float scaleFactor = getScalingFactor ();
return autoScaleDown(size, deviceZoom);
}

public static float[] autoScaleDown (float size[], int zoom) {
if (zoom == 100 || size == null) return size;
float scaleFactor = getScalingFactor (zoom);
float scaledSize[] = new float[size.length];
for (int i = 0; i < scaledSize.length; i++) {
scaledSize[i] = size[i] / scaleFactor;
Expand All @@ -152,49 +156,74 @@ public static float[] autoScaleDown (float size[]) {
* Auto-scale down float array dimensions if enabled for Drawable class.
*/
public static float[] autoScaleDown (Drawable drawable, float size[]) {
return autoScaleDown(drawable, size, deviceZoom);
}

public static float[] autoScaleDown (Drawable drawable, float size[], int zoom) {
if (drawable != null && !drawable.isAutoScalable ()) return size;
return autoScaleDown (size);
return autoScaleDown(size, zoom);
}

/**
* 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, deviceZoom);
}

public static int autoScaleDown (int size, int zoom) {
if (zoom == 100 || size == SWT.DEFAULT) return size;
float scaleFactor = getScalingFactor (zoom);
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, deviceZoom);
}

public static int autoScaleDown (Drawable drawable, int size, int zoom) {
if (drawable != null && !drawable.isAutoScalable ()) return size;
return autoScaleDown (size);
return autoScaleDown (size, zoom);
}

/**
* Auto-scale down float dimensions.
*/
public static float autoScaleDown (float size) {
if (deviceZoom == 100 || size == SWT.DEFAULT) return size;
float scaleFactor = getScalingFactor ();
return autoScaleDown(size, deviceZoom);
}

public static float autoScaleDown (float size, int zoom) {
if (zoom == 100 || size == SWT.DEFAULT) return size;
float scaleFactor = getScalingFactor (zoom);
return (size / scaleFactor);
}

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

public static float autoScaleDown (Drawable drawable, float size, int zoom) {
if (drawable != null && !drawable.isAutoScalable ()) return size;
return autoScaleDown (size);
return autoScaleDown (size, zoom);
}

/**
* 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, deviceZoom);
}

public static Point autoScaleDown (Point point, int zoom) {
if (zoom == 100 || point == null) return point;
float scaleFactor = getScalingFactor (zoom);
Point scaledPoint = new Point (0,0);
scaledPoint.x = Math.round (point.x / scaleFactor);
scaledPoint.y = Math.round (point.y / scaleFactor);
Expand All @@ -205,18 +234,26 @@ 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, deviceZoom);
}

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

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

public static Rectangle autoScaleDown (Rectangle rect, int zoom) {
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), zoom);
Point scaledBottomRight = DPIUtil.autoScaleDown (new Point (rect.x + rect.width, rect.y + rect.height), zoom);

scaledRect.x = scaledTopLeft.x;
scaledRect.y = scaledTopLeft.y;
Expand All @@ -229,7 +266,12 @@ public static Rectangle autoScaleDown (Rectangle rect) {
*/
public static Rectangle autoScaleDown (Drawable drawable, Rectangle rect) {
if (drawable != null && !drawable.isAutoScalable ()) return rect;
return autoScaleDown (rect);
return autoScaleDown(rect, deviceZoom);
}

public static Rectangle autoScaleDown (Drawable drawable, Rectangle rect, int deviceZoom) {
if (drawable != null && !drawable.isAutoScalable ()) return rect;
return autoScaleDown (rect, deviceZoom);
}

/**
Expand Down Expand Up @@ -273,7 +315,7 @@ private static ImageData autoScaleImageData (Device device, final ImageData imag
yield result;
}
default -> imageData.scaledTo (scaledWidth, scaledHeight);
};
};
}

/**
Expand Down Expand Up @@ -311,8 +353,12 @@ public static ImageData autoScaleUp (Device device, final ElementAtZoom<ImageDat
}

public static int[] autoScaleUp(int[] pointArray) {
return autoScaleUp(pointArray, deviceZoom);
}

public static int[] autoScaleUp(int[] pointArray, int deviceZoom) {
if (deviceZoom == 100 || pointArray == null) return pointArray;
float scaleFactor = getScalingFactor ();
float scaleFactor = getScalingFactor (deviceZoom);
int [] returnArray = new int[pointArray.length];
for (int i = 0; i < pointArray.length; i++) {
returnArray [i] = Math.round (pointArray [i] * scaleFactor);
Expand All @@ -321,23 +367,25 @@ public static int[] autoScaleUp(int[] pointArray) {
}

public static int[] autoScaleUp(Drawable drawable, int[] pointArray) {
if (drawable != null && !drawable.isAutoScalable ()) return pointArray;
return autoScaleUp (pointArray);
return autoScaleUp(drawable, pointArray, deviceZoom);
}

public static int[] autoScaleUp(Drawable drawable, int[] pointArray, int deviceZoom) {
if (drawable != null && !drawable.isAutoScalable ()) return pointArray;
return autoScaleUp (pointArray, deviceZoom);
}
/**
* 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, deviceZoom);
}

/**
* Auto-scale up int dimensions to match the given zoom level
*/
public static int autoScaleUp (int size, int zoom) {
if (zoom == 100 || size == SWT.DEFAULT) return size;
float scaleFactor = getScalingFactor (zoom);
return Math.round (size * scaleFactor);
}
Expand All @@ -355,27 +403,43 @@ public static int autoScaleUpUsingNativeDPI (int size) {
* Auto-scale up int dimensions if enabled for Drawable class.
*/
public static int autoScaleUp (Drawable drawable, int size) {
return autoScaleUp(drawable, size, deviceZoom);
}

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

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

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

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

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

/**
* Returns a new scaled up Point.
*/
public static Point autoScaleUp (Point point) {
if (deviceZoom == 100 || point == null) return point;
float scaleFactor = getScalingFactor ();
return autoScaleUp(point, deviceZoom);
}

public static Point autoScaleUp (Point point, int zoom) {
if (zoom == 100 || point == null) return point;
float scaleFactor = getScalingFactor (zoom);
Point scaledPoint = new Point (0,0);
scaledPoint.x = Math.round (point.x * scaleFactor);
scaledPoint.y = Math.round (point.y * scaleFactor);
Expand All @@ -386,18 +450,26 @@ public static Point autoScaleUp (Point point) {
* Returns a new scaled up Point if enabled for Drawable class.
*/
public static Point autoScaleUp (Drawable drawable, Point point) {
return autoScaleUp (drawable, point, deviceZoom);
}

public static Point autoScaleUp (Drawable drawable, Point point, int zoom) {
if (drawable != null && !drawable.isAutoScalable ()) return point;
return autoScaleUp (point);
return autoScaleUp (point, zoom);
}

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

public static Rectangle autoScaleUp (Rectangle rect, int zoom) {
if (zoom == 100 || rect == null) return rect;
Rectangle scaledRect = new Rectangle (0,0,0,0);
Point scaledTopLeft = DPIUtil.autoScaleUp (new Point (rect.x, rect.y));
Point scaledBottomRight = DPIUtil.autoScaleUp (new Point (rect.x + rect.width, rect.y + rect.height));
Point scaledTopLeft = DPIUtil.autoScaleUp (new Point (rect.x, rect.y), zoom);
Point scaledBottomRight = DPIUtil.autoScaleUp (new Point (rect.x + rect.width, rect.y + rect.height), zoom);

scaledRect.x = scaledTopLeft.x;
scaledRect.y = scaledTopLeft.y;
Expand All @@ -410,8 +482,12 @@ public static Rectangle autoScaleUp (Rectangle rect) {
* Returns a new scaled up Rectangle if enabled for Drawable class.
*/
public static Rectangle autoScaleUp (Drawable drawable, Rectangle rect) {
return autoScaleUp(drawable, rect, deviceZoom);
}

public static Rectangle autoScaleUp (Drawable drawable, Rectangle rect, int deviceZoom) {
if (drawable != null && !drawable.isAutoScalable ()) return rect;
return autoScaleUp (rect);
return autoScaleUp (rect, deviceZoom);
}

/**
Expand All @@ -430,6 +506,9 @@ private static float getScalingFactor (int shellDeviceZoom) {
if (useCairoAutoScale) {
return 1;
}
if (shellDeviceZoom <= 0) {
shellDeviceZoom = deviceZoom;
}
return shellDeviceZoom / 100f;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1384,7 +1384,7 @@ LRESULT wmNotifyChild (NMHDR hdr, long wParam, long lParam) {

int x = margin + (isRadioOrCheck() ? radioOrCheckTextPadding : 3);
int y = Math.max (0, (nmcd.bottom - imageBounds.height) / 2);
gc.drawImage (image, DPIUtil.autoScaleDown(x), DPIUtil.autoScaleDown(y));
gc.drawImage (image, DPIUtil.autoScaleDown(x, getZoom()), DPIUtil.autoScaleDown(y, getZoom()));
gc.dispose ();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ public Canvas (Composite parent, int style) {
* @since 3.2
*/
public void drawBackground (GC gc, int x, int y, int width, int height) {
x = DPIUtil.autoScaleUp(x);
y = DPIUtil.autoScaleUp(y);
width = DPIUtil.autoScaleUp(width);
height = DPIUtil.autoScaleUp(height);
x = DPIUtil.autoScaleUp(x, getZoom());
y = DPIUtil.autoScaleUp(y, getZoom());
width = DPIUtil.autoScaleUp(width, getZoom());
height = DPIUtil.autoScaleUp(height, getZoom());
drawBackgroundInPixels(gc, x, y, width, height, 0, 0);
}

Expand Down Expand Up @@ -199,12 +199,12 @@ void reskinChildren (int flags) {
*/
public void scroll (int destX, int destY, int x, int y, int width, int height, boolean all) {
checkWidget ();
destX = DPIUtil.autoScaleUp(destX);
destY = DPIUtil.autoScaleUp(destY);
x = DPIUtil.autoScaleUp(x);
y = DPIUtil.autoScaleUp(y);
width = DPIUtil.autoScaleUp(width);
height = DPIUtil.autoScaleUp(height);
destX = DPIUtil.autoScaleUp(destX, getZoom());
destY = DPIUtil.autoScaleUp(destY, getZoom());
x = DPIUtil.autoScaleUp(x, getZoom());
y = DPIUtil.autoScaleUp(y, getZoom());
width = DPIUtil.autoScaleUp(width, getZoom());
height = DPIUtil.autoScaleUp(height, getZoom());
scrollInPixels(destX, destY, x, y, width, height, all);
}

Expand Down
Loading

0 comments on commit 8a77a4e

Please sign in to comment.