Skip to content

Commit 550ad72

Browse files
ptzieglerazoitl
authored andcommitted
Handle HiDPI scaling in Zest Graph
This replicates the HiDPI scaling in GEF diagrams that was introduced with 1bd2883, albeit in a simplified form. Zest doesn't have an EditPart viewer and instead, the root layer is directly embedded into the Graph widget. When the native auto-scaling provided by SWT is disabled for the Graph, a zoom listener is added that automatically synchronizes the monitor zoom of the widget with the scale of the root figure.
1 parent 18564bc commit 550ad72

File tree

5 files changed

+534
-20
lines changed

5 files changed

+534
-20
lines changed

org.eclipse.draw2d/src/org/eclipse/draw2d/internal/InternalDraw2dUtils.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ public class InternalDraw2dUtils {
2929
*/
3030
private static final String DRAW2D_DISABLE_AUTOSCALE = "draw2d.disableAutoscale"; //$NON-NLS-1$
3131

32+
/**
33+
* Internal flag for fetching the shell zoom
34+
*/
35+
private static final String DATA_SHELL_ZOOM = "SHELL_ZOOM"; //$NON-NLS-1$
36+
3237
public static boolean disableAutoscale;
3338

3439
static {
@@ -41,4 +46,17 @@ public static void configureForAutoscalingMode(Control control) {
4146
control.setData("AUTOSCALE_DISABLED", true); //$NON-NLS-1$
4247
}
4348
}
49+
50+
public static double calculateScale(Control control) {
51+
if (!InternalDraw2dUtils.disableAutoscale || control == null) {
52+
return 1.0;
53+
}
54+
int shellZooom;
55+
try {
56+
shellZooom = (int) control.getData(InternalDraw2dUtils.DATA_SHELL_ZOOM);
57+
} catch (ClassCastException | NullPointerException e) {
58+
shellZooom = 100;
59+
}
60+
return shellZooom / 100.0;
61+
}
4462
}

org.eclipse.gef/src/org/eclipse/gef/ui/parts/AbstractEditPartViewer.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,6 @@ public abstract class AbstractEditPartViewer implements EditPartViewer {
118118
private KeyHandler keyHandler;
119119
private PropertyChangeSupport changeSupport;
120120

121-
/**
122-
* Internal flag for fetching the shell zoom
123-
*/
124-
private static final String DATA_SHELL_ZOOM = "SHELL_ZOOM"; //$NON-NLS-1$
125-
126121
/**
127122
* Constructs the viewer and calls {@link #init()}.
128123
*/
@@ -496,7 +491,7 @@ protected void hookControl() {
496491
control.addListener(SWT.ZoomChanged,
497492
e -> setProperty(InternalGEFPlugin.MONITOR_SCALE_PROPERTY, e.detail / 100.0));
498493
}
499-
setProperty(InternalGEFPlugin.MONITOR_SCALE_PROPERTY, calculateScale());
494+
setProperty(InternalGEFPlugin.MONITOR_SCALE_PROPERTY, InternalDraw2dUtils.calculateScale(control));
500495
}
501496

502497
/**
@@ -867,17 +862,4 @@ protected void unhookControl() {
867862
@Override
868863
public void unregisterAccessibleEditPart(AccessibleEditPart acc) {
869864
}
870-
871-
private double calculateScale() {
872-
if (!InternalDraw2dUtils.disableAutoscale || control == null) {
873-
return 1.0;
874-
}
875-
int shellZooom;
876-
try {
877-
shellZooom = (int) control.getData(DATA_SHELL_ZOOM);
878-
} catch (ClassCastException | NullPointerException e) {
879-
shellZooom = 100;
880-
}
881-
return shellZooom / 100.0;
882-
}
883865
}

org.eclipse.zest.core/src/org/eclipse/zest/core/widgets/Graph.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import org.eclipse.draw2d.geometry.Point;
7575
import org.eclipse.draw2d.geometry.Rectangle;
7676
import org.eclipse.draw2d.internal.FileImageDataProvider;
77+
import org.eclipse.draw2d.internal.InternalDraw2dUtils;
7778

7879
/**
7980
* Holds the nodes and connections for the graph.
@@ -239,6 +240,12 @@ public void dispatchMouseMoved(org.eclipse.swt.events.MouseEvent me) {
239240
this.zoomListener = new ZoomGestureListener();
240241
this.rotateListener = new RotateGestureListener();
241242

243+
if (InternalDraw2dUtils.disableAutoscale) {
244+
InternalDraw2dUtils.configureForAutoscalingMode(this);
245+
addListener(SWT.ZoomChanged, event -> rootlayer.setScale(event.detail / 100.0));
246+
rootlayer.setScale(InternalDraw2dUtils.calculateScale(this));
247+
}
248+
242249
this.addPaintListener(event -> {
243250
if (shouldSheduleLayout) {
244251
applyLayoutInternal(true);

org.eclipse.zest.doc.isv/guide-src/graph.adoc

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,23 @@ Draw2D.
9696

9797
NOTE: Graph connections are always added to the _Graph_, regardless of whether
9898
the node are inside a sub-graph. This means that it is possible to e.g. connect
99-
two vertices in separate sub-graphs.
99+
two vertices in separate sub-graphs.
100+
101+
== HighDPI Support
102+
103+
The `*Figures*` used for visualization of the `*GraphItems*` are painted
104+
using absolute coordinates. In combination with fractional scaling, this may
105+
result in rounding errors, causing visual glitches in the graph.
106+
107+
By setting the `*draw2d.disableAutoscale*` system property to `*true*`, the
108+
native scaling capability of the underlying `*Graph*`, which is normally
109+
provided by SWT, is disabled and instead done by Draw2D.
110+
111+
When a `*Graph*` is created, a `*ZoomChanged*` listener is registered. The
112+
current monitor zoom (and any changes to it) are used as scale for the
113+
`*RootLayer*`.
114+
115+
image:images/highdpi.svg[Relation between the FigureCanvas, EditPartViewer and RootEditPart with respect to the device zoom]
116+
117+
NOTE: Fractional scaling is currently only supported for Windows. On Linux and
118+
MacOS, this system property is ignored.

0 commit comments

Comments
 (0)