Add support for notification actions on macOS

This commit is contained in:
Samuel Attard 2017-06-23 20:39:42 +10:00
parent 873a315538
commit 4f0d48f164
16 changed files with 136 additions and 13 deletions

View file

@ -14,8 +14,35 @@
#include "brightray/browser/browser_client.h" #include "brightray/browser/browser_client.h"
#include "native_mate/constructor.h" #include "native_mate/constructor.h"
#include "native_mate/dictionary.h" #include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
#include "url/gurl.h" #include "url/gurl.h"
namespace mate {
template<>
struct Converter<brightray::NotificationAction> {
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
brightray::NotificationAction* out) {
mate::Dictionary dict;
if (!ConvertFromV8(isolate, val, &dict))
return false;
if (!dict.Get("type", &(out->type))) {
return false;
}
dict.Get("label", &(out->label));
return true;
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
brightray::NotificationAction val) {
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
dict.Set("label", val.label);
dict.Set("type", val.type);
return dict.GetHandle();
}
};
} // namespace mate
namespace atom { namespace atom {
namespace api { namespace api {
@ -38,6 +65,7 @@ Notification::Notification(v8::Isolate* isolate,
opts.Get("silent", &silent_); opts.Get("silent", &silent_);
opts.Get("replyPlaceholder", &reply_placeholder_); opts.Get("replyPlaceholder", &reply_placeholder_);
opts.Get("hasReply", &has_reply_); opts.Get("hasReply", &has_reply_);
opts.Get("actions", &actions_);
} }
} }
@ -97,6 +125,19 @@ void Notification::SetHasReply(bool new_has_reply) {
has_reply_ = new_has_reply; has_reply_ = new_has_reply;
} }
void Notification::SetActions(
const std::vector<brightray::NotificationAction> actions) {
actions_ = actions;
}
std::vector<brightray::NotificationAction> Notification::GetActions() {
return actions_;
}
void Notification::NotificationAction(int index) {
Emit("action", index);
}
void Notification::NotificationClick() { void Notification::NotificationClick() {
Emit("click"); Emit("click");
} }
@ -122,7 +163,7 @@ void Notification::Show() {
notification_ = presenter_->CreateNotification(this); notification_ = presenter_->CreateNotification(this);
if (notification_) { if (notification_) {
notification_->Show(title_, body_, "", GURL(), icon_.AsBitmap(), silent_, notification_->Show(title_, body_, "", GURL(), icon_.AsBitmap(), silent_,
has_reply_, reply_placeholder_); has_reply_, reply_placeholder_, actions_);
} }
} }
} }
@ -144,7 +185,9 @@ void Notification::BuildPrototype(v8::Isolate* isolate,
.SetProperty("replyPlaceholder", &Notification::GetReplyPlaceholder, .SetProperty("replyPlaceholder", &Notification::GetReplyPlaceholder,
&Notification::SetReplyPlaceholder) &Notification::SetReplyPlaceholder)
.SetProperty("hasReply", &Notification::GetHasReply, .SetProperty("hasReply", &Notification::GetHasReply,
&Notification::SetHasReply); &Notification::SetHasReply)
.SetProperty("actions", &Notification::GetActions,
&Notification::SetActions);
} }
} // namespace api } // namespace api

View file

@ -31,6 +31,7 @@ class Notification : public mate::TrackableObject<Notification>,
v8::Local<v8::FunctionTemplate> prototype); v8::Local<v8::FunctionTemplate> prototype);
// NotificationDelegate: // NotificationDelegate:
void NotificationAction(int index) override;
void NotificationClick() override; void NotificationClick() override;
void NotificationReplied(const std::string& reply) override; void NotificationReplied(const std::string& reply) override;
void NotificationDisplayed() override; void NotificationDisplayed() override;
@ -51,6 +52,7 @@ class Notification : public mate::TrackableObject<Notification>,
bool GetSilent(); bool GetSilent();
base::string16 GetReplyPlaceholder(); base::string16 GetReplyPlaceholder();
bool GetHasReply(); bool GetHasReply();
std::vector<brightray::NotificationAction> GetActions();
// Prop Setters // Prop Setters
void SetTitle(const base::string16& new_title); void SetTitle(const base::string16& new_title);
@ -58,6 +60,7 @@ class Notification : public mate::TrackableObject<Notification>,
void SetSilent(bool new_silent); void SetSilent(bool new_silent);
void SetReplyPlaceholder(const base::string16& new_reply_placeholder); void SetReplyPlaceholder(const base::string16& new_reply_placeholder);
void SetHasReply(bool new_has_reply); void SetHasReply(bool new_has_reply);
void SetActions(const std::vector<brightray::NotificationAction> actions);
private: private:
base::string16 title_; base::string16 title_;
@ -68,6 +71,7 @@ class Notification : public mate::TrackableObject<Notification>,
bool silent_ = false; bool silent_ = false;
base::string16 reply_placeholder_; base::string16 reply_placeholder_;
bool has_reply_ = false; bool has_reply_ = false;
std::vector<brightray::NotificationAction> actions_;
brightray::NotificationPresenter* presenter_; brightray::NotificationPresenter* presenter_;

