From 84b8c1f7213cbbcd0dd6e368dab3b7826e3b38f8 Mon Sep 17 00:00:00 2001
From: Eike Ziller <eike.ziller@digia.com>
Date: Mon, 24 Jun 2013 16:25:25 +0200
Subject: [PATCH] Adapt the escape key logic for external windows.

Change-Id: I294ba18e8ca91060e5d0ef8f81885642e045a975
Reviewed-by: David Schulz <david.schulz@digia.com>
---
 .../editormanager/editormanager.cpp           | 97 +++++++++++++++++++
 .../coreplugin/editormanager/editormanager.h  |  1 +
 src/plugins/coreplugin/mainwindow.cpp         | 45 +--------
 3 files changed, 99 insertions(+), 44 deletions(-)

diff --git a/src/plugins/coreplugin/editormanager/editormanager.cpp b/src/plugins/coreplugin/editormanager/editormanager.cpp
index b1c729c49b3..56a5131be6f 100644
--- a/src/plugins/coreplugin/editormanager/editormanager.cpp
+++ b/src/plugins/coreplugin/editormanager/editormanager.cpp
@@ -29,10 +29,14 @@
 
 #include "editormanager.h"
 #include "editorview.h"
+#include "findplaceholder.h"
 #include "openeditorswindow.h"
 #include "openeditorsview.h"
 #include "openeditorsmodel.h"
 #include "openwithdialog.h"
+#include "outputpane.h"
+#include "outputpanemanager.h"
+#include "rightpane.h"
 #include "documentmanager.h"
 #include "icore.h"
 #include "ieditor.h"
@@ -852,6 +856,99 @@ void EditorManager::addNativeDirActions(QMenu *contextMenu, const QModelIndex &e
     contextMenu->addAction(d->m_openTerminalAction);
 }
 
