From 518a29a02e622cdbb8f53225b65e03f31c03f504 Mon Sep 17 00:00:00 2001 From: Denis Danilov Date: Sun, 17 Sep 2023 23:13:33 +0200 Subject: [PATCH] fix app quit logic --- .clang-format | 2 -- src/MainWindow.cpp | 17 ++++++++++++----- src/MainWindow.h | 5 +++++ 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/.clang-format b/.clang-format index bf4bbdd..4220310 100644 --- a/.clang-format +++ b/.clang-format @@ -56,8 +56,6 @@ IndentWidth: 2 #PackConstructorInitializers: CurrentLine PointerAlignment: Left -ReferenceAlignment: Left - #QualifierAlignment: Left ReferenceAlignment: Left diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 9de8d67..949b078 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -8,8 +8,8 @@ #include "TrackControls.h" #include "Version.h" +#include #include -#include #include #include #include @@ -98,7 +98,7 @@ void MainWindow::addQuitItemToMenu(QMenu* menu) const #endif auto* quit_app = menu->addAction(tr("Quit")); - connect(quit_app, &QAction::triggered, qApp, &QCoreApplication::quit); + connect(quit_app, &QAction::triggered, this, &MainWindow::quit); } #if defined(Q_OS_WIN) || defined(Q_OS_LINUX) @@ -222,14 +222,12 @@ void MainWindow::resumePausedTracks() #if defined(Q_OS_WIN) || defined(Q_OS_LINUX) void MainWindow::closeEvent(QCloseEvent* event) { - if (!m_tray_available) + if (m_quit || !m_tray_available) { event->accept(); return; } - if (!m_tray_icon->isVisible()) { event->accept(); } - windowHide(); event->ignore(); } @@ -243,6 +241,15 @@ void MainWindow::mousePressEvent(QMouseEvent* event) } } +void MainWindow::quit() +{ +#if defined(Q_OS_WIN) || defined(Q_OS_LINUX) + m_quit = true; +#endif + + QApplication::quit(); +} + #if defined(Q_OS_WIN) || defined(Q_OS_LINUX) void MainWindow::setupTrayIcon() { diff --git a/src/MainWindow.h b/src/MainWindow.h index 103d97c..d73f0ed 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -3,6 +3,7 @@ #pragma once +#include #include #include #include @@ -48,6 +49,9 @@ public slots: #endif void mousePressEvent(QMouseEvent* event) override; +private slots: + void quit(); + private: #if defined(Q_OS_WIN) || defined(Q_OS_LINUX) void setupTrayIcon(); @@ -66,6 +70,7 @@ public slots: bool m_tray_available; QPointer m_tray_icon; QPointer m_tray_menu; + QAtomicInteger m_quit{false}; #endif QPointer m_mouse_menu; #if defined(Q_OS_LINUX)