Add support for minimize and start to tray.

This commit is contained in:
Felix Kauselmann 2018-05-26 16:20:24 +02:00 committed by Luis Ángel San Martín
parent 23c597d444
commit e587f59ddb
9 changed files with 103 additions and 61 deletions

View File

@ -221,6 +221,11 @@ SOURCES += comic_flow.cpp \
SOURCES += ../common/gl/yacreader_flow_gl.cpp SOURCES += ../common/gl/yacreader_flow_gl.cpp
} }
macx {
HEADERS += trayhandler.h
OBJECTIVE_SOURCES += trayhandler.mm
}
include(./server/server.pri) include(./server/server.pri)
include(../custom_widgets/custom_widgets_yacreaderlibrary.pri) include(../custom_widgets/custom_widgets_yacreaderlibrary.pri)

View File

@ -86,6 +86,9 @@
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#include <shellapi.h> #include <shellapi.h>
#endif #endif
#ifdef Q_OS_MACOS
#include "trayhandler.h"
#endif
LibraryWindow::LibraryWindow() LibraryWindow::LibraryWindow()
: QMainWindow(), fullscreen(false), previousFilter(""), fetching(false), status(LibraryWindow::Normal), removeError(false) : QMainWindow(), fullscreen(false), previousFilter(""), fetching(false), status(LibraryWindow::Normal), removeError(false)
@ -139,35 +142,41 @@ void LibraryWindow::setupUI()
} }
/* //disabled until icons are ready and macos native code is done /* //disabled until icons are ready and macos native code is done
trayIcon.setIcon(QApplication::windowIcon()); trayIcon.setIcon(QApplication::windowIcon());
trayIcon.show();
connect(&trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(trayActivation(QSystemTrayIcon::ActivationReason)));
} }
} else
{
// TODO: Luis: This is a placeholder. Add MacOS, Windows and maybe a fallback
// for other systems here.
trayIcon.setIcon(QIcon(":/images/iconLibrary.png"));
}
void LibraryWindow::trayActivation(QSystemTrayIcon::ActivationReason reason) connect(&trayIcon, &QSystemTrayIcon::activated,
{ [=] (QSystemTrayIcon::ActivationReason reason) {
if (reason == QSystemTrayIcon::Trigger) if (reason == QSystemTrayIcon::Trigger)
{ {
setWindowState((windowState() & ~Qt::WindowMinimized)); #ifdef Q_OS_MACOS
show(); OSXShowDockIcon();
activateWindow(); #endif
raise(); 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)
{ {
if (event->type() == QEvent::WindowStateChange && isMinimized()) if (event->type() == QEvent::WindowStateChange && isMinimized() &&
{ trayIcon.isVisible())
hide(); {
} #ifdef Q_OS_MACOS
else OSXHideDockIcon();
{ #endif
hide();
return;
}
QMainWindow::changeEvent(event); QMainWindow::changeEvent(event);
} }
}*/
void LibraryWindow::doLayout() void LibraryWindow::doLayout()
{ {

View File

@ -385,7 +385,7 @@ public slots:
void checkMaxNumLibraries(); void checkMaxNumLibraries();
void showErrorUpgradingLibrary(const QString &path); void showErrorUpgradingLibrary(const QString &path);
//void changeEvent(QEvent *event); void changeEvent(QEvent *event);
private: private:
//fullscreen mode in Windows for preventing this bug: QTBUG-41309 https://bugreports.qt.io/browse/QTBUG-41309 //fullscreen mode in Windows for preventing this bug: QTBUG-41309 https://bugreports.qt.io/browse/QTBUG-41309
@ -394,9 +394,6 @@ private:
QSize previousSize; QSize previousSize;
std::future<void> upgradeLibraryFuture; std::future<void> upgradeLibraryFuture;
QSystemTrayIcon trayIcon; QSystemTrayIcon trayIcon;
private slots:
//void trayActivation(QSystemTrayIcon::ActivationReason reason);
}; };
#endif #endif

