Skip to content

Commit 2700cb6

Browse files
committed
Keep the restored window on-screen
Tested with 2 monitors in extended desktop configuration. The window is considered "on-screen" if any part of the window intersects any of the active display areas, which seems to be the standard way of dealing with this. If the window is off-screen it gets restored to its default size and centered on the active display (unless it was maximised, in which case it remains maximised on the visible display)
1 parent 1738458 commit 2700cb6

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

src/com/modsim/gui/GUI.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,28 @@ public void showUI(boolean arg) {
7575
Preferences prefs = Preferences.userNodeForPackage(GUI.class);
7676
if (prefs.getBoolean("window_stored", false)) {
7777
// Window border has been stored
78-
int window_x, window_y, window_width, window_height;
79-
window_x = prefs.getInt("window_x", 0);
80-
window_y = prefs.getInt("window_y", 0);
81-
window_height = prefs.getInt("window_height", 600);
82-
window_width = prefs.getInt("window_width", 800);
83-
frame.setBounds(window_x, window_y, window_width, window_height);
84-
// Restore maximise state
78+
int windowX, windowY, windowWidth, windowHeight;
79+
windowX = prefs.getInt("window_x", 0);
80+
windowY = prefs.getInt("window_y", 0);
81+
windowHeight = prefs.getInt("window_height", 600);
82+
windowWidth = prefs.getInt("window_width", 800);
83+
// Check the window is within the boundaries of the active display(s)
84+
GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
85+
boolean onDesktop = false;
86+
for (int i = 0; i < devices.length; i++) {
87+
Rectangle displayBounds = devices[i].getDefaultConfiguration().getBounds();
88+
if (displayBounds.intersects(windowX, windowY, windowWidth, windowHeight)) {
89+
onDesktop = true;
90+
}
91+
}
92+
if (onDesktop) {
93+
frame.setBounds(windowX, windowY, windowWidth, windowHeight);
94+
}
95+
else {
96+
frame.pack();
97+
frame.setLocationRelativeTo(null);
98+
}
99+
// Restores the maximise state
85100
if (prefs.getBoolean("window_maximised", false)) {
86101
frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
87102
}

0 commit comments

Comments
 (0)