Ghidra is a software reverse engineering (SRE) framework
GP-5932 - Updated off-screen window handling
dragonmacher3fb9ede4625b
diff --git a/Ghidra/Framework/Docking/src/main/java/docking/widgets/tab/TabListPopup.java b/Ghidra/Framework/Docking/src/main/java/docking/widgets/tab/TabListPopup.javaindex ebf3e4b058..cca35cd7e0 100644--- a/Ghidra/Framework/Docking/src/main/java/docking/widgets/tab/TabListPopup.java+++ b/Ghidra/Framework/Docking/src/main/java/docking/widgets/tab/TabListPopup.java@@ -4,9 +4,9 @@* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at- *+ ** http://www.apache.org/licenses/LICENSE-2.0- *+ ** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.@@ -113,7 +113,7 @@ public class TabListPopup<T> extends JDialog {Point centerPoint = WindowUtilities.centerOnComponent(getParent(), this);bounds.setLocation(centerPoint);- WindowUtilities.ensureEntirelyOnScreen(getParent(), bounds);+ WindowUtilities.ensureEntirelyOnScreen(this, bounds);setBounds(bounds);return;}@@ -125,7 +125,7 @@ public class TabListPopup<T> extends JDialog {bounds.setLocation(x, y);// fixes problem where popup gets clipped when going across screens- WindowUtilities.ensureOnScreen(component, bounds);+ WindowUtilities.ensureEntirelyOnScreen(this, bounds);setBounds(bounds);}diff --git a/Ghidra/Framework/Generic/src/main/java/generic/util/WindowUtilities.java b/Ghidra/Framework/Generic/src/main/java/generic/util/WindowUtilities.javaindex 19ecb20210..9f47123a69 100644--- a/Ghidra/Framework/Generic/src/main/java/generic/util/WindowUtilities.java+++ b/Ghidra/Framework/Generic/src/main/java/generic/util/WindowUtilities.java@@ -200,7 +200,7 @@ public class WindowUtilities {private static Point center(Rectangle area, Dimension d) {- // restrict to bounds size+ // restrict to the smallest sizeRectangle b = area;int userWidth = Math.min(b.width, d.width);int userHeigh = Math.min(b.height, d.height);@@ -288,18 +288,30 @@ public class WindowUtilities {*/public static void ensureOnScreen(Component c, Rectangle bounds) {- Shape visibleScreenBounds = getVisibleScreenBounds();- if (visibleScreenBounds.intersects(bounds)) {- return; // some portion of the window is visible+ Shape visibleScreenShape = getVisibleScreenBounds();+ if (visibleScreenShape.contains(bounds)) {+ return; // the given shape is completely on the screen}- Rectangle screen = getScreenBounds(c);- if (screen == null) {- throw new IllegalArgumentException("Component is not on screen: " + c);+ if (!visibleScreenShape.intersects(bounds)) {+ // The bounds are completely off-screen+ Rectangle screen = getScreenBounds(c);+ if (screen == null) {+ throw new IllegalArgumentException("Component is not on screen: " + c);+ }++ Point newPoint = center(screen, bounds.getSize());+ bounds.setLocation(newPoint);+ return; // some portion of the window is visible}- Point newPoint = center(screen, bounds.getSize());- bounds.setLocation(newPoint);+ // The given bounds are partially on-screen. We only wish to move the bounds enough to get+ // the window bar on screen. This allows users to grab and move the window.+ Rectangle vsb = visibleScreenShape.getBounds();+ if (bounds.y < vsb.y) {+ // above the top+ bounds.y = vsb.y + 30; // move enough to clear any task bar+ }}/**@@ -316,8 +328,8 @@ public class WindowUtilities {*/public static void ensureEntirelyOnScreen(Component c, Rectangle bounds) {- Shape visibleScreenBounds = getVisibleScreenBounds();- if (visibleScreenBounds.contains(bounds)) {+ Shape visibleScreenShape = getVisibleScreenBounds();+ if (visibleScreenShape.contains(bounds)) {return; // the given shape is completely on the screen}@@ -326,8 +338,49 @@ public class WindowUtilities {throw new IllegalArgumentException("Component is not on screen: " + c);}- Point newPoint = center(screen, bounds.getSize());- bounds.setLocation(newPoint);+ /*+ Some things we wish to avoid:+ - resizing the bounds when it is close to the screen size (on some OSes, full-screen+ windows will fail the contains() test)+ - moving a partially off-screen window to the center of the screen (this is jarring+ for users with multiple windows that are considered one big screen)+ */++ //+ // First make sure the window will fit on the screen.+ // 'pad' is for checking to avoid full-size windows with odd OS borders. This is an+ // arbitrary number that is larger than the 'move' amount below.+ int pad = 50;+ int screenArea = screen.width * screen.height;+ int boundsArea = bounds.width * bounds.height;+ if (boundsArea > screenArea) {+ int newWidth = screen.width - pad;+ int newHeight = screen.height - pad;+ bounds.setSize(newWidth, newHeight);+ }++ // Next, move the window as little as possible to get fully on-screen.+ // 'move' is the amount to move the window in the x and y direction when the window is+ // off-screen. This is an arbitrary number that is smaller than 'pad' above and is based on+ // OS features, like the task bar.+ int move = 30;+ if (bounds.y < screen.y) {+ // above the top+ bounds.y = screen.y + move;+ }+ else if (bounds.y + bounds.height > screen.y + screen.height) {+ // below the bottom+ bounds.y = (screen.y + screen.height) - (bounds.height + move);+ }++ if (bounds.x < screen.x) {+ // to the left+ bounds.x = screen.x + move;+ }+ else if (bounds.x + bounds.width > screen.x + screen.width) {+ // to the right+ bounds.x = (screen.x + screen.width) - (bounds.width + move);+ }}private static ScreenBounds doGetScreenBounds(Point p) {