Remove OS specific implementations

This commit is contained in:
Samuel Attard 2017-05-29 21:18:18 +10:00
parent 058bdfbced
commit c741b584a1
No known key found for this signature in database
GPG key ID: 273DC1869D8F13EF
11 changed files with 40 additions and 1038 deletions

View file

@ -9,15 +9,21 @@
#include "atom/browser/api/atom_api_menu.h"
#include "atom/browser/browser.h"
#include "atom/browser/ui/notification_delegate_adapter.h"
#include "atom/common/api/atom_api_native_image.h"
#include "atom/common/native_mate_converters/gfx_converter.h"
#include "atom/common/native_mate_converters/image_converter.h"
#include "atom/common/native_mate_converters/string16_converter.h"
#include "atom/common/node_includes.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_task_runner_handle.h"
#include "common/string_conversion.h"
#include "native_mate/constructor.h"
#include "native_mate/dictionary.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/image/image.h"
#include "url/gurl.h"
namespace atom {
@ -121,6 +127,33 @@ void Notification::OnShown() {
Emit("show");
}
void Notification::NotifyPropsUpdated() {}
// Showing notifications
void Notification::Show() {
SkBitmap image = *(new SkBitmap);
if (has_icon_) {
image = *(icon_.ToSkBitmap());
}
std::unique_ptr<AtomNotificationDelegateAdapter> adapter(
new AtomNotificationDelegateAdapter(this));
auto notif = presenter_->CreateNotification(adapter.get());
if (notif) {
ignore_result(adapter.release()); // it will release itself automatically.
GURL nullUrl = *(new GURL);
notif->Show(title_, body_, "", nullUrl, image, silent_, has_reply_, reply_placeholder_);
}
}
bool initialized_ = false;
void Notification::OnInitialProps() {
if (!initialized_) {
presenter_ = brightray::NotificationPresenter::Create();
initialized_ = true;
}
}
// static
void Notification::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {

View file

@ -12,6 +12,8 @@
#include "atom/browser/api/trackable_object.h"
#include "atom/browser/ui/notification_observer.h"
#include "base/strings/utf_string_conversions.h"
#include "brightray/browser/notification.h"
#include "brightray/browser/notification_presenter.h"
#include "native_mate/handle.h"
#include "ui/gfx/image/image.h"
@ -69,6 +71,7 @@ class Notification : public mate::TrackableObject<Notification>,
bool silent_ = false;
base::string16 reply_placeholder_ = base::UTF8ToUTF16("");
bool has_reply_ = false;
brightray::NotificationPresenter* presenter_;
int id_;

View file

@ -1,129 +0,0 @@
// Copyright (c) 2014 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/api/atom_api_notification.h"
#include <libnotify/notify.h>
#include "atom/browser/browser.h"
#include "brightray/browser/linux/libnotify_loader.h"
#include "brightray/browser/linux/libnotify_notification.h"
#include "chrome/browser/ui/libgtkui/skia_utils_gtk.h"
#include "third_party/skia/include/core/SkBitmap.h"
namespace atom {
namespace api {
bool initialized_ = false;
bool available_ = false;
LibNotifyLoader libnotify_loader_;
bool HasCapability(const std::string& capability) {
bool result = false;
GList* capabilities = libnotify_loader_.notify_get_server_caps();
if (g_list_find_custom(capabilities, capability.c_str(),
(GCompareFunc)g_strcmp0) != NULL)
result = true;
g_list_free_full(capabilities, g_free);
return result;
}
bool NotifierSupportsActions() {
if (getenv("ELECTRON_USE_UBUNTU_NOTIFIER"))
return false;
static bool notify_has_result = false;
static bool notify_result = false;
if (notify_has_result)
return notify_result;
notify_result = HasCapability("actions");
return notify_result;
}
void log_and_clear_error(GError* error, const char* context) {
LOG(ERROR) << context
<< ": domain=" << error->domain
<< " code=" << error->code
<< " message=\"" << error->message << '"';
g_error_free(error);
}
void Notification::Show() {
if (!available_) return;
NotifyNotification* notification_ = libnotify_loader_.notify_notification_new(
base::UTF16ToUTF8(title_).c_str(),
base::UTF16ToUTF8(body_).c_str(),
nullptr);
GQuark id = g_quark_from_string(std::to_string(id_).c_str());
g_object_set(G_OBJECT(notification_), "id", id, NULL);
// NB: On Unity and on any other DE using Notify-OSD, adding a notification
// action will cause the notification to display as a modal dialog box.
// Can't make this work, need linux help :D
// if (NotifierSupportsActions()) {
// libnotify_loader_.notify_notification_add_action(
// notification_, "default", "View", OnClickedCallback, this,
// nullptr);
// }
if (has_icon_) {
SkBitmap image = *(icon_.ToSkBitmap());
GdkPixbuf* pixbuf = libgtkui::GdkPixbufFromSkBitmap(image);
libnotify_loader_.notify_notification_set_image_from_pixbuf(
notification_, pixbuf);
libnotify_loader_.notify_notification_set_timeout(
notification_, NOTIFY_EXPIRES_DEFAULT);
g_object_unref(pixbuf);
}
// Always try to append notifications.
// Unique tags can be used to prevent this.
if (HasCapability("append")) {
libnotify_loader_.notify_notification_set_hint_string(
notification_, "append", "true");
} else if (HasCapability("x-canonical-append")) {
libnotify_loader_.notify_notification_set_hint_string(
notification_, "x-canonical-append", "true");
}
GError* error = nullptr;
libnotify_loader_.notify_notification_show(notification_, &error);
if (error) {
log_and_clear_error(error, "notify_notification_show");
return;
}
OnShown();
}
void Notification::OnInitialProps() {
if (!initialized_) {
initialized_ = true;
if (!libnotify_loader_.Load("libnotify.so.4") && // most common one
!libnotify_loader_.Load("libnotify.so.5") &&
!libnotify_loader_.Load("libnotify.so.1") &&
!libnotify_loader_.Load("libnotify.so")) {
return;
}
Browser* browser = Browser::Get();
if (!libnotify_loader_.notify_is_initted() &&
!libnotify_loader_.notify_init(browser->GetName().c_str())) {
return;
}
available_ = true;
}
}
void Notification::NotifyPropsUpdated() {}
} // namespace api
} // namespace atom

View file

@ -1,56 +0,0 @@
// Copyright (c) 2014 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/api/atom_api_notification.h"
#import <Foundation/Foundation.h>
#include "atom/browser/browser.h"
#include "atom/browser/ui/notification_delegate_adapter.h"
#include "base/mac/mac_util.h"
#include "base/mac/scoped_nsobject.h"
#include "base/strings/sys_string_conversions.h"
#include "brightray/browser/notification.h"
#include "brightray/browser/notification_presenter.h"
#include "brightray/browser/mac/notification_presenter_mac.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/png_codec.h"
#include "url/gurl.h"
namespace atom {
namespace api {
brightray::NotificationPresenterMac* presenter;
void Notification::Show() {
SkBitmap image = *(new SkBitmap);
if (has_icon_) {
image = *(icon_.ToSkBitmap());
}
std::unique_ptr<AtomNotificationDelegateAdapter> adapter(
new AtomNotificationDelegateAdapter(this));
auto notif = presenter->CreateNotification(adapter.get());
if (notif) {
ignore_result(adapter.release()); // it will release itself automatically.
GURL nullUrl = *(new GURL);
notif->Show(title_, body_, "", nullUrl, image, silent_, has_reply_, reply_placeholder_);
}
}
bool initialized_ = false;
void Notification::OnInitialProps() {
if (!initialized_) {
presenter = new brightray::NotificationPresenterMac;
initialized_ = true;
}
}
void Notification::NotifyPropsUpdated() {
}
} // namespace api
} // namespace atom

View file

@ -1,111 +0,0 @@
// Copyright (c) 2014 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/api/atom_api_notification.h"
#include "atom/browser/browser.h"
#include "atom/browser/ui/notification_delegate_adapter.h"
#include "atom/browser/ui/win/toast_handler.h"
#include "atom/browser/ui/win/toast_lib.h"
#include "base/files/file_util.h"
#include "base/md5.h"
#include "base/strings/utf_string_conversions.h"
#include "brightray/browser/notification.h"
#include "brightray/browser/notification_presenter.h"
#include "brightray/browser/win/notification_presenter_win.h"
#include "brightray/browser/win/notification_presenter_win7.h"
#include "common/string_conversion.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/png_codec.h"
#include "url/gurl.h"
namespace atom {
namespace api {
bool can_toast_ = true;
bool initialized_ = false;
brightray::NotificationPresenterWin7* presenter;
base::ScopedTempDir temp_dir_;
bool SaveIconToPath(const SkBitmap& bitmap, const base::FilePath& path) {
std::vector<unsigned char> png_data;
if (!gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_data))
return false;
char* data = reinterpret_cast<char*>(&png_data[0]);
int size = static_cast<int>(png_data.size());
return base::WriteFile(path, data, size) == size;
}
void Notification::Show() {
SkBitmap image = *(new SkBitmap);
if (has_icon_) {
image = *(icon_.ToSkBitmap());
}
if (can_toast_) {
atom::AtomToastHandler* handler = new atom::AtomToastHandler(this);
WinToastLib::WinToastTemplate::WinToastTemplateType toastType =
WinToastLib::WinToastTemplate::TextOneLine;
if (!has_icon_) {
if (body_ != L"") {
toastType = WinToastLib::WinToastTemplate::TextTwoLines;
} else {
toastType = WinToastLib::WinToastTemplate::TextOneLine;
}
} else {
if (body_ != L"") {
toastType = WinToastLib::WinToastTemplate::ImageWithTwoLines;
} else {
toastType = WinToastLib::WinToastTemplate::ImageWithOneLine;
}
}
WinToastLib::WinToastTemplate toast =
WinToastLib::WinToastTemplate(toastType);
std::string filename =
base::MD5String(base::UTF16ToUTF8(icon_path_)) + ".png";
base::FilePath savePath =
temp_dir_.GetPath().Append(base::UTF8ToUTF16(filename));
if (has_icon_ && SaveIconToPath(image, savePath)) {
toast.setImagePath(savePath.value());
}
toast.setTextField(title_,
WinToastLib::WinToastTemplate::TextField::FirstLine);
toast.setTextField(body_,
WinToastLib::WinToastTemplate::TextField::SecondLine);
toast.setSilent(silent_);
WinToastLib::WinToast::instance()->showToast(toast, handler);
OnShown();
} else {
AtomNotificationDelegateAdapter* adapter =
new AtomNotificationDelegateAdapter(this);
auto notif = presenter->CreateNotification(adapter);
GURL nullUrl = *(new GURL);
notif->Show(title_, body_, "", nullUrl, image, silent_);
}
}
void Notification::OnInitialProps() {
if (!initialized_) {
Browser* browser = Browser::Get();
WinToastLib::WinToast::instance()->setAppName(
base::UTF8ToUTF16(browser->GetName()));
WinToastLib::WinToast::instance()->setAppUserModelId(
browser->GetAppUserModelID());
can_toast_ = WinToastLib::WinToast::instance()->initialize();
temp_dir_.CreateUniqueTempDir();
}
if (!can_toast_) {
presenter = new brightray::NotificationPresenterWin7;
}
}
void Notification::NotifyPropsUpdated() {}
} // namespace api
} // namespace atom