From 38cc56efee6d3ba6a685c3a698b7bf7589d42420 Mon Sep 17 00:00:00 2001 From: Ludovic Galabru Date: Fri, 5 Aug 2016 23:02:59 +0200 Subject: [PATCH 1/2] :apple: Send events as native macOS notifications --- .../api/atom_api_system_preferences.cc | 4 ++++ .../browser/api/atom_api_system_preferences.h | 8 +++++++ .../api/atom_api_system_preferences_mac.mm | 22 +++++++++++++++++++ docs/api/system-preferences.md | 16 ++++++++++++++ 4 files changed, 50 insertions(+) diff --git a/atom/browser/api/atom_api_system_preferences.cc b/atom/browser/api/atom_api_system_preferences.cc index b473d9f7ffb0..ba6c9720da6e 100644 --- a/atom/browser/api/atom_api_system_preferences.cc +++ b/atom/browser/api/atom_api_system_preferences.cc @@ -50,6 +50,10 @@ void SystemPreferences::BuildPrototype( #if defined(OS_WIN) .SetMethod("isAeroGlassEnabled", &SystemPreferences::IsAeroGlassEnabled) #elif defined(OS_MACOSX) + .SetMethod("postNotification", + &SystemPreferences::PostNotification) + .SetMethod("postLocalNotification", + &SystemPreferences::PostLocalNotification) .SetMethod("subscribeNotification", &SystemPreferences::SubscribeNotification) .SetMethod("unsubscribeNotification", diff --git a/atom/browser/api/atom_api_system_preferences.h b/atom/browser/api/atom_api_system_preferences.h index 3c780059375c..f855ab0ebb3d 100644 --- a/atom/browser/api/atom_api_system_preferences.h +++ b/atom/browser/api/atom_api_system_preferences.h @@ -9,6 +9,7 @@ #include "atom/browser/api/event_emitter.h" #include "base/callback.h" +#include "base/values.h" #include "native_mate/handle.h" namespace base { @@ -32,6 +33,10 @@ class SystemPreferences : public mate::EventEmitter { using NotificationCallback = base::Callback< void(const std::string&, const base::DictionaryValue&)>; + void PostNotification(const std::string& name, + const base::DictionaryValue& user_info); + void PostLocalNotification(const std::string& name, + const base::DictionaryValue& user_info); int SubscribeNotification(const std::string& name, const NotificationCallback& callback); void UnsubscribeNotification(int id); @@ -49,6 +54,9 @@ class SystemPreferences : public mate::EventEmitter { ~SystemPreferences() override; #if defined(OS_MACOSX) + void DoPostNotification(const std::string& name, + const base::DictionaryValue& user_info, + bool is_local); int DoSubscribeNotification(const std::string& name, const NotificationCallback& callback, bool is_local); diff --git a/atom/browser/api/atom_api_system_preferences_mac.mm b/atom/browser/api/atom_api_system_preferences_mac.mm index d4264dd6e87a..8f4251796245 100644 --- a/atom/browser/api/atom_api_system_preferences_mac.mm +++ b/atom/browser/api/atom_api_system_preferences_mac.mm @@ -28,6 +28,28 @@ std::map g_id_map; } // namespace +void SystemPreferences::PostNotification(const std::string& name, + const base::DictionaryValue& user_info) { + DoPostNotification(name, user_info, false); +} + +void SystemPreferences::PostLocalNotification(const std::string& name, + const base::DictionaryValue& user_info) { + DoPostNotification(name, user_info, true); +} + +void SystemPreferences::DoPostNotification(const std::string& name, + const base::DictionaryValue& user_info, bool is_local) { + NSNotificationCenter* center = is_local ? + [NSNotificationCenter defaultCenter] : + [NSDistributedNotificationCenter defaultCenter]; + [center + postNotificationName:base::SysUTF8ToNSString(name) + object:nil + userInfo:DictionaryValueToNSDictionary(user_info) + ]; +} + int SystemPreferences::SubscribeNotification( const std::string& name, const NotificationCallback& callback) { return DoSubscribeNotification(name, callback, false); diff --git a/docs/api/system-preferences.md b/docs/api/system-preferences.md index 0ac63b1b3c6a..e370b8e07ea1 100644 --- a/docs/api/system-preferences.md +++ b/docs/api/system-preferences.md @@ -17,6 +17,22 @@ This method returns `true` if the system is in Dark Mode, and `false` otherwise. This method returns `true` if the Swipe between pages setting is on, and `false` otherwise. +### `systemPreferences.postNotification(event, userInfo)` _macOS_ + +* `event` String +* `userInfo` Dictionary + +Posts `event` as native notifications of macOS. The `userInfo` is an Object +that contains the user information dictionary sent along with the notification. + +### `systemPreferences.postLocalNotification(event, userInfo)` _macOS_ + +* `event` String +* `userInfo` Dictionary + +Posts `event` as native notifications of macOS. The `userInfo` is an Object +that contains the user information dictionary sent along with the notification. + ### `systemPreferences.subscribeNotification(event, callback)` _macOS_ * `event` String From a5351407c5f353498d7491e87b6862865295d8f8 Mon Sep 17 00:00:00 2001 From: Ludovic Galabru Date: Tue, 9 Aug 2016 05:57:02 -0400 Subject: [PATCH 2/2] Fixing indentation --- atom/browser/api/atom_api_system_preferences_mac.mm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/atom/browser/api/atom_api_system_preferences_mac.mm b/atom/browser/api/atom_api_system_preferences_mac.mm index 8f4251796245..aee0b4631613 100644 --- a/atom/browser/api/atom_api_system_preferences_mac.mm +++ b/atom/browser/api/atom_api_system_preferences_mac.mm @@ -45,8 +45,8 @@ void SystemPreferences::DoPostNotification(const std::string& name, [NSDistributedNotificationCenter defaultCenter]; [center postNotificationName:base::SysUTF8ToNSString(name) - object:nil - userInfo:DictionaryValueToNSDictionary(user_info) + object:nil + userInfo:DictionaryValueToNSDictionary(user_info) ]; }