Merge pull request #50 from selmf/mac_tray_icon

Minimize to tray, start to tray.
This commit is contained in:
Luis Ángel San Martín 2019-09-22 12:26:02 +02:00 committed by GitHub
commit eab114aa66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 261 additions and 42 deletions

View File

@ -115,6 +115,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 +185,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 \
@ -221,6 +223,11 @@ SOURCES += comic_flow.cpp \
SOURCES += ../common/gl/yacreader_flow_gl.cpp
}
macx {
HEADERS += trayhandler.h
OBJECTIVE_SOURCES += trayhandler.mm
}
include(./server/server.pri)
include(../custom_widgets/custom_widgets_yacreaderlibrary.pri)

View File

@ -71,5 +71,6 @@
<file alias="images/sidebar/delete_sidebar@2x.png">../images/sidebar/delete_sidebar_osx@2x.png</file>
<file alias="images/sidebar/addLabelIcon@2x.png">../images/sidebar/addLabelIcon_osx@2x.png</file>
<file alias="images/sidebar/renameListIcon@2x.png">../images/sidebar/renameListIcon_osx@2x.png</file>
<file>macostrayicon.svg</file>
</qresource>
</RCC>

View File

@ -41,5 +41,6 @@
<file>../images/lists/label_yellow.png</file>
<file>../images/lists/list.png</file>
<file>../images/empty_reading_list.png</file>
<file>icon.ico</file>
</qresource>
</RCC>

View File

@ -81,12 +81,16 @@
#include "yacreader_comics_views_manager.h"
#include "trayicon_controller.h"
#include "QsLog.h"
#ifdef Q_OS_WIN
#include <shellapi.h>
#endif
using namespace YACReader;
LibraryWindow::LibraryWindow()
: QMainWindow(), fullscreen(false), previousFilter(""), fetching(false), status(LibraryWindow::Normal), removeError(false)
{
@ -136,37 +140,26 @@ void LibraryWindow::setupUI()
else
//if(settings->value(USE_OPEN_GL).toBool() == false)
showMaximized();
}
/* //disabled until icons are ready and macos native code is done
trayIcon.setIcon(QApplication::windowIcon());
trayIcon.show();
connect(&trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(trayActivation(QSystemTrayIcon::ActivationReason)));
}
trayIconController = new TrayIconController(settings, this);
}
void LibraryWindow::trayActivation(QSystemTrayIcon::ActivationReason reason)
/*void LibraryWindow::changeEvent(QEvent *event)
{
if (reason == QSystemTrayIcon::Trigger)
{
setWindowState((windowState() & ~Qt::WindowMinimized));
show();
activateWindow();
raise();
}
}
void LibraryWindow::changeEvent(QEvent *event)
{
if (event->type() == QEvent::WindowStateChange && isMinimized())
{
hide();
}
else
{
QMainWindow::changeEvent(event);
}
if (event->type() == QEvent::WindowStateChange && isMinimized() &&
trayIcon.isVisible()) {
#ifdef Q_OS_MACOS
OSXHideDockIcon();
#endif
hide();
} else if (event->type() == QEvent::WindowStateChange) {
#ifdef Q_OS_MACOS
OSXShowDockIcon();
#endif
show();
}
}*/
void LibraryWindow::doLayout()
@ -2281,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()
@ -2318,6 +2312,14 @@ void LibraryWindow::showImportComicsInfo()
#include "startup.h"
extern Startup *s;
void LibraryWindow::closeEvent(QCloseEvent *event)
{
if (!trayIconController->handleCloseToTrayIcon(event)) {
event->accept();
closeApp();
}
}
void LibraryWindow::prepareToCloseApp()
{
s->stop();
settings->setValue(MAIN_WINDOW_GEOMETRY, saveGeometry());
@ -2326,8 +2328,13 @@ void LibraryWindow::closeEvent(QCloseEvent *event)
sideBar->close();
QApplication::instance()->processEvents();
event->accept();
QMainWindow::closeEvent(event);
}
void LibraryWindow::closeApp()
{
prepareToCloseApp();
qApp->exit(0);
}
void LibraryWindow::showNoLibrariesWidget()
@ -2506,14 +2513,14 @@ void LibraryWindow::showFoldersContextMenu(const QPoint &point)
/*
void LibraryWindow::showSocial()
{
socialDialog->move(this->mapToGlobal(QPoint(width()-socialDialog->width()-10, centralWidget()->pos().y()+10)));
socialDialog->move(this->mapToGlobal(QPoint(width()-socialDialog->width()-10, centralWidget()->pos().y()+10)));
QModelIndexList indexList = getSelectedComics();
QModelIndexList indexList = getSelectedComics();
ComicDB comic = dmCV->getComic(indexList.at(0));
ComicDB comic = dmCV->getComic(indexList.at(0));
socialDialog->setComic(comic,currentPath());
socialDialog->setHidden(false);
socialDialog->setComic(comic,currentPath());
socialDialog->setHidden(false);
}*/
void LibraryWindow::libraryAlreadyExists(const QString &name)

