This is an automated email from the ASF dual-hosted git repository.

hansva 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 37fcb109f9 Issue #9176 : Open status-bar menus upward from the clicked 
button (#9206)
37fcb109f9 is described below

commit 37fcb109f9f1410b029b1c470e73758df825467d
Author: Matt Casters <[email protected]>
AuthorDate: Tue Nov 3 11:15:56 2026 +0100

    Issue #9176 : Open status-bar menus upward from the clicked button (#9206)
    
    The project, environment, and git menus were anchored to the whole
    status bar, six pixels below it. Windows top-aligns a popup to that
    point, so the menu drops below the window when the desktop continues
    under it. A maximized window puts the point in the taskbar, and SWT
    then scales it with the primary monitor's zoom.
---
 .../main/java/org/apache/hop/git/GitGuiPlugin.java |  14 +-
 .../apache/hop/projects/gui/ProjectsGuiPlugin.java |  33 +---
 .../apache/hop/ui/core/gui/GuiToolbarWidgets.java  |  85 +++++++++-
 .../apache/hop/ui/core/gui/PopupMenuPlacement.java | 179 +++++++++++++++++++++
 .../hop/ui/core/gui/PopupMenuPlacementTest.java    |  96 +++++++++++
 5 files changed, 366 insertions(+), 41 deletions(-)

diff --git 
a/plugins/misc/git/src/main/java/org/apache/hop/git/GitGuiPlugin.java 
b/plugins/misc/git/src/main/java/org/apache/hop/git/GitGuiPlugin.java
index 932562458d..0e71e10cf4 100644
--- a/plugins/misc/git/src/main/java/org/apache/hop/git/GitGuiPlugin.java
+++ b/plugins/misc/git/src/main/java/org/apache/hop/git/GitGuiPlugin.java
@@ -79,9 +79,6 @@ import org.eclipse.jgit.diff.DiffEntry.ChangeType;
 import org.eclipse.jgit.merge.MergeStrategy;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.graphics.Color;
-import org.eclipse.swt.graphics.Point;
-import org.eclipse.swt.graphics.Rectangle;
-import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Menu;
 import org.eclipse.swt.widgets.MenuItem;
@@ -490,15 +487,10 @@ public class GitGuiPlugin
       separator = true)
   public void showGitContextMenu() {
     GuiToolbarWidgets statusWidgets = 
HopGui.getInstance().getStatusToolbarWidgets();
-    Control control = statusWidgets.getWidgetsMap().get(ID_TOOLBAR_ITEM_GIT);
-    if (control != null && !control.isDisposed()) {
-      Rectangle rect = control.getBounds();
-      int menuGap = 6;
-      Point pt = control.getParent().toDisplay(new Point(rect.x, rect.y + 
rect.height + menuGap));
-      Menu menu = createGitContextMenu();
-      menu.setLocation(pt);
-      menu.setVisible(true);
+    if (statusWidgets == null) {
+      return;
     }
+    statusWidgets.showMenuAbove(ID_TOOLBAR_ITEM_GIT, createGitContextMenu());
   }
 
   private Menu createGitContextMenu() {
diff --git 
a/plugins/misc/projects/src/main/java/org/apache/hop/projects/gui/ProjectsGuiPlugin.java
 
b/plugins/misc/projects/src/main/java/org/apache/hop/projects/gui/ProjectsGuiPlugin.java
index 9db4114a1d..ff9ca9d4ad 100644
--- 
a/plugins/misc/projects/src/main/java/org/apache/hop/projects/gui/ProjectsGuiPlugin.java
+++ 
b/plugins/misc/projects/src/main/java/org/apache/hop/projects/gui/ProjectsGuiPlugin.java
@@ -102,11 +102,8 @@ import 
org.apache.hop.ui.pipeline.transform.BaseTransformDialog;
 import org.apache.hop.workflow.config.WorkflowRunConfiguration;
 import org.apache.hop.workflow.engines.local.LocalWorkflowRunConfiguration;
 import org.eclipse.swt.SWT;
-import org.eclipse.swt.graphics.Point;
-import org.eclipse.swt.graphics.Rectangle;
 import org.eclipse.swt.layout.FormLayout;
 import org.eclipse.swt.widgets.Button;
-import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Menu;
 import org.eclipse.swt.widgets.MenuItem;
@@ -882,17 +879,11 @@ public class ProjectsGuiPlugin {
       image = "project.svg",
       toolTip = "i18n::HopGui.Toolbar.Project.Tooltip")
   public void showProjectContextMenu() {
-    Control control =
-        
HopGui.getInstance().getStatusToolbarWidgets().getControlForMenu(ID_TOOLBAR_ITEM_PROJECT);
-    if (control != null && !control.isDisposed()) {
-      Rectangle rect = control.getBounds();
-      int menuGap = 6;
-      Point location =
-          control.getParent().toDisplay(new Point(rect.x, rect.y + rect.height 
+ menuGap));
-      Menu menu = createProjectContextMenu();
-      menu.setLocation(location);
-      menu.setVisible(true);
+    GuiToolbarWidgets widgets = HopGui.getInstance().getStatusToolbarWidgets();
+    if (widgets == null) {
+      return;
     }
+    widgets.showMenuAbove(ID_TOOLBAR_ITEM_PROJECT, createProjectContextMenu());
   }
 
   private Menu createProjectContextMenu() {
@@ -1069,19 +1060,11 @@ public class ProjectsGuiPlugin {
       image = "environment.svg",
       toolTip = "i18n::HopGui.Toolbar.Environment.Tooltip")
   public void showEnvironmentContextMenu() {
-    Control control =
-        HopGui.getInstance()
-            .getStatusToolbarWidgets()
-            .getControlForMenu(ID_TOOLBAR_ITEM_ENVIRONMENT);
-    if (control != null && !control.isDisposed()) {
-      Rectangle rect = control.getBounds();
-      int menuGap = 6;
-      Point location =
-          control.getParent().toDisplay(new Point(rect.x, rect.y + rect.height 
+ menuGap));
-      Menu menu = createEnvironmentContextMenu();
-      menu.setLocation(location);
-      menu.setVisible(true);
+    GuiToolbarWidgets widgets = HopGui.getInstance().getStatusToolbarWidgets();
+    if (widgets == null) {
+      return;
     }
+    widgets.showMenuAbove(ID_TOOLBAR_ITEM_ENVIRONMENT, 
createEnvironmentContextMenu());
   }
 
   public void selectProject() {
diff --git a/ui/src/main/java/org/apache/hop/ui/core/gui/GuiToolbarWidgets.java 
b/ui/src/main/java/org/apache/hop/ui/core/gui/GuiToolbarWidgets.java
index 47bbe4e21b..b3326f55ae 100644
--- a/ui/src/main/java/org/apache/hop/ui/core/gui/GuiToolbarWidgets.java
+++ b/ui/src/main/java/org/apache/hop/ui/core/gui/GuiToolbarWidgets.java
@@ -53,6 +53,8 @@ import org.eclipse.swt.events.PaintListener;
 import org.eclipse.swt.graphics.Color;
 import org.eclipse.swt.graphics.GC;
 import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.graphics.Rectangle;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.layout.GridLayout;
 import org.eclipse.swt.layout.RowData;
@@ -64,6 +66,8 @@ import org.eclipse.swt.widgets.Control;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Listener;
+import org.eclipse.swt.widgets.Menu;
+import org.eclipse.swt.widgets.Monitor;
 import org.eclipse.swt.widgets.Text;
 import org.eclipse.swt.widgets.ToolBar;
 import org.eclipse.swt.widgets.ToolItem;
@@ -911,18 +915,89 @@ public class GuiToolbarWidgets extends BaseGuiWidgets 
implements IToolbarWidgetR
   }
 
   /**
-   * Return the Control to use for positioning a popup menu for the given 
toolbar item. A ToolItem
-   * is not a Control, and getControl() is only set for SEPARATOR items (the 
wrapped label/widget).
-   * For BUTTON items we store the composite (image+text) in widgetsMap; use 
that for menu
-   * positioning.
+   * Return the control stored for a toolbar item. A {@link ToolItem} is not a 
control. Desktop
+   * buttons store the parent {@link ToolBar}, so its bounds start at the left 
edge of the bar and
+   * cannot place a popup on that button. Use {@link #showMenuAbove(String, 
Menu)} or {@link
+   * #findToolItem(String)} for that.
    *
    * @param id toolbar item id (e.g. {@code ID_TOOLBAR_ITEM_PROJECT})
-   * @return the Control to use for getBounds() / toDisplay(), or null if not 
found
+   * @return the stored control, or null if not found
    */
   public Control getControlForMenu(String id) {
     return widgetsMap.get(id);
   }
 
+  /**
+   * Open {@code menu} upward from toolbar item {@code id}. The menu's 
bottom-left corner sits on
+   * the item's top-left, which is where a click on the status bar should open.
+   *
+   * <p>Falls back to the pointer when the item is not on screen yet. The 
pointer is still kept on
+   * the monitor under it, so a second monitor with its own scale does not 
steal the placement.
+   */
+  public void showMenuAbove(String id, Menu menu) {
+    if (menu == null || menu.isDisposed()) {
+      return;
+    }
+    Rectangle anchor = anchorForMenu(id);
+    Monitor monitor = monitorFor(id, menu);
+    if (anchor == null) {
+      Point cursor = menu.getDisplay().getCursorLocation();
+      anchor = new Rectangle(cursor.x, cursor.y, 1, 1);
+      monitor = monitorContaining(menu.getDisplay(), cursor.x, cursor.y, 
monitor);
+    }
+    PopupMenuPlacement.showAbove(menu, anchor, monitor);
+  }
+
+  private Rectangle anchorForMenu(String id) {
+    ToolItem item = findToolItem(id);
+    if (item != null && !item.isDisposed()) {
+      ToolBar bar = item.getParent();
+      if (bar != null && !bar.isDisposed()) {
+        Rectangle bounds = item.getBounds();
+        if (bounds.width > 0 && bounds.height > 0) {
+          Point origin = bar.toDisplay(bounds.x, bounds.y);
+          return new Rectangle(origin.x, origin.y, bounds.width, 
bounds.height);
+        }
+      }
+    }
+    Control control = findControl(id);
+    if (control != null && !control.isDisposed() && !(control instanceof 
ToolBar)) {
+      Point size = control.getSize();
+      if (size.x > 0 && size.y > 0) {
+        Point origin = control.toDisplay(0, 0);
+        return new Rectangle(origin.x, origin.y, size.x, size.y);
+      }
+    }
+    return null;
+  }
+
+  private Monitor monitorFor(String id, Menu menu) {
+    ToolItem item = findToolItem(id);
+    if (item != null && !item.isDisposed()) {
+      ToolBar bar = item.getParent();
+      if (bar != null && !bar.isDisposed()) {
+        return bar.getMonitor();
+      }
+    }
+    Control control = findControl(id);
+    if (control != null && !control.isDisposed() && !(control instanceof 
ToolBar)) {
+      return control.getMonitor();
+    }
+    return menu.getShell() == null ? null : menu.getShell().getMonitor();
+  }
+
+  private static Monitor monitorContaining(Display display, int x, int y, 
Monitor fallback) {
+    if (display == null || display.isDisposed()) {
+      return fallback;
+    }
+    for (Monitor candidate : display.getMonitors()) {
+      if (candidate.getClientArea().contains(x, y)) {
+        return candidate;
+      }
+    }
+    return fallback;
+  }
+
   /**
    * Set the image of a toolbar item by path. Use this when toggling between 
two icons (e.g. show
    * only selected / show all). In desktop SWT this sets the ToolItem's image; 
in web RWT this
diff --git 
a/ui/src/main/java/org/apache/hop/ui/core/gui/PopupMenuPlacement.java 
b/ui/src/main/java/org/apache/hop/ui/core/gui/PopupMenuPlacement.java
new file mode 100644
index 0000000000..b5f60fb161
--- /dev/null
+++ b/ui/src/main/java/org/apache/hop/ui/core/gui/PopupMenuPlacement.java
@@ -0,0 +1,179 @@
+/*
+ * 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.ui.core.gui;
+
+import java.lang.reflect.Method;
+import org.apache.hop.ui.core.widget.OsHelper;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.GC;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.graphics.Rectangle;
+import org.eclipse.swt.widgets.Menu;
+import org.eclipse.swt.widgets.MenuItem;
+import org.eclipse.swt.widgets.Monitor;
+
+/**
+ * Places a popup so it grows upward from the widget that was clicked.
+ *
+ * <p>SWT top-aligns a popup to the point passed to {@link 
Menu#setLocation(Point)}. A point just
+ * under the status bar therefore drops the menu below the window whenever the 
desktop continues
+ * there. When the window is maximized that point lands in the taskbar, 
outside every monitor work
+ * area. {@code Menu.setLocation} then scales it with the primary monitor's 
zoom, so on a second
+ * monitor with a different scale the menu jumps away from the click. The 
point returned here stays
+ * inside the monitor that contains the anchor.
+ */
+final class PopupMenuPlacement {
+
+  /** {@code SM_CYMENU}. Height of one menu row, in pixels, at the requested 
DPI. */
+  private static final int SM_CYMENU = 0xf;
+
+  /** {@code SM_CYMENUCHECK}. Height of the menu check bitmap. A row is at 
least this tall. */
+  private static final int SM_CYMENUCHECK = 72;
+
+  /**
+   * Frame around the item stack, in points. SWT's own menu bounds add a few 
pixels past the items.
+   */
+  private static final int MENU_CHROME = 8;
+
+  private PopupMenuPlacement() {}
+
+  /**
+   * Top-left of a top-aligned popup whose bottom edge sits on {@code 
anchor.y}. When that point
+   * would leave {@code monitorArea}, it is pulled back inside. The bottom and 
right edges of an SWT
+   * rectangle are exclusive, and a point on that edge is what makes the 
Windows placement fall back
+   * to the primary monitor.
+   */
+  static Point above(Rectangle anchor, int menuHeight, Rectangle monitorArea) {
+    int x = anchor.x;
+    int y = anchor.y - Math.max(menuHeight, 0);
+    if (monitorArea == null || monitorArea.width <= 0 || monitorArea.height <= 
0) {
+      return new Point(x, y);
+    }
+    int right = monitorArea.x + monitorArea.width;
+    int bottom = monitorArea.y + monitorArea.height;
+    if (x < monitorArea.x) {
+      x = monitorArea.x;
+    } else if (x >= right) {
+      x = right - 1;
+    }
+    if (y < monitorArea.y) {
+      y = monitorArea.y;
+    } else if (y >= bottom) {
+      y = bottom - 1;
+    }
+    return new Point(x, y);
+  }
+
+  static void showAbove(Menu menu, Rectangle anchor, Monitor monitor) {
+    Rectangle area = monitor == null ? null : monitor.getClientArea();
+    menu.setLocation(above(anchor, heightOf(menu, monitor), area));
+    menu.setVisible(true);
+  }
+
+  /**
+   * Height of {@code menu} in the same points {@link Menu#setLocation(Point)} 
expects. Windows
+   * reports the row height in pixels at the monitor DPI; other platforms use 
the menu font. The
+   * frame around the items is in points on every platform, because the font 
path is already in
+   * points and must not be scaled a second time.
+   */
+  static int heightOf(Menu menu, Monitor monitor) {
+    if (menu == null || menu.isDisposed()) {
+      return 0;
+    }
+    MenuItem[] items = menu.getItems();
+    if (items.length == 0) {
+      return 0;
+    }
+    int rows = 0;
+    int separators = 0;
+    int tallestImage = 0;
+    for (MenuItem item : items) {
+      if (item.isDisposed()) {
+        continue;
+      }
+      if ((item.getStyle() & SWT.SEPARATOR) != 0) {
+        separators++;
+        continue;
+      }
+      rows++;
+      Image image = item.getImage();
+      if (image != null && !image.isDisposed()) {
+        tallestImage = Math.max(tallestImage, image.getBounds().height);
+      }
+    }
+    if (rows == 0 && separators == 0) {
+      return 0;
+    }
+    int rowPoints = rowHeight(menu, monitor, tallestImage);
+    int separatorPoints = Math.max(6, rowPoints / 3);
+    return rows * rowPoints + separators * separatorPoints + MENU_CHROME;
+  }
+
+  private static int rowHeight(Menu menu, Monitor monitor, int tallestImage) {
+    int rowPoints = -1;
+    if (OsHelper.isWindows() && monitor != null) {
+      int zoom = monitor.getZoom();
+      int rowPixels = windowsRowPixels(zoom);
+      if (rowPixels > 0) {
+        rowPoints = pixelsToPoints(rowPixels, zoom);
+      }
+    }
+    if (rowPoints <= 0) {
+      rowPoints = textHeight(menu) + 8;
+    }
+    if (tallestImage > 0) {
+      rowPoints = Math.max(rowPoints, tallestImage + 2);
+    }
+    return rowPoints;
+  }
+
+  private static int textHeight(Menu menu) {
+    GC gc = new GC(menu.getShell());
+    try {
+      gc.setFont(menu.getDisplay().getSystemFont());
+      return gc.textExtent("Ag").y;
+    } finally {
+      gc.dispose();
+    }
+  }
+
+  private static int windowsRowPixels(int zoom) {
+    try {
+      Class<?> os = Class.forName("org.eclipse.swt.internal.win32.OS");
+      Method forDpi = os.getMethod("GetSystemMetricsForDpi", int.class, 
int.class);
+      int dpi = (zoom <= 0 ? 100 : zoom) * 96 / 100;
+      int cyMenu = (Integer) forDpi.invoke(null, SM_CYMENU, dpi);
+      int cyCheck = (Integer) forDpi.invoke(null, SM_CYMENUCHECK, dpi);
+      return Math.max(cyMenu, cyCheck);
+    } catch (Exception | LinkageError e) {
+      return -1;
+    }
+  }
+
+  /** Same rounding as {@code DPIUtil.pixelToPoint}. */
+  static int pixelsToPoints(int pixels, int zoom) {
+    if (pixels <= 0) {
+      return 0;
+    }
+    if (zoom == 100 || zoom <= 0) {
+      return pixels;
+    }
+    return Math.round(pixels * 100f / zoom);
+  }
+}
diff --git 
a/ui/src/test/java/org/apache/hop/ui/core/gui/PopupMenuPlacementTest.java 
b/ui/src/test/java/org/apache/hop/ui/core/gui/PopupMenuPlacementTest.java
new file mode 100644
index 0000000000..4a6b233ff7
--- /dev/null
+++ b/ui/src/test/java/org/apache/hop/ui/core/gui/PopupMenuPlacementTest.java
@@ -0,0 +1,96 @@
+/*
+ * 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.ui.core.gui;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.graphics.Rectangle;
+import org.junit.jupiter.api.Test;
+
+class PopupMenuPlacementTest {
+
+  @Test
+  void menuBottomSitsOnTheAnchorTop() {
+    Rectangle anchor = new Rectangle(400, 700, 80, 24);
+    Rectangle monitor = new Rectangle(0, 0, 1920, 1080);
+
+    Point where = PopupMenuPlacement.above(anchor, 200, monitor);
+
+    assertEquals(400, where.x);
+    assertEquals(500, where.y);
+  }
+
+  @Test
+  void tallMenuStaysOnTheAnchorMonitor() {
+    // A menu taller than the space above the button would otherwise start on 
the monitor
+    // stacked above this one. That monitor can have a different scale.
+    Rectangle anchor = new Rectangle(100, 30, 40, 20);
+    Rectangle monitor = new Rectangle(0, 0, 800, 600);
+
+    Point where = PopupMenuPlacement.above(anchor, 200, monitor);
+
+    assertEquals(100, where.x);
+    assertEquals(0, where.y);
+    assertTrue(monitor.contains(where));
+  }
+
+  @Test
+  void pointOnTheTaskbarIsPulledInsideTheWorkArea() {
+    // The old anchor was the status bar plus a gap. On a maximized window 
that point is the
+    // taskbar, and an SWT rectangle does not contain its bottom edge. Windows 
then scales the
+    // menu with the primary monitor.
+    Rectangle monitor = new Rectangle(1920, 0, 2560, 1440);
+    Rectangle anchor = new Rectangle(2100, 1440, 120, 28);
+
+    Point where = PopupMenuPlacement.above(anchor, 0, monitor);
+
+    assertEquals(2100, where.x);
+    assertEquals(1439, where.y);
+    assertTrue(monitor.contains(where));
+  }
+
+  @Test
+  void secondaryMonitorKeepsItsOwnOrigin() {
+    Rectangle monitor = new Rectangle(-1920, 0, 1920, 1080);
+    Rectangle anchor = new Rectangle(-400, 1000, 60, 24);
+
+    Point where = PopupMenuPlacement.above(anchor, 80, monitor);
+
+    assertEquals(-400, where.x);
+    assertEquals(920, where.y);
+    assertTrue(monitor.contains(where));
+  }
+
+  @Test
+  void noMonitorLeavesThePointWhereTheMenuWouldOpen() {
+    Point where = PopupMenuPlacement.above(new Rectangle(5, 8, 1, 1), 20, 
null);
+
+    assertEquals(5, where.x);
+    assertEquals(-12, where.y);
+  }
+
+  @Test
+  void pixelsToPointsMatchesSwtRounding() {
+    assertEquals(25, PopupMenuPlacement.pixelsToPoints(25, 100));
+    assertEquals(100, PopupMenuPlacement.pixelsToPoints(150, 150));
+    assertEquals(96, PopupMenuPlacement.pixelsToPoints(144, 150));
+    assertEquals(0, PopupMenuPlacement.pixelsToPoints(0, 150));
+  }
+}

Reply via email to