Skip to content

Commit

Permalink
[ADDED] MacOS下点击x按钮不退出程序 (vnotex#1102)
Browse files Browse the repository at this point in the history
Stay in the tray after closing main window on macOS.
  • Loading branch information
zeroleo12345 authored and tamlok committed Dec 10, 2019
1 parent a2841c3 commit 2b6493c
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "vsingleinstanceguard.h"
#include "vconfigmanager.h"
#include "vpalette.h"
#include "vapplication.h"

VConfigManager *g_config;

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions src/src.pro
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ TRANSLATIONS += translations/vnote_zh_CN.ts \
}

SOURCES += main.cpp\
vapplication.cpp \
vimagehosting.cpp \
vmainwindow.cpp \
vdirectorytree.cpp \
Expand Down Expand Up @@ -165,6 +166,7 @@ SOURCES += main.cpp\
dialog/vinserttabledialog.cpp

HEADERS += vmainwindow.h \
vapplication.h \
vdirectorytree.h \
vimagehosting.h \
vnote.h \
Expand Down
18 changes: 18 additions & 0 deletions src/vapplication.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 27 additions & 0 deletions src/vapplication.h
Original file line number Diff line number Diff line change
@@ -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
10 changes: 9 additions & 1 deletion src/vmainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 2b6493c

Please sign in to comment.