* fix PR #1601

* use mono icon on macOS
This commit is contained in:
Le Tan 2020-12-19 04:26:43 -08:00 committed by GitHub
parent cb14461f58
commit 3cc882829a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 214 additions and 132 deletions

View File

@ -41,10 +41,6 @@ void CoreConfig::init(const QJsonObject &p_app,
if (m_toolBarIconSize <= 0) { if (m_toolBarIconSize <= 0) {
m_toolBarIconSize = 16; m_toolBarIconSize = 16;
} }
if (!isUndefinedKey(appObj, userObj, "minimize_to_system_tray")) {
m_minimizeToSystemTray = READBOOL(QStringLiteral("minimize_to_system_tray")) ? 1 : 0;
}
} }
QJsonObject CoreConfig::toJson() const QJsonObject CoreConfig::toJson() const
@ -54,9 +50,6 @@ QJsonObject CoreConfig::toJson() const
obj[QStringLiteral("locale")] = m_locale; obj[QStringLiteral("locale")] = m_locale;
obj[QStringLiteral("shortcuts")] = saveShortcuts(); obj[QStringLiteral("shortcuts")] = saveShortcuts();
obj[QStringLiteral("toolbar_icon_size")] = m_toolBarIconSize; obj[QStringLiteral("toolbar_icon_size")] = m_toolBarIconSize;
if (m_minimizeToSystemTray != -1) {
obj[QStringLiteral("minimize_to_system_tray")] = m_minimizeToSystemTray > 0;
}
return obj; return obj;
} }
@ -130,11 +123,3 @@ void CoreConfig::setToolBarIconSize(int p_size)
Q_ASSERT(p_size > 0); Q_ASSERT(p_size > 0);
updateConfig(m_toolBarIconSize, p_size, this); updateConfig(m_toolBarIconSize, p_size, this);
} }
int CoreConfig::getMinimizeToSystemTray() const {
return m_minimizeToSystemTray;
}
void CoreConfig::setMinimizeToSystemTray(bool state){
updateConfig(m_minimizeToSystemTray, int(state), this);
}

View File

@ -48,9 +48,6 @@ namespace vnotex
int getToolBarIconSize() const; int getToolBarIconSize() const;
void setToolBarIconSize(int p_size); void setToolBarIconSize(int p_size);
int getMinimizeToSystemTray() const;
void setMinimizeToSystemTray(bool state);
static const QStringList &getAvailableLocales(); static const QStringList &getAvailableLocales();
private: private:
@ -70,12 +67,6 @@ namespace vnotex
// Icon size of MainWindow tool bar. // Icon size of MainWindow tool bar.
int m_toolBarIconSize = 16; int m_toolBarIconSize = 16;
// Whether to minimize to tray.
// -1 for prompting for user;
// 0 for disabling minimizing to system tray;
// 1 for enabling minimizing to system tray;
int m_minimizeToSystemTray = -1;
static QStringList s_availableLocales; static QStringList s_availableLocales;
}; };
} // ns vnotex } // ns vnotex

View File

@ -73,6 +73,8 @@ namespace vnotex
}; };
Q_DECLARE_FLAGS(FindOptions, FindOption); Q_DECLARE_FLAGS(FindOptions, FindOption);
enum { RESTART_EXIT_CODE = 1000 };
} // ns vnotex } // ns vnotex
Q_DECLARE_OPERATORS_FOR_FLAGS(vnotex::FindOptions); Q_DECLARE_OPERATORS_FOR_FLAGS(vnotex::FindOptions);

View File

@ -130,10 +130,13 @@ namespace vnotex
const QJsonObject &p_user, const QJsonObject &p_user,
const QString &p_key) const QString &p_key)
{ {
if (p_user.find(p_key) == p_user.end() && p_default.find(p_key) == p_default.end()) { return !p_default.contains(p_key) && !p_user.contains(p_key);
return true; }
}
return false; static bool isUndefinedKey(const QJsonObject &p_obj,
const QString &p_key)
{
return !p_obj.contains(p_key);
} }
template <typename T> template <typename T>

View File

