* 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) {
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
@ -54,9 +50,6 @@ QJsonObject CoreConfig::toJson() const
obj[QStringLiteral("locale")] = m_locale;
obj[QStringLiteral("shortcuts")] = saveShortcuts();
obj[QStringLiteral("toolbar_icon_size")] = m_toolBarIconSize;
if (m_minimizeToSystemTray != -1) {
obj[QStringLiteral("minimize_to_system_tray")] = m_minimizeToSystemTray > 0;
}
return obj;
}
@ -130,11 +123,3 @@ void CoreConfig::setToolBarIconSize(int p_size)
Q_ASSERT(p_size > 0);
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;
void setToolBarIconSize(int p_size);
int getMinimizeToSystemTray() const;
void setMinimizeToSystemTray(bool state);
static const QStringList &getAvailableLocales();
private:
@ -70,12 +67,6 @@ namespace vnotex
// Icon size of MainWindow tool bar.
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;
};
} // ns vnotex

View File

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

View File

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

View File

@ -72,7 +72,7 @@ void SessionConfig::loadCore(const QJsonObject &p_session)
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"));
} else {
// Enable system title bar on macOS by default.
@ -82,6 +82,10 @@ void SessionConfig::loadCore(const QJsonObject &p_session)
m_systemTitleBarEnabled = false;
#endif
}
if (!isUndefinedKey(coreObj, QStringLiteral("minimize_to_system_tray"))) {
m_minimizeToSystemTray = readBool(coreObj, QStringLiteral("minimize_to_system_tray")) ? 1 : 0;
}
}
QJsonObject SessionConfig::saveCore() const
@ -91,6 +95,9 @@ QJsonObject SessionConfig::saveCore() const
coreObj[QStringLiteral("current_notebook_root_folder_path")] = m_currentNotebookRootFolderPath;
coreObj[QStringLiteral("opengl")] = openGLToString(m_openGL);
coreObj[QStringLiteral("system_title_bar")] = m_systemTitleBarEnabled;
if (m_minimizeToSystemTray != -1) {
coreObj[QStringLiteral("minimize_to_system_tray")] = m_minimizeToSystemTray > 0;
}
return coreObj;
}
@ -260,3 +267,13 @@ void SessionConfig::setSystemTitleBarEnabled(bool p_enabled)
{
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 OpenGL stringToOpenGL(const QString &p_str);
int getMinimizeToSystemTray() const;
void setMinimizeToSystemTray(bool p_enabled);
private:
void loadCore(const QJsonObject &p_session);
@ -105,6 +108,12 @@ namespace vnotex
// Whether use system's title bar or not.
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

View File

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

View File

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

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

View File

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

View File

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

View File

@ -57,21 +57,23 @@ void GeneralPage::setupUI()
}
#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);
addSearchItem(label, m_systemTrayCheckBox->toolTip(), m_systemTrayCheckBox);
connect(m_systemTrayCheckBox, &QCheckBox::stateChanged,
this, &GeneralPage::pageIsChanged);
}
#endif
}
void GeneralPage::loadInternal()
{
const auto &coreConfig = ConfigMgr::getInst().getCoreConfig();
const auto &sessionConfig = ConfigMgr::getInst().getSessionConfig();
{
int idx = m_localeComboBox->findData(coreConfig.getLocale());
@ -80,22 +82,21 @@ void GeneralPage::loadInternal()
}
if (m_openGLComboBox) {
const auto &sessionConfig = ConfigMgr::getInst().getSessionConfig();
int idx = m_openGLComboBox->findData(sessionConfig.getOpenGL());
Q_ASSERT(idx != -1);
m_openGLComboBox->setCurrentIndex(idx);
}
if (m_systemTrayCheckBox) {
auto toTray = coreConfig.getMinimizeToSystemTray();
if(toTray)
m_systemTrayCheckBox->setChecked(true);
int toTray = sessionConfig.getMinimizeToSystemTray();
m_systemTrayCheckBox->setChecked(toTray > 0);
}
}
void GeneralPage::saveInternal()
{
auto &coreConfig = ConfigMgr::getInst().getCoreConfig();
auto &sessionConfig = ConfigMgr::getInst().getSessionConfig();
{
auto locale = m_localeComboBox->currentData().toString();
@ -103,13 +104,13 @@ void GeneralPage::saveInternal()
}
if (m_openGLComboBox) {
auto &sessionConfig = ConfigMgr::getInst().getSessionConfig();
int opt = m_openGLComboBox->currentData().toInt();
sessionConfig.setOpenGL(static_cast<SessionConfig::OpenGL>(opt));
}
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;
QCheckBox *m_systemTrayCheckBox = nullptr;
};
}

View File

