Browse Source

[ADDED] MacOS下点击x按钮不退出程序 (#1102)

Stay in the tray after closing main window on macOS.
lyn 6 years ago
parent
commit
2b6493c4e5
6 changed files with 60 additions and 2 deletions
  1. 1 0
      CMakeLists.txt
  2. 3 1
      src/main.cpp
  3. 2 0
      src/src.pro
  4. 18 0
      src/vapplication.cpp
  5. 27 0
      src/vapplication.h
  6. 9 1
      src/vmainwindow.cpp

+ 1 - 0
CMakeLists.txt

@@ -13,6 +13,7 @@ set(CMAKE_AUTOUIC ON)
 set(CMAKE_AUTORCC ON)
 
 ## Default Qt5 path
+set(Qt5_DIR "$ENV{QTDIR}/lib/cmake/Qt5")
 if(WIN32)
   set(Qt5_DIR "c:/Qt/Qt5.9.7/5.9.7/msvc2017_64/lib/cmake/Qt5/" CACHE PATH "directory where Qt5Config.cmake exists.")
 elseif(APPLE)

+ 3 - 1
src/main.cpp

@@ -16,6 +16,7 @@
 #include "vsingleinstanceguard.h"
 #include "vconfigmanager.h"
 #include "vpalette.h"
+#include "vapplication.h"
 
 VConfigManager *g_config;
 
@@ -159,7 +160,7 @@ int main(int argc, char *argv[])
     }
 #endif
 
-    QApplication app(argc, argv);
+    VApplication app(argc, argv);
 
     // The file path passed via command line arguments.
     QStringList filePaths = VUtils::filterFilePathsToOpen(app.arguments().mid(1));
@@ -264,6 +265,7 @@ int main(int argc, char *argv[])
 
     w.kickOffStartUpTimer(filePaths);
 
+    app.setWindow(&w);
     int ret = app.exec();
     if (ret == RESTART_EXIT_CODE) {
         // Ask to restart VNote.

+ 2 - 0
src/src.pro

@@ -28,6 +28,7 @@ TRANSLATIONS += translations/vnote_zh_CN.ts \
 }
 
 SOURCES += main.cpp\
+    vapplication.cpp \
     vimagehosting.cpp \
     vmainwindow.cpp \
     vdirectorytree.cpp \
@@ -165,6 +166,7 @@ SOURCES += main.cpp\
     dialog/vinserttabledialog.cpp
 
 HEADERS  += vmainwindow.h \
+    vapplication.h \
     vdirectorytree.h \
     vimagehosting.h \
     vnote.h \

+ 18 - 0
src/vapplication.cpp

@@ -0,0 +1,18 @@
+#include "vapplication.h"
+
+VApplication::VApplication(int &argc, char **argv)
+    : QApplication(argc, argv)
+{
+    connect(this, &QApplication::applicationStateChanged, this, &VApplication::onApplicationStateChanged);
+}
+
+void VApplication::onApplicationStateChanged(Qt::ApplicationState state)
+{
+#if defined(Q_OS_MACOS) || defined(Q_OS_MAC)
+    if(state == Qt::ApplicationActive) {
+        this->window->show();
+        // Need to call raise() in macOS.
+        this->window->raise();
+    }
+#endif
+}

+ 27 - 0
src/vapplication.h

@@ -0,0 +1,27 @@
+#ifndef VAPPLICATION_H
+#define VAPPLICATION_H
+
+#include <QApplication>
+#include <QDebug>
+#include "vmainwindow.h"
+
+class VApplication : public QApplication
+{
+    Q_OBJECT
+public:
+    explicit VApplication(int &argc, char **argv);
+
+    void setWindow(VMainWindow * window)
+    {
+        this->window = window;
+    }
+
+
+public slots:
+    void onApplicationStateChanged(Qt::ApplicationState state);
+
+private:
+    VMainWindow *window;
+};
+
+#endif // VAPPLICATION_H

+ 9 - 1
src/vmainwindow.cpp

@@ -131,6 +131,9 @@ VMainWindow::VMainWindow(VSingleInstanceGuard *p_guard, QWidget *p_parent)
     initUpdateTimer();
 
     registerCaptainAndNavigationTargets();
+#if defined(Q_OS_MACOS) || defined(Q_OS_MAC)
+    QApplication::setQuitOnLastWindowClosed(false);
+#endif
 }
 
 void VMainWindow::initSharedMemoryWatcher()
@@ -2251,7 +2254,10 @@ void VMainWindow::closeEvent(QCloseEvent *event)
 
 #if defined(Q_OS_MACOS) || defined(Q_OS_MAC)
     // Do not support minimized to tray on macOS.
-    isExit = true;
+    if (!isExit) {
+        event->accept();
+        return;
+    }
 #endif
 
     if (!isExit && g_config->getMinimizeToStystemTray() == -1) {
@@ -2747,9 +2753,11 @@ void VMainWindow::initTrayIcon()
 
     connect(m_trayIcon, &QSystemTrayIcon::activated,
             this, [this](QSystemTrayIcon::ActivationReason p_reason){
+#if !defined(Q_OS_MACOS) && !defined(Q_OS_MAC)
                 if (p_reason == QSystemTrayIcon::Trigger) {
                     this->showMainWindow();
                 }
+#endif
             });
 
     m_trayIcon->show();