Skip to content

Commit

Permalink
Implementation of GC to enable multi zoom level support for win32 whi…
Browse files Browse the repository at this point in the history
…ch can be

extended by other resources and widgets.

Contributes to
eclipse-platform#62 and eclipse-platform#131
  • Loading branch information
amartya4256 committed May 13, 2024
1 parent b58f379 commit 40f7c63
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 100 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@


import org.eclipse.swt.*;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.internal.win32.*;

/**
Expand Down Expand Up @@ -47,6 +48,8 @@ public final class GCData {
public float[] lineDashes;
public float lineMiterLimit = 10;
public int alpha = 0xFF;
public int deviceZoom;
public int nativeDeviceZoom;

public Image image;
public PAINTSTRUCT ps;
Expand All @@ -57,4 +60,13 @@ public final class GCData {
public float gdipXOffset, gdipYOffset;
public int uiState = 0;
public boolean focusDrawn;

int getNativeDeviceZoom() {
return nativeDeviceZoom;
}

void setNativeDeviceZoom(int nativeDeviceZoom) {
this.nativeDeviceZoom = nativeDeviceZoom;
this.deviceZoom = DPIUtil.getZoomForAutoscaleProperty(nativeDeviceZoom);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public final class Image extends Resource implements Drawable {
/**
* Attribute to cache current device zoom level
*/
private int currentDeviceZoom = 100;
int currentDeviceZoom = 100;

/**
* width of the image
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3563,6 +3563,12 @@ public void setMenu (Menu menu) {
this.menu = menu;
}

void setNativeZoomForGCData(GCData data) {
int nativeDeviceZoom = getShell().getNativeZoom();
data.nativeDeviceZoom = nativeDeviceZoom;
data.deviceZoom = DPIUtil.getZoomForAutoscaleProperty(nativeDeviceZoom);
}

/**
* Sets the orientation of the receiver, which must be one
* of the constants <code>SWT.LEFT_TO_RIGHT</code> or <code>SWT.RIGHT_TO_LEFT</code>.
Expand Down Expand Up @@ -3657,6 +3663,14 @@ public void setRedraw (boolean redraw) {
}
}

@Override
void sendEvent(int eventType, Event event, boolean send) {
if(event != null && event.gc != null && event.gc.getGCData() != null) {
setNativeZoomForGCData(event.gc.getGCData());
}
super.sendEvent(eventType, event, send);
}

/**
* Sets the shape of the control to the region specified
* by the argument. When the argument is null, the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ void wmDrawChildImage(DRAWITEMSTRUCT struct) {
}

GCData data = new GCData();
setNativeZoomForGCData(data);
data.device = display;
GC gc = GC.win32_new (struct.hDC, data);
Image image = getEnabled () ? this.image : new Image (display, this.image, SWT.IMAGE_DISABLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,20 @@ public class Shell extends Decorations {
ToolTip [] toolTips;
long hwndMDIClient, lpstrTip, toolTipHandle, balloonTipHandle, menuItemToolTipHandle;
int minWidth = SWT.DEFAULT, minHeight = SWT.DEFAULT, maxWidth = SWT.DEFAULT, maxHeight = SWT.DEFAULT;
private int nativeZoom;
/**
* the native zoom of the monitor the shell is drawn on
* (Warning: This field is platform dependent)
* <p>
* <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT
* public API. It is marked public only so that it can be shared
* within the packages provided by SWT. It is not available on all
* platforms and should never be accessed from application code.
* </p>
*
* @noreference This field is not intended to be referenced by clients.
* @since 3.126
*/
public int nativeZoom;
long [] brushes;
boolean showWithParent, fullScreen, wasMaximized, modified, center;
String toolTitle, balloonTitle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,20 @@
*/
public abstract class Widget {

private int zoom;
/**
* the zoom level of the widget
* (Warning: This field is platform dependent)
* <p>
* <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT
* public API. It is marked public only so that it can be shared
* within the packages provided by SWT. It is not available on all
* platforms and should never be accessed from application code.
* </p>
*
* @noreference This field is not intended to be referenced by clients.
* @since 3.126
*/
public int zoom;
int style, state;
Display display;
EventTable eventTable;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*******************************************************************************
* Copyright (c) 2024 Yatta Solutions
*
* 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:
* Yatta Solutions - initial API and implementation
*******************************************************************************/
package org.eclipse.swt.graphics;

import static org.junit.Assert.assertEquals;

import java.util.concurrent.CompletableFuture;

import org.eclipse.swt.SWT;
import org.eclipse.swt.internal.DPIUtil;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Shell;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class GCWin32Tests {
private Display display;
private int initialZoom;

@Before
public void setUp() {
initialZoom = DPIUtil.getDeviceZoom();
display = Display.getDefault();
DPIUtil.setDeviceZoom(100);
}

@After
public void tearDown() {
DPIUtil.setDeviceZoom(initialZoom);
}

@Test
public void gcZoomLevelMustChangeOnShellZoomChange() {
CompletableFuture<Integer> deviceZoom1 = new CompletableFuture<>();
CompletableFuture<Integer> deviceZoom2 = new CompletableFuture<>();
int zoom = DPIUtil.getDeviceZoom();
Shell shell = new Shell(display);
shell.addListener(SWT.Paint, event -> {
deviceZoom1.complete(event.gc.getGCData().deviceZoom);
});
shell.addListener(SWT.ZoomChanged, event -> {
deviceZoom2.complete(event.gc.getGCData().deviceZoom);
});
shell.open();
assertEquals("GCData must have a zoom level equal to the actual zoom level of the widget/shell", shell.zoom, (int) deviceZoom1.join());

int newSWTZoom = zoom * 2;
Event swtEvent = new Event();
swtEvent.type = SWT.ZoomChanged;
swtEvent.gc = GC.win32_new(shell, new GCData());
swtEvent.widget = shell;
DPIUtil.setDeviceZoom(newSWTZoom);
shell.zoom = newSWTZoom;
shell.nativeZoom = DPIUtil.getZoomForAutoscaleProperty(newSWTZoom);
shell.notifyListeners(SWT.ZoomChanged, swtEvent);

assertEquals("GCData must have a zoom level equal to the actual zoom level of the widget/shell on zoomChanged event", shell.zoom, (int) deviceZoom2.join());
}

@Test
public void drawnElementsShouldScaleUpToTheRightZoomLevel() {
int zoom = DPIUtil.getDeviceZoom();
int scalingFactor = 2;
Shell shell = new Shell(display);
GC gc = GC.win32_new(shell, new GCData());
gc.getGCData().deviceZoom = zoom * scalingFactor;
gc.getGCData().lineWidth = 10;
assertEquals("DPIUtil calls with getDeviceZoom should scale to the right value", gc.getGCData().lineWidth, gc.getLineWidth() * scalingFactor, 0);
}

}

0 comments on commit 40f7c63

Please sign in to comment.