From 0f9270ed7dcfe57034f2844aabf54ae2ddb50241 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Wed, 24 Apr 2024 16:51:40 +0200 Subject: [PATCH] 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 --- .../editormanager/editormanager.cpp | 62 ++++++++++++------- .../editormanager/editormanager_p.h | 3 + .../coreplugin/editormanager/editorview.cpp | 32 +++++++++- 3 files changed, 72 insertions(+), 25 deletions(-) diff --git a/src/plugins/coreplugin/editormanager/editormanager.cpp b/src/plugins/coreplugin/editormanager/editormanager.cpp index 9041d2bd4a8..95eb0def6c0 100644 --- a/src/plugins/coreplugin/editormanager/editormanager.cpp +++ b/src/plugins/coreplugin/editormanager/editormanager.cpp @@ -1724,6 +1724,8 @@ void EditorManagerPrivate::setCurrentEditor(IEditor *editor, bool ignoreNavigati updateActions(); + if (QTC_GUARD(!d->m_currentView.isEmpty()) && d->m_currentView.constFirst() != previousView) + emit d->currentViewChanged(); if (d->m_currentEditor != previousEditor) emit m_instance->currentEditorChanged(d->m_currentEditor); } @@ -1742,6 +1744,8 @@ void EditorManagerPrivate::setCurrentView(EditorView *view) previousView->update(); if (d->m_currentView.constFirst()) view->update(); + + emit d->currentViewChanged(); } setCurrentEditor(view->currentEditor()); @@ -1869,6 +1873,8 @@ void EditorManagerPrivate::addEditorArea(EditorArea *area) // If we didn't find a better view, so be it }, Qt::QueuedConnection); + connect(area, &SplitterOrView::splitStateChanged, d, &EditorManagerPrivate::viewCountChanged); + emit d->viewCountChanged(); } 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 - if (currentEditorView()) - return; - // we need to set a new current editor or view - if (!newActiveArea) { - // 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 - newActiveArea = d->m_editorAreas.first(); - } + if (!currentEditorView()) { + // we need to set a new current editor or view + if (!newActiveArea) { + // 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 + newActiveArea = d->m_editorAreas.first(); + } - // check if the focusWidget points to some view - SplitterOrView *focusSplitterOrView = nullptr; - QWidget *candidate = newActiveArea->focusWidget(); - while (candidate && candidate != newActiveArea) { - if ((focusSplitterOrView = qobject_cast(candidate))) - break; - candidate = candidate->parentWidget(); + // check if the focusWidget points to some view + SplitterOrView *focusSplitterOrView = nullptr; + QWidget *candidate = newActiveArea->focusWidget(); + while (candidate && candidate != newActiveArea) { + if ((focusSplitterOrView = qobject_cast(candidate))) + break; + 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 - 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); + emit viewCountChanged(); } void EditorManagerPrivate::autoSave() @@ -2665,6 +2673,14 @@ QList EditorManagerPrivate::allEditorViews() 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. */ diff --git a/src/plugins/coreplugin/editormanager/editormanager_p.h b/src/plugins/coreplugin/editormanager/editormanager_p.h index 789acd16e15..db5bf759aec 100644 --- a/src/plugins/coreplugin/editormanager/editormanager_p.h +++ b/src/plugins/coreplugin/editormanager/editormanager_p.h @@ -61,6 +61,7 @@ public: static EditorArea *mainEditorArea(); static EditorView *currentEditorView(); static QList allEditorViews(); + static bool hasMoreThanOneview(); static void setCurrentEditor(IEditor *editor, bool ignoreNavigationHistory = false); static IEditor *openEditor(EditorView *view, const Utils::FilePath &filePath, @@ -137,6 +138,8 @@ public slots: signals: void placeholderTextChanged(const QString &text); + void currentViewChanged(); + void viewCountChanged(); private: static void gotoNextDocHistory(); diff --git a/src/plugins/coreplugin/editormanager/editorview.cpp b/src/plugins/coreplugin/editormanager/editorview.cpp index 6aa20510cb4..ae9c047a777 100644 --- a/src/plugins/coreplugin/editormanager/editorview.cpp +++ b/src/plugins/coreplugin/editormanager/editorview.cpp @@ -14,10 +14,11 @@ #include #include -#include -#include #include #include +#include +#include +#include #include #include @@ -125,6 +126,33 @@ EditorView::EditorView(SplitterOrView *parentSplitterOrView, QWidget *parent) : this, &EditorView::openDroppedFiles); 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;