Editors: Highlight the view that has focus

If there is more than one editor view, i.e. if the view is split or an
additional editor window open, paint a small line in the highlight color
below the editor tool bar to indicate which view is "current", and
therefore the target for opening files, and keystrokes (if it also has
focus).

Fixes: QTCREATORBUG-23654
Change-Id: I9950c133a6633c6e68943c038669895dce1dd7ef
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
Eike Ziller
2024-04-24 16:51:40 +02:00
parent 1c04192c51
commit 0f9270ed7d
3 changed files with 72 additions and 25 deletions

View File

@@ -1724,6 +1724,8 @@ void EditorManagerPrivate::setCurrentEditor(IEditor *editor, bool ignoreNavigati
updateActions(); updateActions();
if (QTC_GUARD(!d->m_currentView.isEmpty()) && d->m_currentView.constFirst() != previousView)
emit d->currentViewChanged();
if (d->m_currentEditor != previousEditor) if (d->m_currentEditor != previousEditor)
emit m_instance->currentEditorChanged(d->m_currentEditor); emit m_instance->currentEditorChanged(d->m_currentEditor);
} }
@@ -1742,6 +1744,8 @@ void EditorManagerPrivate::setCurrentView(EditorView *view)
previousView->update(); previousView->update();
if (d->m_currentView.constFirst()) if (d->m_currentView.constFirst())
view->update(); view->update();
emit d->currentViewChanged();
} }
setCurrentEditor(view->currentEditor()); setCurrentEditor(view->currentEditor());
@@ -1869,6 +1873,8 @@ void EditorManagerPrivate::addEditorArea(EditorArea *area)
// If we didn't find a better view, so be it // If we didn't find a better view, so be it
}, },
Qt::QueuedConnection); Qt::QueuedConnection);
connect(area, &SplitterOrView::splitStateChanged, d, &EditorManagerPrivate::viewCountChanged);
emit d->viewCountChanged();
} }
void EditorManagerPrivate::splitNewWindow(EditorView *view) void EditorManagerPrivate::splitNewWindow(EditorView *view)
@@ -2280,31 +2286,33 @@ void EditorManagerPrivate::editorAreaDestroyed(QObject *area)
} }
} }
// check if the destroyed editor area had the current view or current editor // check if the destroyed editor area had the current view or current editor
if (currentEditorView()) if (!currentEditorView()) {
return; // we need to set a new current editor or view
// we need to set a new current editor or view if (!newActiveArea) {
if (!newActiveArea) { // some window managers behave weird and don't activate another window
// some window managers behave weird and don't activate another window // or there might be a Qt Creator toplevel activated that doesn't have editor windows
// or there might be a Qt Creator toplevel activated that doesn't have editor windows newActiveArea = d->m_editorAreas.first();
newActiveArea = d->m_editorAreas.first(); }
}
// check if the focusWidget points to some view // check if the focusWidget points to some view
SplitterOrView *focusSplitterOrView = nullptr; SplitterOrView *focusSplitterOrView = nullptr;
QWidget *candidate = newActiveArea->focusWidget(); QWidget *candidate = newActiveArea->focusWidget();
while (candidate && candidate != newActiveArea) { while (candidate && candidate != newActiveArea) {
if ((focusSplitterOrView = qobject_cast<SplitterOrView *>(candidate))) if ((focusSplitterOrView = qobject_cast<SplitterOrView *>(candidate)))
break; break;
candidate = candidate->parentWidget(); candidate = candidate->parentWidget();
}
// focusWidget might have been 0
if (!focusSplitterOrView)
focusSplitterOrView = newActiveArea->findFirstView()->parentSplitterOrView();
QTC_ASSERT(focusSplitterOrView, focusSplitterOrView = newActiveArea);
EditorView *focusView
= focusSplitterOrView->findFirstView(); // can be just focusSplitterOrView
QTC_ASSERT(focusView, focusView = newActiveArea->findFirstView());
if (QTC_GUARD(focusView))
EditorManagerPrivate::activateView(focusView);
} }
// focusWidget might have been 0 emit viewCountChanged();
if (!focusSplitterOrView)
focusSplitterOrView = newActiveArea->findFirstView()->parentSplitterOrView();
QTC_ASSERT(focusSplitterOrView, focusSplitterOrView = newActiveArea);
EditorView *focusView = focusSplitterOrView->findFirstView(); // can be just focusSplitterOrView
QTC_ASSERT(focusView, focusView = newActiveArea->findFirstView());
QTC_ASSERT(focusView, return);
EditorManagerPrivate::activateView(focusView);
} }
void EditorManagerPrivate::autoSave() void EditorManagerPrivate::autoSave()
@@ -2665,6 +2673,14 @@ QList<EditorView *> EditorManagerPrivate::allEditorViews()
return views; return views;
} }
bool EditorManagerPrivate::hasMoreThanOneview()
{
if (d->m_editorAreas.size() > 1)
return true;
QTC_ASSERT(d->m_editorAreas.size() > 0, return false);
return d->m_editorAreas.constFirst()->isSplitter();
}
/*! /*!
Returns the pointer to the instance. Only use for connecting to signals. Returns the pointer to the instance. Only use for connecting to signals.
*/ */

