Ghidra is a software reverse engineering (SRE) framework
Fall back to the last active window in getJavaActiveWindow()
Leon Zandmane154a888709d
diff --git a/Ghidra/Framework/Docking/src/main/java/docking/DockingWindowManager.java b/Ghidra/Framework/Docking/src/main/java/docking/DockingWindowManager.javaindex 238afd7df5..4a3e878d9e 100644--- a/Ghidra/Framework/Docking/src/main/java/docking/DockingWindowManager.java+++ b/Ghidra/Framework/Docking/src/main/java/docking/DockingWindowManager.java@@ -2032,10 +2032,11 @@ public class DockingWindowManager implements PropertyChangeListener, Placeholderto the original dialog, since it is a progress dialog that will go away.F -The user acts in a detached window, a dialog closes, and a new dialog is shownbefore the OS has made the detached window active again, so Java reports no- active window. The new dialog should use the window the user last worked in,- not the main tool frame, or that frame comes to the front when the dialog- closes. This applies to case 4 as well: with no active window, a background- thread's dialog also uses the last active window.+ active window, or still reports the closed dialog. The new dialog should use+ the window the user last worked in, not the main tool frame, or that frame+ comes to the front when the dialog closes. This applies to case 4 as well:+ with no showing active window, a background thread's dialog also uses the last+ active window.For now, the easiest mental model to use is to always prefer the active non-transient@@ -2048,20 +2049,9 @@ public class DockingWindowManager implements PropertyChangeListener, PlaceholderWindow defaultWindow = dwm != null ? dwm.getRootFrame() : null;if (parent == null) {Window w = getActiveNonTransientWindow();- if (w == null && dwm != null) {- // Java reports no active window, for example while the OS is still activating a- // window after another one closed. Use the window the user last worked in. (See- // Use Case 'F' above.)- w = getNonTransientWindowFor(dwm.lastActiveWindow);- }return w == null ? defaultWindow : w;}- Window w = getNonTransientWindowFor(parent);- return w == null ? defaultWindow : w;- }-- private static Window getNonTransientWindowFor(Component parent) {Component c = parent;while (c != null) {// Note: using a Frame here means that we will not find and use a dialog that is a@@ -2073,7 +2063,7 @@ public class DockingWindowManager implements PropertyChangeListener, Placeholder}c = c.getParent();}- return null;+ return defaultWindow;}private static boolean isNonTransientWindow(Component c) {@@ -2131,13 +2121,24 @@ public class DockingWindowManager implements PropertyChangeListener, Placeholderprivate static Window getJavaActiveWindow() {KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();Window activeWindow = kfm.getActiveWindow();- if (activeWindow == null) {+ if (activeWindow != null && activeWindow.isShowing()) {+ return activeWindow;+ }++ // Don't let non-showing windows be considered active. There may be no active window, or+ // a window that has just closed, when a dialog is shown before Java has finished updating+ // the KeyboardFocusManager's active window. (See Use Case 'F' in getParentWindow().)+ DockingWindowManager dwm = getActiveInstance();+ if (dwm == null) {return null;}- if (!activeWindow.isShowing()) {- return null; // don't let non-showing windows be considered active++ // the last active window may be a dialog that has since closed; use its owner+ Window w = dwm.lastActiveWindow;+ while (w != null && !w.isShowing()) {+ w = w.getOwner();}- return activeWindow;+ return w;}private static Window getActiveNonTransientWindow() {diff --git a/Ghidra/Framework/Docking/src/test.slow/java/docking/DialogParentFallbackTest.java b/Ghidra/Framework/Docking/src/test.slow/java/docking/DialogParentFallbackTest.javaindex 605542f46b..63bb54ea0e 100644--- a/Ghidra/Framework/Docking/src/test.slow/java/docking/DialogParentFallbackTest.java+++ b/Ghidra/Framework/Docking/src/test.slow/java/docking/DialogParentFallbackTest.java@@ -18,6 +18,7 @@ package docking;import static org.junit.Assert.*;import java.awt.*;+import java.util.function.Function;import javax.swing.*;@@ -70,6 +71,32 @@ public class DialogParentFallbackTest extends AbstractDockingTest {assertSame(detachedWindow, owner);}+ @Test+ public void testNoActiveWindow_LastActiveIsClosedModalDialog_CentersOnItsOwner() {+ Window detachedWindow = showDetachedProvider(true);+ runSwing(() -> detachedWindow.setBounds(40, 60, 500, 400));+ JDialog closedDialog = runSwing(() -> new JDialog(detachedWindow, "Closed Dialog",+ Dialog.ModalityType.APPLICATION_MODAL));+ setLastActiveWindow(closedDialog);+ fakeActiveWindow = null;++ Rectangle dialogBounds = showDialogAndGetBounds();+ Rectangle ownerBounds = runSwing(() -> detachedWindow.getBounds());+ assertEquals(ownerBounds.getCenterX(), dialogBounds.getCenterX(), 1);+ assertEquals(ownerBounds.getCenterY(), dialogBounds.getCenterY(), 1);+ }++ @Test+ public void testActiveWindowIsClosedDialog_UsesLastActiveWindow() {+ Window detachedWindow = showDetachedProvider(true);+ setLastActiveWindow(detachedWindow);+ fakeActiveWindow = runSwing(() -> new JDialog(detachedWindow, "Closed Dialog",+ Dialog.ModalityType.APPLICATION_MODAL));++ Window owner = showDialogAndGetOwner();+ assertSame(detachedWindow, owner);+ }+@Testpublic void testNoActiveWindow_NoLastActiveWindow_UsesToolFrame() {setLastActiveWindow(null);@@ -115,6 +142,14 @@ public class DialogParentFallbackTest extends AbstractDockingTest {}private Window showDialogAndGetOwner() {+ return showDialogAndGet(Window::getOwner);+ }++ private Rectangle showDialogAndGetBounds() {+ return showDialogAndGet(Window::getBounds);+ }++ private <T> T showDialogAndGet(Function<Window, T> getter) {// Replace the focus manager only while the dialog is open, since other tests rely on the// state of the real focus manager@@ -126,11 +161,11 @@ public class DialogParentFallbackTest extends AbstractDockingTest {TestDialog dialog = new TestDialog();executeOnSwingWithoutBlocking(() -> DockingWindowManager.showDialog(dialog));waitForDialogComponent(TestDialog.class);- Window owner =- runSwing(() -> SwingUtilities.getWindowAncestor(dialog.getComponent()).getOwner());+ Window window = runSwing(() -> SwingUtilities.getWindowAncestor(dialog.getComponent()));+ T value = runSwing(() -> getter.apply(window));runSwing(() -> dialog.close());waitForSwing();- return owner;+ return value;}finally {KeyboardFocusManager.setCurrentKeyboardFocusManager(originalFocusManager);