Drag popup from click initiated anywhere in the window

Hello out there in ignition land! :astonished: I have a question about moving windows…
I would like the users to have the ability to click anywhere on a popup window and drag the window to wherever they like. The behavior I am trying to imitate is that of the designer login window (attached). In this window you can click (pretty much) anywhere and drag the window around.
I have messed with the mouseMotion.mouseDragged event a bit…I figure you will have to evaluate the initial position of the window (not so sure here) and of the mouse (system.gui.convertPointToScreen, i think?) and continuously update them using the event mentioned. I’m not sure I’m going down the right track or not…
Am I approaching this correctly? Is there a function built-in that I’m unaware of (I did scour the manual for a while)? Is this designer login window powered by voodoo magic?
Any help is much appreciated!

This should work.

Add the following custom properties on the popup.
mX (Integer)
mY (Integer)

On the mousePressed event of the popup, enter the following:

event.source.mX = event.x event.source.mY = event.y

On the mouseDragged event of the popup, enter the following:

[code]
window = system.gui.getParentWindow(event)
mouseX = event.x
mouseY = event.y
windowX = window.getX()
windowY = window.getY()

window.setLocation(mouseX+windowX-event.source.mX,mouseY+windowY-event.source.mY)[/code]

13 Likes

Finally got a chance to try this and it works like a charm. Thanks a bunch jaepark! :prayer:

Hi, @jaepark it works great. How can I limit the dragging to the edge of my project area.

2 Likes

@jaepark I also would like to know how to set the “Bounds” of the window, so it does not go off the screen when dragging. I setup a custom header on all of my windows and use the script in the header. My close button is also in my header. I’m trying to get the same result that you would expect when dragging a titled window around. It usually stops at the edges of the project window.

Thanks.

@chris_d_sanders, this should do what you’re looking for:

mousePressed event script:

from javax.swing import JFrame, SwingUtilities

# Get window size.
window = system.gui.getParentWindow(event)
windowW = window.size.width
windowH = window.size.height

# Get application frame content pane size.
frame = SwingUtilities.getAncestorOfClass(JFrame, window)
frameW = frame.contentPane.size.width
frameH = frame.contentPane.size.height

# Set max positions to keep window within content pane.
event.source.putClientProperty('maxX', int(frameW - windowW))
event.source.putClientProperty('maxY', int(frameH - windowH))

# Set drag start location.
event.source.putClientProperty('dragStartX', event.x)
event.source.putClientProperty('dragStartY', event.y)

mouseDragged event script:

# Get values set when mouse pressed.
startX = event.source.getClientProperty('dragStartX')
startY = event.source.getClientProperty('dragStartY')
maxX = event.source.getClientProperty('maxX')
maxY = event.source.getClientProperty('maxY')

# Get current values.
mouseX = event.x
mouseY = event.y
window = system.gui.getParentWindow(event)
windowX = window.x
windowY = window.y

# Move window with drag, keeping it within content pane.
moveToX = min(max(mouseX + windowX - startX, 0), maxX)
moveToY = min(max(mouseY + windowY - startY, 0), maxY)
window.setLocation(moveToX, moveToY)
3 Likes