View File

@ -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,8 @@ public slots:
void checkMaxNumLibraries();
void showErrorUpgradingLibrary(const QString &path);
//void changeEvent(QEvent *event);
void prepareToCloseApp();
void closeApp();
private:
//fullscreen mode in Windows for preventing this bug: QTBUG-41309 https://bugreports.qt.io/browse/QTBUG-41309
@ -393,10 +397,8 @@ private:
QPoint previousPos;
QSize previousSize;
std::future<void> upgradeLibraryFuture;
QSystemTrayIcon trayIcon;
private slots:
//void trayActivation(QSystemTrayIcon::ActivationReason reason);
TrayIconController *trayIconController;
};
#endif

View File

@ -0,0 +1 @@
<svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024"><defs><style>.cls-1{fill:#fff;}</style></defs><title>macostrayicon</title><path class="cls-1" d="M863,420c.07,1.35.15,2.71.23,4.07,1.85,51.43-1.77,102.3-23.84,149.6C799.89,658.4,735,711.58,641.71,727.79c-5,.86-8.34,2.66-11.39,6.61-26.89,34.82-57,66.52-91.64,93.76a349.72,349.72,0,0,1-84,48.51c-14.76,6.05-24.68,6.14-39.85,2.24,11.35-13.65,22.67-27,33.72-40.63,14.58-17.95,24.69-38.19,31.82-60.23a63.73,63.73,0,0,0,.1-40.46c-2.17-6.85-5.54-9.16-12.72-9.83-52.14-4.84-104.6-7-156.18-16.84-26.13-5-51.79-12.14-73.46-28.36C223.89,671.89,213,657.7,201.6,644c-18.72-22.69-30.23-49.36-33.29-77.39-4.78-43.79-5.75-88.2,4.84-131.78,5.1-19.69,12.44-38.56,20.87-57,4.48-9.81,11.52-17.91,18.4-25.95,20.42-23.89,43-45.11,69-63.38C344.4,244.25,415.52,224,490,212.14c35.83-5.69,71.83-9.79,108.14-8.25,59.23,2.52,115.82,15.17,166.67,47.27,50.06,31.6,77.35,78.9,91.95,134.86,1.92,7.35.87,15.58,6.09,22C860.69,412.06,862.71,416,863,420Zm-464.2-27.77c4.1-9.05.59-16.07-6.69-21.14-6.71-4.67-14.31-4.55-20.81.6-7.51,5.95-9.7,13.63-4.95,22.55,2.73,7.72,8.94,10.58,16.31,10.33C390.6,404.32,396.48,400.31,398.83,392.24ZM422,395c4,8.31,11.51,8.41,19.11,8.41H546.93q63.59,0,127.17,0c9.75,0,17.57-2.6,20.17-13.29,2.94-14-4.38-23.26-19.3-23.46-21.72-.28-43.45-.08-65.17-.08q-84.4,0-168.8,0C422,366.67,413.86,379.12,422,395Zm134.41,93.77c39.58,0,79.15.1,118.72-.06,13.05-.05,22.08-9.48,20.37-20.51-1.55-10-9.54-16-21.95-16q-66.3-.14-132.61,0c-33.52,0-67-.07-100.54,0-13.59,0-22.72,8.13-22.15,19.17.53,10.36,9,17.35,21.59,17.38C478.73,488.83,517.58,488.77,556.44,488.76Zm.23,85.58c39.21,0,78.42.06,117.64,0,12.9,0,21.77-7.91,21.5-18.6-.25-10-9.25-18-21.18-18q-117.63-.15-235.27-.05c-5.05,0-10,.55-14,4-5.84,5-9.36,11.49-6.75,18.84,3,8.55,9.28,13.93,19.37,13.89C477.53,574.23,517.1,574.33,556.67,574.34Zm-156.1-17.29c-.05-9.82-8.62-18.22-18.55-18.19a18.35,18.35,0,0,0-18,18.53,18.56,18.56,0,0,0,18.36,18.31C392,575.62,400.62,566.8,400.57,557.05ZM382.35,453.62a17.83,17.83,0,0,0-18.3,17.94,18.38,18.38,0,0,0,18,18.52c9.63.12,18.36-8.47,18.51-18.21A18.28,18.28,0,0,0,382.35,453.62Z"/><path class="cls-1" d="M512,17C238.62,17,17,238.62,17,512s221.62,495,495,495,495-221.62,495-495S785.38,17,512,17Zm0,935.81C268.55,952.81,71.19,755.45,71.19,512S268.55,71.19,512,71.19,952.81,268.55,952.81,512,755.45,952.81,512,952.81Z"/></svg>