@ -72,7 +72,7 @@ void SessionConfig::loadCore(const QJsonObject &p_session)
m_openGL = stringToOpenGL(option); m_openGL = stringToOpenGL(option);
} }
if (coreObj.contains(QStringLiteral("system_title_bar"))) { if (!isUndefinedKey(coreObj, QStringLiteral("system_title_bar"))) {
m_systemTitleBarEnabled = readBool(coreObj, QStringLiteral("system_title_bar")); m_systemTitleBarEnabled = readBool(coreObj, QStringLiteral("system_title_bar"));
} else { } else {
// Enable system title bar on macOS by default. // Enable system title bar on macOS by default.
@ -82,6 +82,10 @@ void SessionConfig::loadCore(const QJsonObject &p_session)
m_systemTitleBarEnabled = false; m_systemTitleBarEnabled = false;
#endif #endif
} }
if (!isUndefinedKey(coreObj, QStringLiteral("minimize_to_system_tray"))) {
m_minimizeToSystemTray = readBool(coreObj, QStringLiteral("minimize_to_system_tray")) ? 1 : 0;
}
} }
QJsonObject SessionConfig::saveCore() const QJsonObject SessionConfig::saveCore() const
@ -91,6 +95,9 @@ QJsonObject SessionConfig::saveCore() const
coreObj[QStringLiteral("current_notebook_root_folder_path")] = m_currentNotebookRootFolderPath; coreObj[QStringLiteral("current_notebook_root_folder_path")] = m_currentNotebookRootFolderPath;
coreObj[QStringLiteral("opengl")] = openGLToString(m_openGL); coreObj[QStringLiteral("opengl")] = openGLToString(m_openGL);
coreObj[QStringLiteral("system_title_bar")] = m_systemTitleBarEnabled; coreObj[QStringLiteral("system_title_bar")] = m_systemTitleBarEnabled;
if (m_minimizeToSystemTray != -1) {
coreObj[QStringLiteral("minimize_to_system_tray")] = m_minimizeToSystemTray > 0;
}
return coreObj; return coreObj;
} }
@ -260,3 +267,13 @@ void SessionConfig::setSystemTitleBarEnabled(bool p_enabled)
{ {
updateConfig(m_systemTitleBarEnabled, p_enabled, this); updateConfig(m_systemTitleBarEnabled, p_enabled, this);
} }
int SessionConfig::getMinimizeToSystemTray() const
{
return m_minimizeToSystemTray;
}
void SessionConfig::setMinimizeToSystemTray(bool p_enabled)
{
updateConfig(m_minimizeToSystemTray, p_enabled ? 1 : 0, this);
}

View File

@ -79,6 +79,9 @@ namespace vnotex
static QString openGLToString(OpenGL p_option); static QString openGLToString(OpenGL p_option);
static OpenGL stringToOpenGL(const QString &p_str); static OpenGL stringToOpenGL(const QString &p_str);
int getMinimizeToSystemTray() const;
void setMinimizeToSystemTray(bool p_enabled);
private: private:
void loadCore(const QJsonObject &p_session); void loadCore(const QJsonObject &p_session);
@ -105,6 +108,12 @@ namespace vnotex
// Whether use system's title bar or not. // Whether use system's title bar or not.
bool m_systemTitleBarEnabled = false; bool m_systemTitleBarEnabled = false;
// Whether to minimize to tray.
// -1 for prompting for user;
// 0 for disabling minimizing to system tray;
// 1 for enabling minimizing to system tray.
int m_minimizeToSystemTray = -1;
}; };
} // ns vnotex } // ns vnotex

View File

@ -23,8 +23,6 @@ bool SingleInstanceGuard::tryRun()
// this will attach to the old segment, then exit, freeing the old segment. // this will attach to the old segment, then exit, freeing the old segment.
if (m_sharedMemory.attach()) { if (m_sharedMemory.attach()) {
qInfo() << "another instance is running"; qInfo() << "another instance is running";
// So try to show it?
showInstance();
return false; return false;
} }

View File

@ -74,5 +74,7 @@
<file>icons/find_replace_editor.svg</file> <file>icons/find_replace_editor.svg</file>
<file>logo/vnote.svg</file> <file>logo/vnote.svg</file>
<file>logo/vnote.png</file> <file>logo/vnote.png</file>
<file>logo/256x256/vnote.png</file>
<file>logo/vnote_mono.png</file>
</qresource> </qresource>
</RCC> </RCC>

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

