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

Stay in the tray after closing main window on macOS.
This commit is contained in:
lyn 2019-12-10 20:46:22 +08:00 committed by Le Tan
parent a2841c399b
commit 2b6493c4e5
6 changed files with 60 additions and 2 deletions

View File

@ -13,6 +13,7 @@ set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON) set(CMAKE_AUTORCC ON)
## Default Qt5 path ## Default Qt5 path
set(Qt5_DIR "$ENV{QTDIR}/lib/cmake/Qt5")
if(WIN32) 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.") 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) elseif(APPLE)

View File

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

View File

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

18
src/vapplication.cpp Normal file
View File

@ -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
src/vapplication.h Normal file
View File

@ -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

View File

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