Ghidra is a software reverse engineering (SRE) framework
Parent dialogs to the tool's last active window when no window is active
Leon Zandman33572e454aad
diff --git a/Ghidra/Framework/Docking/src/main/java/docking/DockingWindowManager.java b/Ghidra/Framework/Docking/src/main/java/docking/DockingWindowManager.javaindex 0bfc2d2e00..238afd7df5 100644--- a/Ghidra/Framework/Docking/src/main/java/docking/DockingWindowManager.java+++ b/Ghidra/Framework/Docking/src/main/java/docking/DockingWindowManager.java@@ -2030,6 +2030,12 @@ public class DockingWindowManager implements PropertyChangeListener, PlaceholderE -A long-running API shows a non-modal progress dialog. This API then shows aresults dialog which is also non-modal. We do not want to parent the new dialogto 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 shown+ before 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.For now, the easiest mental model to use is to always prefer the active non-transient@@ -2042,9 +2048,20 @@ 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@@ -2056,7 +2073,7 @@ public class DockingWindowManager implements PropertyChangeListener, Placeholder}c = c.getParent();}- return defaultWindow;+ return null;}private static boolean isNonTransientWindow(Component c) {diff --git a/Ghidra/Framework/Docking/src/test.slow/java/docking/DialogParentFallbackTest.java b/Ghidra/Framework/Docking/src/test.slow/java/docking/DialogParentFallbackTest.javanew file mode 100644index 0000000000..605542f46b--- /dev/null+++ b/Ghidra/Framework/Docking/src/test.slow/java/docking/DialogParentFallbackTest.java@@ -0,0 +1,183 @@+/* ###+ * IP: GHIDRA+ *+ * 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.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package docking;++import static org.junit.Assert.*;++import java.awt.*;++import javax.swing.*;++import org.junit.*;++import docking.test.AbstractDockingTest;+import generic.test.TestUtils;++/**+ * Tests which window becomes the parent of a dialog when Java reports no active window. Java's+ * active window is controlled by a focus manager installed by this test, and the tool's last+ * active window is set directly, so that the tests do not depend on the OS window focus.+ */+public class DialogParentFallbackTest extends AbstractDockingTest {++ private FakeDockingTool tool;+ private Window fakeActiveWindow;++ @Before+ public void setUp() {+ tool = new FakeDockingTool();+ runSwing(() -> tool.setVisible(true));+ waitForSwing();+ }++ @After+ public void tearDown() {+ closeAllWindows();+ }++ @Test+ public void testNoActiveWindow_UsesLastActiveWindow() {+ Window detachedWindow = showDetachedProvider(true);+ setLastActiveWindow(detachedWindow);+ fakeActiveWindow = null;++ Window owner = showDialogAndGetOwner();+ assertSame(detachedWindow, owner);+ }++ @Test+ public void testNoActiveWindow_LastActiveIsClosedModalDialog_UsesItsOwner() {+ Window detachedWindow = showDetachedProvider(true);+ JDialog closedDialog = runSwing(() -> new JDialog(detachedWindow, "Closed Dialog",+ Dialog.ModalityType.APPLICATION_MODAL));+ setLastActiveWindow(closedDialog);+ fakeActiveWindow = null;++ Window owner = showDialogAndGetOwner();+ assertSame(detachedWindow, owner);+ }++ @Test+ public void testNoActiveWindow_NoLastActiveWindow_UsesToolFrame() {+ setLastActiveWindow(null);+ fakeActiveWindow = null;++ Window owner = showDialogAndGetOwner();+ assertSame(tool.getToolFrame(), owner);+ }++ @Test+ public void testActiveWindow_PreferredOverLastActiveWindow() {+ Window detachedWindow = showDetachedProvider(true);+ setLastActiveWindow(detachedWindow);+ fakeActiveWindow = tool.getToolFrame();++ Window owner = showDialogAndGetOwner();+ assertSame(tool.getToolFrame(), owner);+ }++ @Test+ public void testNoActiveWindow_LastActiveWindowCannotBeParent_UsesToolFrame() {+ Window detachedWindow = showDetachedProvider(false);+ setLastActiveWindow(detachedWindow);+ fakeActiveWindow = null;++ Window owner = showDialogAndGetOwner();+ assertSame(tool.getToolFrame(), owner);+ }++ private Window showDetachedProvider(boolean canBeParent) {+ DetachedProvider provider = new DetachedProvider(canBeParent);+ runSwing(() -> tool.addComponentProvider(provider, true));+ waitForSwing();+ Window window = runSwing(() -> tool.getWindowManager().getProviderWindow(provider));+ assertNotNull(window);+ assertNotSame(tool.getToolFrame(), window);+ return window;+ }++ private void setLastActiveWindow(Window window) {+ runSwing(() -> TestUtils.setInstanceField("lastActiveWindow", tool.getWindowManager(),+ window));+ }++ private Window showDialogAndGetOwner() {++ // Replace the focus manager only while the dialog is open, since other tests rely on the+ // state of the real focus manager+ KeyboardFocusManager originalFocusManager =+ KeyboardFocusManager.getCurrentKeyboardFocusManager();+ KeyboardFocusManager.setCurrentKeyboardFocusManager(+ new FakeActiveWindowFocusManager(fakeActiveWindow));+ try {+ TestDialog dialog = new TestDialog();+ executeOnSwingWithoutBlocking(() -> DockingWindowManager.showDialog(dialog));+ waitForDialogComponent(TestDialog.class);+ Window owner =+ runSwing(() -> SwingUtilities.getWindowAncestor(dialog.getComponent()).getOwner());+ runSwing(() -> dialog.close());+ waitForSwing();+ return owner;+ }+ finally {+ KeyboardFocusManager.setCurrentKeyboardFocusManager(originalFocusManager);+ waitForSwing();+ }+ }++ private class DetachedProvider extends ComponentProvider {+ private JPanel panel = new JPanel();+ private boolean canBeParent;++ DetachedProvider(boolean canBeParent) {+ super(tool, "Detached Provider", "Test Owner");+ this.canBeParent = canBeParent;+ panel.setPreferredSize(new Dimension(200, 200));+ setDefaultWindowPosition(WindowPosition.WINDOW);+ }++ @Override+ public JComponent getComponent() {+ return panel;+ }++ @Override+ public boolean canBeParent() {+ return canBeParent;+ }+ }++ private static class TestDialog extends DialogComponentProvider {+ TestDialog() {+ super("Test Dialog", true);+ addWorkPanel(new JPanel());+ addCancelButton();+ }+ }++ private static class FakeActiveWindowFocusManager extends DefaultKeyboardFocusManager {+ private Window activeWindow;++ FakeActiveWindowFocusManager(Window activeWindow) {+ this.activeWindow = activeWindow;+ }++ @Override+ public Window getActiveWindow() {+ return activeWindow;+ }+ }+}