Skip to content
Merged
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 @@ -23,6 +23,7 @@

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.draw2d.internal.InternalDraw2dUtils;

/**
* Provides miscellaneous Figure operations.
Expand Down Expand Up @@ -73,7 +74,12 @@ public static FontMetrics getFontMetrics(Font f) {
@Deprecated
protected static GC getGC() {
if (gc == null) {
gc = new GC(new Shell());
Shell shell = new Shell();
InternalDraw2dUtils.configureForAutoscalingMode(shell);
gc = new GC(shell);
if (InternalDraw2dUtils.disableAutoscale) {
gc.setAdvanced(true);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is a bit interesting. Whatever you do here, will break some scenarios, depending whether the GC that is used to render the Figure is on advanced mode or not. The "safest" approach would probably be to always and everywhere use a GC in advanced mode - at least when the flag is set to true. Is there anything speaking against that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've got some mixed feelings about using advanced mode everywhere. Here is fine, of course, given that the GC should only be used internally. But if there is ever a need to create a GC somewhere else, it's incredibly easy to forget about it and you suddenly end up with inconsistent and hard to debug problems.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it is out of your control anyway, isn't it? I cannot check in the code currently, but If you have a SWTGraphics with scaling, you'll get advanced Mode. If there is no scaling, you won't.

Copy link
Contributor

@ptziegler ptziegler Sep 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just want to avoid situations where one now has to always do:

SWTGraphics graphics = new SWTGraphics(gc);
graphics.setAdvanced(true);

Or do you always (assuming the flag is set) in the SWTGraphics constructor? The problem is that I can't really predict what side-effects this might cause, so I'm uncomfortable with that idea.

On a different note: I assume this issue exists outside of Draw2D as well? Wouldn't it make more sense to enable this in SWT, rather than here? From what I remember, the GC instance is initialized with the Drawable it is created for.

There you could check whether auto-scaling is disabled for that widget and then activate advanced mode if necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, that this shouldn't be on the side of consumers of GEF. Just wanted to point out, that it isn't (per se) tied to having auto-scaling disabled, but to when one of the methods of a GC is used, so that the advanced mode is enabled -> e.g drawPath or setTransform or any of the other methods. And that makes it actually very complicated. I tried to come up with some simple snippet and got to something like:

GC gc = new GC(canvas);
System.out.println(gc.textExtent("Representing a half adder"));
gc.drawPath(new Path(shell.getDisplay()));
System.out.println(gc.textExtent("Representing a half adder"));

and the output is

Point {202, 25}
Point {203, 24}

So, e.g. one Figure draws a Path, the next one calculates and renders text differently.
I don't know, if this effect is similar problematic in GTK and cocoa or only in the windows GC, but this is really something to have a look at. Your idea with initializing it like that in SWT could be a way to go, we will need to think about it. But it is more messed up than I thought until now.

}
appliedFont = gc.getFont();
}
return gc;
Expand Down Expand Up @@ -424,5 +430,4 @@ public static boolean isNotFullyClipped(IFigure figure) {
}
return !figBounds.isEmpty();
}

}
76 changes: 73 additions & 3 deletions org.eclipse.draw2d/src/org/eclipse/draw2d/PopUpHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.internal.InternalDraw2dUtils;

/**
* Provides abstract support for classes that manage popups. Popups in Draw2d
Expand Down Expand Up @@ -62,7 +64,7 @@ protected PopUpHelper(Control c) {
*/
protected PopUpHelper(Control c, int shellStyle) {
control = c;
this.shellStyle = shellStyle | SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE;
this.shellStyle = shellStyle | SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE | SWT.DOUBLE_BUFFERED;
}

/**
Expand All @@ -72,9 +74,8 @@ protected PopUpHelper(Control c, int shellStyle) {
* @return the newly created LightweightSystem
* @since 2.0
*/
@SuppressWarnings("static-method")
protected LightweightSystem createLightweightSystem() {
return new LightweightSystem();
return InternalDraw2dUtils.disableAutoscale ? new PopupHelperLightweightSystem() : new LightweightSystem();
}

/**
Expand Down Expand Up @@ -219,4 +220,73 @@ protected void show() {
tipShowing = true;
}

private class PopupHelperLightweightSystem extends LightweightSystem {

/**
* Data that can be set to scale this widget at 100%.
*/
private static final String DATA_AUTOSCALE_DISABLED = "AUTOSCALE_DISABLED"; //$NON-NLS-1$

/**
* Internal flag for fetching the shell zoom
*/
private static final String DATA_SHELL_ZOOM = "SHELL_ZOOM"; //$NON-NLS-1$

/**
* Returns the zoom of the monitor this widget is assigned to.
*
* @return 1.0 at 100%, 1.25 at 125% etc.
*/
private static double getMonitorZoomScale(Control c) {
int shellZooom;
try {
shellZooom = (int) c.getData(DATA_SHELL_ZOOM);
} catch (ClassCastException | NullPointerException e) {
shellZooom = 100;
}
return shellZooom / 100.0;
}

/**
* The scalable pane that is injected between the root figure and the contents
* of this viewport.
*/
private final ScalableLayeredPane scalablePane;

private PopupHelperLightweightSystem() {
scalablePane = new ScalableLayeredPane(true);
scalablePane.setLayoutManager(new StackLayout());
getRootFigure().add(scalablePane);
}