View File

@ -20,6 +20,9 @@
#include "yacreader_libraries.h" #include "yacreader_libraries.h"
#include "exit_check.h" #include "exit_check.h"
#include "opengl_checker.h" #include "opengl_checker.h"
#ifdef Q_OS_MACOS
#include "trayhandler.h"
#endif
#include "QsLog.h" #include "QsLog.h"
#include "QsLogDest.h" #include "QsLogDest.h"
@ -127,9 +130,12 @@ int main(int argc, char **argv)
app.setApplicationVersion(VERSION); app.setApplicationVersion(VERSION);
app.setAttribute(Qt::AA_UseHighDpiPixmaps); app.setAttribute(Qt::AA_UseHighDpiPixmaps);
if (QIcon::hasThemeIcon("YACReaderLibrary")) { // Set window icon according to Freedesktop icon specification
app.setWindowIcon(QIcon::fromTheme("YACReaderLibrary")); // 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"; QString destLog = YACReader::getSettingsPath() + "/yacreaderlibrary.log";
QDir().mkpath(YACReader::getSettingsPath()); QDir().mkpath(YACReader::getSettingsPath());
@ -210,18 +216,18 @@ int main(int argc, char **argv)
} }
#ifdef SERVER_RELEASE #ifdef SERVER_RELEASE
QSettings *settings = new QSettings(YACReader::getSettingsPath() + "/YACReaderLibrary.ini", QSettings::IniFormat); //TODO unificar la creaci<63>n del fichero de config con el servidor QSettings * settings = new QSettings(YACReader::getSettingsPath()+"/YACReaderLibrary.ini",QSettings::IniFormat); //TODO unificar la creaci<63>n del fichero de config con el servidor
settings->beginGroup("libraryConfig"); settings->beginGroup("libraryConfig");
s = new Startup(); s = new Startup();
if (settings->value(SERVER_ON, true).toBool()) { if (settings->value(SERVER_ON, true).toBool()) {
s->start(); s->start();
} }
#endif #endif
QLOG_INFO() << "YACReaderLibrary attempting to start"; QLOG_INFO() << "YACReaderLibrary attempting to start";
logSystemAndConfig(); logSystemAndConfig();
if (YACReaderLocalServer::isRunning()) //s<>lo se permite una instancia de YACReaderLibrary if (YACReaderLocalServer::isRunning()) //s<>lo se permite una instancia de YACReaderLibrary
{ {
@ -235,24 +241,24 @@ int main(int argc, char **argv)
auto mw = new LibraryWindow(); auto mw = new LibraryWindow();
mw->connect(localServer, SIGNAL(comicUpdated(quint64, const ComicDB &)), mw, SLOT(updateComicsView(quint64, const ComicDB &)), Qt::QueuedConnection); mw->connect(localServer,SIGNAL(comicUpdated(quint64, const ComicDB &)),mw,SLOT(updateComicsView(quint64, const ComicDB &)), Qt::QueuedConnection);
//connections to localServer //connections to localServer
mw->show(); mw->show();
int ret = app.exec(); int ret = app.exec();
QLOG_INFO() << "YACReaderLibrary closed with exit code :" << ret; QLOG_INFO() << "YACReaderLibrary closed with exit code :" << ret;
YACReader::exitCheck(ret); YACReader::exitCheck(ret);
//shutdown //shutdown
s->stop(); s->stop();
delete s; delete s;
localServer->close(); localServer->close();
delete localServer; delete localServer;
delete mw; delete mw;
QsLogging::Logger::destroyInstance(); QsLogging::Logger::destroyInstance();

View File

@ -43,6 +43,9 @@ OptionsDialog::OptionsDialog(QWidget *parent)
#ifndef NO_OPENGL #ifndef NO_OPENGL
sw->hide(); sw->hide();
#endif #endif
// Tray icon settings
QGroupBox * trayIconBox = new QGroupBox(tr("Tray icon settings"));
QVBoxLayout * trayLayout = new QVBoxLayout();
auto apiKeyLayout = new QVBoxLayout(); auto apiKeyLayout = new QVBoxLayout();
auto apiKeyButton = new QPushButton(tr("Edit Comic Vine API key")); auto apiKeyButton = new QPushButton(tr("Edit Comic Vine API key"));
@ -115,6 +118,7 @@ OptionsDialog::OptionsDialog(QWidget *parent)
auto generalW = new QWidget; auto generalW = new QWidget;
generalW->setLayout(generalLayout); generalW->setLayout(generalLayout);
generalLayout->addWidget(trayIconBox);
generalLayout->addWidget(shortcutsBox); generalLayout->addWidget(shortcutsBox);
generalLayout->addWidget(apiKeyBox); generalLayout->addWidget(apiKeyBox);
generalLayout->addStretch(); generalLayout->addStretch();
@ -146,6 +150,10 @@ void OptionsDialog::restoreOptions(QSettings *settings)
{ {
YACReaderOptionsDialog::restoreOptions(settings); YACReaderOptionsDialog::restoreOptions(settings);
trayIconCheckbox->setChecked(settings->value(MINIMIZE_TO_TRAY, true).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(); bool useBackgroundImage = settings->value(USE_BACKGROUND_IMAGE_IN_GRID_VIEW, true).toBool();
useBackgroundImageCheck->setChecked(useBackgroundImage); useBackgroundImageCheck->setChecked(useBackgroundImage);

View File

@ -19,23 +19,20 @@ public slots:
void editApiKey(); void editApiKey();
void restoreOptions(QSettings *settings) override; void restoreOptions(QSettings *settings) override;
private slots: private slots:
void useBackgroundImageCheckClicked(bool checked); void useBackgroundImageCheckClicked(bool checked);
void backgroundImageOpacitySliderChanged(int value); void backgroundImageOpacitySliderChanged(int value);
void backgroundImageBlurRadiusSliderChanged(int value); void backgroundImageBlurRadiusSliderChanged(int value);
void useCurrentComicCoverCheckClicked(bool checked); void useCurrentComicCoverCheckClicked(bool checked);
void resetToDefaults(); void resetToDefaults();
private:
private: QCheckBox * useBackgroundImageCheck;
QCheckBox *useBackgroundImageCheck; QCheckBox * useCurrentComicCoverCheck;
QCheckBox *useCurrentComicCoverCheck; QSlider * backgroundImageOpacitySlider;
QSlider *backgroundImageOpacitySlider; QSlider * backgroundImageBlurRadiusSlider;
QSlider *backgroundImageBlurRadiusSlider; QLabel * opacityLabel;
QLabel *opacityLabel; QLabel * blurLabel;
QLabel *blurLabel; QPushButton * resetButton;
QPushButton *resetButton;
QCheckBox *displayContinueReadingBannerCheck;
}; };
#endif #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

@ -17,6 +17,8 @@
#define FULLSCREEN "FULLSCREEN" #define FULLSCREEN "FULLSCREEN"
#define Y_WINDOW_GEOMETRY "GEOMETRY" #define Y_WINDOW_GEOMETRY "GEOMETRY"
#define MAXIMIZED "MAXIMIZED" #define MAXIMIZED "MAXIMIZED"
#define MINIMIZE_TO_TRAY "MINIMIZE_TO_TRAY"
#define START_TO_TRAY "START_TO_TRAY"
#define DOUBLE_PAGE "DOUBLE_PAGE" #define DOUBLE_PAGE "DOUBLE_PAGE"
#define DOUBLE_MANGA_PAGE "DOUBLE_MANGA_PAGE" #define DOUBLE_MANGA_PAGE "DOUBLE_MANGA_PAGE"
#define BACKGROUND_COLOR "BACKGROUND_COLOR" #define BACKGROUND_COLOR "BACKGROUND_COLOR"