Fix edge case when focusing FocusScope and tab bar is focused

When we focus a FocusScope we focus the last focused widget
inside that scope. But it could happen that the last focused widget
was the tab bar itself, which isn't very useful.

Fixes issue #188
This commit is contained in:
Sergio Martins
2021-04-18 23:58:30 +01:00
parent 819fec6061
commit 818acc0ff1

View File

@@ -43,6 +43,22 @@ public:
m_inCtor = false;
}
/// @brief Returns whether the last focused widget is the tab widget itself
bool lastFocusedIsTabWidget() const
{
if (!m_lastFocusedInScope)
return false;
if (auto metaObj = m_lastFocusedInScope->metaObject()) {
const auto className = QLatin1String(metaObj->className());
return className == QLatin1String("KDDockWidgets::TabBarWidget")
|| className == QLatin1String("KDDockWidgets::TabBarQuick");
}
return false;
}
~Private() override;
void setIsFocused(bool);
@@ -83,7 +99,10 @@ WidgetType *FocusScope::focusedWidget() const
void FocusScope::focus(Qt::FocusReason reason)
{
if (d->m_lastFocusedInScope) {
if (d->m_lastFocusedInScope && !d->lastFocusedIsTabWidget()) {
// When we focus the FocusScope, we give focus to the last focused widget, but let's
// do better than focusing a tab widget. The tab widget itself being focused isn't
// very useful.
d->m_lastFocusedInScope->setFocus(reason);
} else {
if (auto frame = qobject_cast<Frame*>(d->m_thisWidget)) {