View File

@ -8,6 +8,7 @@
#include <QOpenGLContext> #include <QOpenGLContext>
#include <QDateTime> #include <QDateTime>
#include <QSysInfo> #include <QSysInfo>
#include <QProcess>
#include <core/configmgr.h> #include <core/configmgr.h>
#include <core/mainconfig.h> #include <core/mainconfig.h>
@ -16,13 +17,12 @@
#include <core/singleinstanceguard.h> #include <core/singleinstanceguard.h>
#include <core/vnotex.h> #include <core/vnotex.h>
#include <core/logger.h> #include <core/logger.h>
#include <core/global.h>
#include <widgets/mainwindow.h> #include <widgets/mainwindow.h>
#include <QWebEngineSettings> #include <QWebEngineSettings>
#include <core/exception.h> #include <core/exception.h>
#include <widgets/messageboxhelper.h> #include <widgets/messageboxhelper.h>
#include <QProcess>
using namespace vnotex; using namespace vnotex;
void loadTranslators(QApplication &p_app); void loadTranslators(QApplication &p_app);
@ -120,7 +120,7 @@ int main(int argc, char *argv[])
loadTranslators(app); loadTranslators(app);
MainWindow window(nullptr); MainWindow window;
window.show(); window.show();
VNoteX::getInst().getThemeMgr().setBaseBackground(window.palette().color(QPalette::Base)); VNoteX::getInst().getThemeMgr().setBaseBackground(window.palette().color(QPalette::Base));
@ -129,7 +129,7 @@ int main(int argc, char *argv[])
int ret = app.exec(); int ret = app.exec();
if (ret == RESTART_EXIT_CODE) { if (ret == RESTART_EXIT_CODE) {
// Ask to restart VNote. // Asked to restart VNote.
guard.exit(); guard.exit();
QProcess::startDetached(qApp->applicationFilePath(), QStringList()); QProcess::startDetached(qApp->applicationFilePath(), QStringList());
return 0; return 0;

View File

@ -43,7 +43,7 @@ void EditorPage::setupUI()
m_toolBarIconSizeSpinBox->setRange(1, 48); m_toolBarIconSizeSpinBox->setRange(1, 48);
m_toolBarIconSizeSpinBox->setSingleStep(1); m_toolBarIconSizeSpinBox->setSingleStep(1);
const QString label(tr("Toolbar icon size:")); const QString label(tr("Tool bar icon size:"));
mainLayout->addRow(label, m_toolBarIconSizeSpinBox); mainLayout->addRow(label, m_toolBarIconSizeSpinBox);
addSearchItem(label, m_toolBarIconSizeSpinBox->toolTip(), m_toolBarIconSizeSpinBox); addSearchItem(label, m_toolBarIconSizeSpinBox->toolTip(), m_toolBarIconSizeSpinBox);
connect(m_toolBarIconSizeSpinBox, QOverload<int>::of(&QSpinBox::valueChanged), connect(m_toolBarIconSizeSpinBox, QOverload<int>::of(&QSpinBox::valueChanged),

View File

@ -28,6 +28,7 @@ namespace vnotex
void setupUI(); void setupUI();
QComboBox *m_autoSavePolicyComboBox = nullptr; QComboBox *m_autoSavePolicyComboBox = nullptr;
QSpinBox *m_toolBarIconSizeSpinBox = nullptr; QSpinBox *m_toolBarIconSizeSpinBox = nullptr;
}; };
} }

View File