@ -16,8 +16,7 @@
#include <QApplication>
#include <QShortcut>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QIcon>
#include <QWindowStateChangeEvent>
#include "toolbox.h"
#include "notebookexplorer.h"
@ -30,12 +29,14 @@
#include <core/coreconfig.h>
#include <core/events.h>
#include <core/fileopenparameters.h>
#include <core/global.h>
#include <widgets/dialogs/scrolldialog.h>
#include "viewwindow.h"
#include "outlineviewer.h"
#include <utils/widgetutils.h>
#include "navigationmodemgr.h"
#include <widgets/messageboxhelper.h>
#include "messageboxhelper.h"
#include "systemtrayhelper.h"
#include <vtoolbar.h>
@ -50,18 +51,11 @@ MainWindow::MainWindow(QWidget *p_parent)
setupUI();
initSystemTrayIcon();
setupShortcuts();
loadStateAndGeometry();
#if defined(Q_OS_MACOS) || defined(Q_OS_MAC)
QApplication::setQuitOnLastWindowClosed(false);
#endif
// The signal is particularly useful if your application has
// to do some last-second cleanup.
// 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.
connect(qApp, &QCoreApplication::aboutToQuit,
this, &MainWindow::closeOnQuit);
@ -91,6 +85,7 @@ void MainWindow::setupUI()
setupDocks();
setupToolBar();
setupStatusBar();
setupSystemTray();
activateDock(m_docks[DockIndex::NavigationDock]);
}
@ -289,36 +284,27 @@ void MainWindow::setupNotebookExplorer(QWidget *p_parent)
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();
bool isExit = m_requestQuit;
m_requestQuit = 0;
if (isVisible()) {
saveStateAndGeometry();
}
#if defined(Q_OS_MACOS) || defined(Q_OS_MAC)
#if defined(Q_OS_MACOS)
// Do not support minimized to tray on macOS.
if (!isExit) {
p_event->accept();
return;
}
isExit = true;
#endif
if(!isExit && toTray == -1){
int ret = MessageBoxHelper::questionYesNo(MessageBoxHelper::Question,
tr("Close VNote"),
tr("Do you want to minimize VNote to system tray "
"instead of quitting it when closing VNote?"),
tr("Close %1").arg(qApp->applicationName()),
tr("Do you want to minimize %1 to system tray "
"instead of quitting when closing %1?").arg(qApp->applicationName()),
tr("You could change the option in Settings later."),
this);
if (ret == QMessageBox::Yes) {
ConfigMgr::getInst().getCoreConfig().setMinimizeToSystemTray(true);
hide();
ConfigMgr::getInst().getSessionConfig().setMinimizeToSystemTray(true);
} else if (ret == QMessageBox::No) {
ConfigMgr::getInst().getCoreConfig().setMinimizeToSystemTray(false);
ConfigMgr::getInst().getSessionConfig().setMinimizeToSystemTray(false);
isExit = true;
} else {
p_event->ignore();
@ -326,10 +312,11 @@ void MainWindow::closeEvent(QCloseEvent *p_event)
}
}
if(isExit || toTray == 0 || !m_trayIcon->isVisible()){
// really to quit, process workspace
// TODO: process workspace
if (isVisible()) {
saveStateAndGeometry();
}
if (isExit || !m_trayIcon->isVisible()) {
// Signal out the close event.
auto event = QSharedPointer<Event>::create();
event->m_response = true;
@ -341,7 +328,7 @@ void MainWindow::closeEvent(QCloseEvent *p_event)
}
QMainWindow::closeEvent(p_event);
qApp->quit();
qApp->exit(exitCode > -1 ? exitCode : 0);
} else {
hide();
p_event->ignore();
@ -523,7 +510,7 @@ void MainWindow::setStayOnTop(bool p_enabled)
bool shown = isVisible();
Qt::WindowFlags flags = windowFlags();
Qt::WindowFlags magicFlag = Qt::WindowStaysOnTopHint;
const Qt::WindowFlags magicFlag = Qt::WindowStaysOnTopHint;
if (p_enabled) {
setWindowFlags(flags | magicFlag);
} else {
@ -535,42 +522,49 @@ void MainWindow::setStayOnTop(bool p_enabled)
}
}
void MainWindow::initSystemTrayIcon(){
QMenu *menu = new QMenu(this);
QAction *showMainWindowAct = menu->addAction(tr("Show VNote"));
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
});
void MainWindow::setupSystemTray()
{
m_trayIcon = SystemTrayHelper::setupSystemTray(this);
m_trayIcon->show();
}
void MainWindow::restart(){
QCoreApplication::exit(RESTART_EXIT_CODE);
void MainWindow::restart()
{
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 "statusbarhelper.h"
#define RESTART_EXIT_CODE 1000
class QDockWidget;
class QSystemTrayIcon;
@ -47,6 +45,10 @@ namespace vnotex
void restart();
void showMainWindow();
void quitApp();
signals:
void mainWindowStarted();
@ -61,6 +63,8 @@ namespace vnotex
protected:
void closeEvent(QCloseEvent *p_event) Q_DECL_OVERRIDE;
void changeEvent(QEvent *p_event) Q_DECL_OVERRIDE;
private slots:
void closeOnQuit();
@ -105,13 +109,7 @@ namespace vnotex
void setupShortcuts();
// Init system tray and correspondign context menu.
void initSystemTrayIcon();
// Tray icon.
QSystemTrayIcon *m_trayIcon;
bool m_requestQuit = false;
void setupSystemTray();
ToolBarHelper m_toolBarHelper;
@ -131,6 +129,13 @@ namespace vnotex
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

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->addAction(MainWindow::tr("Quit"),
menu,
[p_win]() {
p_win->quitApp();
});
menu->addAction(MainWindow::tr("Restart"),
menu,
[p_win]() {

View File

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

View File

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