From e1f71baed6671299012b15a062f0dfc1275962c2 Mon Sep 17 00:00:00 2001 From: Ilya Fedin Date: Fri, 24 Jan 2025 23:35:03 +0400 Subject: [PATCH] Move native notifications option check logic to cross platform code --- .../linux/notifications_manager_linux.cpp | 21 ++++----------- .../platform/mac/notifications_manager_mac.mm | 6 +---- .../win/notifications_manager_win.cpp | 10 +++---- .../window/notifications_manager.cpp | 27 ++++++++++++++++--- .../window/notifications_manager.h | 2 +- 5 files changed, 34 insertions(+), 32 deletions(-) diff --git a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp index 892d0abd8..d03c83efb 100644 --- a/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/notifications_manager_linux.cpp @@ -640,22 +640,11 @@ void Create(Window::Notifications::System *system) { const auto managerSetter = [=]( XdgNotifications::NotificationsProxy proxy) { - using ManagerType = Window::Notifications::ManagerType; - if ((Core::App().settings().nativeNotifications() || Enforced()) - && Supported()) { - if (system->manager().type() != ManagerType::Native) { - auto manager = std::make_unique(system); - manager->_private->init(proxy); - system->setManager(std::move(manager)); - } - } else if (Enforced()) { - if (system->manager().type() != ManagerType::Dummy) { - using DummyManager = Window::Notifications::DummyManager; - system->setManager(std::make_unique(system)); - } - } else if (system->manager().type() != ManagerType::Default) { - system->setManager(nullptr); - } + system->setManager([=] { + auto manager = std::make_unique(system); + manager->_private->init(proxy); + return manager; + }); }; const auto counter = std::make_shared(2); diff --git a/Telegram/SourceFiles/platform/mac/notifications_manager_mac.mm b/Telegram/SourceFiles/platform/mac/notifications_manager_mac.mm index 8c7148ac1..3be39ef84 100644 --- a/Telegram/SourceFiles/platform/mac/notifications_manager_mac.mm +++ b/Telegram/SourceFiles/platform/mac/notifications_manager_mac.mm @@ -197,11 +197,7 @@ bool ByDefault() { } void Create(Window::Notifications::System *system) { - if (Supported()) { - system->setManager(std::make_unique(system)); - } else { - system->setManager(nullptr); - } + system->setManager([=] { return std::make_unique(system); }); } class Manager::Private : public QObject { diff --git a/Telegram/SourceFiles/platform/win/notifications_manager_win.cpp b/Telegram/SourceFiles/platform/win/notifications_manager_win.cpp index f73d2184f..48e187cbf 100644 --- a/Telegram/SourceFiles/platform/win/notifications_manager_win.cpp +++ b/Telegram/SourceFiles/platform/win/notifications_manager_win.cpp @@ -416,14 +416,10 @@ bool ByDefault() { } void Create(Window::Notifications::System *system) { - if (Core::App().settings().nativeNotifications() && Supported()) { + system->setManager([=] { auto result = std::make_unique(system); - if (result->init()) { - system->setManager(std::move(result)); - return; - } - } - system->setManager(nullptr); + return result->init() ? std::move(result) : nullptr; + }); } class Manager::Private { diff --git a/Telegram/SourceFiles/window/notifications_manager.cpp b/Telegram/SourceFiles/window/notifications_manager.cpp index 6b8666bfc..f11b30587 100644 --- a/Telegram/SourceFiles/window/notifications_manager.cpp +++ b/Telegram/SourceFiles/window/notifications_manager.cpp @@ -184,9 +184,30 @@ void System::createManager() { Platform::Notifications::Create(this); } -void System::setManager(std::unique_ptr manager) { - _manager = std::move(manager); - if (!_manager) { +void System::setManager(Fn()> create) { + Expects(_manager != nullptr); + const auto guard = gsl::finally([&] { + Ensures(_manager != nullptr); + }); + + if ((Core::App().settings().nativeNotifications() + || Platform::Notifications::Enforced()) + && Platform::Notifications::Supported()) { + if (_manager->type() == ManagerType::Native) { + return; + } + + if (auto manager = create()) { + _manager = std::move(manager); + return; + } + } + + if (Platform::Notifications::Enforced()) { + if (_manager->type() != ManagerType::Dummy) { + _manager = std::make_unique(this); + } + } else if (_manager->type() != ManagerType::Default) { _manager = std::make_unique(this); } } diff --git a/Telegram/SourceFiles/window/notifications_manager.h b/Telegram/SourceFiles/window/notifications_manager.h index e04314219..fa88f6c0a 100644 --- a/Telegram/SourceFiles/window/notifications_manager.h +++ b/Telegram/SourceFiles/window/notifications_manager.h @@ -99,7 +99,7 @@ public: [[nodiscard]] Main::Session *findSession(uint64 sessionId) const; void createManager(); - void setManager(std::unique_ptr manager); + void setManager(Fn()> create); [[nodiscard]] Manager &manager() const; void checkDelayed();