Drag Popup Outside of Project

Since "this unfinished post" was mine, I figured I would go ahead and finish it. Here's a version that will map the screens according to the way they appear in the graphics environment instead of the way windows arbitrarily decides to index them. It will move the primary desktop to the top left screen and then open a new desktop in each additional screen from left to right and top to bottom:

# Import:
# ...GraphicsEnvironment for mapping the screens as they are in reality
# ...JFrame and SwingUtilities for manipulating the primary desktop into the upper left screen
from java.awt import GraphicsEnvironment
from javax.swing import JFrame, SwingUtilities

# Get the screen bounds from the graphics environment and sort them from right to left, top to bottom
# reference = https://stackoverflow.com/questions/4233476/sort-a-list-by-multiple-attributes
screens = sorted([device.defaultConfiguration.bounds for device in GraphicsEnvironment.getLocalGraphicsEnvironment().screenDevices], key=lambda screen: (screen.x, screen.y))

# Get the JFrame of the primary open window
primaryDesktop = SwingUtilities.getAncestorOfClass(JFrame, system.gui.getOpenedWindows()[0])

# Iterate through each screen, putting the primary in the top left, 
# ...and opening a new desktop in each of the other screens,
for index, screen in enumerate(screens):
	if index == 0: # This will be the upper left screen
		primaryDesktop.setLocation(screen.x, screen.y)
		primaryDesktop.setSize(screen.width, screen.height)
	else:
		# Desktop {}".format(index + 1) = Each desktop opened after the primary desktop, 
		# will be assigned incrementally named handles "Desktop 2", "Desktop 3", etc
		# By not specifying a screen index in the openDesktop call,
		# ...openDesktop will automatically map the screens according to the graphics environment coordinates instead of specific screen coordinates
		system.gui.openDesktop(handle = "Desktop {}".format(index + 1), x = screen.x, y = screen.y, width = screen.width, height = screen.height)
		system.nav.desktop("Desktop {}".format(index)).swapTo('Default_Window_Path')
3 Likes