After

Width:  |  Height:  |  Size: 2.3 KiB

View File

@ -20,6 +20,9 @@
#include "yacreader_libraries.h"
#include "exit_check.h"
#include "opengl_checker.h"
#ifdef Q_OS_MACOS
#include "trayhandler.h"
#endif
#include "QsLog.h"
#include "QsLogDest.h"
@ -127,9 +130,12 @@ int main(int argc, char **argv)
app.setApplicationVersion(VERSION);
app.setAttribute(Qt::AA_UseHighDpiPixmaps);
// Set window icon according to Freedesktop icon specification
// This is mostly relevant for Linux and other Unix systems
if (QIcon::hasThemeIcon("YACReaderLibrary")) {
app.setWindowIcon(QIcon::fromTheme("YACReaderLibrary"));
}
// TODO: We might want to add a fallback icon here.
QString destLog = YACReader::getSettingsPath() + "/yacreaderlibrary.log";
QDir().mkpath(YACReader::getSettingsPath());
@ -239,7 +245,15 @@ int main(int argc, char **argv)
//connections to localServer
mw->show();
// start as tray
if (!settings->value(START_TO_TRAY, false).toBool() || !settings->value(CLOSE_TO_TRAY, false).toBool()) {
mw->show();
}
#ifdef Q_OS_MACOS
else {
OSXHideDockIcon();
}
#endif
int ret = app.exec();

View File

