Skip to content

Commit

Permalink
🍎 Send events as native macOS notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
lgalabru committed Aug 6, 2016
1 parent 5eeadb0 commit 38cc56e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions atom/browser/api/atom_api_system_preferences.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions atom/browser/api/atom_api_system_preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -32,6 +33,10 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences> {
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);
Expand All @@ -49,6 +54,9 @@ class SystemPreferences : public mate::EventEmitter<SystemPreferences> {
~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);
Expand Down
22 changes: 22 additions & 0 deletions atom/browser/api/atom_api_system_preferences_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@

} // 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);
Expand Down
16 changes: 16 additions & 0 deletions docs/api/system-preferences.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 38cc56e

Please sign in to comment.