View file

@ -4,6 +4,8 @@
#include "brightray/browser/linux/libnotify_notification.h" #include "brightray/browser/linux/libnotify_notification.h"
#include <vector>
#include "base/files/file_enumerator.h" #include "base/files/file_enumerator.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
@ -90,7 +92,9 @@ void LibnotifyNotification::Show(const base::string16& title,
const SkBitmap& icon, const SkBitmap& icon,
bool silent, bool silent,
bool has_reply, bool has_reply,
const base::string16& reply_placeholder) { const base::string16& reply_placeholder,
const std::vector<NotificationAction> actions
) {
notification_ = libnotify_loader_.notify_notification_new( notification_ = libnotify_loader_.notify_notification_new(
base::UTF16ToUTF8(title).c_str(), base::UTF16ToUTF8(title).c_str(),
base::UTF16ToUTF8(body).c_str(), base::UTF16ToUTF8(body).c_str(),

View file

@ -6,6 +6,7 @@
#define BRIGHTRAY_BROWSER_LINUX_LIBNOTIFY_NOTIFICATION_H_ #define BRIGHTRAY_BROWSER_LINUX_LIBNOTIFY_NOTIFICATION_H_
#include <string> #include <string>
#include <vector>
#include "brightray/browser/linux/libnotify_loader.h" #include "brightray/browser/linux/libnotify_loader.h"
#include "brightray/browser/notification.h" #include "brightray/browser/notification.h"
@ -29,7 +30,8 @@ class LibnotifyNotification : public Notification {
const SkBitmap& icon, const SkBitmap& icon,
bool silent, bool silent,
bool has_reply, bool has_reply,
const base::string16& reply_placeholder) override; const base::string16& reply_placeholder,
const std::vector<NotificationAction> actions) override;
void Dismiss() override; void Dismiss() override;
private: private:

View file

@ -8,6 +8,7 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#include <string> #include <string>
#include <vector>
#include "base/mac/scoped_nsobject.h" #include "base/mac/scoped_nsobject.h"
#include "brightray/browser/notification.h" #include "brightray/browser/notification.h"
@ -28,16 +29,19 @@ class CocoaNotification : public Notification {
const SkBitmap& icon, const SkBitmap& icon,
bool silent, bool silent,
const bool has_reply, const bool has_reply,
const base::string16& reply_placeholder) override; const base::string16& reply_placeholder,
const std::vector<NotificationAction> actions) override;
void Dismiss() override; void Dismiss() override;
void NotificationDisplayed(); void NotificationDisplayed();
void NotificationReplied(const std::string& reply); void NotificationReplied(const std::string& reply);
void NotificationButtonClicked();
NSUserNotification* notification() const { return notification_; } NSUserNotification* notification() const { return notification_; }
private: private:
base::scoped_nsobject<NSUserNotification> notification_; base::scoped_nsobject<NSUserNotification> notification_;
int actionIndex_;
DISALLOW_COPY_AND_ASSIGN(CocoaNotification); DISALLOW_COPY_AND_ASSIGN(CocoaNotification);
}; };

View file

