forked from qt-creator/qt-creator
Fix focus handling when leaving locator
Pressing escape or triggering an item should get your focus back to where you were, also in external editor and help windows. Fixes triggering editor related menu items like "Close" from locator. Task-number: QTCREATORBUG-20071 Change-Id: I0111b846e9476697a7bba7856bb2cc4b5bef7979 Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -592,6 +592,8 @@ LocatorWidget::LocatorWidget(Locator *locator) :
|
|||||||
updatePlaceholderText(locateCmd);
|
updatePlaceholderText(locateCmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
connect(qApp, &QApplication::focusChanged, this, &LocatorWidget::updatePreviousFocusWidget);
|
||||||
|
|
||||||
connect(locator, &Locator::filtersChanged, this, &LocatorWidget::updateFilterList);
|
connect(locator, &Locator::filtersChanged, this, &LocatorWidget::updateFilterList);
|
||||||
updateFilterList();
|
updateFilterList();
|
||||||
}
|
}
|
||||||
@@ -620,6 +622,28 @@ void LocatorWidget::updateFilterList()
|
|||||||
m_filterMenu->addAction(m_configureAction);
|
m_filterMenu->addAction(m_configureAction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool LocatorWidget::isInMainWindow() const
|
||||||
|
{
|
||||||
|
return window() == ICore::mainWindow();
|
||||||
|
}
|
||||||
|
|
||||||
|
void LocatorWidget::updatePreviousFocusWidget(QWidget *previous, QWidget *current)
|
||||||
|
{
|
||||||
|
const auto isInLocator = [this](QWidget *w) { return w == this || isAncestorOf(w); };
|
||||||
|
if (isInLocator(current) && !isInLocator(previous))
|
||||||
|
m_previousFocusWidget = previous;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void resetFocus(QPointer<QWidget> previousFocus, bool isInMainWindow)
|
||||||
|
{
|
||||||
|
if (previousFocus) {
|
||||||
|
previousFocus->setFocus();
|
||||||
|
ICore::raiseWindow(previousFocus);
|
||||||
|
} else if (isInMainWindow) {
|
||||||
|
ModeManager::setFocusToCurrentMode();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool LocatorWidget::eventFilter(QObject *obj, QEvent *event)
|
bool LocatorWidget::eventFilter(QObject *obj, QEvent *event)
|
||||||
{
|
{
|
||||||
if (obj == m_fileLineEdit && event->type() == QEvent::ShortcutOverride) {
|
if (obj == m_fileLineEdit && event->type() == QEvent::ShortcutOverride) {
|
||||||
@@ -706,7 +730,12 @@ bool LocatorWidget::eventFilter(QObject *obj, QEvent *event)
|
|||||||
case Qt::Key_Escape:
|
case Qt::Key_Escape:
|
||||||
if (!ke->modifiers()) {
|
if (!ke->modifiers()) {
|
||||||
event->accept();
|
event->accept();
|
||||||
QTimer::singleShot(0, this, &LocatorWidget::setFocusToCurrentMode);
|
QTimer::singleShot(0,
|
||||||
|
this,
|
||||||
|
[focus = m_previousFocusWidget,
|
||||||
|
isInMainWindow = isInMainWindow()] {
|
||||||
|
resetFocus(focus, isInMainWindow);
|
||||||
|
});
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -723,11 +752,6 @@ bool LocatorWidget::eventFilter(QObject *obj, QEvent *event)
|
|||||||
return QWidget::eventFilter(obj, event);
|
return QWidget::eventFilter(obj, event);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LocatorWidget::setFocusToCurrentMode()
|
|
||||||
{
|
|
||||||
ModeManager::setFocusToCurrentMode();
|
|
||||||
}
|
|
||||||
|
|
||||||
void LocatorWidget::showPopupDelayed()
|
void LocatorWidget::showPopupDelayed()
|
||||||
{
|
{
|
||||||
m_updateRequested = true;
|
m_updateRequested = true;
|
||||||
@@ -858,7 +882,7 @@ void LocatorWidget::acceptEntry(int row)
|
|||||||
entry.filter->accept(entry, &newText, &selectionStart, &selectionLength);
|
entry.filter->accept(entry, &newText, &selectionStart, &selectionLength);
|
||||||
if (newText.isEmpty()) {
|
if (newText.isEmpty()) {
|
||||||
emit hidePopup();
|
emit hidePopup();
|
||||||
m_fileLineEdit->clearFocus();
|
resetFocus(m_previousFocusWidget, isInMainWindow());
|
||||||
} else {
|
} else {
|
||||||
showText(newText, selectionStart, selectionLength);
|
showText(newText, selectionStart, selectionLength);
|
||||||
}
|
}
|
||||||
|
@@ -79,9 +79,10 @@ private:
|
|||||||
void showConfigureDialog();
|
void showConfigureDialog();
|
||||||
void addSearchResults(int firstIndex, int endIndex);
|
void addSearchResults(int firstIndex, int endIndex);
|
||||||
void handleSearchFinished();
|
void handleSearchFinished();
|
||||||
void setFocusToCurrentMode();
|
|
||||||
void updateFilterList();
|
void updateFilterList();
|
||||||
|
bool isInMainWindow() const;
|
||||||
|
|
||||||
|
void updatePreviousFocusWidget(QWidget *previous, QWidget *current);
|
||||||
bool eventFilter(QObject *obj, QEvent *event) override;
|
bool eventFilter(QObject *obj, QEvent *event) override;
|
||||||
|
|
||||||
void updateCompletionList(const QString &text);
|
void updateCompletionList(const QString &text);
|
||||||
@@ -103,6 +104,7 @@ private:
|
|||||||
QWidget *m_progressIndicator = nullptr;
|
QWidget *m_progressIndicator = nullptr;
|
||||||
QTimer m_showProgressTimer;
|
QTimer m_showProgressTimer;
|
||||||
Utils::optional<int> m_rowRequestedForAccept;
|
Utils::optional<int> m_rowRequestedForAccept;
|
||||||
|
QPointer<QWidget> m_previousFocusWidget;
|
||||||
};
|
};
|
||||||
|
|
||||||
class LocatorPopup : public QWidget
|
class LocatorPopup : public QWidget
|
||||||
|
Reference in New Issue
Block a user