diff --git a/CMakeLists.txt b/CMakeLists.txt index 0db8ed3f..669822af 100644 --- a/CMakeLists.txt +++ b/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) diff --git a/src/main.cpp b/src/main.cpp index cdc4715d..0e37615f 100644 --- a/src/main.cpp +++ b/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. diff --git a/src/src.pro b/src/src.pro index f0cef11e..41d9d2f4 100644 --- a/src/src.pro +++ b/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 \ diff --git a/src/vapplication.cpp b/src/vapplication.cpp new file mode 100644 index 00000000..816ebc81 --- /dev/null +++ b/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 +} diff --git a/src/vapplication.h b/src/vapplication.h new file mode 100644 index 00000000..7913d7ed --- /dev/null +++ b/src/vapplication.h @@ -0,0 +1,27 @@ +#ifndef VAPPLICATION_H +#define VAPPLICATION_H + +#include +#include +#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 \ No newline at end of file diff --git a/src/vmainwindow.cpp b/src/vmainwindow.cpp index 9533b7a7..3f93f6a1 100644 --- a/src/vmainwindow.cpp +++ b/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();