[UI] Refocus on Tab when completing

This commit is contained in:
Lukas Brübach 2026-10-09 12:59:27 +02:00
parent bc8289e48f
commit c9ce34a00a

View file

@ -48,32 +48,37 @@ void LineEditCompleter::focusOutEvent(QFocusEvent *e)
{ {
LineEditUnfocusable::focusOutEvent(e); LineEditUnfocusable::focusOutEvent(e);
// Only commit the highlighted completion when focus moves away via Tab. // Commit completion when focus moves away via Tab if popup is visible.
// Other focus losses (e.g. the unfocus shortcut / Escape) must simply close // Other focus losses (e.g. the unfocus shortcut / Escape) must simply close
// the popup without inserting anything. // the popup without inserting anything.
if (e->reason() != Qt::TabFocusReason) { bool popupWasVisible = false;
hideCompleterPopups(); for (auto &info : completers) {
if (info.completer->popup()->isVisible()) {
popupWasVisible = true;
const QModelIndex currentIndex = info.completer->popup()->currentIndex();
QString completionText;
if (currentIndex.isValid()) {
completionText = currentIndex.data().toString();
} else {
completionText = info.completer->currentCompletion();
}
if (!completionText.isEmpty()) {
insertCompletion(info.completer, completionText);
}
}
}
hideCompleterPopups();
if (popupWasVisible && e->reason() == Qt::TabFocusReason) {
// Refocus the edit so Tab doesn't move focus away
setFocus();
e->accept();
return; return;
} }
for (auto &info : completers) { if (popupWasVisible) {
if (!info.completer->popup()->isVisible()) { return;
continue;
}
const QModelIndex currentIndex = info.completer->popup()->currentIndex();
QString completionText;
if (currentIndex.isValid()) {
completionText = currentIndex.data().toString();
} else {
completionText = info.completer->currentCompletion();
}
if (!completionText.isEmpty()) {
insertCompletion(info.completer, completionText);
}
} }
hideCompleterPopups();
} }
void LineEditCompleter::keyPressEvent(QKeyEvent *event) void LineEditCompleter::keyPressEvent(QKeyEvent *event)