mirror of
https://github.com/YACReader/yacreader
synced 2025-05-28 03:10:27 -04:00
Extract the system tray functionality to its own class
LibraryWindow is already a monster, so we'll try to not make it bigger.
This commit is contained in:
parent
3c07e7269a
commit
8e99d9e59f
@ -13,6 +13,7 @@ INCLUDEPATH += . \
|
||||
./comic_vine/model
|
||||
|
||||
DEFINES += SERVER_RELEASE NOMINMAX YACREADER_LIBRARY
|
||||
QMAKE_MAC_SDK = macosx10.15
|
||||
|
||||
# load default build flags
|
||||
include (../config.pri)
|
||||
@ -115,6 +116,7 @@ HEADERS += comic_flow.h \
|
||||
../common/pdf_comic.h \
|
||||
no_libraries_widget.h \
|
||||
import_widget.h \
|
||||
trayicon_controller.h \
|
||||
yacreader_local_server.h \
|
||||
yacreader_main_toolbar.h \
|
||||
comics_remover.h \
|
||||
@ -184,6 +186,7 @@ SOURCES += comic_flow.cpp \
|
||||
../common/onstart_flow_selection_dialog.cpp \
|
||||
no_libraries_widget.cpp \
|
||||
import_widget.cpp \
|
||||
trayicon_controller.cpp \
|
||||
yacreader_local_server.cpp \
|
||||
yacreader_main_toolbar.cpp \
|
||||
comics_remover.cpp \
|
||||
|
@ -81,14 +81,15 @@
|
||||
|
||||
#include "yacreader_comics_views_manager.h"
|
||||
|
||||
#include "trayicon_controller.h"
|
||||
|
||||
#include "QsLog.h"
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
#include <shellapi.h>
|
||||
#endif
|
||||
#ifdef Q_OS_MACOS
|
||||
#include "trayhandler.h"
|
||||
#endif
|
||||
|
||||
using namespace YACReader;
|
||||
|
||||
LibraryWindow::LibraryWindow()
|
||||
: QMainWindow(), fullscreen(false), previousFilter(""), fetching(false), status(LibraryWindow::Normal), removeError(false)
|
||||
@ -140,50 +141,26 @@ void LibraryWindow::setupUI()
|
||||
//if(settings->value(USE_OPEN_GL).toBool() == false)
|
||||
showMaximized();
|
||||
|
||||
// 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
|
||||
trayIconController = new TrayIconController(settings, this);
|
||||
}
|
||||
|
||||
connect(&trayIcon, &QSystemTrayIcon::activated,
|
||||
[=](QSystemTrayIcon::ActivationReason reason) {
|
||||
if (reason == QSystemTrayIcon::Trigger) {
|
||||
#ifdef Q_OS_MACOS
|
||||
OSXShowDockIcon();
|
||||
#endif
|
||||
setWindowState((windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
|
||||
show();
|
||||
}
|
||||
});
|
||||
trayIcon.setVisible(settings->value(MINIMIZE_TO_TRAY, true).toBool());
|
||||
}
|
||||
|
||||
void LibraryWindow::changeEvent(QEvent *event)
|
||||
/*void LibraryWindow::changeEvent(QEvent *event)
|
||||
{
|
||||
QMainWindow::changeEvent(event);
|
||||
|
||||
if (event->type() == QEvent::WindowStateChange && isMinimized() &&
|
||||
trayIcon.isVisible()) {
|
||||
#ifdef Q_OS_MACOS
|
||||
OSXHideDockIcon();
|
||||
#endif
|
||||
hide();
|
||||
return;
|
||||
}
|
||||
QMainWindow::changeEvent(event);
|
||||
} else if (event->type() == QEvent::WindowStateChange) {
|
||||
#ifdef Q_OS_MACOS
|
||||
OSXShowDockIcon();
|
||||
#endif
|
||||
show();
|
||||
}
|
||||
}*/
|
||||
|
||||
void LibraryWindow::doLayout()
|
||||
{
|
||||
@ -2297,8 +2274,9 @@ void LibraryWindow::importLibrary(QString clc, QString destPath, QString name)
|
||||
|
||||
void LibraryWindow::reloadOptions()
|
||||
{
|
||||
//comicFlow->setFlowType(flowType);
|
||||
comicsViewsManager->comicsView->updateConfig(settings);
|
||||
|
||||
trayIconController->updateIconVisibility();
|
||||
}
|
||||
|
||||
QString LibraryWindow::currentPath()
|
||||
@ -2334,6 +2312,13 @@ void LibraryWindow::showImportComicsInfo()
|
||||
#include "startup.h"
|
||||
extern Startup *s;
|
||||
void LibraryWindow::closeEvent(QCloseEvent *event)
|
||||
{
|
||||
if (!trayIconController->handleCloseToTrayIcon(event)) {
|
||||
closeApp(event);
|
||||
}
|
||||
}
|
||||
|
||||
void LibraryWindow::closeApp(QCloseEvent *event)
|
||||
{
|
||||
s->stop();
|
||||
settings->setValue(MAIN_WINDOW_GEOMETRY, saveGeometry());
|
||||
|
@ -5,7 +5,6 @@
|
||||
#include <QMap>
|
||||
#include <QModelIndex>
|
||||
#include <QFileInfo>
|
||||
#include <QSystemTrayIcon>
|
||||
|
||||
#include "yacreader_global_gui.h"
|
||||
#include "yacreader_libraries.h"
|
||||
@ -77,6 +76,10 @@ class EmptySpecialListWidget;
|
||||
class EmptyReadingListWidget;
|
||||
class YACReaderComicsViewsManager;
|
||||
|
||||
namespace YACReader {
|
||||
class TrayIconController;
|
||||
}
|
||||
|
||||
#include "comic_db.h"
|
||||
|
||||
using namespace YACReader;
|
||||
@ -385,7 +388,7 @@ public slots:
|
||||
void checkMaxNumLibraries();
|
||||
void showErrorUpgradingLibrary(const QString &path);
|
||||
|
||||
void changeEvent(QEvent *event);
|
||||
void closeApp(QCloseEvent *event);
|
||||
|
||||
private:
|
||||
//fullscreen mode in Windows for preventing this bug: QTBUG-41309 https://bugreports.qt.io/browse/QTBUG-41309
|
||||
@ -393,7 +396,8 @@ private:
|
||||
QPoint previousPos;
|
||||
QSize previousSize;
|
||||
std::future<void> upgradeLibraryFuture;
|
||||
QSystemTrayIcon trayIcon;
|
||||
|
||||
TrayIconController *trayIconController;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
100
YACReaderLibrary/trayicon_controller.cpp
Normal file
100
YACReaderLibrary/trayicon_controller.cpp
Normal file
@ -0,0 +1,100 @@
|
||||
#include "trayicon_controller.h"
|
||||
|
||||
#include "yacreader_global_gui.h"
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QMessageBox>
|
||||
|
||||
#ifdef Q_OS_MACOS
|
||||
#include "trayhandler.h"
|
||||
#endif
|
||||
|
||||
using namespace YACReader;
|
||||
|
||||
TrayIconController::TrayIconController(QSettings *settings, QMainWindow *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) {
|
||||
if (reason == QSystemTrayIcon::DoubleClick) {
|
||||
showWindow();
|
||||
}
|
||||
});
|
||||
|
||||
updateIconVisibility();
|
||||
|
||||
auto restoreAction = new QAction(tr("&Restore"), this);
|
||||
connect(restoreAction, &QAction::triggered, this, &TrayIconController::showWindow);
|
||||
|
||||
auto quitAction = new QAction(tr("&Quit"), this);
|
||||
connect(quitAction, &QAction::triggered, window, &QMainWindow::close);
|
||||
|
||||
trayIconMenu = new QMenu(this->window);
|
||||
trayIconMenu->addAction(restoreAction);
|
||||
trayIconMenu->addSeparator();
|
||||
trayIconMenu->addAction(quitAction);
|
||||
|
||||
trayIcon.setContextMenu(trayIconMenu);
|
||||
}
|
||||
|
||||
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();
|
||||
#endif
|
||||
window->showNormal();
|
||||
window->raise(); // for MacOS
|
||||
window->activateWindow(); // for Windows
|
||||
}
|
34
YACReaderLibrary/trayicon_controller.h
Normal file
34
YACReaderLibrary/trayicon_controller.h
Normal file
@ -0,0 +1,34 @@
|
||||
#ifndef TRAYICON_CONTROLLER_H
|
||||
#define TRAYICON_CONTROLLER_H
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QSystemTrayIcon>
|
||||
|
||||
namespace YACReader {
|
||||
|
||||
class TrayIconController : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
TrayIconController(QSettings *settings,
|
||||
QMainWindow *window);
|
||||
|
||||
void updateIconVisibility();
|
||||
|
||||
bool handleCloseToTrayIcon(QCloseEvent *event);
|
||||
|
||||
QSystemTrayIcon trayIcon;
|
||||
|
||||
public slots:
|
||||
void showWindow();
|
||||
|
||||
private:
|
||||
QSettings *settings;
|
||||
QMainWindow *window;
|
||||
|
||||
QMenu *trayIconMenu;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // TRAYICON_CONTROLLER_H
|
Loading…
Reference in New Issue
Block a user