diff --git a/src/librssguard/gui/notifications/toastnotificationsmanager.cpp b/src/librssguard/gui/notifications/toastnotificationsmanager.cpp index 16f98627d..64bbe2cd3 100644 --- a/src/librssguard/gui/notifications/toastnotificationsmanager.cpp +++ b/src/librssguard/gui/notifications/toastnotificationsmanager.cpp @@ -3,10 +3,10 @@ #include "gui/notifications/toastnotificationsmanager.h" #include "3rd-party/boolinq/boolinq.h" -#include "gui/notifications/basetoastnotification.h" - #include "gui/notifications/articlelistnotification.h" +#include "gui/notifications/basetoastnotification.h" #include "gui/notifications/toastnotification.h" +#include "miscellaneous/settings.h" #include #include @@ -29,11 +29,14 @@ QString ToastNotificationsManager::textForPosition(ToastNotificationsManager::No } } -ToastNotificationsManager::ToastNotificationsManager(NotificationPosition position, int screen, QObject* parent) - : QObject(parent), m_position(position), m_screen(screen), m_articleListNotification(nullptr) {} +ToastNotificationsManager::ToastNotificationsManager(QObject* parent) + : QObject(parent), m_position(ToastNotificationsManager::NotificationPosition::TopRight), m_screen(0), m_margins(0), + m_articleListNotification(nullptr) { + resetNotifications(false); +} ToastNotificationsManager::~ToastNotificationsManager() { - clear(); + clear(true); } QList ToastNotificationsManager::activeNotifications() const { @@ -56,16 +59,59 @@ void ToastNotificationsManager::setPosition(NotificationPosition position) { m_position = position; } -void ToastNotificationsManager::resetNotifications() {} +void ToastNotificationsManager::resetNotifications(bool reload_existing_notifications) { + m_position = qApp->settings() + ->value(GROUP(GUI), SETTING(GUI::ToastNotificationsPosition)) + .value(); + m_screen = qApp->settings()->value(GROUP(GUI), SETTING(GUI::ToastNotificationsScreen)).toInt(); + m_margins = qApp->settings()->value(GROUP(GUI), SETTING(GUI::ToastNotificationsMargin)).toInt(); -void ToastNotificationsManager::clear() { + auto notif_width = qApp->settings()->value(GROUP(GUI), SETTING(GUI::ToastNotificationsWidth)).toInt(); + + if (reload_existing_notifications) { + auto notif = m_activeNotifications; + + clear(false); + + while (!notif.isEmpty()) { + BaseToastNotification* one_notif = notif.takeLast(); + + one_notif->setFixedWidth(notif_width); + + processNotification(one_notif); + } + } +} + +void ToastNotificationsManager::clear(bool delete_from_memory) { for (BaseToastNotification* notif : m_activeNotifications) { - closeNotification(notif, true); + closeNotification(notif, delete_from_memory); } m_activeNotifications.clear(); } +void ToastNotificationsManager::processNotification(BaseToastNotification* notif) { + notif->show(); + + auto* screen = moveToProperScreen(notif); + auto notif_new_pos = cornerForNewNotification(screen->availableGeometry()); + + // Make sure notification is finally resized. + notif->adjustSize(); + qApp->processEvents(); + + // Move notification, at this point we already need to know its precise size. + moveNotificationToCorner(notif, notif_new_pos); + + // Remove out-of-bounds old notifications and shift existing + // ones to make space for new notifications. + removeOutOfBoundsNotifications(notif->height()); + makeSpaceForNotification(notif->height()); + + m_activeNotifications.prepend(notif); +} + void ToastNotificationsManager::showNotification(Notification::Event event, const GuiMessage& msg, const GuiAction& action) { @@ -89,26 +135,7 @@ void ToastNotificationsManager::showNotification(Notification::Event event, hookNotification(notif); } - // Insert new notification into free space. - notif->show(); - - auto* screen = moveToProperScreen(notif); - - auto notif_new_pos = cornerForNewNotification(screen->availableGeometry()); - - // Make sure notification is finally resized. - notif->adjustSize(); - qApp->processEvents(); - - // Move notification, at this point we already need to know its precise size. - moveNotificationToCorner(notif, notif_new_pos); - - // Remove out-of-bounds old notifications and shift existing - // ones to make space for new notifications. - removeOutOfBoundsNotifications(notif->height()); - makeSpaceForNotification(notif->height()); - - m_activeNotifications.prepend(notif); + processNotification(notif); } void ToastNotificationsManager::closeNotification(BaseToastNotification* notif, bool delete_from_memory) { @@ -146,17 +173,17 @@ QScreen* ToastNotificationsManager::activeScreen() const { QPoint ToastNotificationsManager::cornerForNewNotification(QRect screen_rect) { switch (m_position) { case ToastNotificationsManager::TopLeft: - return screen_rect.topLeft() + QPoint(NOTIFICATIONS_MARGIN, NOTIFICATIONS_MARGIN); + return screen_rect.topLeft() + QPoint(m_margins, m_margins); case ToastNotificationsManager::TopRight: - return screen_rect.topRight() - QPoint(NOTIFICATIONS_MARGIN, -NOTIFICATIONS_MARGIN); + return screen_rect.topRight() - QPoint(m_margins, -m_margins); case ToastNotificationsManager::BottomLeft: - return screen_rect.bottomLeft() - QPoint(-NOTIFICATIONS_MARGIN, NOTIFICATIONS_MARGIN); + return screen_rect.bottomLeft() - QPoint(-m_margins, m_margins); case ToastNotificationsManager::BottomRight: default: - return screen_rect.bottomRight() - QPoint(NOTIFICATIONS_MARGIN, NOTIFICATIONS_MARGIN); + return screen_rect.bottomRight() - QPoint(m_margins, m_margins); } } @@ -222,7 +249,7 @@ void ToastNotificationsManager::makeSpaceForNotification(int height_to_make_spac } // Move it all down. - notif->move(notif->pos().x(), shift_down(notif->pos().y(), (height_to_make_space + NOTIFICATIONS_MARGIN))); + notif->move(notif->pos().x(), shift_down(notif->pos().y(), (height_to_make_space + m_margins))); break; } @@ -242,7 +269,7 @@ void ToastNotificationsManager::makeSpaceForNotification(int height_to_make_spac } // Move it all up. - notif->move(notif->pos().x(), shift_up(notif->pos().y(), height_to_make_space + NOTIFICATIONS_MARGIN)); + notif->move(notif->pos().x(), shift_up(notif->pos().y(), height_to_make_space + m_margins)); break; } } @@ -254,8 +281,8 @@ void ToastNotificationsManager::removeOutOfBoundsNotifications(int height_to_res int available_height = screen->availableSize().height(); - while (boolinq::from(m_activeNotifications).sum([](BaseToastNotification* notif) { - return notif->height() + NOTIFICATIONS_MARGIN; + while (boolinq::from(m_activeNotifications).sum([this](BaseToastNotification* notif) { + return notif->height() + m_margins; }) + height_to_reserve > available_height) { if (!m_activeNotifications.isEmpty()) { diff --git a/src/librssguard/gui/notifications/toastnotificationsmanager.h b/src/librssguard/gui/notifications/toastnotificationsmanager.h index e155a490e..e80d6b53f 100644 --- a/src/librssguard/gui/notifications/toastnotificationsmanager.h +++ b/src/librssguard/gui/notifications/toastnotificationsmanager.h @@ -27,9 +27,7 @@ class ToastNotificationsManager : public QObject { static QString textForPosition(ToastNotificationsManager::NotificationPosition pos); - explicit ToastNotificationsManager(ToastNotificationsManager::NotificationPosition position, - int screen, - QObject* parent = nullptr); + explicit ToastNotificationsManager(QObject* parent = nullptr); virtual ~ToastNotificationsManager(); QList activeNotifications() const; @@ -42,11 +40,11 @@ class ToastNotificationsManager : public QObject { NotificationPosition position() const; void setPosition(NotificationPosition position); - void resetNotifications(); + void resetNotifications(bool reload_existing_notifications); public slots: - void clear(); - void showNotification(Notification::Event event, const GuiMessage& msg, const GuiAction& action); + void clear(bool delete_from_memory); + void showNotification(Notification::Event event, const GuiMessage& msg, const GuiAction& action = {}); private slots: void closeNotification(BaseToastNotification* notif, bool delete_from_memory); @@ -60,6 +58,7 @@ class ToastNotificationsManager : public QObject { QScreen* moveToProperScreen(BaseToastNotification* notif); QPoint cornerForNewNotification(QRect screen_rect); + void processNotification(BaseToastNotification* notif); void initializeArticleListNotification(); void hookNotification(BaseToastNotification* notif); void moveNotificationToCorner(BaseToastNotification* notif, QPoint corner); @@ -69,6 +68,7 @@ class ToastNotificationsManager : public QObject { private: NotificationPosition m_position; int m_screen; + int m_margins; // List of all displayed notifications, newest notifications are in the beginning of the list // and oldest at the end. diff --git a/src/librssguard/gui/settings/settingsnotifications.cpp b/src/librssguard/gui/settings/settingsnotifications.cpp index 6adcceb62..7e3fd8090 100644 --- a/src/librssguard/gui/settings/settingsnotifications.cpp +++ b/src/librssguard/gui/settings/settingsnotifications.cpp @@ -28,8 +28,6 @@ SettingsNotifications::SettingsNotifications(Settings* settings, QWidget* parent connect(m_ui.m_rbNativeNotifications, &QRadioButton::toggled, this, &SettingsNotifications::requireRestart); connect(m_ui.m_sbScreen, QOverload::of(&QSpinBox::valueChanged), this, &SettingsNotifications::dirtifySettings); - connect(m_ui.m_sbScreen, QOverload::of(&QSpinBox::valueChanged), this, &SettingsNotifications::requireRestart); - connect(m_ui.m_sbMargin, QOverload::of(&QSpinBox::valueChanged), this, &SettingsNotifications::dirtifySettings); connect(m_ui.m_sbWidth, QOverload::of(&QSpinBox::valueChanged), this, &SettingsNotifications::dirtifySettings); @@ -39,10 +37,6 @@ SettingsNotifications::SettingsNotifications(Settings* settings, QWidget* parent QOverload::of(&QComboBox::currentIndexChanged), this, &SettingsNotifications::dirtifySettings); - connect(m_ui.m_cbCustomNotificationsPosition, - QOverload::of(&QComboBox::currentIndexChanged), - this, - &SettingsNotifications::requireRestart); } void SettingsNotifications::loadSettings() { @@ -97,7 +91,13 @@ void SettingsNotifications::saveSettings() { m_ui.m_cbCustomNotificationsPosition->currentData() .value()); - // qApp->m_toastNotifications + qApp->toastNotifications()->resetNotifications(true); + qApp->toastNotifications()->showNotification(Notification::Event::GeneralEvent, + GuiMessage(tr("How do I look?"), + tr("Just testing new notifications settings. " + "That's all."), + QSystemTrayIcon::MessageIcon::Warning)); + onEndSaveSettings(); } diff --git a/src/librssguard/miscellaneous/application.cpp b/src/librssguard/miscellaneous/application.cpp index 7e9ac3031..ff5620b6e 100644 --- a/src/librssguard/miscellaneous/application.cpp +++ b/src/librssguard/miscellaneous/application.cpp @@ -112,14 +112,9 @@ Application::Application(const QString& id, int& argc, char** argv, const QStrin m_database = new DatabaseFactory(this); m_downloadManager = nullptr; m_notifications = new NotificationFactory(this); - m_toastNotifications = - settings()->value(GROUP(GUI), SETTING(GUI::UseToastNotifications)).toBool() - ? new ToastNotificationsManager(settings() - ->value(GROUP(GUI), SETTING(GUI::ToastNotificationsPosition)) - .value(), - settings()->value(GROUP(GUI), SETTING(GUI::ToastNotificationsScreen)).toInt(), - this) - : nullptr; + m_toastNotifications = settings()->value(GROUP(GUI), SETTING(GUI::UseToastNotifications)).toBool() + ? new ToastNotificationsManager(this) + : nullptr; m_shouldRestart = false; #if defined(Q_OS_WIN) @@ -428,6 +423,10 @@ void Application::displayLogMessageInDialog(const QString& message) { } } +ToastNotificationsManager* Application::toastNotifications() const { + return m_toastNotifications; +} + QThreadPool* Application::workHorsePool() const { return m_workHorsePool; } diff --git a/src/librssguard/miscellaneous/application.h b/src/librssguard/miscellaneous/application.h index a32ba146e..8c9922b9d 100644 --- a/src/librssguard/miscellaneous/application.h +++ b/src/librssguard/miscellaneous/application.h @@ -132,6 +132,8 @@ class RSSGUARD_DLLSPEC Application : public SingleApplication { SystemTrayIcon* trayIcon(); NotificationFactory* notifications() const; NodeJs* nodejs() const; + QThreadPool* workHorsePool() const; + ToastNotificationsManager* toastNotifications() const; QIcon desktopAwareIcon() const; @@ -151,6 +153,8 @@ class RSSGUARD_DLLSPEC Application : public SingleApplication { QString cacheFolder(); + int customAdblockPort() const; + QString replaceDataUserDataFolderPlaceholder(QString text) const; QStringList replaceDataUserDataFolderPlaceholder(QStringList texts) const; @@ -190,10 +194,6 @@ class RSSGUARD_DLLSPEC Application : public SingleApplication { // Custom debug/console log handler. static void performLogging(QtMsgType type, const QMessageLogContext& context, const QString& msg); - int customAdblockPort() const; - - QThreadPool* workHorsePool() const; - public slots: // Restarts the application. void restart();