mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 05:49:53 +08:00
[ADDED] MacOS下点击x按钮不退出程序 (#1102)
Stay in the tray after closing main window on macOS.
This commit is contained in:
parent
a2841c399b
commit
2b6493c4e5
@ -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)
|
||||
|
@ -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.
|
||||
|
@ -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
src/vapplication.cpp
Normal file
18
src/vapplication.cpp
Normal 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
27
src/vapplication.h
Normal 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
|
@ -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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user