Refactor notification options
This commit is contained in:
parent
b8be81f101
commit
7eb14243eb
11 changed files with 80 additions and 116 deletions
|
@ -162,8 +162,16 @@ void Notification::Show() {
|
|||
if (presenter_) {
|
||||
notification_ = presenter_->CreateNotification(this);
|
||||
if (notification_) {
|
||||
notification_->Show(title_, body_, "", GURL(), icon_.AsBitmap(), silent_,
|
||||
has_reply_, reply_placeholder_, actions_);
|
||||
brightray::NotificationOptions options;
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -85,19 +85,10 @@ LibnotifyNotification::~LibnotifyNotification() {
|
|||
}
|
||||
}
|
||||
|
||||
void LibnotifyNotification::Show(const base::string16& title,
|
||||
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
|
||||
) {
|
||||
void LibnotifyNotification::Show(const NotificationOptions& options) {
|
||||
notification_ = libnotify_loader_.notify_notification_new(
|
||||
base::UTF16ToUTF8(title).c_str(),
|
||||
base::UTF16ToUTF8(body).c_str(),
|
||||
base::UTF16ToUTF8(options.title).c_str(),
|
||||
base::UTF16ToUTF8(options.msg).c_str(),
|
||||
nullptr);
|
||||
|
||||
g_signal_connect(
|
||||
|
@ -111,8 +102,8 @@ void LibnotifyNotification::Show(const base::string16& title,
|
|||
nullptr);
|
||||
}
|
||||
|
||||
if (!icon.drawsNothing()) {
|
||||
GdkPixbuf* pixbuf = libgtkui::GdkPixbufFromSkBitmap(icon);
|
||||
if (!options.icon.drawsNothing()) {
|
||||
GdkPixbuf* pixbuf = libgtkui::GdkPixbufFromSkBitmap(options.icon);
|
||||
libnotify_loader_.notify_notification_set_image_from_pixbuf(
|
||||
notification_, pixbuf);
|
||||
libnotify_loader_.notify_notification_set_timeout(
|
||||
|
@ -120,8 +111,8 @@ void LibnotifyNotification::Show(const base::string16& title,
|
|||
g_object_unref(pixbuf);
|
||||
}
|
||||
|
||||
if (!tag.empty()) {
|
||||
GQuark id = g_quark_from_string(tag.c_str());
|
||||
if (!options.tag.empty()) {
|
||||
GQuark id = g_quark_from_string(options.tag.c_str());
|
||||
g_object_set(G_OBJECT(notification_), "id", id, NULL);
|
||||
}
|
||||
|
||||
|
|
|
@ -23,15 +23,7 @@ class LibnotifyNotification : public Notification {
|
|||
static bool Initialize();
|
||||
|
||||
// Notification:
|
||||
void Show(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) override;
|
||||
void Show(const NotificationOptions& options) override;
|
||||
void Dismiss() override;
|
||||
|
||||
private:
|
||||
|
|
|
@ -22,15 +22,7 @@ class CocoaNotification : public Notification {
|
|||
~CocoaNotification();
|
||||
|
||||
// Notification:
|
||||
void Show(const base::string16& title,
|
||||
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 Show(const NotificationOptions& options) override;
|
||||
void Dismiss() override;
|
||||
|
||||
void NotificationDisplayed();
|
||||
|
|
|
@ -24,27 +24,19 @@ CocoaNotification::~CocoaNotification() {
|
|||
removeDeliveredNotification:notification_];
|
||||
}
|
||||
|
||||
void CocoaNotification::Show(const base::string16& title,
|
||||
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) {
|
||||
void CocoaNotification::Show(const NotificationOptions& options) {
|
||||
notification_.reset([[NSUserNotification alloc] init]);
|
||||
[notification_ setTitle:base::SysUTF16ToNSString(title)];
|
||||
[notification_ setInformativeText:base::SysUTF16ToNSString(body)];
|
||||
[notification_ setTitle:base::SysUTF16ToNSString(options.title)];
|
||||
[notification_ setInformativeText:base::SysUTF16ToNSString(options.msg)];
|
||||
|
||||
if ([notification_ respondsToSelector:@selector(setContentImage:)] &&
|
||||
!icon.drawsNothing()) {
|
||||
!options.icon.drawsNothing()) {
|
||||
NSImage* image = skia::SkBitmapToNSImageWithColorSpace(
|
||||
icon, base::mac::GetGenericRGBColorSpace());
|
||||
options.icon, base::mac::GetGenericRGBColorSpace());
|
||||
[notification_ setContentImage:image];
|
||||
}
|
||||
|
||||
if (silent) {
|
||||
if (options.silent) {
|
||||
[notification_ setSoundName:nil];
|
||||
} else {
|
||||
[notification_ setSoundName:NSUserNotificationDefaultSoundName];
|
||||
|
@ -52,8 +44,8 @@ void CocoaNotification::Show(const base::string16& title,
|
|||
|
||||
[notification_ setHasActionButton:false];
|
||||
|
||||
for (size_t i = 0; i < actions.size(); i++) {
|
||||
NotificationAction action = actions[i];
|
||||
for (size_t i = 0; i < options.actions.size(); i++) {
|
||||
NotificationAction action = options.actions[i];
|
||||
|
||||
if (action.type == base::UTF8ToUTF16("button")) {
|
||||
[notification_ setHasActionButton:true];
|
||||
|
@ -62,8 +54,8 @@ void CocoaNotification::Show(const base::string16& title,
|
|||
}
|
||||
}
|
||||
|
||||
if (has_reply) {
|
||||
[notification_ setResponsePlaceholder:base::SysUTF16ToNSString(reply_placeholder)];
|
||||
if (options.has_reply) {
|
||||
[notification_ setResponsePlaceholder:base::SysUTF16ToNSString(options.reply_placeholder)];
|
||||
[notification_ setHasReplyButton:true];
|
||||
}
|
||||
|
||||
|
|
|
@ -10,9 +10,8 @@
|
|||
|
||||
#include "base/memory/weak_ptr.h"
|
||||
#include "base/strings/string16.h"
|
||||
|
||||
class GURL;
|
||||
class SkBitmap;
|
||||
#include "third_party/skia/include/core/SkBitmap.h"
|
||||
#include "url/gurl.h"
|
||||
|
||||
namespace brightray {
|
||||
|
||||
|
@ -24,20 +23,24 @@ struct NotificationAction {
|
|||
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 {
|
||||
public:
|
||||
virtual ~Notification();
|
||||
|
||||
// Shows the notification.
|
||||
virtual void Show(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) = 0;
|
||||
virtual void Show(const NotificationOptions& options) = 0;
|
||||
// Closes the notification, this instance will be destroyed after the
|
||||
// notification gets closed.
|
||||
virtual void Dismiss() = 0;
|
||||
|
|
|
@ -29,12 +29,19 @@ void OnWebNotificationAllowed(base::WeakPtr<Notification> notification,
|
|||
bool allowed) {
|
||||
if (!notification)
|
||||
return;
|
||||
if (allowed)
|
||||
notification->Show(data.title, data.body, data.tag, data.icon, icon,
|
||||
audio_muted ? true : data.silent, false,
|
||||
base::UTF8ToUTF16(""), {});
|
||||
else
|
||||
if (allowed) {
|
||||
brightray::NotificationOptions options;
|
||||
options.title = data.title;
|
||||
options.msg = data.body;
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -10,50 +10,49 @@
|
|||
|
||||
namespace brightray {
|
||||
|
||||
void Win32Notification::Show(
|
||||
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) {
|
||||
void Win32Notification::Show(const NotificationOptions& options) {
|
||||
auto presenter = static_cast<NotificationPresenterWin7*>(this->presenter());
|
||||
if (!presenter) return;
|
||||
|
||||
HBITMAP image = NULL;
|
||||
|
||||
if (!icon.drawsNothing()) {
|
||||
if (icon.colorType() == kBGRA_8888_SkColorType) {
|
||||
icon.lockPixels();
|
||||
if (!options.icon.drawsNothing()) {
|
||||
if (options.icon.colorType() == kBGRA_8888_SkColorType) {
|
||||
options.icon.lockPixels();
|
||||
|
||||
BITMAPINFOHEADER bmi = { sizeof(BITMAPINFOHEADER) };
|
||||
bmi.biWidth = icon.width();
|
||||
bmi.biHeight = -icon.height();
|
||||
bmi.biWidth = options.icon.width();
|
||||
bmi.biHeight = -options.icon.height();
|
||||
bmi.biPlanes = 1;
|
||||
bmi.biBitCount = 32;
|
||||
bmi.biCompression = BI_RGB;
|
||||
|
||||
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),
|
||||
DIB_RGB_COLORS);
|
||||
ReleaseDC(NULL, hdcScreen);
|
||||
|
||||
icon.unlockPixels();
|
||||
options.icon.unlockPixels();
|
||||
}
|
||||
}
|
||||
|
||||
Win32Notification* existing = nullptr;
|
||||
if (!tag.empty()) existing = presenter->GetNotificationObjectByTag(tag);
|
||||
if (!options.tag.empty())
|
||||
existing = presenter->GetNotificationObjectByTag(options.tag);
|
||||
|
||||
if (existing) {
|
||||
existing->tag_.clear();
|
||||
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 {
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -10,11 +10,7 @@ class Win32Notification : public brightray::Notification {
|
|||
NotificationPresenterWin7* presenter) :
|
||||
Notification(delegate, presenter) {
|
||||
}
|
||||
void Show(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) override;
|
||||
void Show(const NotificationOptions& options) override;
|
||||
void Dismiss() override;
|
||||
|
||||
const DesktopNotificationController::Notification& GetRef() const {
|
||||
|
|
|
@ -85,23 +85,15 @@ WindowsToastNotification::~WindowsToastNotification() {
|
|||
}
|
||||
}
|
||||
|
||||
void WindowsToastNotification::Show(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) {
|
||||
void WindowsToastNotification::Show(const NotificationOptions& options) {
|
||||
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;
|
||||
if (FAILED(GetToastXml(toast_manager_.Get(), title, msg, icon_path, silent,
|
||||
&toast_xml))) {
|
||||
if (FAILED(GetToastXml(toast_manager_.Get(), options.title, options.msg,
|
||||
icon_path, options.silent, &toast_xml))) {
|
||||
NotificationFailed();
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -51,15 +51,7 @@ class WindowsToastNotification : public Notification {
|
|||
|
||||
protected:
|
||||
// Notification:
|
||||
void Show(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) override;
|
||||
void Show(const NotificationOptions& options) override;
|
||||
void Dismiss() override;
|
||||
|
||||
private:
|
||||
|
|
Loading…
Reference in a new issue