Search in sources :

Example 21 with ApplicationEx

use of com.intellij.openapi.application.ex.ApplicationEx in project intellij-community by JetBrains.

the class IdeEventQueue method fixStickyFocusedComponents.

public void fixStickyFocusedComponents(@Nullable AWTEvent e) {
    if (e != null && !(e instanceof InputEvent))
        return;
    final KeyboardFocusManager mgr = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    if (Registry.is("actionSystem.fixStickyFocusedWindows")) {
        fixStickyWindow(mgr, mgr.getActiveWindow(), "setGlobalActiveWindow");
        fixStickyWindow(mgr, mgr.getFocusedWindow(), "setGlobalFocusedWindow");
    }
    if (Registry.is("actionSystem.fixNullFocusedComponent")) {
        final Component focusOwner = mgr.getFocusOwner();
        if (focusOwner == null || !focusOwner.isShowing() || focusOwner instanceof JFrame || focusOwner instanceof JDialog) {
            final Application app = ApplicationManager.getApplication();
            if (app instanceof ApplicationEx && !((ApplicationEx) app).isLoaded()) {
                return;
            }
            boolean mouseEventsAhead = isMouseEventAhead(e);
            boolean focusTransferredNow = IdeFocusManager.getGlobalInstance().isFocusBeingTransferred();
            boolean okToFixFocus = !mouseEventsAhead && !focusTransferredNow;
            if (okToFixFocus) {
                Window showingWindow = mgr.getActiveWindow();
                if (showingWindow == null) {
                    Method getNativeFocusOwner = ReflectionUtil.getDeclaredMethod(KeyboardFocusManager.class, "getNativeFocusOwner");
                    if (getNativeFocusOwner != null) {
                        try {
                            Object owner = getNativeFocusOwner.invoke(mgr);
                            if (owner instanceof Component) {
                                showingWindow = UIUtil.getWindow((Component) owner);
                            }
                        } catch (Exception e1) {
                            LOG.debug(e1);
                        }
                    }
                }
                if (showingWindow != null) {
                    final IdeFocusManager fm = IdeFocusManager.findInstanceByComponent(showingWindow);
                    ExpirableRunnable maybeRequestDefaultFocus = new ExpirableRunnable() {

                        @Override
                        public void run() {
                            if (getPopupManager().requestDefaultFocus(false))
                                return;
                            final Application app = ApplicationManager.getApplication();
                            if (app != null && app.isActive()) {
                                fm.requestDefaultFocus(false);
                            }
                        }

                        @Override
                        public boolean isExpired() {
                            return !UIUtil.isMeaninglessFocusOwner(mgr.getFocusOwner());
                        }
                    };
                    fm.revalidateFocus(maybeRequestDefaultFocus);
                }
            }
        }
    }
}
Also used : IdeFocusManager(com.intellij.openapi.wm.IdeFocusManager) Method(java.lang.reflect.Method) ApplicationEx(com.intellij.openapi.application.ex.ApplicationEx) IdeaApplication(com.intellij.idea.IdeaApplication)

Aggregations

ApplicationEx (com.intellij.openapi.application.ex.ApplicationEx)21 NotNull (org.jetbrains.annotations.NotNull)5 IOException (java.io.IOException)4 Application (com.intellij.openapi.application.Application)3 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)2 Project (com.intellij.openapi.project.Project)2 SmartList (com.intellij.util.SmartList)2 THashMap (gnu.trove.THashMap)2 HyperlinkEvent (javax.swing.event.HyperlinkEvent)2 HighlightingPass (com.intellij.codeHighlighting.HighlightingPass)1 TextEditorHighlightingPass (com.intellij.codeHighlighting.TextEditorHighlightingPass)1 AsyncFuture (com.intellij.concurrency.AsyncFuture)1 AsyncUtil (com.intellij.concurrency.AsyncUtil)1 JobLauncher (com.intellij.concurrency.JobLauncher)1 GeneralSettings (com.intellij.ide.GeneralSettings)1 SaveAndSyncHandlerImpl (com.intellij.ide.SaveAndSyncHandlerImpl)1 CachesInvalidator (com.intellij.ide.caches.CachesInvalidator)1 IdeaApplication (com.intellij.idea.IdeaApplication)1 IdeaTestApplication (com.intellij.idea.IdeaTestApplication)1 MockApplication (com.intellij.mock.MockApplication)1