Refactor notification options

This commit is contained in:
Samuel Attard 2017-06-24 21:03:27 +10:00
parent b8be81f101
commit 7eb14243eb
11 changed files with 80 additions and 116 deletions

View file

@ -162,8 +162,16 @@ void Notification::Show() {
if (presenter_) { if (presenter_) {
notification_ = presenter_->CreateNotification(this); notification_ = presenter_->CreateNotification(this);
if (notification_) { if (notification_) {
notification_->Show(title_, body_, "", GURL(), icon_.AsBitmap(), silent_, brightray::NotificationOptions options;
has_reply_, reply_placeholder_, actions_); options.title = title_;
options.msg = body_;
options.icon_url = GURL();
options.icon = icon_.AsBitmap();
options.silent = silent_;
options.has_reply = has_reply_;
options.reply_placeholder = reply_placeholder_;
options.actions = actions_;
notification_->Show(options);
} }
} }
} }

View file

@ -85,19 +85,10 @@ LibnotifyNotification::~LibnotifyNotification() {
} }
} }
void LibnotifyNotification::Show(const base::string16& title, void LibnotifyNotification::Show(const NotificationOptions& options) {
const base::string16& body,
const std::string& tag,
const GURL& icon_url,
const SkBitmap& icon,
bool silent,
bool has_reply,
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(options.title).c_str(),
base::UTF16ToUTF8(body).c_str(), base::UTF16ToUTF8(options.msg).c_str(),
nullptr); nullptr);
g_signal_connect( g_signal_connect(
@ -111,8 +102,8 @@ void LibnotifyNotification::Show(const base::string16& title,
nullptr); nullptr);
} }
if (!icon.drawsNothing()) { if (!options.icon.drawsNothing()) {
GdkPixbuf* pixbuf = libgtkui::GdkPixbufFromSkBitmap(icon); GdkPixbuf* pixbuf = libgtkui::GdkPixbufFromSkBitmap(options.icon);
libnotify_loader_.notify_notification_set_image_from_pixbuf( libnotify_loader_.notify_notification_set_image_from_pixbuf(
notification_, pixbuf); notification_, pixbuf);
libnotify_loader_.notify_notification_set_timeout( libnotify_loader_.notify_notification_set_timeout(
@ -120,8 +111,8 @@ void LibnotifyNotification::Show(const base::string16& title,
g_object_unref(pixbuf); g_object_unref(pixbuf);
} }
if (!tag.empty()) { if (!options.tag.empty()) {
GQuark id = g_quark_from_string(tag.c_str()); GQuark id = g_quark_from_string(options.tag.c_str());
g_object_set(G_OBJECT(notification_), "id", id, NULL); g_object_set(G_OBJECT(notification_), "id", id, NULL);
} }

View file

@ -23,15 +23,7 @@ class LibnotifyNotification : public Notification {
static bool Initialize(); static bool Initialize();
// Notification: // Notification:
void Show(const base::string16& title, void Show(const NotificationOptions& options) override;
const base::string16& msg,
const std::string& tag,
const GURL& icon_url,
const SkBitmap& icon,
bool silent,
bool has_reply,
const base::string16& reply_placeholder,
const std::vector<NotificationAction> actions) override;
void Dismiss() override; void Dismiss() override;
private: private:

View file

@ -22,15 +22,7 @@ class CocoaNotification : public Notification {
~CocoaNotification(); ~CocoaNotification();
// Notification: // Notification:
void Show(const base::string16& title, void Show(const NotificationOptions& options) override;
const base::string16& msg,
const std::string& tag,
const GURL& icon_url,
const SkBitmap& icon,
bool silent,
const bool has_reply,
const base::string16& reply_placeholder,
const std::vector<NotificationAction> actions) override;
void Dismiss() override; void Dismiss() override;
void NotificationDisplayed(); void NotificationDisplayed();

View file

@ -24,27 +24,19 @@ CocoaNotification::~CocoaNotification() {
removeDeliveredNotification:notification_]; removeDeliveredNotification:notification_];
} }
void CocoaNotification::Show(const base::string16& title, void CocoaNotification::Show(const NotificationOptions& options) {
const base::string16& body,
const std::string& tag,
const GURL& icon_url,
const SkBitmap& icon,
bool silent,
bool has_reply,
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(options.title)];
[notification_ setInformativeText:base::SysUTF16ToNSString(body)]; [notification_ setInformativeText:base::SysUTF16ToNSString(options.msg)];
if ([notification_ respondsToSelector:@selector(setContentImage:)] && if ([notification_ respondsToSelector:@selector(setContentImage:)] &&
!icon.drawsNothing()) { !options.icon.drawsNothing()) {
NSImage* image = skia::SkBitmapToNSImageWithColorSpace( NSImage* image = skia::SkBitmapToNSImageWithColorSpace(
icon, base::mac::GetGenericRGBColorSpace()); options.icon, base::mac::GetGenericRGBColorSpace());
[notification_ setContentImage:image]; [notification_ setContentImage:image];
} }
if (silent) { if (options.silent) {
[notification_ setSoundName:nil]; [notification_ setSoundName:nil];
} else { } else {
[notification_ setSoundName:NSUserNotificationDefaultSoundName]; [notification_ setSoundName:NSUserNotificationDefaultSoundName];
@ -52,8 +44,8 @@ void CocoaNotification::Show(const base::string16& title,
[notification_ setHasActionButton:false]; [notification_ setHasActionButton:false];
for (size_t i = 0; i < actions.size(); i++) { for (size_t i = 0; i < options.actions.size(); i++) {
NotificationAction action = actions[i]; NotificationAction action = options.actions[i];
if (action.type == base::UTF8ToUTF16("button")) { if (action.type == base::UTF8ToUTF16("button")) {
[notification_ setHasActionButton:true]; [notification_ setHasActionButton:true];
@ -62,8 +54,8 @@ void CocoaNotification::Show(const base::string16& title,
} }
} }
if (has_reply) { if (options.has_reply) {
[notification_ setResponsePlaceholder:base::SysUTF16ToNSString(reply_placeholder)]; [notification_ setResponsePlaceholder:base::SysUTF16ToNSString(options.reply_placeholder)];
[notification_ setHasReplyButton:true]; [notification_ setHasReplyButton:true];
} }

View file

@ -10,9 +10,8 @@
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "base/strings/string16.h" #include "base/strings/string16.h"
#include "third_party/skia/include/core/SkBitmap.h"
class GURL; #include "url/gurl.h"
class SkBitmap;
namespace brightray { namespace brightray {
@ -24,20 +23,24 @@ struct NotificationAction {
base::string16 text; base::string16 text;
}; };
struct NotificationOptions {
base::string16 title;
base::string16 msg;
std::string tag;
GURL icon_url;
SkBitmap icon;
bool silent;
bool has_reply;
base::string16 reply_placeholder;
std::vector<NotificationAction> actions;
};
class Notification { class Notification {
public: public:
virtual ~Notification(); virtual ~Notification();
// Shows the notification. // Shows the notification.
virtual void Show(const base::string16& title, virtual void Show(const NotificationOptions& options) = 0;
const base::string16& msg,
const std::string& tag,
const GURL& icon_url,
const SkBitmap& icon,
bool silent,
bool has_reply,
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

@ -29,12 +29,19 @@ void OnWebNotificationAllowed(base::WeakPtr<Notification> notification,
bool allowed) { bool allowed) {
if (!notification) if (!notification)
return; return;
if (allowed) if (allowed) {
notification->Show(data.title, data.body, data.tag, data.icon, icon, brightray::NotificationOptions options;
audio_muted ? true : data.silent, false, options.title = data.title;
base::UTF8ToUTF16(""), {}); options.msg = data.body;
else options.tag = data.tag;
options.icon_url = data.icon;
options.icon = icon;
options.silent = audio_muted ? true : data.silent;
options.has_reply = false;
notification->Show(options);
} else {
notification->Destroy(); notification->Destroy();
}
} }
} // namespace } // namespace

View file

@ -10,50 +10,49 @@
namespace brightray { namespace brightray {
void Win32Notification::Show( void Win32Notification::Show(const NotificationOptions& options) {
const base::string16& title, const base::string16& msg,
const std::string& tag, const GURL& icon_url,
const SkBitmap& icon, bool silent,
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;
HBITMAP image = NULL; HBITMAP image = NULL;
if (!icon.drawsNothing()) { if (!options.icon.drawsNothing()) {
if (icon.colorType() == kBGRA_8888_SkColorType) { if (options.icon.colorType() == kBGRA_8888_SkColorType) {
icon.lockPixels(); options.icon.lockPixels();
BITMAPINFOHEADER bmi = { sizeof(BITMAPINFOHEADER) }; BITMAPINFOHEADER bmi = { sizeof(BITMAPINFOHEADER) };
bmi.biWidth = icon.width(); bmi.biWidth = options.icon.width();
bmi.biHeight = -icon.height(); bmi.biHeight = -options.icon.height();
bmi.biPlanes = 1; bmi.biPlanes = 1;
bmi.biBitCount = 32; bmi.biBitCount = 32;
bmi.biCompression = BI_RGB; bmi.biCompression = BI_RGB;
HDC hdcScreen = GetDC(NULL); HDC hdcScreen = GetDC(NULL);
image = CreateDIBitmap(hdcScreen, &bmi, CBM_INIT, icon.getPixels(), image = CreateDIBitmap(hdcScreen, &bmi, CBM_INIT,
options.icon.getPixels(),
reinterpret_cast<BITMAPINFO*>(&bmi), reinterpret_cast<BITMAPINFO*>(&bmi),
DIB_RGB_COLORS); DIB_RGB_COLORS);
ReleaseDC(NULL, hdcScreen); ReleaseDC(NULL, hdcScreen);
icon.unlockPixels(); options.icon.unlockPixels();
} }
} }
Win32Notification* existing = nullptr; Win32Notification* existing = nullptr;
if (!tag.empty()) existing = presenter->GetNotificationObjectByTag(tag); if (!options.tag.empty())
existing = presenter->GetNotificationObjectByTag(options.tag);
if (existing) { if (existing) {
existing->tag_.clear(); existing->tag_.clear();
this->notification_ref_ = std::move(existing->notification_ref_); this->notification_ref_ = std::move(existing->notification_ref_);
this->notification_ref_.Set(title, msg, image); this->notification_ref_.Set(options.title, options.msg, image);
} else { } else {
this->notification_ref_ = presenter->AddNotification(title, msg, image); this->notification_ref_ = presenter->AddNotification(options.title,
options.msg,
image);
} }
this->tag_ = tag; this->tag_ = options.tag;
if (image) DeleteObject(image); if (image) DeleteObject(image);
} }

View file

@ -10,11 +10,7 @@ class Win32Notification : public brightray::Notification {
NotificationPresenterWin7* presenter) : NotificationPresenterWin7* presenter) :
Notification(delegate, presenter) { Notification(delegate, presenter) {
} }
void Show(const base::string16& title, const base::string16& msg, void Show(const NotificationOptions& options) override;
const std::string& tag, const GURL& icon_url,
const SkBitmap& icon, bool silent,
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

@ -85,23 +85,15 @@ WindowsToastNotification::~WindowsToastNotification() {
} }
} }
void WindowsToastNotification::Show(const base::string16& title, void WindowsToastNotification::Show(const NotificationOptions& options) {
const base::string16& msg,
const std::string& tag,
const GURL& icon_url,
const SkBitmap& icon,
bool silent,
bool has_reply,
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(
options.icon,
options.icon_url);
ComPtr<IXmlDocument> toast_xml; ComPtr<IXmlDocument> toast_xml;
if (FAILED(GetToastXml(toast_manager_.Get(), title, msg, icon_path, silent, if (FAILED(GetToastXml(toast_manager_.Get(), options.title, options.msg,
&toast_xml))) { icon_path, options.silent, &toast_xml))) {
NotificationFailed(); NotificationFailed();
return; return;
} }

View file

@ -51,15 +51,7 @@ class WindowsToastNotification : public Notification {
protected: protected:
// Notification: // Notification:
void Show(const base::string16& title, void Show(const NotificationOptions& options) override;
const base::string16& msg,
const std::string& tag,
const GURL& icon_url,
const SkBitmap& icon,
bool silent,
bool has_reply,
const base::string16& reply_placeholder,
const std::vector<NotificationAction> actions) override;
void Dismiss() override; void Dismiss() override;
private: private: