From b5d9eee489b2d564aed6f7734047c841ff54ada1 Mon Sep 17 00:00:00 2001 From: John Preston Date: Wed, 8 Mar 2017 10:31:05 +0300 Subject: [PATCH] Fix macOS native notification callbacks. --- Telegram/SourceFiles/mainwindow.cpp | 2 +- .../platform/mac/notifications_manager_mac.mm | 32 +++++++++++-------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/Telegram/SourceFiles/mainwindow.cpp b/Telegram/SourceFiles/mainwindow.cpp index 8396374056..cc95970fb9 100644 --- a/Telegram/SourceFiles/mainwindow.cpp +++ b/Telegram/SourceFiles/mainwindow.cpp @@ -187,7 +187,7 @@ void MainWindow::firstShow() { trayIconMenu->addAction(notificationActionText, this, SLOT(toggleDisplayNotifyFromTray()))->setEnabled(true); trayIconMenu->addAction(lang(lng_quit_from_tray), this, SLOT(quitFromTray()))->setEnabled(true); } - workmodeUpdated(Global::WorkMode().value()); + Global::RefWorkMode().setForced(Global::WorkMode().value(), true); psFirstShow(); updateTrayMenu(); diff --git a/Telegram/SourceFiles/platform/mac/notifications_manager_mac.mm b/Telegram/SourceFiles/platform/mac/notifications_manager_mac.mm index 8f30b5ee16..59c8aa6893 100644 --- a/Telegram/SourceFiles/platform/mac/notifications_manager_mac.mm +++ b/Telegram/SourceFiles/platform/mac/notifications_manager_mac.mm @@ -39,27 +39,26 @@ NSImage *qt_mac_create_nsimage(const QPixmap &pm); @interface NotificationDelegate : NSObject { } -- (id) init; +- (id) initWithManager:(std::shared_ptr)manager; - (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification; - (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification; @end @implementation NotificationDelegate { + +std::weak_ptr _manager; + } -- (id) init { +- (id) initWithManager:(std::shared_ptr)manager { if (self = [super init]) { + _manager = manager; } return self; } - (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification { - auto manager = ManagerInstance.data(); - if (!manager) { - return; - } - NSDictionary *notificationUserInfo = [notification userInfo]; NSNumber *launchIdObject = [notificationUserInfo objectForKey:@"launch"]; auto notificationLaunchId = launchIdObject ? [launchIdObject unsignedLongLongValue] : 0ULL; @@ -78,9 +77,13 @@ NSImage *qt_mac_create_nsimage(const QPixmap &pm); auto notificationMsgId = msgObject ? [msgObject intValue] : 0; if (notification.activationType == NSUserNotificationActivationTypeReplied) { auto notificationReply = QString::fromUtf8([[[notification response] string] UTF8String]); - manager->notificationReplied(notificationPeerId, notificationMsgId, notificationReply); + if (auto manager = _manager.lock()) { + (*manager)->notificationReplied(notificationPeerId, notificationMsgId, notificationReply); + } } else if (notification.activationType == NSUserNotificationActivationTypeContentsClicked) { - manager->notificationActivated(notificationPeerId, notificationMsgId); + if (auto manager = _manager.lock()) { + (*manager)->notificationActivated(notificationPeerId, notificationMsgId); + } } [center removeDeliveredNotification: notification]; @@ -122,7 +125,7 @@ void CustomNotificationShownHook(QWidget *widget) { class Manager::Private : public QObject, private base::Subscriber { public: - Private(); + Private(Manager *manager); void showNotification(PeerData *peer, MsgId msgId, const QString &title, const QString &subtitle, const QString &msg, bool hideNameAndPhoto, bool hideReplyButton); void clearAll(); void clearFromHistory(History *history); @@ -131,11 +134,14 @@ public: ~Private(); private: - NotificationDelegate *_delegate; + std::shared_ptr _guarded; + NotificationDelegate *_delegate = nullptr; }; -Manager::Private::Private() : _delegate([[NotificationDelegate alloc] init]) { +Manager::Private::Private(Manager *manager) +: _guarded(std::make_shared(manager)) +, _delegate([[NotificationDelegate alloc] initWithManager:_guarded]) { updateDelegate(); subscribe(Global::RefWorkMode(), [this](DBIWorkMode mode) { // We need to update the delegate _after_ the tray icon change was done in Qt. @@ -220,7 +226,7 @@ Manager::Private::~Private() { } Manager::Manager(Window::Notifications::System *system) : NativeManager(system) -, _private(std::make_unique()) { +, _private(std::make_unique(this)) { } Manager::~Manager() = default;