Open popup at mouse cursor

I have a template with a button in it that is used to launch a popup.

  1. I would like to open the popup at the mouse cursor, but am struggling to get the mouse coordinates relative to the parent window.

  2. The other thing I might want to do instead of using the mouse coordinates, is to use the coordinates of the button itself, relative to the client, so that I can display the popup slightly to the left/right of the template object itself to still keep it visible. In this case, how would I get the coordinates of the button component, relative to the client?

I'm sure I should know how to do this...

Cheers

Edit: I've gotten a little further.

def setRelativeLocation(event, window, compRelX, compRelY):
	windowX = window.getX()
	windowY = window.getY()
	
	screenRelX, screenRelY = system.gui.convertPointToScreen(compRelX, compRelY, event)

	window.setLocation(screenRelX, screenRelY)

#Called on button mouseReleased event
win = system.nav.openWindowInstance('Popups/ppuSAW_Valve', {'DeviceName': event.source.parent.DeviceName})
setRelativeLocation(event, win, event.x, event.y)

This opens the popup almost in the correct location if the client window is in the top left of the screen. Correct x (although it may be off by a few pixels due to the width of the client window border) but the y is wrong as the coordinates are relative to the main window and not the whole client window which includes the main window and a docked header window. How can I get the coordinates relative to the whole client window as a whole? I don't want to have to hardcode this, or look for open windows and get their heights, etc. as I would also like to accommodate for other events that may change the client size such as:

  • staging mode header
  • updates available header
  • other docked windows that may be opened and closed for alerts similar to the updates available header

If the client window is windowed (not fullscreen) and the client is moved around the screen, the popup opens in completely the wrong positions as it opens the popup relative to if the client window was in the top left of the monitor.
How do I get the component's coordinates relative to the actual client window?

1 Like

I am stuck trying this exact thing and ran into the same problem.

I cant find a way to get the relative position per screen used - because if I move the vision client to my 2nd monitor the whole thing breaks and trys to open off-screen.

Did you ever get any further with this?

Ahem...

from javax.swing import SwingUtilities
window = system.nav.openWindow('Popup Path', {})
newPoint = SwingUtilities.convertPoint(event.source, event.x, event.y, system.gui.getParentWindow(event).rootContainer)
window.setLocation(newPoint.x, newPoint.y)
2 Likes

IA has now made this a GUI function, so no more need to import java, but I'm struggling to trying to make sure that the popup does not extend outside the bounds of the client window.

#x and y position for popup, relative to entire screen
xOpen, yOpen = system.gui.convertPointToScreen(event.x, event.y, event)

#open window
window = system.nav.openWindowInstance('Popups/PopUp')

#parent window
parentWindow = system.gui.getParentWindow(event)

#calculate where the end of the poup is
xEnd = xOpen + window.width
yEnd = yOpen + window.height

#how do I find x and y max (width and height of screen)??
#xMax = 1920
#yMax = 1080

#make sure end of popup is not off-screen
if xEnd > xMax:
	xOpen = xOpen - (xEnd - xMax)
	
if yEnd > yMax:
	yOpen = yOpen - (yEnd - yMax)

#set window location
window.setLocation(xOpen,yOpen)

I thought maybe I could use the system.gui.convertPointToScreen() again, but it needs an event.

I'm pretty sure this function existing back in 2023 as well, but from memory it doesn't do what I needed here. I haven't used Vision in a while though, so :man_shrugging:

Here is a link to a forum post with my approach to this problem:
move-popup-within-bounds-of-client-window