View File

@@ -61,6 +61,7 @@ public:
static EditorArea *mainEditorArea(); static EditorArea *mainEditorArea();
static EditorView *currentEditorView(); static EditorView *currentEditorView();
static QList<EditorView *> allEditorViews(); static QList<EditorView *> allEditorViews();
static bool hasMoreThanOneview();
static void setCurrentEditor(IEditor *editor, bool ignoreNavigationHistory = false); static void setCurrentEditor(IEditor *editor, bool ignoreNavigationHistory = false);
static IEditor *openEditor(EditorView *view, static IEditor *openEditor(EditorView *view,
const Utils::FilePath &filePath, const Utils::FilePath &filePath,
@@ -137,6 +138,8 @@ public slots:
signals: signals:
void placeholderTextChanged(const QString &text); void placeholderTextChanged(const QString &text);
void currentViewChanged();
void viewCountChanged();
private: private:
static void gotoNextDocHistory(); static void gotoNextDocHistory();

View File

@@ -14,10 +14,11 @@
#include <utils/algorithm.h> #include <utils/algorithm.h>
#include <utils/infobar.h> #include <utils/infobar.h>
#include <utils/qtcassert.h>
#include <utils/theme/theme.h>
#include <utils/layoutbuilder.h> #include <utils/layoutbuilder.h>
#include <utils/link.h> #include <utils/link.h>
#include <utils/overlaywidget.h>
#include <utils/qtcassert.h>
#include <utils/theme/theme.h>
#include <utils/utilsicons.h> #include <utils/utilsicons.h>
#include <QFileInfo> #include <QFileInfo>
@@ -125,6 +126,33 @@ EditorView::EditorView(SplitterOrView *parentSplitterOrView, QWidget *parent) :
this, &EditorView::openDroppedFiles); this, &EditorView::openDroppedFiles);
updateNavigatorActions(); updateNavigatorActions();
auto currentViewOverlay = new OverlayWidget;
currentViewOverlay->attachToWidget(this);
currentViewOverlay->setPaintFunction([this](QWidget *w, QPainter &p, QPaintEvent *) {
const int width = 2;
const QPoint margin{0, width};
p.setPen({w->palette().color(QPalette::Highlight), width});
p.drawLine(
m_toolBar->geometry().bottomLeft() + margin,
m_toolBar->geometry().bottomRight() + margin);
});
currentViewOverlay->setVisible(false);
const auto updateCurrentViewOverlay = [this, currentViewOverlay] {
currentViewOverlay->setVisible(
EditorManagerPrivate::hasMoreThanOneview()
&& EditorManagerPrivate::currentEditorView() == this);
};
connect(
EditorManagerPrivate::instance(),
&EditorManagerPrivate::currentViewChanged,
currentViewOverlay,
updateCurrentViewOverlay);
connect(
EditorManagerPrivate::instance(),
&EditorManagerPrivate::viewCountChanged,
currentViewOverlay,
updateCurrentViewOverlay);
} }
EditorView::~EditorView() = default; EditorView::~EditorView() = default;