@ -57,21 +57,23 @@ void GeneralPage::setupUI()
} }
#endif #endif
#if not defined(Q_OS_MACOS) #if !defined(Q_OS_MACOS)
{ {
m_systemTrayCheckBox = WidgetsFactory::createCheckBox("System tray"); const QString label(tr("Minimize to system tray"));
m_systemTrayCheckBox = WidgetsFactory::createCheckBox(label, this);
m_systemTrayCheckBox->setToolTip(tr("Minimize to system tray when closing"));
mainLayout->addRow(m_systemTrayCheckBox); mainLayout->addRow(m_systemTrayCheckBox);
addSearchItem(label, m_systemTrayCheckBox->toolTip(), m_systemTrayCheckBox);
connect(m_systemTrayCheckBox, &QCheckBox::stateChanged, connect(m_systemTrayCheckBox, &QCheckBox::stateChanged,
this, &GeneralPage::pageIsChanged); this, &GeneralPage::pageIsChanged);
} }
#endif #endif
} }
void GeneralPage::loadInternal() void GeneralPage::loadInternal()
{ {
const auto &coreConfig = ConfigMgr::getInst().getCoreConfig(); const auto &coreConfig = ConfigMgr::getInst().getCoreConfig();
const auto &sessionConfig = ConfigMgr::getInst().getSessionConfig();
{ {
int idx = m_localeComboBox->findData(coreConfig.getLocale()); int idx = m_localeComboBox->findData(coreConfig.getLocale());
@ -80,22 +82,21 @@ void GeneralPage::loadInternal()
} }
if (m_openGLComboBox) { if (m_openGLComboBox) {
const auto &sessionConfig = ConfigMgr::getInst().getSessionConfig();
int idx = m_openGLComboBox->findData(sessionConfig.getOpenGL()); int idx = m_openGLComboBox->findData(sessionConfig.getOpenGL());
Q_ASSERT(idx != -1); Q_ASSERT(idx != -1);
m_openGLComboBox->setCurrentIndex(idx); m_openGLComboBox->setCurrentIndex(idx);
} }
if(m_systemTrayCheckBox){ if (m_systemTrayCheckBox) {
auto toTray = coreConfig.getMinimizeToSystemTray(); int toTray = sessionConfig.getMinimizeToSystemTray();
if(toTray) m_systemTrayCheckBox->setChecked(toTray > 0);
m_systemTrayCheckBox->setChecked(true);
} }
} }
void GeneralPage::saveInternal() void GeneralPage::saveInternal()
{ {
auto &coreConfig = ConfigMgr::getInst().getCoreConfig(); auto &coreConfig = ConfigMgr::getInst().getCoreConfig();
auto &sessionConfig = ConfigMgr::getInst().getSessionConfig();
{ {
auto locale = m_localeComboBox->currentData().toString(); auto locale = m_localeComboBox->currentData().toString();
@ -103,13 +104,13 @@ void GeneralPage::saveInternal()
} }
if (m_openGLComboBox) { if (m_openGLComboBox) {
auto &sessionConfig = ConfigMgr::getInst().getSessionConfig();
int opt = m_openGLComboBox->currentData().toInt(); int opt = m_openGLComboBox->currentData().toInt();
sessionConfig.setOpenGL(static_cast<SessionConfig::OpenGL>(opt)); sessionConfig.setOpenGL(static_cast<SessionConfig::OpenGL>(opt));
} }
if(m_systemTrayCheckBox) { if (m_systemTrayCheckBox) {
coreConfig.setMinimizeToSystemTray(m_systemTrayCheckBox->isChecked()); // This will override the -1 state. That is fine.
sessionConfig.setMinimizeToSystemTray(m_systemTrayCheckBox->isChecked());
} }
} }

View File

@ -29,7 +29,6 @@ namespace vnotex
QComboBox *m_openGLComboBox = nullptr; QComboBox *m_openGLComboBox = nullptr;
QCheckBox *m_systemTrayCheckBox = nullptr; QCheckBox *m_systemTrayCheckBox = nullptr;
}; };
} }

View File

