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 "native_mate/constructor.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.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 api {
@ -38,6 +65,7 @@ Notification::Notification(v8::Isolate* isolate,
opts.Get("silent", &silent_);
opts.Get("replyPlaceholder", &reply_placeholder_);
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;
}
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() {
Emit("click");
}
@ -122,7 +163,7 @@ void Notification::Show() {
notification_ = presenter_->CreateNotification(this);
if (notification_) {
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,
&Notification::SetReplyPlaceholder)
.SetProperty("hasReply", &Notification::GetHasReply,
&Notification::SetHasReply);
&Notification::SetHasReply)
.SetProperty("actions", &Notification::GetActions,
&Notification::SetActions);
}
} // namespace api

View file

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