Skip to content

Commit 3d6f654

Browse files
committed
Simplify configuration for Draw2D auto-scaling mode
Enabling the Draw2D auto-scaling mode currently requires three steps: 1) Disable the native SWT auto-scaling 2) Hook a ZoomChanged listener to the underlying canvas 3) Update the zoom of the ScalableFigure All of these steps are currently possible via individual methods in the InternalDraw2dUtils. With this change, those methods are combined into a single method, greatly reducing the amount of changes required in the calling classes.
1 parent 550ad72 commit 3d6f654

File tree

6 files changed

+27
-51
lines changed

6 files changed

+27
-51
lines changed

org.eclipse.draw2d/src/org/eclipse/draw2d/FigureUtilities.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2010 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -75,7 +75,9 @@ public static FontMetrics getFontMetrics(Font f) {
7575
protected static GC getGC() {
7676
if (gc == null) {
7777
Shell shell = new Shell();
78-
InternalDraw2dUtils.configureForAutoscalingMode(shell);
78+
InternalDraw2dUtils.configureForAutoscalingMode(shell, event -> {
79+
// ignored
80+
});
7981
gc = new GC(shell);
8082
if (InternalDraw2dUtils.disableAutoscale) {
8183
gc.setAdvanced(true);

org.eclipse.draw2d/src/org/eclipse/draw2d/PopUpHelper.java

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -222,31 +222,6 @@ protected void show() {
222222

223223
private class PopupHelperLightweightSystem extends LightweightSystem {
224224

225-
/**
226-
* Data that can be set to scale this widget at 100%.
227-
*/
228-
private static final String DATA_AUTOSCALE_DISABLED = "AUTOSCALE_DISABLED"; //$NON-NLS-1$
229-
230-
/**
231-
* Internal flag for fetching the shell zoom
232-
*/
233-
private static final String DATA_SHELL_ZOOM = "SHELL_ZOOM"; //$NON-NLS-1$
234-
235-
/**
236-
* Returns the zoom of the monitor this widget is assigned to.
237-
*
238-
* @return 1.0 at 100%, 1.25 at 125% etc.
239-
*/
240-
private static double getMonitorZoomScale(Control c) {
241-
int shellZooom;
242-
try {
243-
shellZooom = (int) c.getData(DATA_SHELL_ZOOM);
244-
} catch (ClassCastException | NullPointerException e) {
245-
shellZooom = 100;
246-
}
247-
return shellZooom / 100.0;
248-
}
249-
250225
/**
251226
* The scalable pane that is injected between the root figure and the contents
252227
* of this viewport.
@@ -274,9 +249,7 @@ public void setControl(Canvas c) {
274249
return;
275250
}
276251

277-
c.setData(DATA_AUTOSCALE_DISABLED, true);
278-
c.addListener(SWT.ZoomChanged, e -> setScale(e.detail / 100.0));
279-
setScale(getMonitorZoomScale(c));
252+
InternalDraw2dUtils.configureForAutoscalingMode(c, this::setScale);
280253

281254
super.setControl(c);
282255
}

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
*******************************************************************************/
1313
package org.eclipse.draw2d.internal;
1414

15+
import java.util.function.Consumer;
16+
1517
import org.eclipse.swt.SWT;
1618
import org.eclipse.swt.widgets.Control;
1719

@@ -34,29 +36,34 @@ public class InternalDraw2dUtils {
3436
*/
3537
private static final String DATA_SHELL_ZOOM = "SHELL_ZOOM"; //$NON-NLS-1$
3638

39+
/**
40+
* Data that can be set to scale this widget at 100%.
41+
*/
42+
private static final String DATA_AUTOSCALE_DISABLED = "AUTOSCALE_DISABLED"; //$NON-NLS-1$
43+
3744
public static boolean disableAutoscale;
3845

3946
static {
4047
disableAutoscale = "win32".equals(SWT.getPlatform()) //$NON-NLS-1$
4148
&& Boolean.parseBoolean(System.getProperty(DRAW2D_DISABLE_AUTOSCALE));
4249
}
4350

44-
public static void configureForAutoscalingMode(Control control) {
45-
if (control != null && disableAutoscale) {
46-
control.setData("AUTOSCALE_DISABLED", true); //$NON-NLS-1$
51+
public static void configureForAutoscalingMode(Control control, Consumer<Double> zoomConsumer) {
52+
if (control == null || !disableAutoscale) {
53+
return;
4754
}
55+
control.setData(InternalDraw2dUtils.DATA_AUTOSCALE_DISABLED, true);
56+
control.addListener(SWT.ZoomChanged, e -> zoomConsumer.accept(e.detail / 100.0));
57+
zoomConsumer.accept(InternalDraw2dUtils.calculateScale(control));
4858
}
4959

50-
public static double calculateScale(Control control) {
51-
if (!InternalDraw2dUtils.disableAutoscale || control == null) {
52-
return 1.0;
53-
}
54-
int shellZooom;
60+
private static double calculateScale(Control control) {
61+
int shellZoom;
5562
try {
56-
shellZooom = (int) control.getData(InternalDraw2dUtils.DATA_SHELL_ZOOM);
63+
shellZoom = (int) control.getData(InternalDraw2dUtils.DATA_SHELL_ZOOM);
5764
} catch (ClassCastException | NullPointerException e) {
58-
shellZooom = 100;
65+
shellZoom = 100;
5966
}
60-
return shellZooom / 100.0;
67+
return shellZoom / 100.0;
6168
}
6269
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.util.Map;
2323
import java.util.concurrent.CopyOnWriteArrayList;
2424

25-
import org.eclipse.swt.SWT;
2625
import org.eclipse.swt.dnd.DND;
2726
import org.eclipse.swt.dnd.DragSource;
2827
import org.eclipse.swt.dnd.DropTarget;
@@ -488,10 +487,9 @@ protected void hookControl() {
488487
control.setMenu(contextMenu.createContextMenu(getControl()));
489488
}
490489
if (InternalDraw2dUtils.disableAutoscale) {
491-
control.addListener(SWT.ZoomChanged,
492-
e -> setProperty(InternalGEFPlugin.MONITOR_SCALE_PROPERTY, e.detail / 100.0));
490+
InternalDraw2dUtils.configureForAutoscalingMode(control,
491+
scale -> setProperty(InternalGEFPlugin.MONITOR_SCALE_PROPERTY, scale));
493492
}
494-
setProperty(InternalGEFPlugin.MONITOR_SCALE_PROPERTY, InternalDraw2dUtils.calculateScale(control));
495493
}
496494

497495
/**

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2010 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials are made available under the
55
* terms of the Eclipse Public License 2.0 which is available at
@@ -21,7 +21,6 @@
2121
import org.eclipse.draw2d.geometry.Dimension;
2222
import org.eclipse.draw2d.geometry.Point;
2323
import org.eclipse.draw2d.geometry.Rectangle;
24-
import org.eclipse.draw2d.internal.InternalDraw2dUtils;
2524

2625
import org.eclipse.gef.EditPart;
2726
import org.eclipse.gef.GraphicalEditPart;
@@ -54,7 +53,6 @@ public ScrollingGraphicalViewer() {
5453
@Override
5554
public final Control createControl(Composite parent) {
5655
FigureCanvas control = new FigureCanvas(parent, getLightweightSystem());
57-
InternalDraw2dUtils.configureForAutoscalingMode(control);
5856
setControl(control);
5957
hookRootFigure();
6058
return getControl();

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,7 @@ public void dispatchMouseMoved(org.eclipse.swt.events.MouseEvent me) {
241241
this.rotateListener = new RotateGestureListener();
242242

243243
if (InternalDraw2dUtils.disableAutoscale) {
244-
InternalDraw2dUtils.configureForAutoscalingMode(this);
245-
addListener(SWT.ZoomChanged, event -> rootlayer.setScale(event.detail / 100.0));
246-
rootlayer.setScale(InternalDraw2dUtils.calculateScale(this));
244+
InternalDraw2dUtils.configureForAutoscalingMode(this, rootlayer::setScale);
247245
}
248246

249247
this.addPaintListener(event -> {

0 commit comments

Comments
 (0)