diff --git a/kate/katemainwindow.h b/kate/katemainwindow.h --- a/kate/katemainwindow.h +++ b/kate/katemainwindow.h @@ -584,6 +584,7 @@ protected: bool event(QEvent *e) override; void mousePressEvent(QMouseEvent *e) override; + void closeEvent(QCloseEvent *e) override; }; #endif diff --git a/kate/katemainwindow.cpp b/kate/katemainwindow.cpp --- a/kate/katemainwindow.cpp +++ b/kate/katemainwindow.cpp @@ -1191,6 +1191,43 @@ } } +void KateMainWindow::closeEvent(QCloseEvent *e) +{ + // Find out if Kate is closed directly by the user or + // by the session manager because the session is closed + const bool closedByUser = !qApp->isSavingSession(); + const bool documentCount = KateApp::self()->documentManager()->documentList().count() > 1; + const bool windowCount = KateApp::self()->mainWindowsCount() == 1; + const bool askConfirmation = closedByUser && documentCount && windowCount; + + if (askConfirmation) { + KGuiItem yes(i18nc("@action:button 'Quit Kate' button", "&Quit %1", QGuiApplication::applicationDisplayName()), + QIcon::fromTheme(QStringLiteral("application-exit"))); + KGuiItem no(i18n("C&lose Current Tab"), QIcon::fromTheme(QStringLiteral("tab-close"))); + + const auto result = KMessageBox::warningYesNoCancel(window(), + i18n("You have multiple tabs open in this window, are you sure you want to quit?"), + i18n("Confirm Closing"), + yes, + no, + KStandardGuiItem::cancel(), + i18n("Do not ask again")); + + auto view = viewManager()->activeView(); + switch (result) { + case KMessageBox::Yes: + break; + case KMessageBox::No: + KateApp::self()->documentManager()->closeDocument(view->document()); + Q_FALLTHROUGH(); + default: + e->ignore(); + return; + } + } + KXmlGuiWindow::closeEvent(e); +} + void KateMainWindow::slotFocusPrevTab() { if (m_viewManager->activeViewSpace()) {