Move Popup Within Bounds of Client Window

Im getting this error now.

This is what I have on the button that calls the popup. I did put Alex script in the library.
from javax.swing import SwingUtilities
param1 = event.source.ScreenJump
window = system.nav.openWindowInstance('Popup/NAV SCREEN', {'Page' : param1})
newPoint = SwingUtilities.convertPoint(event.source, event.x, event.y, system.gui.getParentWindow(event).rootContainer)
window.setLocation(newPoint.x, newPoint.y)

# Call the library script and pass in the event and the window,
# ...so the script can properly postion window before it is painted
PopupPositionScripts.moveWindowRelative(event, window)

Well the error is because you're trying to pass event and window when the script only takes 1 arg (window).

Regardless, why are you trying to convert points and move outside the script? The script is just going to move it anyway. This script opens the window in the position where the mouse was when the event fired. All you need is this

window = system.nav.openWindowInstance('Popup/NAV SCREEN', {'Page' : param1})
PopupPositionScripts.moveWindowRelative(window)

this is what I have now.

param1 = event.source.ScreenJump
window = system.nav.openWindowInstance('Popup/NAV SCREEN', {'Page' : param1})
PopupPositionScripts.moveWindowRelative(event, window)

I still get this error.

:man_facepalming:

Compare yours:

Against @amarks':

Now, go back and look at what that error message says.

1 Like

This is what I have on the library.

i added event on the "def moveWindowRelative(event, window)" in the library.

Look at line 4 of that screenshot.

:man_facepalming:

1 Like

Sorry im a begginer on this scripting. I added the event there. Now there is no error coming up but the popup still go out of bound.

You didn't need to add the event there, you needed to remove the event from your script that calls moveWindowRelative as it's not needed.

Regardless, you're probably still missing something. Can you paste the entire script from your visionWindowOpened event? And what window is that visionWindowOpened script inside? It should be inside your popup window, not the main window, but wanted to clarify.

I dont have any script in the popup window visionWindowOpened. I only have script on the library and the caller button.

The script everyone has been trying to help you with here should be in the visionWindowOpened event of the popup window. That's the way this was originally intended to work I believe (at least that's how I've implemented it).

Try just putting this in the visionWindowOpened event script for your faceplate/popup as a test:

# The following scripting will position the popup nearby the mouse location
# it will use by default a 100 pixel margin to keep the popup on screen
# Developed to be called from a popup window's visionWindowOpened event handler
# Import the necessary Javax Swing classes
from javax.swing import JFrame,SwingUtilities

# Get an unambiguous reference to the 'popupWindow' component from the event source
popupWindow = event.source

# Find the nearest ancestor JFrame of the 'popupWindow' component which will be the parent window that the popup was opened from
parentJFrame = SwingUtilities.getAncestorOfClass(JFrame, popupWindow)

# Set a margin value for positioning the 'popupWindow'
parentJFrameMargin = 100

# Try to get the mouse position relative to the 'parent' JFrame
try:
	popupWindowX = parentJFrame.getMousePosition(True).x
	popupWindowY = parentJFrame.getMousePosition(True).y
# If an exception occurs (e.g., mouse position not available), set default values of (0,0) [Upper left corner] plus the specified margin
except:
	popupWindowX = parentJFrameMargin
	popupWindowY = parentJFrameMargin
	
# Ensure that the 'popupWindow' is within the boundaries of the 'parent' JFrame
if popupWindowX < parentJFrameMargin:
	popupWindowX = parentJFrameMargin
elif popupWindowX + popupWindow.width > parentJFrame.width - parentJFrameMargin:
	popupWindowX = parentJFrame.width - popupWindow.width - parentJFrameMargin
if  popupWindowY < parentJFrameMargin:
	popupWindowY = parentJFrameMargin
elif popupWindowY + popupWindow.height > parentJFrame.height - parentJFrameMargin:
	popupWindowY = parentJFrame.height - popupWindow.height - parentJFrameMargin 

# Set the new location of the 'popupWindow' based on the calculated positions
popupWindow.setLocation(popupWindowX, popupWindowY)

That's not how the script I provided handles it. Putting this in the vision window opened script assumes that you want to move it every time.

That's how I have all my popups. I want them to open every time near the mouse cursor. Saves me from putting it on every button that can trigger the faceplate and now my faceplates/popups can be called with just a regular event by selecting a window and passing my tagPath parameter so visually for anyone less experienced at Ignition they can pop up the window any other way easily without knowing scripting. (While I think if you want to get into Igntion development, you really should learn scripting, I don't always know if my customers will.)

1 Like

Yes that works. It moves the popup inside the window. The only thing is its a little far away from mouse click location but this works for me. The margin in the script is not to for that right?

The margin only comes into effect when the window is close to the bounds of the client window (monitor in fullscreen mode). The margin is the minimum distance that the edge of the popup window must be from the edge of the client window.

By the way I just tried your script and put it in library as per your direction and i changed the margin to 100 and it works also same as Michael's script location. The same issue thou the popup is a bit away from mouse click.

the red dot is where i click the mouse
image

I've deployed this script in multiple projects, and never had an issue with it, so I'm not sure what's going on here, but I doubt the margin parameter is the cause of what's depicted.

When I originally developed this, I found that the popup didn't look good when it appeared smashed up against the edge of the window, so added that margin variable to move the popup away from the edge some arbitrary amount that looked good. The measurement is in pixels, so a value of 100 will prevent edges of the popup from being within 100 pixels of the edges of the window. While increasing that variable could push the popup more to toward the center of the parent window, I doubt it will help much in keeping the popup closer to the mouse.

2 Likes

I think what @Melvin_Miclat was pointing out is that the upper left corner isn't exactly where you click. It is odd and on my system it's about 10 pixels to the right and 20 pixels down from where the cursor is as long as you've clicked something in the middle of the window that won't cause the margins to kick in. It does look like he hit a right margin in his example though that pushed it to the left a little, but it works well for me too.