Skip to content

Refactoring DPIUtils #2277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,14 @@ public static Shell new_Shell (final Display display, final Canvas parent) {
SWT.error (SWT.ERROR_NOT_IMPLEMENTED, e);
}
if (handle == 0) SWT.error (SWT.ERROR_INVALID_ARGUMENT, null, " [peer not created]");

final Shell shell = Shell.win32_new (display, handle);
final ComponentListener listener = new ComponentAdapter () {
@Override
public void componentResized (ComponentEvent e) {
display.syncExec (() -> {
if (shell.isDisposed()) return;
Dimension dim = parent.getSize ();
shell.setSize(DPIUtil.autoScaleDown(new Point(dim.width, dim.height))); // To Points
shell.setSize(DPIUtil.scaleDown(new Point(dim.width, dim.height), DPIUtil.getDeviceZoom())); // To Points
});
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1880,7 +1880,7 @@ void handleDOMEvent (OleEvent e) {
int screenY = pVarResult.getInt();
pVarResult.dispose();

Point position = DPIUtil.autoScaleDown(new Point(screenX, screenY)); // To Points
Point position = DPIUtil.scaleDown(new Point(screenX, screenY), DPIUtil.getDeviceZoom()); // To Points
position = browser.getDisplay().map(null, browser, position);
newEvent.x = position.x; newEvent.y = position.y;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,8 @@ int ShowContextMenu(int dwID, long ppt, long pcmdtReserved, long pdispReserved)
Event event = new Event();
POINT pt = new POINT();
OS.MoveMemory(pt, ppt, POINT.sizeof);
pt.x = DPIUtil.autoScaleDown(pt.x); // To Points
pt.y = DPIUtil.autoScaleDown(pt.y); // To Points
pt.x = DPIUtil.scaleDown(pt.x, DPIUtil.getDeviceZoom()); // To Points
pt.y = DPIUtil.scaleDown(pt.y, DPIUtil.getDeviceZoom()); // To Points
event.x = pt.x;
event.y = pt.y;
browser.notifyListeners(SWT.MenuDetect, event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1822,11 +1822,11 @@ public String toString () {
* @noreference This method is not intended to be referenced by clients.
*/
public static void drawScaled(GC gc, Image original, int width, int height, float scaleFactor) {
gc.drawImage (original, 0, 0, DPIUtil.autoScaleDown (width), DPIUtil.autoScaleDown (height),
gc.drawImage (original, 0, 0, CocoaDPIUtil.autoScaleDown (width), CocoaDPIUtil.autoScaleDown (height),
/* E.g. destWidth here is effectively DPIUtil.autoScaleDown (scaledWidth), but avoiding rounding errors.
* Nevertheless, we still have some rounding errors due to the point-based API GC#drawImage(..).
*/
0, 0, Math.round (DPIUtil.autoScaleDown (width * scaleFactor)), Math.round (DPIUtil.autoScaleDown (height * scaleFactor)));
0, 0, Math.round (CocoaDPIUtil.autoScaleDown (width * scaleFactor)), Math.round (CocoaDPIUtil.autoScaleDown (height * scaleFactor)));
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*******************************************************************************
* Copyright (c) 2022 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
* Daniel Kruegler - #420 - [High DPI] "swt.autoScale" should add new "half" option
* Yatta Solutions - #131 - Additional methods to specify target zoom directly
*******************************************************************************/
package org.eclipse.swt.internal;

/**
* This class hold common constants and utility functions w.r.t. to SWT high DPI
* functionality.
* <p>
* The {@code autoScaleUp(..)} methods convert from API coordinates (in SWT
* points) to internal high DPI coordinates (in pixels) that interface with
* native widgets.
* </p>
* <p>
* The {@code autoScaleDown(..)} convert from high DPI pixels to API coordinates
* (in SWT points).
* </p>
*
* @since 3.105
*/
public class CocoaDPIUtil {

/**
* Auto-scale down int dimensions.
*/
public static int autoScaleDown(int size) {
return DPIUtil.scaleDown(size, DPIUtil.deviceZoom);
}

/**
* Auto-scale down float dimensions.
*/
public static float autoScaleDown(float size) {
return DPIUtil.scaleDown(size, DPIUtil.deviceZoom);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2613,31 +2613,31 @@ static void buildDitheredGradientChannel(int from, int to, int steps,
static void fillGradientRectangle(GC gc, Device device,
int x, int y, int width, int height, boolean vertical,
RGB fromRGB, RGB toRGB,
int redBits, int greenBits, int blueBits) {
int redBits, int greenBits, int blueBits, int zoom) {
/* Create the bitmap and tile it */
ImageData band = createGradientBand(width, height, vertical,
fromRGB, toRGB, redBits, greenBits, blueBits);
Image image = new Image(device, band);
if ((band.width == 1) || (band.height == 1)) {
gc.drawImage(image, 0, 0, DPIUtil.autoScaleDown(band.width), DPIUtil.autoScaleDown(band.height),
DPIUtil.autoScaleDown(x), DPIUtil.autoScaleDown(y), DPIUtil.autoScaleDown(width),
DPIUtil.autoScaleDown(height));
gc.drawImage(image, 0, 0, DPIUtil.scaleDown(band.width, zoom), DPIUtil.scaleDown(band.height, zoom),
DPIUtil.scaleDown(x, zoom), DPIUtil.scaleDown(y, zoom), DPIUtil.scaleDown(width, zoom),
DPIUtil.scaleDown(height, zoom));
} else {
if (vertical) {
for (int dx = 0; dx < width; dx += band.width) {
int blitWidth = width - dx;
if (blitWidth > band.width) blitWidth = band.width;
gc.drawImage(image, 0, 0, DPIUtil.autoScaleDown(blitWidth), DPIUtil.autoScaleDown(band.height),
DPIUtil.autoScaleDown(dx + x), DPIUtil.autoScaleDown(y), DPIUtil.autoScaleDown(blitWidth),
DPIUtil.autoScaleDown(band.height));
gc.drawImage(image, 0, 0, DPIUtil.scaleDown(blitWidth, zoom), DPIUtil.scaleDown(band.height, zoom),
DPIUtil.scaleDown(dx + x, zoom), DPIUtil.scaleDown(y, zoom), DPIUtil.scaleDown(blitWidth, zoom),
DPIUtil.scaleDown(band.height, zoom));
}
} else {
for (int dy = 0; dy < height; dy += band.height) {
int blitHeight = height - dy;
if (blitHeight > band.height) blitHeight = band.height;
gc.drawImage(image, 0, 0, DPIUtil.autoScaleDown(band.width), DPIUtil.autoScaleDown(blitHeight),
DPIUtil.autoScaleDown(x), DPIUtil.autoScaleDown(dy + y), DPIUtil.autoScaleDown(band.width),
DPIUtil.autoScaleDown(blitHeight));
gc.drawImage(image, 0, 0, DPIUtil.scaleDown(band.width, zoom), DPIUtil.scaleDown(blitHeight, zoom),
DPIUtil.scaleDown(x, zoom), DPIUtil.scaleDown(dy + y, zoom), DPIUtil.scaleDown(band.width, zoom),
DPIUtil.scaleDown(blitHeight, zoom));
}
}
}
Expand Down
Loading