@ -43,6 +43,27 @@ OptionsDialog::OptionsDialog(QWidget *parent)
#ifndef NO_OPENGL
sw->hide();
#endif
// Tray icon settings
QGroupBox *trayIconBox = new QGroupBox(tr("Tray icon settings (experimental)"));
QVBoxLayout *trayLayout = new QVBoxLayout();
trayIconCheckbox = new QCheckBox(tr("Close to tray"));
startToTrayCheckbox = new QCheckBox(tr("Start into the system tray"));
connect(trayIconCheckbox, &QCheckBox::clicked,
[=](bool checked) {
settings->setValue(CLOSE_TO_TRAY, checked);
startToTrayCheckbox->setEnabled(checked);
emit optionsChanged();
});
connect(startToTrayCheckbox, &QCheckBox::clicked,
[=](bool checked) {
settings->setValue(START_TO_TRAY, checked);
});
trayLayout->addWidget(trayIconCheckbox);
trayLayout->addWidget(startToTrayCheckbox);
trayIconBox->setLayout(trayLayout);
auto apiKeyLayout = new QVBoxLayout();
auto apiKeyButton = new QPushButton(tr("Edit Comic Vine API key"));
@ -115,6 +136,7 @@ OptionsDialog::OptionsDialog(QWidget *parent)
auto generalW = new QWidget;
generalW->setLayout(generalLayout);
generalLayout->addWidget(trayIconBox);
generalLayout->addWidget(shortcutsBox);
generalLayout->addWidget(apiKeyBox);
generalLayout->addStretch();
@ -146,6 +168,10 @@ void OptionsDialog::restoreOptions(QSettings *settings)
{
YACReaderOptionsDialog::restoreOptions(settings);
trayIconCheckbox->setChecked(settings->value(CLOSE_TO_TRAY, false).toBool());
startToTrayCheckbox->setChecked(settings->value(START_TO_TRAY, false).toBool());
startToTrayCheckbox->setEnabled(trayIconCheckbox->isChecked());
bool useBackgroundImage = settings->value(USE_BACKGROUND_IMAGE_IN_GRID_VIEW, true).toBool();
useBackgroundImageCheck->setChecked(useBackgroundImage);

View File

@ -34,8 +34,9 @@ private:
QLabel *opacityLabel;
QLabel *blurLabel;
QPushButton *resetButton;
QCheckBox *displayContinueReadingBannerCheck;
QCheckBox *trayIconCheckbox;
QCheckBox *startToTrayCheckbox;
};
#endif

View File

@ -0,0 +1,7 @@
#ifndef TRAY_HANDLER
#define TRAY_HANDLER
void OSXShowDockIcon();
void OSXHideDockIcon();
#endif

View File

@ -0,0 +1,11 @@
#import <AppKit/AppKit.h>
#include "trayhandler.h"
void OSXShowDockIcon()
{
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
}
void OSXHideDockIcon()
{
[NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory];
}

View File

@ -0,0 +1,102 @@
#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) {
if (reason == QSystemTrayIcon::DoubleClick) {
showWindow();
}
});
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, &LibraryWindow::closeApp);
trayIconMenu = new QMenu(this->window);
trayIconMenu->addAction(restoreAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(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();
#endif
window->showNormal();
window->raise(); // for MacOS
window->activateWindow(); // for Windows
}

View File

@ -0,0 +1,36 @@
#ifndef TRAYICON_CONTROLLER_H
#define TRAYICON_CONTROLLER_H
#include <QtWidgets>
#include <QSystemTrayIcon>
class LibraryWindow;
namespace YACReader {
class TrayIconController : public QObject
{
Q_OBJECT
public:
TrayIconController(QSettings *settings,
LibraryWindow *window);
void updateIconVisibility();
bool handleCloseToTrayIcon(QCloseEvent *event);
QSystemTrayIcon trayIcon;
public slots:
void showWindow();
private:
QSettings *settings;
LibraryWindow *window;
QMenu *trayIconMenu;
};
}
#endif // TRAYICON_CONTROLLER_H

View File

@ -17,6 +17,9 @@
#define FULLSCREEN "FULLSCREEN"
#define Y_WINDOW_GEOMETRY "GEOMETRY"
#define MAXIMIZED "MAXIMIZED"
#define CLOSE_TO_TRAY "CLOSE_TO_TRAY"
#define CLOSE_TO_TRAY_NOTIFIED "CLOSE_TO_TRAY_NOTIFIED"
#define START_TO_TRAY "START_TO_TRAY"
#define DOUBLE_PAGE "DOUBLE_PAGE"
#define DOUBLE_MANGA_PAGE "DOUBLE_MANGA_PAGE"
#define BACKGROUND_COLOR "BACKGROUND_COLOR"