@ -6,6 +6,7 @@
#include "base/mac/mac_util.h" #include "base/mac/mac_util.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "brightray/browser/notification_delegate.h" #include "brightray/browser/notification_delegate.h"
#include "brightray/browser/notification_presenter.h" #include "brightray/browser/notification_presenter.h"
#include "skia/ext/skia_utils_mac.h" #include "skia/ext/skia_utils_mac.h"
@ -30,7 +31,8 @@ void CocoaNotification::Show(const base::string16& title,
const SkBitmap& icon, const SkBitmap& icon,
bool silent, bool silent,
bool has_reply, bool has_reply,
const base::string16& reply_placeholder) { const base::string16& reply_placeholder,
const std::vector<NotificationAction> actions) {
notification_.reset([[NSUserNotification alloc] init]); notification_.reset([[NSUserNotification alloc] init]);
[notification_ setTitle:base::SysUTF16ToNSString(title)]; [notification_ setTitle:base::SysUTF16ToNSString(title)];
[notification_ setInformativeText:base::SysUTF16ToNSString(body)]; [notification_ setInformativeText:base::SysUTF16ToNSString(body)];
@ -48,6 +50,18 @@ void CocoaNotification::Show(const base::string16& title,
[notification_ setSoundName:NSUserNotificationDefaultSoundName]; [notification_ setSoundName:NSUserNotificationDefaultSoundName];
} }
[notification_ setHasActionButton:false];
for (size_t i = 0; i < actions.size(); i++) {
NotificationAction action = actions[i];
if (action.type == base::UTF8ToUTF16("button")) {
[notification_ setHasActionButton:true];
[notification_ setActionButtonTitle:base::SysUTF16ToNSString(action.label)];
actionIndex_ = i;
}
}
if (has_reply) { if (has_reply) {
[notification_ setResponsePlaceholder:base::SysUTF16ToNSString(reply_placeholder)]; [notification_ setResponsePlaceholder:base::SysUTF16ToNSString(reply_placeholder)];
[notification_ setHasReplyButton:true]; [notification_ setHasReplyButton:true];
@ -74,4 +88,9 @@ void CocoaNotification::NotificationReplied(const std::string& reply) {
delegate()->NotificationReplied(reply); delegate()->NotificationReplied(reply);
} }
void CocoaNotification::NotificationButtonClicked() {
if (delegate())
delegate()->NotificationAction(actionIndex_);
}
} // namespace brightray } // namespace brightray

View file

