This is an automated email from the ASF dual-hosted git repository.
bamaer pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/hop.git
The following commit(s) were added to refs/heads/main by this push:
new 6562b7b10b Issue #6751 : Keep the callout press per graph and scale
callouts once (#9329)
6562b7b10b is described below
commit 6562b7b10b87c0640c162a3312a57ca6a3e37d3e
Author: Matt Casters <[email protected]>
AuthorDate: Fri Nov 6 20:23:22 2026 +0100
Issue #6751 : Keep the callout press per graph and scale callouts once
(#9329)
---
.../apache/hop/annotation/AnnotationClicks.java | 71 +++++--
.../apache/hop/annotation/AnnotationGeometry.java | 29 ++-
.../apache/hop/annotation/AnnotationPainter.java | 8 +-
.../hop/annotation/AnnotationClicksTest.java | 214 +++++++++++++++++++++
.../hop/annotation/AnnotationGeometryTest.java | 34 ++--
.../hop/annotation/AnnotationPainterTest.java | 83 +++++++-
6 files changed, 394 insertions(+), 45 deletions(-)
diff --git
a/plugins/misc/annotation/src/main/java/org/apache/hop/annotation/AnnotationClicks.java
b/plugins/misc/annotation/src/main/java/org/apache/hop/annotation/AnnotationClicks.java
index 66138cf991..93404df08d 100644
---
a/plugins/misc/annotation/src/main/java/org/apache/hop/annotation/AnnotationClicks.java
+++
b/plugins/misc/annotation/src/main/java/org/apache/hop/annotation/AnnotationClicks.java
@@ -32,6 +32,7 @@ import
org.apache.hop.ui.hopgui.file.workflow.extension.HopGuiWorkflowGraphExten
import org.apache.hop.workflow.WorkflowMeta;
import org.apache.hop.workflow.action.ActionMeta;
import org.eclipse.swt.events.MouseEvent;
+import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Shell;
/**
@@ -42,21 +43,25 @@ public final class AnnotationClicks {
private static final Class<?> PKG = Annotation.class;
/**
- * True after a press on a callout until the matching release. The UI thread
is the only writer.
+ * Set on the graph canvas for the gesture that pressed a callout. Hop Web
runs every session in
+ * one JVM, so a static flag would let one user's press send another user's
mouse-up through
+ * {@code swallowRelease}. That mouse-up calls {@code
setPreventingDefault(true)}, and the graph
+ * then calls {@code clearSettings()} and returns, dropping the hop,
selection or move.
*/
- private static boolean calloutPressed;
+ private static final String CALLOUT_PRESSED = "annotation-callout-pressed";
private AnnotationClicks() {}
public static void pipelineMouseDown(HopGuiPipelineGraphExtension extension)
{
+ Canvas canvas = pipelineCanvas(extension);
if (!claim(
extension == null ? null : extension.getAreaOwner(),
extension == null ? null : extension.getEvent())) {
- calloutPressed = false;
+ clearPressed(canvas);
return;
}
extension.setPreventingDefault(true);
- calloutPressed = true;
+ markPressed(canvas);
if (extension.getEvent().button != 1) {
return;
}
@@ -68,10 +73,12 @@ public final class AnnotationClicks {
public static void pipelineMouseUp(HopGuiPipelineGraphExtension extension) {
if (extension == null) {
- calloutPressed = false;
return;
}
- swallowRelease(extension.getAreaOwner(), () ->
extension.setPreventingDefault(true));
+ swallowRelease(
+ pipelineCanvas(extension),
+ extension.getAreaOwner(),
+ () -> extension.setPreventingDefault(true));
}
public static void pipelineDoubleClick(HopGuiPipelineGraphExtension
extension) {
@@ -81,14 +88,15 @@ public final class AnnotationClicks {
}
public static void workflowMouseDown(HopGuiWorkflowGraphExtension extension)
{
+ Canvas canvas = workflowCanvas(extension);
if (!claim(
extension == null ? null : extension.getAreaOwner(),
extension == null ? null : extension.getEvent())) {
- calloutPressed = false;
+ clearPressed(canvas);
return;
}
extension.setPreventingDefault(true);
- calloutPressed = true;
+ markPressed(canvas);
if (extension.getEvent().button != 1) {
return;
}
@@ -100,10 +108,12 @@ public final class AnnotationClicks {
public static void workflowMouseUp(HopGuiWorkflowGraphExtension extension) {
if (extension == null) {
- calloutPressed = false;
return;
}
- swallowRelease(extension.getAreaOwner(), () ->
extension.setPreventingDefault(true));
+ swallowRelease(
+ workflowCanvas(extension),
+ extension.getAreaOwner(),
+ () -> extension.setPreventingDefault(true));
}
public static void workflowDoubleClick(HopGuiWorkflowGraphExtension
extension) {
@@ -236,11 +246,46 @@ public final class AnnotationClicks {
&& beforeDashed == afterDashed;
}
- private static void swallowRelease(AreaOwner areaOwner, Runnable
preventDefault) {
- if (!calloutPressed && !isCallout(areaOwner)) {
+ private static Canvas pipelineCanvas(HopGuiPipelineGraphExtension extension)
{
+ if (extension == null || extension.getPipelineGraph() == null) {
+ return null;
+ }
+ return extension.getPipelineGraph().getCanvas();
+ }
+
+ private static Canvas workflowCanvas(HopGuiWorkflowGraphExtension extension)
{
+ if (extension == null || extension.getWorkflowGraph() == null) {
+ return null;
+ }
+ return extension.getWorkflowGraph().getCanvas();
+ }
+
+ /** Remember the press on this graph's canvas. A disposed canvas can no
longer receive the up. */
+ private static void markPressed(Canvas canvas) {
+ if (canvas == null || canvas.isDisposed()) {
+ return;
+ }
+ canvas.setData(CALLOUT_PRESSED, Boolean.TRUE);
+ }
+
+ private static void clearPressed(Canvas canvas) {
+ if (canvas == null || canvas.isDisposed()) {
+ return;
+ }
+ canvas.setData(CALLOUT_PRESSED, null);
+ }
+
+ private static boolean isPressed(Canvas canvas) {
+ return canvas != null
+ && !canvas.isDisposed()
+ && Boolean.TRUE.equals(canvas.getData(CALLOUT_PRESSED));
+ }
+
+ private static void swallowRelease(Canvas canvas, AreaOwner areaOwner,
Runnable preventDefault) {
+ if (!isPressed(canvas) && !isCallout(areaOwner)) {
return;
}
- calloutPressed = false;
+ clearPressed(canvas);
preventDefault.run();
}
diff --git
a/plugins/misc/annotation/src/main/java/org/apache/hop/annotation/AnnotationGeometry.java
b/plugins/misc/annotation/src/main/java/org/apache/hop/annotation/AnnotationGeometry.java
index e42130e9c7..69f08dd886 100644
---
a/plugins/misc/annotation/src/main/java/org/apache/hop/annotation/AnnotationGeometry.java
+++
b/plugins/misc/annotation/src/main/java/org/apache/hop/annotation/AnnotationGeometry.java
@@ -51,28 +51,22 @@ public final class AnnotationGeometry {
/** A straight connector between two points. */
public record Segment(int x1, int y1, int x2, int y2) {}
- /**
- * Scales a pixel constant by the canvas magnification. A non-positive
magnification is treated as
- * 1.
- *
- * @param pixels the unscaled size
- * @param magnification the canvas magnification
- * @return at least 1
- */
- public static int scale(int pixels, float magnification) {
- float factor = magnification > 0f ? magnification : 1f;
- return Math.max(1, Math.round(pixels * factor));
- }
-
/**
* Balloon rectangle for the measured text. Padding is added here. The text
size is the wrapped
* block, not including padding.
*
+ * <p>{@code magnification} is not applied. {@code SwtGc.setTransform} and
{@code
+ * SvgGc.setTransform} already scale everything drawn, and {@code
IGc#textExtent} measures in that
+ * unscaled space. The graph hit-tests with {@code screen2real}, which
divides the canvas point by
+ * the same factor and subtracts the scroll offset before comparing it to
the click rectangle.
+ * That rectangle is this balloon with the offset removed, so a click lands
at every zoom while
+ * the gap and padding stay {@link #GAP_PX} and {@link #PADDING_PX}.
+ *
* @param icon icon rectangle
* @param textWidth wrapped text width
* @param textHeight wrapped text height
* @param placement where the balloon sits; null is {@link
AnnotationPlacement#TOP}
- * @param magnification canvas magnification
+ * @param magnification canvas magnification, accepted and not applied
* @return the balloon, fully outside the icon when the gap is positive
*/
public static Bounds balloon(
@@ -81,8 +75,11 @@ public final class AnnotationGeometry {
int textHeight,
AnnotationPlacement placement,
float magnification) {
- int gap = scale(GAP_PX, magnification);
- int padding = scale(PADDING_PX, magnification);
+ // The graphics transform already scales these pixels. Multiplying by
magnification would
+ // scale the callout a second time. The parameter stays so callers and
tests can pass the
+ // canvas zoom and see that the rectangle does not change.
+ int gap = GAP_PX;
+ int padding = PADDING_PX;
int width = Math.max(0, textWidth) + 2 * padding;
int height = Math.max(0, textHeight) + 2 * padding;
AnnotationPlacement side = placement == null ? AnnotationPlacement.TOP :
placement;
diff --git
a/plugins/misc/annotation/src/main/java/org/apache/hop/annotation/AnnotationPainter.java
b/plugins/misc/annotation/src/main/java/org/apache/hop/annotation/AnnotationPainter.java
index ff8703b448..8849ab1284 100644
---
a/plugins/misc/annotation/src/main/java/org/apache/hop/annotation/AnnotationPainter.java
+++
b/plugins/misc/annotation/src/main/java/org/apache/hop/annotation/AnnotationPainter.java
@@ -63,8 +63,10 @@ public final class AnnotationPainter {
}
gc.setFont(IGc.EFont.NOTE);
+ // Unscaled pixels. setTransform already scales what is drawn, and
textExtent measures in that
+ // same space. The click area below is this rectangle; screen2real
converts the mouse into it.
float magnification = gc.getMagnification();
- int maxWidth =
AnnotationGeometry.scale(AnnotationGeometry.MAX_TEXT_WIDTH_PX, magnification);
+ int maxWidth = AnnotationGeometry.MAX_TEXT_WIDTH_PX;
List<String> lines = AnnotationGeometry.wrap(text, maxWidth, line ->
gc.textExtent(line).x);
if (lines.isEmpty()) {
return;
@@ -87,7 +89,7 @@ public final class AnnotationPainter {
RgbColor background = NotePadStyle.backgroundColor(NotePadType.GENERAL);
RgbColor border = NotePadStyle.borderColor(NotePadType.GENERAL);
RgbColor foreground = NotePadStyle.textColor(NotePadType.GENERAL);
- int radius = AnnotationGeometry.scale(AnnotationGeometry.RADIUS_PX,
magnification);
+ int radius = AnnotationGeometry.RADIUS_PX;
if (annotation.isDashedLine()) {
AnnotationGeometry.Segment connector =
AnnotationGeometry.connector(balloon, icon);
@@ -107,7 +109,7 @@ public final class AnnotationPainter {
balloon.x(), balloon.y(), balloon.width(), balloon.height(), radius,
radius);
gc.setForeground(foreground.red(), foreground.green(), foreground.blue());
- int padding = AnnotationGeometry.scale(AnnotationGeometry.PADDING_PX,
magnification);
+ int padding = AnnotationGeometry.PADDING_PX;
int textX = balloon.x() + padding;
int textY = balloon.y() + padding;
for (int i = 0; i < lines.size(); i++) {
diff --git
a/plugins/misc/annotation/src/test/java/org/apache/hop/annotation/AnnotationClicksTest.java
b/plugins/misc/annotation/src/test/java/org/apache/hop/annotation/AnnotationClicksTest.java
new file mode 100644
index 0000000000..7fc26ae217
--- /dev/null
+++
b/plugins/misc/annotation/src/test/java/org/apache/hop/annotation/AnnotationClicksTest.java
@@ -0,0 +1,214 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hop.annotation;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.nullable;
+import static org.mockito.Mockito.doAnswer;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.hop.core.gui.AreaOwner;
+import org.apache.hop.core.gui.DPoint;
+import org.apache.hop.core.gui.Point;
+import org.apache.hop.ui.hopgui.file.pipeline.HopGuiPipelineGraph;
+import
org.apache.hop.ui.hopgui.file.pipeline.extension.HopGuiPipelineGraphExtension;
+import org.apache.hop.ui.hopgui.file.workflow.HopGuiWorkflowGraph;
+import
org.apache.hop.ui.hopgui.file.workflow.extension.HopGuiWorkflowGraphExtension;
+import org.eclipse.swt.events.MouseEvent;
+import org.eclipse.swt.widgets.Canvas;
+import org.eclipse.swt.widgets.Event;
+import org.junit.jupiter.api.Test;
+
+class AnnotationClicksTest {
+
+ private static final String PRESSED = "annotation-callout-pressed";
+
+ @Test
+ void pressOnOneGraphDoesNotSwallowAnotherGraphsMouseUp() {
+ Graph pipelineA = new Graph(true);
+ Graph pipelineB = new Graph(true);
+ Graph workflowB = new Graph(false);
+
+ AnnotationClicks.pipelineMouseDown(pipelineA.pipelineDown(callout(), 1));
+ assertEquals(Boolean.TRUE, pipelineA.data.get(PRESSED));
+ assertNull(pipelineB.data.get(PRESSED));
+
+ HopGuiPipelineGraphExtension otherPipeline =
pipelineB.pipelineUp(notCallout());
+ AnnotationClicks.pipelineMouseUp(otherPipeline);
+ assertFalse(otherPipeline.isPreventingDefault());
+ assertEquals(Boolean.TRUE, pipelineA.data.get(PRESSED));
+
+ HopGuiWorkflowGraphExtension otherWorkflow =
workflowB.workflowUp(notCallout());
+ AnnotationClicks.workflowMouseUp(otherWorkflow);
+ assertFalse(otherWorkflow.isPreventingDefault());
+ assertEquals(Boolean.TRUE, pipelineA.data.get(PRESSED));
+ assertNull(workflowB.data.get(PRESSED));
+
+ // Releasing on a callout is still claimed, and only that graph's flag is
cleared.
+ HopGuiPipelineGraphExtension onCallout = pipelineB.pipelineUp(callout());
+ AnnotationClicks.pipelineMouseUp(onCallout);
+ assertTrue(onCallout.isPreventingDefault());
+ assertEquals(Boolean.TRUE, pipelineA.data.get(PRESSED));
+ assertNull(pipelineB.data.get(PRESSED));
+
+ HopGuiPipelineGraphExtension release = pipelineA.pipelineUp(notCallout());
+ AnnotationClicks.pipelineMouseUp(release);
+ assertTrue(release.isPreventingDefault());
+ assertNull(pipelineA.data.get(PRESSED));
+
+ HopGuiPipelineGraphExtension again = pipelineA.pipelineUp(notCallout());
+ AnnotationClicks.pipelineMouseUp(again);
+ assertFalse(again.isPreventingDefault());
+
+ Graph workflowA = new Graph(false);
+ AnnotationClicks.workflowMouseDown(workflowA.workflowDown(callout(), 1));
+ assertEquals(Boolean.TRUE, workflowA.data.get(PRESSED));
+ HopGuiPipelineGraphExtension pipelineRelease =
pipelineB.pipelineUp(notCallout());
+ AnnotationClicks.pipelineMouseUp(pipelineRelease);
+ assertFalse(pipelineRelease.isPreventingDefault());
+ assertEquals(Boolean.TRUE, workflowA.data.get(PRESSED));
+
+ HopGuiWorkflowGraphExtension workflowRelease =
workflowA.workflowUp(notCallout());
+ AnnotationClicks.workflowMouseUp(workflowRelease);
+ assertTrue(workflowRelease.isPreventingDefault());
+ assertNull(workflowA.data.get(PRESSED));
+ }
+
+ @Test
+ void mouseDownOffTheCalloutClearsOnlyThatGraph() {
+ Graph pipelineA = new Graph(true);
+ Graph pipelineB = new Graph(true);
+ AnnotationClicks.pipelineMouseDown(pipelineA.pipelineDown(callout(), 1));
+ AnnotationClicks.pipelineMouseDown(pipelineB.pipelineDown(notCallout(),
1));
+ assertEquals(Boolean.TRUE, pipelineA.data.get(PRESSED));
+ assertNull(pipelineB.data.get(PRESSED));
+
+ AnnotationClicks.pipelineMouseDown(pipelineA.pipelineDown(notCallout(),
1));
+ assertNull(pipelineA.data.get(PRESSED));
+ HopGuiPipelineGraphExtension up = pipelineA.pipelineUp(notCallout());
+ AnnotationClicks.pipelineMouseUp(up);
+ assertFalse(up.isPreventingDefault());
+ }
+
+ @Test
+ void nullGraphAndDisposedCanvasDoNotThrow() {
+ assertDoesNotThrow(() -> AnnotationClicks.pipelineMouseDown(null));
+ assertDoesNotThrow(() -> AnnotationClicks.pipelineMouseUp(null));
+ assertDoesNotThrow(() -> AnnotationClicks.workflowMouseDown(null));
+ assertDoesNotThrow(() -> AnnotationClicks.workflowMouseUp(null));
+
+ HopGuiPipelineGraph graph = mock(HopGuiPipelineGraph.class);
+ when(graph.getCanvas()).thenReturn(null);
+ HopGuiPipelineGraphExtension down =
+ new HopGuiPipelineGraphExtension(graph, mouse(1), new Point(0, 0),
callout());
+ AnnotationClicks.pipelineMouseDown(down);
+ assertTrue(down.isPreventingDefault());
+ HopGuiPipelineGraphExtension up =
+ new HopGuiPipelineGraphExtension(graph, mouse(1), new Point(0, 0),
notCallout());
+ AnnotationClicks.pipelineMouseUp(up);
+ assertFalse(up.isPreventingDefault());
+
+ Canvas disposed = mock(Canvas.class);
+ when(disposed.isDisposed()).thenReturn(true);
+ when(graph.getCanvas()).thenReturn(disposed);
+ HopGuiPipelineGraphExtension disposedDown =
+ new HopGuiPipelineGraphExtension(graph, mouse(1), new Point(0, 0),
callout());
+ AnnotationClicks.pipelineMouseDown(disposedDown);
+ assertTrue(disposedDown.isPreventingDefault());
+ verify(disposed, never()).setData(anyString(), nullable(Object.class));
+ }
+
+ private static AreaOwner callout() {
+ return new AreaOwner(
+ AreaOwner.AreaType.CUSTOM,
+ 0,
+ 0,
+ 10,
+ 10,
+ new DPoint(0, 0),
+ new AnnotationHit("anchor"),
+ "anchor");
+ }
+
+ private static AreaOwner notCallout() {
+ return new AreaOwner(AreaOwner.AreaType.NOTE, 0, 0, 10, 10, new DPoint(0,
0), "note", "note");
+ }
+
+ private static MouseEvent mouse(int button) {
+ Event event = new Event();
+ event.button = button;
+ event.widget = mock(Canvas.class);
+ return new MouseEvent(event);
+ }
+
+ /** One graph and the data stored on its canvas. */
+ private static final class Graph {
+ private final Map<String, Object> data = new HashMap<>();
+ private final HopGuiPipelineGraph pipeline;
+ private final HopGuiWorkflowGraph workflow;
+
+ private Graph(boolean pipelineGraph) {
+ Canvas canvas = mock(Canvas.class);
+ when(canvas.isDisposed()).thenReturn(false);
+ when(canvas.getData(anyString()))
+ .thenAnswer(invocation -> data.get(invocation.getArgument(0,
String.class)));
+ doAnswer(
+ invocation -> {
+ data.put(invocation.getArgument(0, String.class),
invocation.getArgument(1));
+ return null;
+ })
+ .when(canvas)
+ .setData(anyString(), nullable(Object.class));
+ if (pipelineGraph) {
+ pipeline = mock(HopGuiPipelineGraph.class);
+ when(pipeline.getCanvas()).thenReturn(canvas);
+ workflow = null;
+ } else {
+ pipeline = null;
+ workflow = mock(HopGuiWorkflowGraph.class);
+ when(workflow.getCanvas()).thenReturn(canvas);
+ }
+ }
+
+ private HopGuiPipelineGraphExtension pipelineDown(AreaOwner area, int
button) {
+ return new HopGuiPipelineGraphExtension(pipeline, mouse(button), new
Point(0, 0), area);
+ }
+
+ private HopGuiPipelineGraphExtension pipelineUp(AreaOwner area) {
+ return pipelineDown(area, 1);
+ }
+
+ private HopGuiWorkflowGraphExtension workflowDown(AreaOwner area, int
button) {
+ return new HopGuiWorkflowGraphExtension(workflow, mouse(button), new
Point(0, 0), area);
+ }
+
+ private HopGuiWorkflowGraphExtension workflowUp(AreaOwner area) {
+ return workflowDown(area, 1);
+ }
+ }
+}
diff --git
a/plugins/misc/annotation/src/test/java/org/apache/hop/annotation/AnnotationGeometryTest.java
b/plugins/misc/annotation/src/test/java/org/apache/hop/annotation/AnnotationGeometryTest.java
index 24d6d1157e..7e39775f07 100644
---
a/plugins/misc/annotation/src/test/java/org/apache/hop/annotation/AnnotationGeometryTest.java
+++
b/plugins/misc/annotation/src/test/java/org/apache/hop/annotation/AnnotationGeometryTest.java
@@ -27,6 +27,7 @@ import org.apache.hop.annotation.AnnotationGeometry.Segment;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
+import org.junit.jupiter.params.provider.ValueSource;
class AnnotationGeometryTest {
@@ -35,12 +36,11 @@ class AnnotationGeometryTest {
@ParameterizedTest
@EnumSource(AnnotationPlacement.class)
void balloonSitsOutsideTheIconOnThatSide(AnnotationPlacement placement) {
- int gap = AnnotationGeometry.scale(AnnotationGeometry.GAP_PX, 1f);
+ int gap = AnnotationGeometry.GAP_PX;
Bounds balloon = AnnotationGeometry.balloon(ICON, 40, 16, placement, 1f);
assertFalse(balloon.overlaps(ICON));
- assertEquals(
- 40 + 2 * AnnotationGeometry.scale(AnnotationGeometry.PADDING_PX, 1f),
balloon.width());
+ assertEquals(40 + 2 * AnnotationGeometry.PADDING_PX, balloon.width());
switch (placement) {
case TOP -> {
assertEquals(ICON.y(), balloon.y() + balloon.height() + gap);
@@ -78,15 +78,27 @@ class AnnotationGeometryTest {
}
}
- @Test
- void magnifiedGapMovesTheBalloonFurtherFromTheIcon() {
- Bounds near = AnnotationGeometry.balloon(ICON, 20, 10,
AnnotationPlacement.TOP, 1f);
- Bounds far = AnnotationGeometry.balloon(ICON, 20, 10,
AnnotationPlacement.TOP, 2f);
- int gap = AnnotationGeometry.scale(AnnotationGeometry.GAP_PX, 2f);
+ @ParameterizedTest
+ @ValueSource(floats = {0.5f, 1f, 2f})
+ void balloonSizeDoesNotDependOnMagnification(float magnification) {
+ for (AnnotationPlacement placement : AnnotationPlacement.values()) {
+ assertEquals(
+ AnnotationGeometry.balloon(ICON, 40, 16, placement, 1f),
+ AnnotationGeometry.balloon(ICON, 40, 16, placement, magnification),
+ placement.name());
+ }
+
+ Bounds balloon =
+ AnnotationGeometry.balloon(ICON, 40, 16, AnnotationPlacement.TOP,
magnification);
+ assertEquals(40 + 2 * AnnotationGeometry.PADDING_PX, balloon.width());
+ assertEquals(16 + 2 * AnnotationGeometry.PADDING_PX, balloon.height());
+ assertEquals(AnnotationGeometry.GAP_PX, ICON.y() - (balloon.y() +
balloon.height()));
+ assertEquals(ICON.centerX(), balloon.centerX());
- assertEquals(24, gap);
- assertEquals(ICON.y(), far.y() + far.height() + gap);
- assertTrue(far.y() < near.y());
+ Bounds right =
+ AnnotationGeometry.balloon(ICON, 40, 16, AnnotationPlacement.RIGHT,
magnification);
+ assertEquals(AnnotationGeometry.GAP_PX, right.x() - (ICON.x() +
ICON.width()));
+ assertEquals(ICON.centerY(), right.centerY());
}
@Test
diff --git
a/plugins/misc/annotation/src/test/java/org/apache/hop/annotation/AnnotationPainterTest.java
b/plugins/misc/annotation/src/test/java/org/apache/hop/annotation/AnnotationPainterTest.java
index 85d7467516..827726b4df 100644
---
a/plugins/misc/annotation/src/test/java/org/apache/hop/annotation/AnnotationPainterTest.java
+++
b/plugins/misc/annotation/src/test/java/org/apache/hop/annotation/AnnotationPainterTest.java
@@ -18,6 +18,7 @@
package org.apache.hop.annotation;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -125,6 +126,65 @@ class AnnotationPainterTest {
assertEquals(0, gc.lineCount);
}
+ @Test
+ void calloutSizeAndClickAreaDoNotDependOnMagnification() {
+ // 25 characters fit in MAX_TEXT_WIDTH_PX (25 * 7 = 175). The whole line
is 217, which fits
+ // only when the wrap width is scaled by the zoom a second time.
+ String text = "1234567890123456789012345 extra";
+ Annotation annotation = new Annotation();
+ annotation.setText(text);
+ annotation.setDashedLine(false);
+ annotation.setPlacement(AnnotationPlacement.TOP);
+ DPoint offset = new DPoint(4, 6);
+ Bounds icon = new Bounds(100, 80, 32, 32);
+
+ AreaOwner first = null;
+ for (float magnification : new float[] {0.5f, 1f, 2f}) {
+ StubGc gc = new StubGc();
+ gc.magnification = magnification;
+ List<AreaOwner> areas = new ArrayList<>();
+ AnnotationPainter.paint(
+ gc, areas, offset, new TransformMeta(), 100, 80, 32, false,
annotation);
+
+ assertEquals(2, gc.drawnText.size(), "magnification " + magnification);
+ Bounds expected =
+ AnnotationGeometry.balloon(icon, 25 * 7, 2 * 10,
AnnotationPlacement.TOP, magnification);
+ assertEquals(expected.x(), gc.roundX);
+ assertEquals(expected.y(), gc.roundY);
+ assertEquals(expected.width(), gc.roundWidth);
+ assertEquals(expected.height(), gc.roundHeight);
+ assertEquals(AnnotationGeometry.RADIUS_PX, gc.roundRadius);
+ assertEquals(expected.x() + AnnotationGeometry.PADDING_PX, gc.textX);
+ assertEquals(expected.y() + AnnotationGeometry.PADDING_PX, gc.textY);
+ assertEquals(25 * 7 + 2 * AnnotationGeometry.PADDING_PX,
expected.width());
+ assertEquals(2 * 10 + 2 * AnnotationGeometry.PADDING_PX,
expected.height());
+
+ AreaOwner area = areas.get(0);
+ assertEquals(expected.width(), area.getArea().width);
+ assertEquals(expected.height(), area.getArea().height);
+ assertEquals(expected.x() - 4, area.getArea().x);
+ assertEquals(expected.y() - 6, area.getArea().y);
+
+ // screen2real: graph = canvas / magnification - offset. The GC
transform drew the balloon
+ // at canvas = magnification * draw, and the click rectangle is draw
minus offset.
+ double canvasX = magnification * expected.centerX();
+ double canvasY = magnification * expected.centerY();
+ int graphX = (int) Math.round(canvasX / magnification - offset.x);
+ int graphY = (int) Math.round(canvasY / magnification - offset.y);
+ assertTrue(area.contains(graphX, graphY), "magnification " +
magnification);
+ assertFalse(area.contains(area.getArea().x - 1, graphY));
+
+ if (first == null) {
+ first = area;
+ } else {
+ assertEquals(first.getArea().x, area.getArea().x);
+ assertEquals(first.getArea().y, area.getArea().y);
+ assertEquals(first.getArea().width, area.getArea().width);
+ assertEquals(first.getArea().height, area.getArea().height);
+ }
+ }
+ }
+
@Test
void painterExtensionReadsTheStoredAnnotation() throws HopException {
TransformMeta transform = new TransformMeta();
@@ -179,10 +239,17 @@ class AnnotationPainterTest {
/** Minimal IGc that records the callout colors and shape. */
private static final class StubGc implements IGc {
private int lineWidth = 1;
+ private float magnification = 1f;
private IGc.EFont font;
private int backgroundRed = -1;
private int roundX;
private int roundY;
+ private int roundWidth;
+ private int roundHeight;
+ private int roundRadius;
+ private int textX;
+ private int textY;
+ private boolean textPositionRecorded;
private int roundRectLineWidth = -1;
private final List<String> drawnText = new ArrayList<>();
private IGc.ELineStyle lineStyle = IGc.ELineStyle.SOLID;
@@ -283,11 +350,20 @@ class AnnotationPainterTest {
@Override
public void drawText(String text, int x, int y) {
- drawnText.add(text);
+ recordText(text, x, y);
}
@Override
public void drawText(String text, int x, int y, boolean transparent) {
+ recordText(text, x, y);
+ }
+
+ private void recordText(String text, int x, int y) {
+ if (!textPositionRecorded) {
+ textX = x;
+ textY = y;
+ textPositionRecorded = true;
+ }
drawnText.add(text);
}
@@ -296,6 +372,9 @@ class AnnotationPainterTest {
int x, int y, int width, int height, int circleWidth, int
circleHeight) {
roundX = x;
roundY = y;
+ roundWidth = width;
+ roundHeight = height;
+ roundRadius = circleWidth;
}
@Override
@@ -321,7 +400,7 @@ class AnnotationPainterTest {
@Override
public float getMagnification() {
- return 1f;
+ return magnification;
}
@Override