Commit 84d27d3b authored by Christoph Cullmann's avatar Christoph Cullmann 🍨
Browse files

ensure focus is inside editor widget on startup

avoid that session restore steals focus
parent fd3ca02e
Pipeline #238120 failed with stage
in 4 minutes and 38 seconds
......@@ -171,9 +171,8 @@ KateMainWindow::KateMainWindow(KConfig *sconfig, const QString &sgroup)
setSidebarsVisibleInternal(false, true);
}
// in all cases: avoid that arbitrary plugin toolviews get focus, like terminal, bug 412227
// we need to delay this a bit due to lazy view creation (and lazy e.g. terminal widget creation)
QTimer::singleShot(0, m_viewManager->activeViewSpace(), SLOT(setFocus()));
// trigger proper focus restore
m_viewManager->triggerActiveViewFocus();
}
KateMainWindow::~KateMainWindow()
......
......@@ -136,6 +136,15 @@ public:
*/
void saveOptions();
/**
* Called if focus should go to the important part of the central widget.
*/
void triggerFocusForCentralWidget() override
{
// just dispatch to view manager
m_viewManager->triggerActiveViewFocus();
}
private:
/**
* Setup actions which pointers are needed already in setupMainWindow
......
......@@ -887,7 +887,7 @@ void Sidebar::collapseSidebar()
}
// Now that tools are hidden, ensure the doc got the focus
m_mainWin->centralWidget()->setFocus();
m_mainWin->triggerFocusForCentralWidget();
}
bool Sidebar::adjustSplitterSections()
......@@ -1208,6 +1208,9 @@ void Sidebar::restoreSession(KConfigGroup &config)
auto sz = config.readEntry(QStringLiteral("Kate-MDI-Sidebar-%1-Splitter").arg(position()), QList<int>());
QTimer::singleShot(100, this, [this, sz]() {
setSizes(sz);
// ensure focus is not stolen
m_mainWin->triggerFocusForCentralWidget();
});
// ...now we are ready to get the final splitter sizes by MainWindow::finishRestore
updateSidebar();
......@@ -1472,7 +1475,7 @@ bool MainWindow::hideToolView(ToolView *widget)
}
const bool ret = widget->sidebar()->hideToolView(widget);
m_centralWidget->setFocus();
triggerFocusForCentralWidget();
return ret;
}
......@@ -1483,7 +1486,7 @@ void MainWindow::hideToolViews()
tv->sidebar()->hideToolView(tv);
}
}
m_centralWidget->setFocus();
triggerFocusForCentralWidget();
}
void MainWindow::startRestore(KConfigBase *config, const QString &group)
......@@ -1546,6 +1549,9 @@ void MainWindow::finishRestore()
for (auto &sidebar : m_sidebars) {
sidebar->updateSidebar();
}
// ensure focus is not stolen
triggerFocusForCentralWidget();
});
}
......
......@@ -498,11 +498,15 @@ public:
/**
* central widget ;)
* use this as parent for your content
* this widget will get focus if a toolview is hidden
* @return central widget
*/
QWidget *centralWidget() const;
/**
* Called if focus should go to the important part of the central widget.
*/
virtual void triggerFocusForCentralWidget() = 0;
protected:
/**
* Status bar area stacked widget.
......
......@@ -85,7 +85,9 @@ KateViewManager::KateViewManager(QWidget *parentW, KateMainWindow *parent)
if (activeViewSpace() && (activeViewSpace()->currentView() || activeViewSpace()->currentWidget()))
return;
// create new view & ensure focus
slotDocumentNew();
triggerActiveViewFocus();
});
} else {
// ensure we have the welcome view if no active view is there
......@@ -1549,7 +1551,9 @@ void KateViewManager::showWelcomeViewIfNeeded()
if (activeViewSpace() && (activeViewSpace()->currentView() || activeViewSpace()->currentWidget()))
return;
// create welcome view & trigger focus of active view/widget
showWelcomeView();
triggerActiveViewFocus();
});
}
......@@ -1587,3 +1591,24 @@ void KateViewManager::saveRecents()
{
mainWindow()->recentFilesAction()->saveEntries(KSharedConfig::openConfig()->group("Recent Files"));
}
void KateViewManager::triggerActiveViewFocus()
{
// delay the focus, needed e.g. on startup
QTimer::singleShot(0, this, [this]() {
// no active view space, bad!
if (!activeViewSpace()) {
return;
}
// else: try to focus either the current active view or widget
if (auto v = activeViewSpace()->currentView()) {
v->setFocus();
return;
}
if (auto v = activeViewSpace()->currentWidget()) {
v->setFocus();
return;
}
});
}
......@@ -46,6 +46,11 @@ public:
KateViewManager(QWidget *parentW, KateMainWindow *parent);
~KateViewManager() override;
/**
* triggered delayed focus set to current active view
*/
void triggerActiveViewFocus();
private:
/**
* create all actions needed for the view manager
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment