From a5026d7a2fc15494cbcf660a2dcd27a8b52a6af6 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Mon, 28 Sep 2020 10:44:09 +0200 Subject: [PATCH] Project Tree: Delay syncing with editor when using the context menu When "Synchronize with Editor" is enabled, every focus change in the application causes the current node in the project tree to be reset to the one corresponding to the current document. In order to still be able to use the context menu, there is a flag in the project tree suppressing this behavior for as long as the context menu is open. However, some actions offered by the context menu open an additional dialog, and it is confusing to users if the current node changes at this point. We therefore extend abovementioned hack so that the suppression of automatic syncing is extended for the lifetime of the function called from the context menu. Fixes: QTCREATORBUG-24699 Change-Id: I209150aee76e534a966efc4d0afe5261d6dcd521 Reviewed-by: hjk --- .../projectexplorer/projectexplorer.cpp | 7 +++++++ src/plugins/projectexplorer/projecttree.cpp | 18 +++++++++++++++++- src/plugins/projectexplorer/projecttree.h | 9 +++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp index 72205a015b8..1ed8c7c37d5 100644 --- a/src/plugins/projectexplorer/projectexplorer.cpp +++ b/src/plugins/projectexplorer/projectexplorer.cpp @@ -1712,6 +1712,7 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er connect(dd->m_filePropertiesAction, &QAction::triggered, this, []() { const Node *currentNode = ProjectTree::currentNode(); QTC_ASSERT(currentNode && currentNode->asFileNode(), return); + ProjectTree::CurrentNodeKeeper nodeKeeper; DocumentManager::showFilePropertiesDialog(currentNode->filePath()); }); connect(dd->m_removeFileAction, &QAction::triggered, @@ -3618,6 +3619,8 @@ void ProjectExplorerPluginPrivate::removeFile() const Node *currentNode = ProjectTree::currentNode(); QTC_ASSERT(currentNode && currentNode->asFileNode(), return); + ProjectTree::CurrentNodeKeeper nodeKeeper; + const Utils::FilePath filePath = currentNode->filePath(); using NodeAndPath = QPair; QList filesToRemove{qMakePair(currentNode, currentNode->filePath())}; @@ -3685,6 +3688,8 @@ void ProjectExplorerPluginPrivate::duplicateFile() Node *currentNode = ProjectTree::currentNode(); QTC_ASSERT(currentNode && currentNode->asFileNode(), return); + ProjectTree::CurrentNodeKeeper nodeKeeper; + FileNode *fileNode = currentNode->asFileNode(); QString filePath = currentNode->filePath().toString(); QFileInfo sourceFileInfo(filePath); @@ -3725,6 +3730,8 @@ void ProjectExplorerPluginPrivate::deleteFile() Node *currentNode = ProjectTree::currentNode(); QTC_ASSERT(currentNode && currentNode->asFileNode(), return); + ProjectTree::CurrentNodeKeeper nodeKeeper; + FileNode *fileNode = currentNode->asFileNode(); QString filePath = currentNode->filePath().toString(); diff --git a/src/plugins/projectexplorer/projecttree.cpp b/src/plugins/projectexplorer/projecttree.cpp index d8b57ce4f1a..0da13be634b 100644 --- a/src/plugins/projectexplorer/projecttree.cpp +++ b/src/plugins/projectexplorer/projecttree.cpp @@ -490,7 +490,23 @@ const QList ProjectTree::siblingsWithSameBaseName(const Node *fileNode) void ProjectTree::hideContextMenu() { - m_focusForContextMenu = nullptr; + if (m_keepCurrentNodeRequests == 0) + m_focusForContextMenu = nullptr; +} + +ProjectTree::CurrentNodeKeeper::CurrentNodeKeeper() + : m_active(ProjectTree::instance()->m_focusForContextMenu) +{ + if (m_active) + ++ProjectTree::instance()->m_keepCurrentNodeRequests; +} + +ProjectTree::CurrentNodeKeeper::~CurrentNodeKeeper() +{ + if (m_active && --ProjectTree::instance()->m_keepCurrentNodeRequests == 0) { + ProjectTree::instance()->m_focusForContextMenu = nullptr; + ProjectTree::instance()->update(); + } } } // namespace ProjectExplorer diff --git a/src/plugins/projectexplorer/projecttree.h b/src/plugins/projectexplorer/projecttree.h index 1765a87d5be..5fe45416812 100644 --- a/src/plugins/projectexplorer/projecttree.h +++ b/src/plugins/projectexplorer/projecttree.h @@ -60,6 +60,14 @@ public: static Node *currentNode(); static Utils::FilePath currentFilePath(); + class CurrentNodeKeeper { + public: + CurrentNodeKeeper(); + ~CurrentNodeKeeper(); + private: + const bool m_active = false; + }; + // Integration with ProjectTreeWidget static void registerWidget(Internal::ProjectTreeWidget *widget); static void unregisterWidget(Internal::ProjectTreeWidget *widget); @@ -131,6 +139,7 @@ private: Node *m_currentNode = nullptr; Project *m_currentProject = nullptr; Internal::ProjectTreeWidget *m_focusForContextMenu = nullptr; + int m_keepCurrentNodeRequests = 0; Core::Context m_lastProjectContext; };