Show the right actions in the menu item locator filter

Instead of showing the actions that are available while locator has
focus, we need to show the actions that were enabled just before.

Task-number: QTCREATORBUG-20626
Change-Id: I16c8370074080f0d395922e0df77ce2aceee7088
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2018-07-16 16:16:05 +02:00
parent 1bf2ff6d92
commit aed2c2c716
4 changed files with 51 additions and 1 deletions

View File

@@ -34,6 +34,8 @@
#include <extensionsystem/pluginmanager.h>
#include <utils/qtcassert.h>
#include <QApplication>
using namespace Core::Internal;
namespace Core {
@@ -95,4 +97,15 @@ QWidget *LocatorManager::createLocatorInputWidget(QWidget *window)
return locatorWidget;
}
bool LocatorManager::locatorHasFocus()
{
QWidget *w = qApp->focusWidget();
while (w) {
if (qobject_cast<LocatorWidget *>(w))
return true;
w = w->parentWidget();
}
return false;
}
} // namespace Core

View File

@@ -44,6 +44,8 @@ public:
static void show(const QString &text, int selectionStart = -1, int selectionLength = 0);
static QWidget *createLocatorInputWidget(QWidget *window);
static bool locatorHasFocus();
};
} // namespace Core

View File

@@ -28,6 +28,8 @@
#include "actionmanager/actioncontainer.h"
#include "actionmanager/actionmanager.h"
#include "coreconstants.h"
#include "icore.h"
#include "locator/locatormanager.h"
#include <utils/algorithm.h>
#include <utils/qtcassert.h>
@@ -38,6 +40,11 @@
#include <QRegularExpression>
#include <QTimer>
uint qHash(const QPointer<QAction> &p, uint seed)
{
return qHash(p.data(), seed);
}
using namespace Core::Internal;
using namespace Core;
@@ -46,6 +53,10 @@ MenuBarFilter::MenuBarFilter()
setId("Actions from the menu");
setDisplayName(tr("Actions from the Menu"));
setShortcutString("t");
connect(ICore::instance(), &ICore::contextAboutToChange, this, [this] {
if (LocatorManager::locatorHasFocus())
updateEnabledActionCache();
});
}
static const QList<QAction *> menuBarActions()
@@ -88,7 +99,7 @@ QList<LocatorFilterEntry> MenuBarFilter::matchesForAction(QAction *action,
QVector<const QMenu *> &processedMenus)
{
QList<LocatorFilterEntry> entries;
if (!action->isEnabled())
if (!m_enabledActions.contains(action))
return entries;
const QString text = Utils::stripAccelerator(action->text());
if (QMenu *menu = action->menu()) {
@@ -149,6 +160,24 @@ static void requestMenuUpdate(const QAction* action)
}
}
void MenuBarFilter::updateEnabledActionCache()
{
m_enabledActions.clear();
QList<QAction *> queue = menuBarActions();
for (QAction *action : qAsConst(queue))
requestMenuUpdate(action);
while (!queue.isEmpty()) {
QAction *action = queue.takeFirst();
if (action->isEnabled()) {
m_enabledActions.insert(action);
if (QMenu *menu = action->menu()) {
if (menu->isEnabled())
queue.append(menu->actions());
}
}
}
}
void Core::Internal::MenuBarFilter::prepareSearch(const QString &entry)
{
Q_UNUSED(entry);

View File

@@ -27,6 +27,10 @@
#include <coreplugin/locator/ilocatorfilter.h>
#include <QAction>
#include <QPointer>
#include <QSet>
QT_BEGIN_NAMESPACE
class QAction;
class QMenu;
@@ -52,8 +56,10 @@ private:
const QStringList &entryPath,
const QStringList &path,
QVector<const QMenu *> &processedMenus);
void updateEnabledActionCache();
QList<LocatorFilterEntry> m_entries;
QSet<QPointer<QAction>> m_enabledActions;
};
} // namespace Internal