From 6bbc4c3113461b268576647bd51dec02d90b6f04 Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Mon, 24 Apr 2017 09:47:18 +1000 Subject: [PATCH] Support Windows 7 notifications from brightray --- atom/browser/api/atom_api_notification_win.cc | 41 +++++++++++++++---- .../ui/notification_delegate_adapter.cc | 21 ++++++++++ .../ui/notification_delegate_adapter.h | 28 +++++++++++++ default_app/default_app.js | 2 +- filenames.gypi | 2 + 5 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 atom/browser/ui/notification_delegate_adapter.cc create mode 100644 atom/browser/ui/notification_delegate_adapter.h diff --git a/atom/browser/api/atom_api_notification_win.cc b/atom/browser/api/atom_api_notification_win.cc index fb27fb6e7a55..93482d481624 100644 --- a/atom/browser/api/atom_api_notification_win.cc +++ b/atom/browser/api/atom_api_notification_win.cc @@ -4,10 +4,16 @@ #include "atom/browser/api/atom_api_notification.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/strings/utf_string_conversions.h" +#include "browser/notification.h" +#include "browser/notification_presenter.h" +#include "browser/win/notification_presenter_win7.h" #include "common/string_conversion.h" +#include "third_party/skia/include/core/SkBitmap.h" +#include "url/gurl.h" namespace atom { @@ -15,18 +21,33 @@ namespace api { bool can_toast_ = true; bool initialized_ = false; +brightray::NotificationPresenter* presenter; void Notification::Show() { - atom::AtomToastHandler* handler = new atom::AtomToastHandler(this); - WinToastLib::WinToastTemplate toast = WinToastLib::WinToastTemplate(WinToastLib::WinToastTemplate::TextTwoLines); - // toast.setImagePath(L"C:\example.png"); - toast.setTextField(title_, WinToastLib::WinToastTemplate::TextField::FirstLine); - toast.setTextField(body_, WinToastLib::WinToastTemplate::TextField::SecondLine); - toast.setSilent(silent_); + if (can_toast_) { + atom::AtomToastHandler* handler = new atom::AtomToastHandler(this); + WinToastLib::WinToastTemplate toast = WinToastLib::WinToastTemplate(WinToastLib::WinToastTemplate::TextTwoLines); + // toast.setImagePath(L"C:\example.png"); + toast.setTextField(title_, WinToastLib::WinToastTemplate::TextField::FirstLine); + toast.setTextField(body_, WinToastLib::WinToastTemplate::TextField::SecondLine); + toast.setSilent(silent_); - WinToastLib::WinToast::instance()->showToast(toast, handler); + WinToastLib::WinToast::instance()->showToast(toast, handler); - OnShown(); + OnShown(); + } else { + AtomNotificationDelegateAdapter* adapter = new AtomNotificationDelegateAdapter(this); + auto notif = presenter->CreateNotification(adapter); + GURL* u = new GURL; + notif->Show( + title_, + body_, + "", + u->Resolve(""), + *(new SkBitmap), + true + ); + } } void Notification::OnInitialProps() { @@ -37,6 +58,10 @@ void Notification::OnInitialProps() { ); can_toast_ = WinToastLib::WinToast::instance()->initialize(); } + can_toast_ = false; + if (!can_toast_) { + presenter = new brightray::NotificationPresenterWin7; + } } void Notification::NotifyPropsUpdated() { diff --git a/atom/browser/ui/notification_delegate_adapter.cc b/atom/browser/ui/notification_delegate_adapter.cc new file mode 100644 index 000000000000..bea8f3f296e9 --- /dev/null +++ b/atom/browser/ui/notification_delegate_adapter.cc @@ -0,0 +1,21 @@ +#include "atom/browser/ui/notification_delegate_adapter.h" + +#include "atom/browser/api/atom_api_notification.h" +#include "browser/notification_delegate.h" + +namespace atom { + +AtomNotificationDelegateAdapter::AtomNotificationDelegateAdapter(atom::api::Notification* target) { + observer_ = target; +} +void AtomNotificationDelegateAdapter::NotificationDisplayed() { + observer_->OnShown(); +} +void AtomNotificationDelegateAdapter::NotificationClosed() {} +void AtomNotificationDelegateAdapter::NotificationClick() { + observer_->OnClicked(); +} +void AtomNotificationDelegateAdapter::NotificationDestroyed() {} +void AtomNotificationDelegateAdapter::NotificationFailed() {} + +} \ No newline at end of file diff --git a/atom/browser/ui/notification_delegate_adapter.h b/atom/browser/ui/notification_delegate_adapter.h new file mode 100644 index 000000000000..2bbbc0512a6c --- /dev/null +++ b/atom/browser/ui/notification_delegate_adapter.h @@ -0,0 +1,28 @@ +// 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 "browser/notification_delegate.h" + +#include "atom/browser/api/atom_api_notification.h" + +#ifndef ATOM_BROWSER_UI_NOTIFICATION_DELEGATE_ADAPTER_H_ +#define ATOM_BROWSER_UI_NOTIFICATION_DELEGATE_ADAPTER_H_ + +namespace atom { + +class AtomNotificationDelegateAdapter : public brightray::NotificationDelegate { + public: + atom::api::Notification* observer_; + AtomNotificationDelegateAdapter(atom::api::Notification* target); + + void NotificationDisplayed(); + void NotificationClosed(); + void NotificationClick(); + void NotificationDestroyed(); + void NotificationFailed(); +}; + +} + +#endif // ATOM_BROWSER_UI_NOTIFICATION_DELEGATE_ADAPTER_H_ \ No newline at end of file diff --git a/default_app/default_app.js b/default_app/default_app.js index 446c159b6c1a..d002a8d24aee 100644 --- a/default_app/default_app.js +++ b/default_app/default_app.js @@ -34,7 +34,7 @@ exports.load = (appUrl) => { silent: true, icon: '/Users/samuel/Downloads/ninja.png', hasReply: true, - replyPlacehodler: 'Type Here!!' + replyPlaceholder: 'Type Here!!' }); n.on('show', () => console.log('showed')); n.on('click', () => console.info('clicked!!')); diff --git a/filenames.gypi b/filenames.gypi index 526fa3ad353f..fb67697965a9 100644 --- a/filenames.gypi +++ b/filenames.gypi @@ -315,6 +315,8 @@ 'atom/browser/ui/message_box_gtk.cc', 'atom/browser/ui/message_box_mac.mm', 'atom/browser/ui/message_box_win.cc', + 'atom/browser/ui/notification_delegate_adapter.h', + 'atom/browser/ui/notification_delegate_adapter.cc', 'atom/browser/ui/notification_observer.h', 'atom/browser/ui/tray_icon.cc', 'atom/browser/ui/tray_icon.h',