@ -16,8 +16,7 @@
#include <QApplication> #include <QApplication>
#include <QShortcut> #include <QShortcut>
#include <QSystemTrayIcon> #include <QSystemTrayIcon>
#include <QMenu> #include <QWindowStateChangeEvent>
#include <QIcon>
#include "toolbox.h" #include "toolbox.h"
#include "notebookexplorer.h" #include "notebookexplorer.h"
@ -30,12 +29,14 @@
#include <core/coreconfig.h> #include <core/coreconfig.h>
#include <core/events.h> #include <core/events.h>
#include <core/fileopenparameters.h> #include <core/fileopenparameters.h>
#include <core/global.h>
#include <widgets/dialogs/scrolldialog.h> #include <widgets/dialogs/scrolldialog.h>
#include "viewwindow.h" #include "viewwindow.h"
#include "outlineviewer.h" #include "outlineviewer.h"
#include <utils/widgetutils.h> #include <utils/widgetutils.h>
#include "navigationmodemgr.h" #include "navigationmodemgr.h"
#include <widgets/messageboxhelper.h> #include "messageboxhelper.h"
#include "systemtrayhelper.h"
#include <vtoolbar.h> #include <vtoolbar.h>
@ -50,18 +51,11 @@ MainWindow::MainWindow(QWidget *p_parent)
setupUI(); setupUI();
initSystemTrayIcon();
setupShortcuts(); setupShortcuts();
loadStateAndGeometry(); loadStateAndGeometry();
#if defined(Q_OS_MACOS) || defined(Q_OS_MAC) // The signal is particularly useful if your application has to do some last-second cleanup.
QApplication::setQuitOnLastWindowClosed(false);
#endif
// The signal is particularly useful if your application has
// to do some last-second cleanup.
// Note that no user interaction is possible in this state. // Note that no user interaction is possible in this state.
connect(qApp, &QCoreApplication::aboutToQuit, connect(qApp, &QCoreApplication::aboutToQuit,
this, &MainWindow::closeOnQuit); this, &MainWindow::closeOnQuit);
@ -91,6 +85,7 @@ void MainWindow::setupUI()
setupDocks(); setupDocks();
setupToolBar(); setupToolBar();
setupStatusBar(); setupStatusBar();
setupSystemTray();
activateDock(m_docks[DockIndex::NavigationDock]); activateDock(m_docks[DockIndex::NavigationDock]);
} }
@ -289,36 +284,27 @@ void MainWindow::setupNotebookExplorer(QWidget *p_parent)
void MainWindow::closeEvent(QCloseEvent *p_event) void MainWindow::closeEvent(QCloseEvent *p_event)
{ {
// TODO: support minimized to system tray. const int toTray = ConfigMgr::getInst().getSessionConfig().getMinimizeToSystemTray();
bool isExit = m_requestQuit > -1 || toTray == 0;
const int exitCode = m_requestQuit;
m_requestQuit = -1;
auto toTray = ConfigMgr::getInst().getCoreConfig().getMinimizeToSystemTray(); #if defined(Q_OS_MACOS)
bool isExit = m_requestQuit;
m_requestQuit = 0;
if (isVisible()) {
saveStateAndGeometry();
}
#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.
if (!isExit) { isExit = true;
p_event->accept();
return;
}
#endif #endif
if(!isExit && toTray == -1){ if(!isExit && toTray == -1){
int ret = MessageBoxHelper::questionYesNo(MessageBoxHelper::Question, int ret = MessageBoxHelper::questionYesNo(MessageBoxHelper::Question,
tr("Close VNote"), tr("Close %1").arg(qApp->applicationName()),
tr("Do you want to minimize VNote to system tray " tr("Do you want to minimize %1 to system tray "
"instead of quitting it when closing VNote?"), "instead of quitting when closing %1?").arg(qApp->applicationName()),
tr("You could change the option in Settings later."), tr("You could change the option in Settings later."),
this); this);
if (ret == QMessageBox::Yes) { if (ret == QMessageBox::Yes) {
ConfigMgr::getInst().getCoreConfig().setMinimizeToSystemTray(true); ConfigMgr::getInst().getSessionConfig().setMinimizeToSystemTray(true);
hide();
} else if (ret == QMessageBox::No) { } else if (ret == QMessageBox::No) {
ConfigMgr::getInst().getCoreConfig().setMinimizeToSystemTray(false); ConfigMgr::getInst().getSessionConfig().setMinimizeToSystemTray(false);
isExit = true; isExit = true;
} else { } else {
p_event->ignore(); p_event->ignore();
@ -326,10 +312,11 @@ void MainWindow::closeEvent(QCloseEvent *p_event)
} }
} }
if(isExit || toTray == 0 || !m_trayIcon->isVisible()){ if (isVisible()) {
// really to quit, process workspace saveStateAndGeometry();
// TODO: process workspace }
if (isExit || !m_trayIcon->isVisible()) {
// Signal out the close event. // Signal out the close event.
auto event = QSharedPointer<Event>::create(); auto event = QSharedPointer<Event>::create();
event->m_response = true; event->m_response = true;
@ -341,8 +328,8 @@ void MainWindow::closeEvent(QCloseEvent *p_event)
} }
QMainWindow::closeEvent(p_event); QMainWindow::closeEvent(p_event);
qApp->quit(); qApp->exit(exitCode > -1 ? exitCode : 0);
}else { } else {
hide(); hide();
p_event->ignore(); p_event->ignore();
} }
@ -523,7 +510,7 @@ void MainWindow::setStayOnTop(bool p_enabled)
bool shown = isVisible(); bool shown = isVisible();
Qt::WindowFlags flags = windowFlags(); Qt::WindowFlags flags = windowFlags();
Qt::WindowFlags magicFlag = Qt::WindowStaysOnTopHint; const Qt::WindowFlags magicFlag = Qt::WindowStaysOnTopHint;
if (p_enabled) { if (p_enabled) {
setWindowFlags(flags | magicFlag); setWindowFlags(flags | magicFlag);
} else { } else {
@ -535,42 +522,49 @@ void MainWindow::setStayOnTop(bool p_enabled)
} }
} }
void MainWindow::initSystemTrayIcon(){ void MainWindow::setupSystemTray()
QMenu *menu = new QMenu(this); {
QAction *showMainWindowAct = menu->addAction(tr("Show VNote")); m_trayIcon = SystemTrayHelper::setupSystemTray(this);
connect(showMainWindowAct, &QAction::triggered,
this, &MainWindow::show);
QAction *exitAct = menu->addAction(tr("Quit"));
connect(exitAct, &QAction::triggered,
this, [this](){
this->m_requestQuit = 1;
this->close();
});
QIcon sysIcon(":/vnotex/data/core/logo/vnote.png");
#if defined(Q_OS_MACOS) || defined(Q_OS_MAC)
sysIcon.setIsMask(true);
#endif
m_trayIcon = new QSystemTrayIcon(sysIcon, this);
m_trayIcon->setToolTip(tr("VNote"));
m_trayIcon->setContextMenu(menu);
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->show();
this->activateWindow();
}
#endif
});
m_trayIcon->show(); m_trayIcon->show();
} }
void MainWindow::restart(){ void MainWindow::restart()
QCoreApplication::exit(RESTART_EXIT_CODE); {
m_requestQuit = RESTART_EXIT_CODE;
close();
}
void MainWindow::changeEvent(QEvent *p_event)
{
if (p_event->type() == QEvent::WindowStateChange) {
QWindowStateChangeEvent *eve = static_cast<QWindowStateChangeEvent *>(p_event);
m_windowOldState = eve->oldState();
}
QMainWindow::changeEvent(p_event);
}
void MainWindow::showMainWindow()
{
if (isMinimized()) {
if (m_windowOldState & Qt::WindowMaximized) {
showMaximized();
} else if (m_windowOldState & Qt::WindowFullScreen) {
showFullScreen();
} else {
showNormal();
}
} else {
show();
// Need to call raise() in macOS.
raise();
}
activateWindow();
}
void MainWindow::quitApp()
{
m_requestQuit = 0;
close();
} }

