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.
This commit is contained in:
Janek Bevendorff 2019-10-26 16:14:28 +02:00 committed by GitHub
parent ebc006c4b9
commit 6a25c8dc84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 45 deletions

View File

@ -37,16 +37,8 @@ bool OSEventFilter::nativeEventFilter(const QByteArray& eventType, void* message
#if defined(Q_OS_UNIX) #if defined(Q_OS_UNIX)
if (eventType == QByteArrayLiteral("xcb_generic_event_t")) { if (eventType == QByteArrayLiteral("xcb_generic_event_t")) {
#elif defined(Q_OS_WIN) #elif defined(Q_OS_WIN)
auto winmsg = static_cast<MSG*>(message); if (eventType == QByteArrayLiteral("windows_generic_MSG")
if (winmsg->message == WM_QUERYENDSESSION) { || eventType == QByteArrayLiteral("windows_dispatcher_MSG")) {
*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")) {
#endif #endif
return autoType()->callEventFilter(message) == 1; return autoType()->callEventFilter(message) == 1;
} }

View File

@ -142,10 +142,6 @@ MainWindow* getMainWindow()
MainWindow::MainWindow() MainWindow::MainWindow()
: m_ui(new Ui::MainWindow()) : m_ui(new Ui::MainWindow())
, m_trayIcon(nullptr)
, m_appExitCalled(false)
, m_appExiting(false)
, m_lastFocusOutTime(0)
{ {
g_MainWindow = this; g_MainWindow = this;
@ -990,31 +986,29 @@ void MainWindow::toggleUsernamesHidden()
void MainWindow::closeEvent(QCloseEvent* event) void MainWindow::closeEvent(QCloseEvent* event)
{ {
// ignore double close events (happens on macOS when closing from the dock)
if (m_appExiting) { if (m_appExiting) {
event->accept(); event->accept();
return; return;
} }
// Don't ignore close event when the app is hidden to tray. // Ignore event and hide to tray if this is not an actual close
// This can occur when the OS issues close events on shutdown. // request by the system's session manager.
if (config()->get("GUI/MinimizeOnClose").toBool() && !isHidden() && !m_appExitCalled) { if (config()->get("GUI/MinimizeOnClose").toBool() && !m_appExitCalled && !isHidden() && !qApp->isSavingSession()) {
event->ignore(); event->ignore();
hideWindow(); hideWindow();
return; return;
} }
bool accept = saveLastDatabases(); m_appExiting = saveLastDatabases();
if (m_appExiting) {
if (accept) {
m_appExiting = true;
saveWindowInformation(); saveWindowInformation();
event->accept(); event->accept();
QApplication::quit(); QApplication::quit();
} else { return;
event->ignore();
} }
m_appExitCalled = false;
event->ignore();
} }
void MainWindow::changeEvent(QEvent* event) void MainWindow::changeEvent(QEvent* event)
@ -1208,15 +1202,14 @@ void MainWindow::processTrayIconTrigger()
toggleWindow(); toggleWindow();
} else if (m_trayIconTriggerReason == QSystemTrayIcon::Trigger } else if (m_trayIconTriggerReason == QSystemTrayIcon::Trigger
|| m_trayIconTriggerReason == QSystemTrayIcon::MiddleClick) { || m_trayIconTriggerReason == QSystemTrayIcon::MiddleClick) {
// Toggle window if hidden // Toggle window if is not in front.
// 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()
#ifdef Q_OS_WIN #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 #else
|| windowHandle()->isActive()) { // If on Linux or macOS, check if the window has focus.
if (hasFocus() || isHidden() || windowHandle()->isActive()) {
#endif #endif
toggleWindow(); toggleWindow();
} else { } else {

View File

@ -144,24 +144,24 @@ private:
const QScopedPointer<Ui::MainWindow> m_ui; const QScopedPointer<Ui::MainWindow> m_ui;
SignalMultiplexer m_actionMultiplexer; SignalMultiplexer m_actionMultiplexer;
QAction* m_clearHistoryAction; QPointer<QAction> m_clearHistoryAction;
QAction* m_searchWidgetAction; QPointer<QAction> m_searchWidgetAction;
QMenu* m_entryContextMenu; QPointer<QMenu> m_entryContextMenu;
QActionGroup* m_lastDatabasesActions; QPointer<QActionGroup> m_lastDatabasesActions;
QActionGroup* m_copyAdditionalAttributeActions; QPointer<QActionGroup> m_copyAdditionalAttributeActions;
InactivityTimer* m_inactivityTimer; QPointer<InactivityTimer> m_inactivityTimer;
InactivityTimer* m_touchIDinactivityTimer; QPointer<InactivityTimer> m_touchIDinactivityTimer;
int m_countDefaultAttributes; int m_countDefaultAttributes;
QSystemTrayIcon* m_trayIcon; QPointer<QSystemTrayIcon> m_trayIcon;
ScreenLockListener* m_screenLockListener; QPointer<ScreenLockListener> m_screenLockListener;
QPointer<SearchWidget> m_searchWidget; QPointer<SearchWidget> m_searchWidget;
Q_DISABLE_COPY(MainWindow) Q_DISABLE_COPY(MainWindow)
bool m_appExitCalled; bool m_appExitCalled = false;
bool m_appExiting; bool m_appExiting = false;
bool m_contextMenuFocusLock; bool m_contextMenuFocusLock = false;
uint m_lastFocusOutTime; uint m_lastFocusOutTime = 0;
QTimer m_trayIconTriggerTimer; QTimer m_trayIconTriggerTimer;
QSystemTrayIcon::ActivationReason m_trayIconTriggerReason; QSystemTrayIcon::ActivationReason m_trayIconTriggerReason;
}; };