diff --git a/platform/platform-impl/src/com/intellij/openapi/wm/impl/FocusManagerImpl.java b/platform/platform-impl/src/com/intellij/openapi/wm/impl/FocusManagerImpl.java index 36aeab68f038..ecb65b6a575a 100644 --- a/platform/platform-impl/src/com/intellij/openapi/wm/impl/FocusManagerImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/wm/impl/FocusManagerImpl.java @@ -29,6 +29,7 @@ import com.intellij.openapi.application.ModalityState; import com.intellij.openapi.application.ex.ApplicationManagerEx; import com.intellij.openapi.components.impl.ServiceManagerImpl; import com.intellij.openapi.diagnostic.Logger; +import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.popup.JBPopup; import com.intellij.openapi.util.*; import com.intellij.openapi.util.registry.Registry; @@ -1071,32 +1072,77 @@ public class FocusManagerImpl extends IdeFocusManager implements Disposable { private class AppListener extends ApplicationActivationListener.Adapter { @Override public void applicationActivated(final IdeFrame ideFrame) { + + if (Registry.is("trace.focus.on.app.activation")) { + LOG.info("Application is activating"); + } + final FocusCommand cmd = myFocusCommandOnAppActivation; ActionCallback callback = myCallbackOnActivation; myFocusCommandOnAppActivation = null; myCallbackOnActivation = null; if (cmd != null) { + if (Registry.is("trace.focus.on.app.activation")) { + LOG.info("Use focus command that we have saved on deactivation"); + LOG.info(cmd.getAllocation()); + } requestFocus(cmd, true).notify(callback); } else { - focusLastFocusedComponent(ideFrame); + Component lastFocusedComponent = focusLastFocusedComponent(ideFrame); + if (Registry.is("trace.focus.on.app.activation")) { + final Project project = ideFrame.getProject(); + if (project != null) { + LOG.info("Use last focused component because we do not have a focus command"); + LOG.info("Component in [" + project.getName() + "]: " + (lastFocusedComponent != null ? lastFocusedComponent.getClass().getName() : null)); + } + } } } @Override public void applicationDeactivated(IdeFrame ideFrame) { + + if (Registry.is("trace.focus.on.app.activation")) { + LOG.info("Application is deactivating"); + } + final Component owner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); Component parent = UIUtil.findUltimateParent(owner); if (parent == ideFrame) { myLastFocusedAtDeactivation.put(ideFrame, owner); + if (Registry.is("trace.focus.on.app.activation")) { + final Project project = ideFrame.getProject(); + String projectName = null; + if (project != null) { + projectName = project.getName(); + } + LOG.info("The next component is set as ths last focused for [" + projectName +"]"); + } + } else { + if (Registry.is("trace.focus.on.app.activation")) { + if (owner != null) { + final Project project = ideFrame.getProject(); + String projectName = null; + if (project != null) { + projectName = project.getName(); + } + LOG.error("The last focused component " + + (owner != null ? owner.getClass().getName() : null) + + " is not in the deactivating frame [" + projectName + "]. Skip it."); + } else { + LOG.info("The last focused component is null"); + } + } } } - private void focusLastFocusedComponent(IdeFrame ideFrame) { + private Component focusLastFocusedComponent(IdeFrame ideFrame) { final KeyboardFocusManager mgr = KeyboardFocusManager.getCurrentKeyboardFocusManager(); + Component c = null; if (mgr.getFocusOwner() == null) { - Component c = getComponent(myLastFocusedAtDeactivation, ideFrame); + c = getComponent(myLastFocusedAtDeactivation, ideFrame); if (c == null || !c.isShowing()) { c = getComponent(myLastFocusedAtDeactivation, ideFrame); } @@ -1119,6 +1165,7 @@ public class FocusManagerImpl extends IdeFocusManager implements Disposable { } myLastFocusedAtDeactivation.remove(ideFrame); + return c; } } diff --git a/platform/util/resources/misc/registry.properties b/platform/util/resources/misc/registry.properties index e68c86255fc1..199ee3e4b330 100644 --- a/platform/util/resources/misc/registry.properties +++ b/platform/util/resources/misc/registry.properties @@ -658,4 +658,8 @@ ide.mac.new.color.picker=false trace.clipboard.events=false trace.clipboard.events.description=Enable detection and logging of OS notifications about external clipboard changes -trace.clipboard.events.restartRequired=true \ No newline at end of file +trace.clipboard.events.restartRequired=true + +trace.focus.on.app.activation=false +trace.focus.on.app.activation.description=Focus tracing on application activation and deactivation +trace.focus.on.app.restartRequired=false