View File

@ -7,8 +7,6 @@
#include "toolbarhelper.h" #include "toolbarhelper.h"
#include "statusbarhelper.h" #include "statusbarhelper.h"
#define RESTART_EXIT_CODE 1000
class QDockWidget; class QDockWidget;
class QSystemTrayIcon; class QSystemTrayIcon;
@ -47,6 +45,10 @@ namespace vnotex
void restart(); void restart();
void showMainWindow();
void quitApp();
signals: signals:
void mainWindowStarted(); void mainWindowStarted();
@ -61,6 +63,8 @@ namespace vnotex
protected: protected:
void closeEvent(QCloseEvent *p_event) Q_DECL_OVERRIDE; void closeEvent(QCloseEvent *p_event) Q_DECL_OVERRIDE;
void changeEvent(QEvent *p_event) Q_DECL_OVERRIDE;
private slots: private slots:
void closeOnQuit(); void closeOnQuit();
@ -105,13 +109,7 @@ namespace vnotex
void setupShortcuts(); void setupShortcuts();
// Init system tray and correspondign context menu. void setupSystemTray();
void initSystemTrayIcon();
// Tray icon.
QSystemTrayIcon *m_trayIcon;
bool m_requestQuit = false;
ToolBarHelper m_toolBarHelper; ToolBarHelper m_toolBarHelper;
@ -131,6 +129,13 @@ namespace vnotex
bool m_layoutReset = false; bool m_layoutReset = false;
// -1: do not request to quit;
// 0 and above: exit code.
int m_requestQuit = -1;
Qt::WindowStates m_windowOldState = Qt::WindowMinimized;
QSystemTrayIcon *m_trayIcon = nullptr;
}; };
} // ns vnotex } // ns vnotex

