yacreader/YACReaderLibrary/trayicon_controller.cpp
Igor Kushnir 2acfbbfac7 Library: add Quit shortcut
The Ctrl+Q shortcut is assigned to Quit action in most applications on
GNU/Linux. Command+Q is used on macOS. The added shortcut should be
automatically mapped to Command+Q on macOS judging by the following
quote from QKeySequence class documentation:
Note: On macOS, references to "Ctrl", Qt::CTRL, Qt::Key_Control and
Qt::ControlModifier correspond to the Command keys on the Macintosh
keyboard

QKeySequence::Quit could be used as the default key sequence in place of
`Qt::CTRL | Qt::Key_Q`. This would leave the shortcut unassigned by
default on Windows. But YACReader doesn't use QKeySequence::StandardKey
anywhere, so perhaps this shortcut should be hard-coded too.

The shortcut is particularly useful when Close to tray option is
enabled, because in this case closing the Library window with a system
window manager shortcut simply hides it.
2021-05-28 19:34:24 +02:00

112 lines
3.0 KiB
C++

#include "trayicon_controller.h"
#include "yacreader_global_gui.h"
#include "library_window.h"
#include <QtWidgets>
#include <QMessageBox>
#ifdef Q_OS_MACOS
#include "trayhandler.h"
#endif
using namespace YACReader;
TrayIconController::TrayIconController(QSettings *settings, LibraryWindow *window)
: QObject(nullptr), settings(settings), window(window)
{
// If a window icon was set in main() we reuse it for the tray too.
// This allows support for third party icon themes on Freedesktop(Linux/Unix)
// systems.
if (!QApplication::windowIcon().isNull()) {
trayIcon.setIcon(QApplication::windowIcon());
} else {
#ifdef Q_OS_WIN
trayIcon.setIcon(QIcon(":/icon.ico"));
#else
#ifdef Q_OS_MACOS
auto icon = QIcon(":/macostrayicon.svg");
icon.setIsMask(true);
trayIcon.setIcon(icon);
#else
trayIcon.setIcon(QIcon(":/images/iconLibrary.png"));
#endif
#endif
}
connect(&trayIcon, &QSystemTrayIcon::activated,
[=](QSystemTrayIcon::ActivationReason reason) {
#ifdef Q_OS_LINUX
auto expectedReason = QSystemTrayIcon::Trigger;
#else
auto expectedReason = QSystemTrayIcon::DoubleClick;
#endif
if (reason == expectedReason) {
showWindow();
}
});
auto restoreAction = new QAction(tr("&Restore"), this);
connect(restoreAction, &QAction::triggered, this, &TrayIconController::showWindow);
trayIconMenu = new QMenu(this->window);
trayIconMenu->addAction(restoreAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(this->window->quitAction);
trayIcon.setContextMenu(trayIconMenu);
updateIconVisibility();
}
void TrayIconController::updateIconVisibility()
{
trayIcon.setVisible(settings->value(CLOSE_TO_TRAY, false).toBool());
}
bool TrayIconController::handleCloseToTrayIcon(QCloseEvent *event)
{
#ifdef Q_OS_OSX
if (!event->spontaneous() || !window->isVisible()) {
return false;
}
#endif
if (trayIcon.isVisible()) {
if (!settings->value(CLOSE_TO_TRAY_NOTIFIED, false).toBool()) {
QMessageBox::information(window, tr("Systray"),
tr("YACReaderLibrary will keep running in the "
"system tray. To terminate the program, "
"choose <b>Quit</b> in the context menu "
"of the system tray icon."));
settings->setValue(CLOSE_TO_TRAY_NOTIFIED, true);
}
#ifdef Q_OS_OSX
OSXHideDockIcon();
#endif
window->hide();
event->ignore();
return true;
} else {
return false;
}
}
void TrayIconController::showWindow()
{
#ifdef Q_OS_MACOS
OSXShowDockIcon();
window->showNormal();
#else
if (window->isMinimized()) {
window->setWindowState((window->windowState() & ~Qt::WindowMinimized));
} else {
window->show();
}
#endif
window->raise(); // for MacOS
window->activateWindow(); // for Windows
}