@ -31,6 +31,8 @@
if (notification) { if (notification) {
if (notif.activationType == NSUserNotificationActivationTypeReplied) { if (notif.activationType == NSUserNotificationActivationTypeReplied) {
notification->NotificationReplied([notif.response.string UTF8String]); notification->NotificationReplied([notif.response.string UTF8String]);
} else if (notif.activationType == NSUserNotificationActivationTypeActionButtonClicked) {
notification->NotificationButtonClicked();
} else { } else {
notification->NotificationClicked(); notification->NotificationClicked();
} }

View file

@ -6,6 +6,7 @@
#define BRIGHTRAY_BROWSER_NOTIFICATION_H_ #define BRIGHTRAY_BROWSER_NOTIFICATION_H_
#include <string> #include <string>
#include <vector>
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
@ -18,6 +19,11 @@ namespace brightray {
class NotificationDelegate; class NotificationDelegate;
class NotificationPresenter; class NotificationPresenter;
struct NotificationAction {
base::string16 type;
base::string16 label;
};
class Notification { class Notification {
public: public:
virtual ~Notification(); virtual ~Notification();
@ -30,7 +36,8 @@ class Notification {
const SkBitmap& icon, const SkBitmap& icon,
bool silent, bool silent,
bool has_reply, bool has_reply,
const base::string16& reply_placeholder) = 0; const base::string16& reply_placeholder,
const std::vector<NotificationAction> actions) = 0;
// Closes the notification, this instance will be destroyed after the // Closes the notification, this instance will be destroyed after the
// notification gets closed. // notification gets closed.
virtual void Dismiss() = 0; virtual void Dismiss() = 0;

View file

@ -21,6 +21,7 @@ class NotificationDelegate : public content::DesktopNotificationDelegate {
// Notification was replied to // Notification was replied to
virtual void NotificationReplied(const std::string& reply) {} virtual void NotificationReplied(const std::string& reply) {}
virtual void NotificationAction(int index) {}
}; };
} // namespace brightray } // namespace brightray

View file

@ -32,7 +32,7 @@ void OnWebNotificationAllowed(base::WeakPtr<Notification> notification,
if (allowed) if (allowed)
notification->Show(data.title, data.body, data.tag, data.icon, icon, notification->Show(data.title, data.body, data.tag, data.icon, icon,
audio_muted ? true : data.silent, false, audio_muted ? true : data.silent, false,
base::UTF8ToUTF16("")); base::UTF8ToUTF16(""), {});
else else
notification->Destroy(); notification->Destroy();
} }

View file

@ -4,6 +4,7 @@
#include <windows.h> #include <windows.h>
#include <string> #include <string>
#include <vector>
#include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkBitmap.h"
@ -13,7 +14,8 @@ void Win32Notification::Show(
const base::string16& title, const base::string16& msg, const base::string16& title, const base::string16& msg,
const std::string& tag, const GURL& icon_url, const std::string& tag, const GURL& icon_url,
const SkBitmap& icon, bool silent, const SkBitmap& icon, bool silent,
bool has_reply, const base::string16& reply_placeholder) { bool has_reply, const base::string16& reply_placeholder,
const std::vector<NotificationAction> actions) {
auto presenter = static_cast<NotificationPresenterWin7*>(this->presenter()); auto presenter = static_cast<NotificationPresenterWin7*>(this->presenter());
if (!presenter) return; if (!presenter) return;

View file

@ -13,7 +13,8 @@ class Win32Notification : public brightray::Notification {
void Show(const base::string16& title, const base::string16& msg, void Show(const base::string16& title, const base::string16& msg,
const std::string& tag, const GURL& icon_url, const std::string& tag, const GURL& icon_url,
const SkBitmap& icon, bool silent, const SkBitmap& icon, bool silent,
bool has_reply, const base::string16& reply_placeholder) override; bool has_reply, const base::string16& reply_placeholder,
const std::vector<NotificationAction> actions) override;
void Dismiss() override; void Dismiss() override;
const DesktopNotificationController::Notification& GetRef() const { const DesktopNotificationController::Notification& GetRef() const {

View file

@ -9,6 +9,7 @@
#include "brightray/browser/win/windows_toast_notification.h" #include "brightray/browser/win/windows_toast_notification.h"
#include <shlobj.h> #include <shlobj.h>
#include <vector>
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "brightray/browser/notification_delegate.h" #include "brightray/browser/notification_delegate.h"
@ -91,7 +92,10 @@ void WindowsToastNotification::Show(const base::string16& title,
const SkBitmap& icon, const SkBitmap& icon,
bool silent, bool silent,
bool has_reply, bool has_reply,
const base::string16& reply_placeholder) { const base::string16& reply_placeholder,
const std::vector<
NotificationAction
> actions) {
auto presenter_win = static_cast<NotificationPresenterWin*>(presenter()); auto presenter_win = static_cast<NotificationPresenterWin*>(presenter());
std::wstring icon_path = presenter_win->SaveIconToFilesystem(icon, icon_url); std::wstring icon_path = presenter_win->SaveIconToFilesystem(icon, icon_url);

View file

@ -13,6 +13,7 @@
#include <windows.ui.notifications.h> #include <windows.ui.notifications.h>
#include <wrl/implements.h> #include <wrl/implements.h>
#include <string> #include <string>
#include <vector>
#include "brightray/browser/notification.h" #include "brightray/browser/notification.h"
@ -57,7 +58,8 @@ class WindowsToastNotification : public Notification {
const SkBitmap& icon, const SkBitmap& icon,
bool silent, bool silent,
bool has_reply, bool has_reply,
const base::string16& reply_placeholder) override; const base::string16& reply_placeholder,
const std::vector<NotificationAction> actions) override;
void Dismiss() override; void Dismiss() override;
private: private:

View file

@ -36,6 +36,7 @@ Returns `Boolean` - Whether or not desktop notifications are supported on the cu
* `icon` [NativeImage](native-image.md) - (optional) An icon to use in the notification * `icon` [NativeImage](native-image.md) - (optional) An icon to use in the notification
* `hasReply` Boolean - (optional) Whether or not to add an inline reply option to the notification. _macOS_ * `hasReply` Boolean - (optional) Whether or not to add an inline reply option to the notification. _macOS_
* `replyPlaceholder` String - (optional) The placeholder to write in the inline reply input field. _macOS_ * `replyPlaceholder` String - (optional) The placeholder to write in the inline reply input field. _macOS_
* `actions` [NotificationAction[]](structures/notification-action.md) - Actions to add to the notification. Please read the available actions and limitations in the `NotificationAction` documentation _macOS_
### Instance Events ### Instance Events
@ -83,6 +84,13 @@ Returns:
Emitted when the user clicks the "Reply" button on a notification with `hasReply: true`. Emitted when the user clicks the "Reply" button on a notification with `hasReply: true`.
#### Event: 'action' _macOS_
Returns:
* `event` Event
* `index` Number - The index of the action that was activated
### Instance Methods ### Instance Methods
Objects created with `new Notification` have the following instance methods: Objects created with `new Notification` have the following instance methods:

View file

@ -0,0 +1,20 @@
# NotificationAction Object
* `type` String - The type of action, can be `button`.
* `label` String - The label for the given action.
## Platform / Action Support
| Action Type | Platform Support | Limitations |
|-------------|------------------|-------------|
| `button` | macOS | Maximum of one button, if multiple are provided only the last is used |
### Button support on macOS
In order for extra notification buttons to work on macOS your app must meet the
following criteria.
* App is signed
* App has it's `NSUserNotificationAlertStyle` set to `alert` in the `info.plist`.
If either of these requirements are not met the button simply won't appear.