View File

@ -0,0 +1,50 @@
#include "systemtrayhelper.h"
#include <QMenu>
#include <QIcon>
#include <QSystemTrayIcon>
#include <QApplication>
#include "mainwindow.h"
#include "widgetsfactory.h"
using namespace vnotex;
QSystemTrayIcon *SystemTrayHelper::setupSystemTray(MainWindow *p_win)
{
#if defined(Q_OS_MACOS)
QIcon icon(":/vnotex/data/core/logo/vnote_mono.png");
icon.setIsMask(true);
#else
QIcon icon(":/vnotex/data/core/logo/256x256/vnote.png");
#endif
auto trayIcon = new QSystemTrayIcon(icon, p_win);
trayIcon->setToolTip(qApp->applicationName());
MainWindow::connect(trayIcon, &QSystemTrayIcon::activated,
p_win, [p_win](QSystemTrayIcon::ActivationReason p_reason) {
#if !defined(Q_OS_MACOS)
if (p_reason == QSystemTrayIcon::Trigger) {
p_win->showMainWindow();
}
#endif
});
auto menu = WidgetsFactory::createMenu(p_win);
trayIcon->setContextMenu(menu);
menu->addAction(MainWindow::tr("Show Main Window"),
menu,
[p_win]() {
p_win->showMainWindow();
});
menu->addAction(MainWindow::tr("Quit"),
menu,
[p_win]() {
p_win->quitApp();
});
return trayIcon;
}

View File

@ -0,0 +1,19 @@
#ifndef SYSTEMTRAYHELPER_H
#define SYSTEMTRAYHELPER_H
class QSystemTrayIcon;
namespace vnotex
{
class MainWindow;
class SystemTrayHelper
{
public:
SystemTrayHelper() = delete;
static QSystemTrayIcon *setupSystemTray(MainWindow *p_win);
};
}
#endif // SYSTEMTRAYHELPER_H

View File

@ -308,6 +308,11 @@ QToolBar *ToolBarHelper::setupSettingsToolBar(MainWindow *p_win, QToolBar *p_too
menu->addSeparator(); menu->addSeparator();
menu->addAction(MainWindow::tr("Quit"),
menu,
[p_win]() {
p_win->quitApp();
});
menu->addAction(MainWindow::tr("Restart"), menu->addAction(MainWindow::tr("Restart"),
menu, menu,
[p_win]() { [p_win]() {

View File

@ -13,7 +13,6 @@
class QLayout; class QLayout;
class QSplitter; class QSplitter;
class QTimer;
namespace vnotex namespace vnotex
{ {

View File

@ -43,6 +43,7 @@ SOURCES += \
$$PWD/outlineprovider.cpp \ $$PWD/outlineprovider.cpp \
$$PWD/outlineviewer.cpp \ $$PWD/outlineviewer.cpp \
$$PWD/propertydefs.cpp \ $$PWD/propertydefs.cpp \
$$PWD/systemtrayhelper.cpp \
$$PWD/textviewwindow.cpp \ $$PWD/textviewwindow.cpp \
$$PWD/toolbarhelper.cpp \ $$PWD/toolbarhelper.cpp \
$$PWD/treeview.cpp \ $$PWD/treeview.cpp \
@ -122,6 +123,7 @@ HEADERS += \
$$PWD/outlineprovider.h \ $$PWD/outlineprovider.h \
$$PWD/outlineviewer.h \ $$PWD/outlineviewer.h \
$$PWD/propertydefs.h \ $$PWD/propertydefs.h \
$$PWD/systemtrayhelper.h \
$$PWD/textviewwindow.h \ $$PWD/textviewwindow.h \
$$PWD/textviewwindowhelper.h \ $$PWD/textviewwindowhelper.h \
$$PWD/toolbarhelper.h \ $$PWD/toolbarhelper.h \