+static void setFocusToEditorViewAndUnmaximizePanes(EditorView *view)
+{
+    QWidget *w = 0;
+    if (view->currentEditor()) {
+        w = view->currentEditor()->widget()->focusWidget();
+        if (!w)
+            w = view->currentEditor()->widget();
+    } else {
+        w = view->focusWidget();
+        if (!w)
+            w = view;
+    }
+    w->setFocus();
+    ICore::raiseWindow(w);
+    if (OutputPanePlaceHolder::getCurrent()
+            && OutputPanePlaceHolder::getCurrent()->window() == view->window()) {
+        // unmaximize output pane if necessary
+        if (OutputPanePlaceHolder::getCurrent()
+                && OutputPanePlaceHolder::getCurrent()->isVisible()
+                && OutputPanePlaceHolder::getCurrent()->isMaximized())
+            OutputPanePlaceHolder::getCurrent()->unmaximize();
+    }
+}
+
+/*!
+    Implements the logic of the escape key shortcut (ReturnToEditor).
+    Should only be called by the shortcut handler.
+    \internal
+*/
+void EditorManager::doEscapeKeyFocusMoveMagic()
+{
+    // use cases to cover:
+    // 1. if app focus is in mode or external window without editor view (e.g. Projects, ext. Help)
+    //      activate & raise the current editor view (can be external)
+    //      if that is in edit mode
+    //        activate edit mode and unmaximize output pane
+    // 2. if app focus is in external window with editor view
+    //      hide find if necessary
+    // 2. if app focus is in mode with editor view
+    //      if current editor view is in external window
+    //        raise and activate current editor view
+    //      otherwise if the current editor view is not app focus
+    //        move focus to editor view in mode and unmaximize output pane
+    //      otherwise if the current view is app focus
+    //        if mode is not edit mode
+    //          if there are extra views (find, help, output)
+    //            hide them
+    //          otherwise
+    //            activate edit mode and unmaximize output pane
+    //        otherwise (i.e. mode is edit mode)
+    //          hide extra views (find, help, output)
+
+    EditorView *editorView = currentEditorView();
+    bool editorViewActive = (qApp->focusWidget() == editorView->focusWidget());
+    bool editorViewVisible = editorView->isVisible();
+    if (!editorViewActive && editorViewVisible) {
+        setFocusToEditorViewAndUnmaximizePanes(editorView);
+        return;
+    }
+    if (!editorViewActive && !editorViewVisible) {
+        // assumption is that editorView is in main window then
+        ModeManager::activateMode(Id(Constants::MODE_EDIT));
+        QTC_CHECK(editorView->isVisible());
+        setFocusToEditorViewAndUnmaximizePanes(editorView);
+        return;
+    }
+    if (editorViewActive) {
+        QTC_CHECK(editorViewVisible);
+        bool stuffHidden = false;
+        QWidget *findPane = FindToolBarPlaceHolder::getCurrent();
+        if (findPane && findPane->isVisibleTo(editorView)) {
+            findPane->hide();
+            stuffHidden = true;
+        }
+        QWidget *outputPane = OutputPanePlaceHolder::getCurrent();
+        if (outputPane && outputPane->isVisibleTo(editorView)) {
+            OutputPaneManager::instance()->slotHide();
+            stuffHidden = true;
+        }
+        QWidget *rightPane = RightPanePlaceHolder::current();
+        if (rightPane && rightPane->isVisibleTo(editorView)) {
+            RightPaneWidget::instance()->setShown(false);
+            stuffHidden = true;
+        }
+        if (!stuffHidden && editorView->window() == ICore::mainWindow()) {
+            // we are in a editor view and there's nothing to hide, switch to edit
+            ModeManager::activateMode(Id(Constants::MODE_EDIT));
+            // next call works only because editor views in main window are shared between modes
+            setFocusToEditorViewAndUnmaximizePanes(editorView);
+        }
+    }
+}
+
 void EditorManager::saveDocumentFromContextMenu()
 {
     IEditor *editor = d->m_contextMenuEditorIndex.data(Qt::UserRole).value<Core::IEditor*>();
diff --git a/src/plugins/coreplugin/editormanager/editormanager.h b/src/plugins/coreplugin/editormanager/editormanager.h
index 38cb29cf888..556dfc65aa8 100644
--- a/src/plugins/coreplugin/editormanager/editormanager.h
+++ b/src/plugins/coreplugin/editormanager/editormanager.h
@@ -210,6 +210,7 @@ public slots:
     void revertToSaved(Core::IEditor *editor);
     void closeEditor();
     void closeOtherEditors();
+    void doEscapeKeyFocusMoveMagic();
 
 private slots:
     void gotoNextDocHistory();
diff --git a/src/plugins/coreplugin/mainwindow.cpp b/src/plugins/coreplugin/mainwindow.cpp
index 6ae0d92318d..80fd6013cda 100644
--- a/src/plugins/coreplugin/mainwindow.cpp
+++ b/src/plugins/coreplugin/mainwindow.cpp
@@ -48,7 +48,6 @@
 #include "mimedatabase.h"
 #include "newdialog.h"
 #include "outputpanemanager.h"
-#include "outputpane.h"
 #include "plugindialog.h"
 #include "progressmanager_p.h"
 #include "progressview.h"
@@ -73,7 +72,6 @@
 #endif
 
 #include <app/app_version.h>
-#include <coreplugin/findplaceholder.h>
 #include <coreplugin/icorelistener.h>
 #include <coreplugin/inavigationwidgetfactory.h>
 #include <coreplugin/settingsdatabase.h>
@@ -877,48 +875,7 @@ IDocument *MainWindow::openFiles(const QStringList &fileNames, ICore::OpenFilesF
 
 void MainWindow::setFocusToEditor()
 {
-    bool focusWasMovedToEditor = false;
-
-    // give focus to the editor if we have one
-    if (IEditor *editor = EditorManager::currentEditor()) {
-        if (qApp->focusWidget() != editor->widget()->focusWidget()) {
-            QWidget *w = editor->widget()->focusWidget();
-            if (!w)
-                w = editor->widget();
-            w->setFocus();
-            focusWasMovedToEditor = w->hasFocus();
-        }
-    }
-
-    // check for some maximized pane which we want to unmaximize
-    if (OutputPanePlaceHolder::getCurrent()
-        && OutputPanePlaceHolder::getCurrent()->isVisible()
-        && OutputPanePlaceHolder::getCurrent()->isMaximized()) {
-        OutputPanePlaceHolder::getCurrent()->unmaximize();
-        return;
-    }
-
-    if (focusWasMovedToEditor)
-        return;
-
-    // check for some visible bar which we want to hide
-    bool stuffVisible =
-            (FindToolBarPlaceHolder::getCurrent() &&
-             FindToolBarPlaceHolder::getCurrent()->isVisible())
-            || (OutputPanePlaceHolder::getCurrent() &&
-                OutputPanePlaceHolder::getCurrent()->isVisible())
-            || (RightPanePlaceHolder::current() &&
-                RightPanePlaceHolder::current()->isVisible());
-    if (stuffVisible) {
-        if (FindToolBarPlaceHolder::getCurrent())
-            FindToolBarPlaceHolder::getCurrent()->hide();
-        OutputPaneManager::instance()->slotHide();
-        RightPaneWidget::instance()->setShown(false);
-        return;
-    }
-
-    // switch to edit mode if necessary
-    ModeManager::activateMode(Id(Constants::MODE_EDIT));
+    m_editorManager->doEscapeKeyFocusMoveMagic();
 }
 
 void MainWindow::showNewItemDialog(const QString &title,
-- 
GitLab