/**
* Updates the scale factor of the scalable pane to the given value.
*
* @param scale The new scale factor.
*/
private void setScale(double scale) {
scalablePane.setScale(scale);
}

@Override
public void setControl(Canvas c) {
if (c == null) {
return;
}

c.setData(DATA_AUTOSCALE_DISABLED, true);
c.addListener(SWT.ZoomChanged, e -> setScale(e.detail / 100.0));
setScale(getMonitorZoomScale(c));

super.setControl(c);
}

@Override
public void setContents(IFigure figure) {
scalablePane.removeAll();
if (figure != null) {
scalablePane.add(figure);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*******************************************************************************
* Copyright (c) 2025 Yatta and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Yatta - initial API and implementation
*******************************************************************************/
package org.eclipse.draw2d.internal;

import java.util.Objects;

import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Control;

import org.eclipse.draw2d.geometry.Dimension;

/**
* Provides miscellaneous Figure operations calculated with the zoom context of
* the the provided {@code Drawable}.
*
* All GC related operations are mirrored from {@code FigureUtilities}
*/
public class DrawableFigureUtilities {
private final GC gc;
private Font appliedFont;
private FontMetrics metrics;

public DrawableFigureUtilities(Control source) {
gc = new GC(source);
source.addDisposeListener(e -> {
gc.dispose();
});
appliedFont = gc.getFont();
}

/**
* Returns the FontMetrics associated with the passed Font.
*
* @param f the font
* @return the FontMetrics for the given font
* @see GC#getFontMetrics()
*/
public FontMetrics getFontMetrics(Font f) {
setFont(f);
if (metrics == null) {
metrics = gc.getFontMetrics();
}
return metrics;
}

/**
* Returns the dimensions of the String <i>s</i> using the font <i>f</i>. Tab
* expansion and carriage return processing are performed.
*
* @param s the string
* @param f the font
* @return the text's dimensions
* @see GC#textExtent(String)
*/
protected org.eclipse.swt.graphics.Point getTextDimension(String s, Font f) {
setFont(f);
return gc.textExtent(s);
}

/**
* Returns the dimensions of the String <i>s</i> using the font <i>f</i>. No tab
* expansion or carriage return processing will be performed.
*
* @param s the string
* @param f the font
* @return the string's dimensions
* @see GC#stringExtent(java.lang.String)
*/
protected org.eclipse.swt.graphics.Point getStringDimension(String s, Font f) {
setFont(f);
return gc.stringExtent(s);
}

/**
* Returns the Dimensions of the given text, converting newlines and tabs
* appropriately.
*
* @param text the text
* @param f the font
* @return the dimensions of the given text
*/
public Dimension getTextExtents(String text, Font f) {
return new Dimension(getTextDimension(text, f));
}

/**
* Returns the Dimensions of <i>s</i> in Font <i>f</i>.
*
* @param s the string
* @param f the font
* @return the dimensions of the given string
*/
public Dimension getStringExtents(String s, Font f) {
return new Dimension(getStringDimension(s, f));
}

/**
* Returns the Dimensions of the given text, converting newlines and tabs
* appropriately.
*
* @param s the string
* @param f the font
* @param result the Dimension that will contain the result of this calculation
*/
public void getTextExtents(String s, Font f, Dimension result) {
org.eclipse.swt.graphics.Point pt = getTextDimension(s, f);
result.width = pt.x;
result.height = pt.y;
}

/**
* Returns the width of <i>s</i> in Font <i>f</i>.
*
* @param s the string
* @param f the font
* @return the width
*/
public int getTextWidth(String s, Font f) {
return getTextDimension(s, f).x;
}

private void setFont(Font f) {
if (Objects.equals(appliedFont, f)) {
return;
}
gc.setFont(f);
appliedFont = f;
metrics = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*******************************************************************************
* Copyright (c) 2025 Yatta and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Yatta - initial API and implementation
*******************************************************************************/

package org.eclipse.draw2d.internal;

import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.widgets.Control;

import org.eclipse.draw2d.TextUtilities;
import org.eclipse.draw2d.geometry.Dimension;

/**
* Provides miscellaneous text operations calculated with the zoom context of
* the the provided {@code Drawable}.
*/
public class DrawableTextUtilities extends TextUtilities {

private final DrawableFigureUtilities figureUtilities;

public DrawableTextUtilities(Control source) {
figureUtilities = new DrawableFigureUtilities(source);
}

/**
* Returns the Dimensions of <i>s</i> in Font <i>f</i>.
*
* @param s the string
* @param f the font
* @return the dimensions of the given string
*/
@Override
public Dimension getStringExtents(String s, Font f) {
return figureUtilities.getStringExtents(s, f);
}

/**
* Returns the Dimensions of the given text, converting newlines and tabs
* appropriately.
*
* @param s the text
* @param f the font
* @return the dimensions of the given text
*/
@Override
public Dimension getTextExtents(String s, Font f) {
return figureUtilities.getTextExtents(s, f);
}

/**
* Gets the font's ascent.
*
* @param font
* @return the font's ascent
*/
@Override
public int getAscent(Font font) {
FontMetrics fm = figureUtilities.getFontMetrics(font);
return fm.getHeight() - fm.getDescent();
}

/**
* Gets the font's descent.
*
* @param font
* @return the font's descent
*/
@Override
public int getDescent(Font font) {
return figureUtilities.getFontMetrics(font).getDescent();
}
}
Loading
Loading