From 6a25c8dc84efe2968cabf27a03efbb4171d133c4 Mon Sep 17 00:00:00 2001 From: Janek Bevendorff Date: Sat, 26 Oct 2019 16:14:28 +0200 Subject: [PATCH] Force app exit if session manager signals a shutdown. (#3666) Resolves #3410. Additionally, "fix" main window toggling behaviour when clicking the tray icon while the window is visible, but not in focus (e.g. hidden by other windows). On platforms other than Windows, the window is now brought to the front if it does not already have focus or is toggled otherwise. Remove obsolete Windows session end handling code. --- src/core/OSEventFilter.cpp | 12 ++---------- src/gui/MainWindow.cpp | 37 +++++++++++++++---------------------- src/gui/MainWindow.h | 26 +++++++++++++------------- 3 files changed, 30 insertions(+), 45 deletions(-) diff --git a/src/core/OSEventFilter.cpp b/src/core/OSEventFilter.cpp index d5873ee8d..f1f4d97a9 100644 --- a/src/core/OSEventFilter.cpp +++ b/src/core/OSEventFilter.cpp @@ -37,16 +37,8 @@ bool OSEventFilter::nativeEventFilter(const QByteArray& eventType, void* message #if defined(Q_OS_UNIX) if (eventType == QByteArrayLiteral("xcb_generic_event_t")) { #elif defined(Q_OS_WIN) - auto winmsg = static_cast(message); - if (winmsg->message == WM_QUERYENDSESSION) { - *result = 1; - return true; - } else if (winmsg->message == WM_ENDSESSION) { - getMainWindow()->appExit(); - *result = 0; - return true; - } else if (eventType == QByteArrayLiteral("windows_generic_MSG") - || eventType == QByteArrayLiteral("windows_dispatcher_MSG")) { + if (eventType == QByteArrayLiteral("windows_generic_MSG") + || eventType == QByteArrayLiteral("windows_dispatcher_MSG")) { #endif return autoType()->callEventFilter(message) == 1; } diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 01b8cf028..b33b322bc 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -142,10 +142,6 @@ MainWindow* getMainWindow() MainWindow::MainWindow() : m_ui(new Ui::MainWindow()) - , m_trayIcon(nullptr) - , m_appExitCalled(false) - , m_appExiting(false) - , m_lastFocusOutTime(0) { g_MainWindow = this; @@ -990,31 +986,29 @@ void MainWindow::toggleUsernamesHidden() void MainWindow::closeEvent(QCloseEvent* event) { - // ignore double close events (happens on macOS when closing from the dock) if (m_appExiting) { event->accept(); return; } - // Don't ignore close event when the app is hidden to tray. - // This can occur when the OS issues close events on shutdown. - if (config()->get("GUI/MinimizeOnClose").toBool() && !isHidden() && !m_appExitCalled) { + // Ignore event and hide to tray if this is not an actual close + // request by the system's session manager. + if (config()->get("GUI/MinimizeOnClose").toBool() && !m_appExitCalled && !isHidden() && !qApp->isSavingSession()) { event->ignore(); hideWindow(); return; } - bool accept = saveLastDatabases(); - - if (accept) { - m_appExiting = true; + m_appExiting = saveLastDatabases(); + if (m_appExiting) { saveWindowInformation(); - event->accept(); QApplication::quit(); - } else { - event->ignore(); + return; } + + m_appExitCalled = false; + event->ignore(); } void MainWindow::changeEvent(QEvent* event) @@ -1208,15 +1202,14 @@ void MainWindow::processTrayIconTrigger() toggleWindow(); } else if (m_trayIconTriggerReason == QSystemTrayIcon::Trigger || m_trayIconTriggerReason == QSystemTrayIcon::MiddleClick) { - // Toggle window if hidden - // If on windows, check if focus switched within the last second because - // clicking the tray icon removes focus from main window - // If on Linux or macOS, check if the window is active - if (isHidden() + // Toggle window if is not in front. #ifdef Q_OS_WIN - || (Clock::currentSecondsSinceEpoch() - m_lastFocusOutTime) <= 1) { + // If on Windows, check if focus switched within the last second because + // clicking the tray icon removes focus from main window. + if (isHidden() || (Clock::currentSecondsSinceEpoch() - m_lastFocusOutTime) <= 1) { #else - || windowHandle()->isActive()) { + // If on Linux or macOS, check if the window has focus. + if (hasFocus() || isHidden() || windowHandle()->isActive()) { #endif toggleWindow(); } else { diff --git a/src/gui/MainWindow.h b/src/gui/MainWindow.h index 71acb1081..81604e176 100644 --- a/src/gui/MainWindow.h +++ b/src/gui/MainWindow.h @@ -144,24 +144,24 @@ private: const QScopedPointer m_ui; SignalMultiplexer m_actionMultiplexer; - QAction* m_clearHistoryAction; - QAction* m_searchWidgetAction; - QMenu* m_entryContextMenu; - QActionGroup* m_lastDatabasesActions; - QActionGroup* m_copyAdditionalAttributeActions; - InactivityTimer* m_inactivityTimer; - InactivityTimer* m_touchIDinactivityTimer; + QPointer m_clearHistoryAction; + QPointer m_searchWidgetAction; + QPointer m_entryContextMenu; + QPointer m_lastDatabasesActions; + QPointer m_copyAdditionalAttributeActions; + QPointer m_inactivityTimer; + QPointer m_touchIDinactivityTimer; int m_countDefaultAttributes; - QSystemTrayIcon* m_trayIcon; - ScreenLockListener* m_screenLockListener; + QPointer m_trayIcon; + QPointer m_screenLockListener; QPointer m_searchWidget; Q_DISABLE_COPY(MainWindow) - bool m_appExitCalled; - bool m_appExiting; - bool m_contextMenuFocusLock; - uint m_lastFocusOutTime; + bool m_appExitCalled = false; + bool m_appExiting = false; + bool m_contextMenuFocusLock = false; + uint m_lastFocusOutTime = 0; QTimer m_trayIconTriggerTimer; QSystemTrayIcon::ActivationReason m_trayIconTriggerReason; };