Code cleanup
This commit is contained in:
		
					parent
					
						
							
								03688b9415
							
						
					
				
			
			
				commit
				
					
						c6196810a6
					
				
			
		
					 11 changed files with 758 additions and 614 deletions
				
			
		| 
						 | 
				
			
			@ -4,6 +4,7 @@
 | 
			
		|||
 | 
			
		||||
#include "atom/browser/api/atom_api_notification.h"
 | 
			
		||||
 | 
			
		||||
#include <map>
 | 
			
		||||
#include <string>
 | 
			
		||||
 | 
			
		||||
#include "atom/browser/api/atom_api_menu.h"
 | 
			
		||||
| 
						 | 
				
			
			@ -25,7 +26,9 @@ namespace api {
 | 
			
		|||
int id_counter = 1;
 | 
			
		||||
std::map<int, Notification*> notifications_;
 | 
			
		||||
 | 
			
		||||
Notification::Notification(v8::Isolate* isolate, v8::Local<v8::Object> wrapper, mate::Arguments* args) {
 | 
			
		||||
Notification::Notification(v8::Isolate* isolate,
 | 
			
		||||
                           v8::Local<v8::Object> wrapper,
 | 
			
		||||
                           mate::Arguments* args) {
 | 
			
		||||
  InitWith(isolate, wrapper);
 | 
			
		||||
 | 
			
		||||
  mate::Dictionary opts;
 | 
			
		||||
| 
						 | 
				
			
			@ -65,19 +68,46 @@ Notification* Notification::FromID(int id) {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
// Getters
 | 
			
		||||
int Notification::GetID() { return id_; } 
 | 
			
		||||
base::string16 Notification::GetTitle() { return title_; }
 | 
			
		||||
base::string16 Notification::GetBody() { return body_; }
 | 
			
		||||
bool Notification::GetSilent() { return silent_; }
 | 
			
		||||
base::string16 Notification::GetReplyPlaceholder() { return reply_placeholder_; }
 | 
			
		||||
bool Notification::GetHasReply() { return has_reply_; }
 | 
			
		||||
int Notification::GetID() {
 | 
			
		||||
  return id_;
 | 
			
		||||
}
 | 
			
		||||
base::string16 Notification::GetTitle() {
 | 
			
		||||
  return title_;
 | 
			
		||||
}
 | 
			
		||||
base::string16 Notification::GetBody() {
 | 
			
		||||
  return body_;
 | 
			
		||||
}
 | 
			
		||||
bool Notification::GetSilent() {
 | 
			
		||||
  return silent_;
 | 
			
		||||
}
 | 
			
		||||
base::string16 Notification::GetReplyPlaceholder() {
 | 
			
		||||
  return reply_placeholder_;
 | 
			
		||||
}
 | 
			
		||||
bool Notification::GetHasReply() {
 | 
			
		||||
  return has_reply_;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Setters
 | 
			
		||||
void Notification::SetTitle(base::string16 new_title) { title_ = new_title; NotifyPropsUpdated(); }
 | 
			
		||||
void Notification::SetBody(base::string16 new_body) { body_ = new_body; NotifyPropsUpdated(); }
 | 
			
		||||
void Notification::SetSilent(bool new_silent) { silent_ = new_silent; NotifyPropsUpdated(); }
 | 
			
		||||
void Notification::SetReplyPlaceholder(base::string16 new_reply_placeholder) { reply_placeholder_ = new_reply_placeholder; NotifyPropsUpdated(); }
 | 
			
		||||
void Notification::SetHasReply(bool new_has_reply) { has_reply_ = new_has_reply; NotifyPropsUpdated(); }
 | 
			
		||||
void Notification::SetTitle(base::string16 new_title) {
 | 
			
		||||
  title_ = new_title;
 | 
			
		||||
  NotifyPropsUpdated();
 | 
			
		||||
}
 | 
			
		||||
void Notification::SetBody(base::string16 new_body) {
 | 
			
		||||
  body_ = new_body;
 | 
			
		||||
  NotifyPropsUpdated();
 | 
			
		||||
}
 | 
			
		||||
void Notification::SetSilent(bool new_silent) {
 | 
			
		||||
  silent_ = new_silent;
 | 
			
		||||
  NotifyPropsUpdated();
 | 
			
		||||
}
 | 
			
		||||
void Notification::SetReplyPlaceholder(base::string16 new_reply_placeholder) {
 | 
			
		||||
  reply_placeholder_ = new_reply_placeholder;
 | 
			
		||||
  NotifyPropsUpdated();
 | 
			
		||||
}
 | 
			
		||||
void Notification::SetHasReply(bool new_has_reply) {
 | 
			
		||||
  has_reply_ = new_has_reply;
 | 
			
		||||
  NotifyPropsUpdated();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Notification::OnClicked() {
 | 
			
		||||
  Emit("click");
 | 
			
		||||
| 
						 | 
				
			
			@ -93,7 +123,7 @@ void Notification::OnShown() {
 | 
			
		|||
 | 
			
		||||
// static
 | 
			
		||||
void Notification::BuildPrototype(v8::Isolate* isolate,
 | 
			
		||||
                          v8::Local<v8::FunctionTemplate> prototype) {
 | 
			
		||||
                                  v8::Local<v8::FunctionTemplate> prototype) {
 | 
			
		||||
  prototype->SetClassName(mate::StringToV8(isolate, "Notification"));
 | 
			
		||||
  mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
 | 
			
		||||
      .MakeDestroyable()
 | 
			
		||||
| 
						 | 
				
			
			@ -102,26 +132,30 @@ void Notification::BuildPrototype(v8::Isolate* isolate,
 | 
			
		|||
      .SetProperty("title", &Notification::GetTitle, &Notification::SetTitle)
 | 
			
		||||
      .SetProperty("body", &Notification::GetBody, &Notification::SetBody)
 | 
			
		||||
      .SetProperty("silent", &Notification::GetSilent, &Notification::SetSilent)
 | 
			
		||||
      .SetProperty("replyPlaceholder", &Notification::GetReplyPlaceholder, &Notification::SetReplyPlaceholder)
 | 
			
		||||
      .SetProperty("hasReply", &Notification::GetHasReply, &Notification::SetHasReply);
 | 
			
		||||
      .SetProperty("replyPlaceholder", &Notification::GetReplyPlaceholder,
 | 
			
		||||
                   &Notification::SetReplyPlaceholder)
 | 
			
		||||
      .SetProperty("hasReply", &Notification::GetHasReply,
 | 
			
		||||
                   &Notification::SetHasReply);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace api
 | 
			
		||||
 | 
			
		||||
}  // namespace atom
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
namespace {
 | 
			
		||||
 | 
			
		||||
using atom::api::Notification;
 | 
			
		||||
 | 
			
		||||
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
 | 
			
		||||
                v8::Local<v8::Context> context, void* priv) {
 | 
			
		||||
void Initialize(v8::Local<v8::Object> exports,
 | 
			
		||||
                v8::Local<v8::Value> unused,
 | 
			
		||||
                v8::Local<v8::Context> context,
 | 
			
		||||
                void* priv) {
 | 
			
		||||
  v8::Isolate* isolate = context->GetIsolate();
 | 
			
		||||
  Notification::SetConstructor(isolate, base::Bind(&Notification::New));
 | 
			
		||||
 | 
			
		||||
  mate::Dictionary dict(isolate, exports);
 | 
			
		||||
  dict.Set("Notification", Notification::GetConstructor(isolate)->GetFunction());
 | 
			
		||||
  dict.Set("Notification",
 | 
			
		||||
           Notification::GetConstructor(isolate)->GetFunction());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -20,7 +20,7 @@ namespace atom {
 | 
			
		|||
namespace api {
 | 
			
		||||
 | 
			
		||||
class Notification : public mate::TrackableObject<Notification>,
 | 
			
		||||
             public NotifictionObserver {
 | 
			
		||||
                     public NotifictionObserver {
 | 
			
		||||
 public:
 | 
			
		||||
  static mate::WrappableBase* New(mate::Arguments* args);
 | 
			
		||||
  static bool HasID(int id);
 | 
			
		||||
| 
						 | 
				
			
			@ -35,7 +35,9 @@ class Notification : public mate::TrackableObject<Notification>,
 | 
			
		|||
  void OnShown() override;
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  Notification(v8::Isolate* isolate, v8::Local<v8::Object> wrapper, mate::Arguments* args);
 | 
			
		||||
  Notification(v8::Isolate* isolate,
 | 
			
		||||
               v8::Local<v8::Object> wrapper,
 | 
			
		||||
               mate::Arguments* args);
 | 
			
		||||
  ~Notification() override;
 | 
			
		||||
 | 
			
		||||
  void OnInitialProps();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -48,7 +48,8 @@ void Notification::Show() {
 | 
			
		|||
 | 
			
		||||
  if (can_toast_) {
 | 
			
		||||
    atom::AtomToastHandler* handler = new atom::AtomToastHandler(this);
 | 
			
		||||
    WinToastLib::WinToastTemplate::WinToastTemplateType toastType = WinToastLib::WinToastTemplate::TextOneLine;
 | 
			
		||||
    WinToastLib::WinToastTemplate::WinToastTemplateType toastType =
 | 
			
		||||
        WinToastLib::WinToastTemplate::TextOneLine;
 | 
			
		||||
    if (!has_icon_) {
 | 
			
		||||
      if (body_ != L"") {
 | 
			
		||||
        toastType = WinToastLib::WinToastTemplate::TextTwoLines;
 | 
			
		||||
| 
						 | 
				
			
			@ -62,40 +63,41 @@ void Notification::Show() {
 | 
			
		|||
        toastType = WinToastLib::WinToastTemplate::ImageWithOneLine;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    WinToastLib::WinToastTemplate toast = WinToastLib::WinToastTemplate(toastType);
 | 
			
		||||
    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));
 | 
			
		||||
    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.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);
 | 
			
		||||
    AtomNotificationDelegateAdapter* adapter =
 | 
			
		||||
        new AtomNotificationDelegateAdapter(this);
 | 
			
		||||
    auto notif = presenter->CreateNotification(adapter);
 | 
			
		||||
    GURL nullUrl = *(new GURL);
 | 
			
		||||
    notif->Show(
 | 
			
		||||
      title_,
 | 
			
		||||
      body_,
 | 
			
		||||
      "",
 | 
			
		||||
      nullUrl,
 | 
			
		||||
      image,
 | 
			
		||||
      silent_
 | 
			
		||||
    );
 | 
			
		||||
    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());
 | 
			
		||||
    WinToastLib::WinToast::instance()->setAppName(
 | 
			
		||||
        base::UTF8ToUTF16(browser->GetName()));
 | 
			
		||||
    WinToastLib::WinToast::instance()->setAppUserModelId(
 | 
			
		||||
        browser->GetAppUserModelID());
 | 
			
		||||
    can_toast_ = WinToastLib::WinToast::instance()->initialize();
 | 
			
		||||
    temp_dir_.CreateUniqueTempDir();
 | 
			
		||||
  }
 | 
			
		||||
| 
						 | 
				
			
			@ -104,10 +106,6 @@ void Notification::OnInitialProps() {
 | 
			
		|||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Notification::NotifyPropsUpdated() {
 | 
			
		||||
  
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
void Notification::NotifyPropsUpdated() {}
 | 
			
		||||
}  // namespace api
 | 
			
		||||
}  // namespace atom
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,3 +1,7 @@
 | 
			
		|||
// 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/ui/notification_delegate_adapter.h"
 | 
			
		||||
 | 
			
		||||
#include "atom/browser/api/atom_api_notification.h"
 | 
			
		||||
| 
						 | 
				
			
			@ -5,17 +9,18 @@
 | 
			
		|||
 | 
			
		||||
namespace atom {
 | 
			
		||||
 | 
			
		||||
AtomNotificationDelegateAdapter::AtomNotificationDelegateAdapter(atom::api::Notification* target) {
 | 
			
		||||
    observer_ = target;
 | 
			
		||||
AtomNotificationDelegateAdapter::AtomNotificationDelegateAdapter(
 | 
			
		||||
    atom::api::Notification* target) {
 | 
			
		||||
  observer_ = target;
 | 
			
		||||
}
 | 
			
		||||
void AtomNotificationDelegateAdapter::NotificationDisplayed() {
 | 
			
		||||
    observer_->OnShown();
 | 
			
		||||
  observer_->OnShown();
 | 
			
		||||
}
 | 
			
		||||
void AtomNotificationDelegateAdapter::NotificationClosed() {}
 | 
			
		||||
void AtomNotificationDelegateAdapter::NotificationClick() {
 | 
			
		||||
    observer_->OnClicked();
 | 
			
		||||
  observer_->OnClicked();
 | 
			
		||||
}
 | 
			
		||||
void AtomNotificationDelegateAdapter::NotificationDestroyed() {}
 | 
			
		||||
void AtomNotificationDelegateAdapter::NotificationFailed() {}
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
}  // namespace atom
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -12,17 +12,17 @@
 | 
			
		|||
namespace atom {
 | 
			
		||||
 | 
			
		||||
class AtomNotificationDelegateAdapter : public brightray::NotificationDelegate {
 | 
			
		||||
  public:
 | 
			
		||||
    atom::api::Notification* observer_;
 | 
			
		||||
    AtomNotificationDelegateAdapter(atom::api::Notification* target);
 | 
			
		||||
 public:
 | 
			
		||||
  atom::api::Notification* observer_;
 | 
			
		||||
  explicit AtomNotificationDelegateAdapter(atom::api::Notification* target);
 | 
			
		||||
 | 
			
		||||
    void NotificationDisplayed();
 | 
			
		||||
    void NotificationClosed();
 | 
			
		||||
    void NotificationClick();
 | 
			
		||||
    void NotificationDestroyed();
 | 
			
		||||
    void NotificationFailed();
 | 
			
		||||
  void NotificationDisplayed();
 | 
			
		||||
  void NotificationClosed();
 | 
			
		||||
  void NotificationClick();
 | 
			
		||||
  void NotificationDestroyed();
 | 
			
		||||
  void NotificationFailed();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
}  // namespace atom
 | 
			
		||||
 | 
			
		||||
#endif // ATOM_BROWSER_UI_NOTIFICATION_DELEGATE_ADAPTER_H_
 | 
			
		||||
#endif  // ATOM_BROWSER_UI_NOTIFICATION_DELEGATE_ADAPTER_H_
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,6 +5,8 @@
 | 
			
		|||
#ifndef ATOM_BROWSER_UI_NOTIFICATION_OBSERVER_H_
 | 
			
		||||
#define ATOM_BROWSER_UI_NOTIFICATION_OBSERVER_H_
 | 
			
		||||
 | 
			
		||||
#include <string>
 | 
			
		||||
 | 
			
		||||
namespace atom {
 | 
			
		||||
 | 
			
		||||
class NotifictionObserver {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -15,14 +15,15 @@ AtomToastHandler::AtomToastHandler(atom::api::Notification* target) {
 | 
			
		|||
 | 
			
		||||
void AtomToastHandler::toastActivated() {
 | 
			
		||||
  observer_->OnClicked();
 | 
			
		||||
};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void AtomToastHandler::toastDismissed(WinToastLib::WinToastHandler::WinToastDismissalReason state) {
 | 
			
		||||
void AtomToastHandler::toastDismissed(
 | 
			
		||||
  WinToastLib::WinToastHandler::WinToastDismissalReason state) {
 | 
			
		||||
  // observer_->OnDismissed();
 | 
			
		||||
};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void AtomToastHandler::toastFailed() {
 | 
			
		||||
  // observer_->OnErrored();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace atom
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,21 +5,22 @@
 | 
			
		|||
#include "atom/browser/ui/win/toast_lib.h"
 | 
			
		||||
#include "atom/browser/api/atom_api_notification.h"
 | 
			
		||||
 | 
			
		||||
#ifndef ATOM_BROWSER_UI_TOAST_HANDLER_H_
 | 
			
		||||
#define ATOM_BROWSER_UI_TOAST_HANDLER_H_
 | 
			
		||||
#ifndef ATOM_BROWSER_UI_WIN_TOAST_HANDLER_H_
 | 
			
		||||
#define ATOM_BROWSER_UI_WIN_TOAST_HANDLER_H_
 | 
			
		||||
 | 
			
		||||
namespace atom {
 | 
			
		||||
 | 
			
		||||
class AtomToastHandler : public WinToastLib::WinToastHandler {
 | 
			
		||||
 public:
 | 
			
		||||
  atom::api::Notification* observer_;
 | 
			
		||||
  AtomToastHandler(atom::api::Notification* target);
 | 
			
		||||
  explicit AtomToastHandler(atom::api::Notification* target);
 | 
			
		||||
 | 
			
		||||
  void toastActivated() override;
 | 
			
		||||
  void toastDismissed(WinToastLib::WinToastHandler::WinToastDismissalReason state);
 | 
			
		||||
  void toastDismissed(
 | 
			
		||||
      WinToastLib::WinToastHandler::WinToastDismissalReason state);
 | 
			
		||||
  void toastFailed();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
}  // namespace atom
 | 
			
		||||
 | 
			
		||||
#endif  // ATOM_BROWSER_UI_TOAST_HANDLER_H_
 | 
			
		||||
#endif  // ATOM_BROWSER_UI_WIN_TOAST_HANDLER_H_
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
										
											
												File diff suppressed because it is too large
												Load diff
											
										
									
								
							| 
						 | 
				
			
			@ -9,8 +9,8 @@
 | 
			
		|||
// copies of the Software, and to permit persons to whom the Software is
 | 
			
		||||
// furnished to do so, subject to the following conditions:
 | 
			
		||||
 | 
			
		||||
// The above copyright notice and this permission notice shall be included in all
 | 
			
		||||
// copies or substantial portions of the Software.
 | 
			
		||||
// The above copyright notice and this permission notice shall be included in
 | 
			
		||||
// all copies or substantial portions of the Software.
 | 
			
		||||
 | 
			
		||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
			
		||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
			
		||||
| 
						 | 
				
			
			@ -20,149 +20,179 @@
 | 
			
		|||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 | 
			
		||||
// SOFTWARE.
 | 
			
		||||
 | 
			
		||||
#ifndef WINTOASTLIB_H
 | 
			
		||||
#define WINTOASTLIB_H
 | 
			
		||||
#include <windows.h>
 | 
			
		||||
#include <SDKDDKVer.h>
 | 
			
		||||
#include <WinUser.h>
 | 
			
		||||
#include <Shobjidl.h>
 | 
			
		||||
#include <wrl/implements.h>
 | 
			
		||||
#include <wrl/event.h>
 | 
			
		||||
#include <windows.ui.notifications.h>
 | 
			
		||||
#include <strsafe.h>
 | 
			
		||||
#include <Psapi.h>
 | 
			
		||||
#include <shlobj.h>
 | 
			
		||||
#include <roapi.h>
 | 
			
		||||
#include <propvarutil.h>
 | 
			
		||||
#include <functiondiscoverykeys.h>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <winstring.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <vector>
 | 
			
		||||
using namespace Microsoft::WRL;
 | 
			
		||||
using namespace ABI::Windows::Data::Xml::Dom;
 | 
			
		||||
using namespace ABI::Windows::Foundation;
 | 
			
		||||
using namespace ABI::Windows::UI::Notifications;
 | 
			
		||||
using namespace Windows::Foundation;
 | 
			
		||||
#ifndef ATOM_BROWSER_UI_WIN_TOAST_LIB_H_
 | 
			
		||||
#define ATOM_BROWSER_UI_WIN_TOAST_LIB_H_
 | 
			
		||||
 | 
			
		||||
#define DEFAULT_SHELL_LINKS_PATH	L"\\Microsoft\\Windows\\Start Menu\\Programs\\"
 | 
			
		||||
#define DEFAULT_LINK_FORMAT			L".lnk"
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <winstring.h>
 | 
			
		||||
 | 
			
		||||
#include <SDKDDKVer.h>
 | 
			
		||||
#include <Shobjidl.h>
 | 
			
		||||
#include <WinUser.h>
 | 
			
		||||
 | 
			
		||||
#include <functiondiscoverykeys.h>
 | 
			
		||||
#include <propvarutil.h>
 | 
			
		||||
#include <roapi.h>
 | 
			
		||||
#include <shlobj.h>
 | 
			
		||||
#include <strsafe.h>
 | 
			
		||||
#include <windows.h>
 | 
			
		||||
#include <windows.ui.notifications.h>
 | 
			
		||||
#include <wrl/event.h>
 | 
			
		||||
#include <wrl/implements.h>
 | 
			
		||||
 | 
			
		||||
#include <Psapi.h>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
using Microsoft::WRL::ComPtr;
 | 
			
		||||
using Microsoft::WRL::Details::ComPtrRef;
 | 
			
		||||
using Microsoft::WRL::Callback;
 | 
			
		||||
using Microsoft::WRL::Implements;
 | 
			
		||||
using Microsoft::WRL::RuntimeClassFlags;
 | 
			
		||||
using Microsoft::WRL::ClassicCom;
 | 
			
		||||
using ABI::Windows::Data::Xml::Dom::IXmlAttribute;
 | 
			
		||||
using ABI::Windows::Data::Xml::Dom::IXmlDocument;
 | 
			
		||||
using ABI::Windows::Data::Xml::Dom::IXmlElement;
 | 
			
		||||
using ABI::Windows::Data::Xml::Dom::IXmlNamedNodeMap;
 | 
			
		||||
using ABI::Windows::Data::Xml::Dom::IXmlNode;
 | 
			
		||||
using ABI::Windows::Data::Xml::Dom::IXmlNodeList;
 | 
			
		||||
using ABI::Windows::Data::Xml::Dom::IXmlText;
 | 
			
		||||
using ABI::Windows::UI::Notifications::ToastDismissalReason;
 | 
			
		||||
using ABI::Windows::UI::Notifications::ToastTemplateType;
 | 
			
		||||
using ABI::Windows::UI::Notifications::IToastNotificationManagerStatics;
 | 
			
		||||
using ABI::Windows::UI::Notifications::IToastNotifier;
 | 
			
		||||
using ABI::Windows::UI::Notifications::IToastNotificationFactory;
 | 
			
		||||
using ABI::Windows::UI::Notifications::IToastNotification;
 | 
			
		||||
using ABI::Windows::UI::Notifications::ToastNotification;
 | 
			
		||||
using ABI::Windows::UI::Notifications::ToastDismissedEventArgs;
 | 
			
		||||
using ABI::Windows::UI::Notifications::IToastDismissedEventArgs;
 | 
			
		||||
using ABI::Windows::UI::Notifications::ToastFailedEventArgs;
 | 
			
		||||
using ABI::Windows::UI::Notifications::IToastFailedEventArgs;
 | 
			
		||||
using ABI::Windows::UI::Notifications::ToastTemplateType_ToastText01;
 | 
			
		||||
using ABI::Windows::Foundation::ITypedEventHandler;
 | 
			
		||||
 | 
			
		||||
#define DEFAULT_SHELL_LINKS_PATH L"\\Microsoft\\Windows\\Start Menu\\Programs\\"
 | 
			
		||||
#define DEFAULT_LINK_FORMAT L".lnk"
 | 
			
		||||
 | 
			
		||||
namespace WinToastLib {
 | 
			
		||||
    class WinToastStringWrapper {
 | 
			
		||||
    public:
 | 
			
		||||
        WinToastStringWrapper(_In_reads_(length) PCWSTR stringRef, _In_ UINT32 length) throw();
 | 
			
		||||
        WinToastStringWrapper(_In_ const std::wstring &stringRef) throw();
 | 
			
		||||
        ~WinToastStringWrapper();
 | 
			
		||||
        HSTRING Get() const throw();
 | 
			
		||||
    private:
 | 
			
		||||
        HSTRING _hstring;
 | 
			
		||||
        HSTRING_HEADER _header;
 | 
			
		||||
class WinToastStringWrapper {
 | 
			
		||||
 public:
 | 
			
		||||
  WinToastStringWrapper(_In_reads_(length) PCWSTR stringRef,
 | 
			
		||||
                        _In_ UINT32 length) throw();
 | 
			
		||||
  explicit WinToastStringWrapper(_In_ const std::wstring& stringRef) throw();
 | 
			
		||||
  ~WinToastStringWrapper();
 | 
			
		||||
  HSTRING Get() const throw();
 | 
			
		||||
 | 
			
		||||
    };
 | 
			
		||||
 private:
 | 
			
		||||
  HSTRING _hstring;
 | 
			
		||||
  HSTRING_HEADER _header;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class WinToastHandler {
 | 
			
		||||
 public:
 | 
			
		||||
  enum WinToastDismissalReason {
 | 
			
		||||
    UserCanceled = ToastDismissalReason::ToastDismissalReason_UserCanceled,
 | 
			
		||||
    ApplicationHidden =
 | 
			
		||||
        ToastDismissalReason::ToastDismissalReason_ApplicationHidden,
 | 
			
		||||
    TimedOut = ToastDismissalReason::ToastDismissalReason_TimedOut
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
    class WinToastHandler {
 | 
			
		||||
    public:
 | 
			
		||||
        enum WinToastDismissalReason {
 | 
			
		||||
            UserCanceled = ToastDismissalReason::ToastDismissalReason_UserCanceled,
 | 
			
		||||
            ApplicationHidden = ToastDismissalReason::ToastDismissalReason_ApplicationHidden,
 | 
			
		||||
            TimedOut = ToastDismissalReason::ToastDismissalReason_TimedOut
 | 
			
		||||
        };
 | 
			
		||||
  virtual void toastActivated() {}
 | 
			
		||||
  virtual void toastDismissed(WinToastDismissalReason state) {}
 | 
			
		||||
  virtual void toastFailed() {}
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
        virtual void toastActivated() {};
 | 
			
		||||
        virtual void toastDismissed(WinToastDismissalReason state) {};
 | 
			
		||||
        virtual void toastFailed() {};
 | 
			
		||||
    };
 | 
			
		||||
class WinToastTemplate {
 | 
			
		||||
 public:
 | 
			
		||||
  enum TextField { FirstLine = 0, SecondLine, ThirdLine, LineCount };
 | 
			
		||||
 | 
			
		||||
    class WinToastTemplate
 | 
			
		||||
    {
 | 
			
		||||
    public:
 | 
			
		||||
        enum TextField {
 | 
			
		||||
            FirstLine = 0,
 | 
			
		||||
            SecondLine,
 | 
			
		||||
            ThirdLine,
 | 
			
		||||
            LineCount
 | 
			
		||||
        };
 | 
			
		||||
  enum WinToastTemplateType {
 | 
			
		||||
    ImageWithOneLine = ToastTemplateType::ToastTemplateType_ToastImageAndText01,
 | 
			
		||||
    ImageWithTwoLines =
 | 
			
		||||
        ToastTemplateType::ToastTemplateType_ToastImageAndText02,
 | 
			
		||||
    ImageWithThreeLines =
 | 
			
		||||
        ToastTemplateType::ToastTemplateType_ToastImageAndText03,
 | 
			
		||||
    ImageWithFourLines =
 | 
			
		||||
        ToastTemplateType::ToastTemplateType_ToastImageAndText04,
 | 
			
		||||
    TextOneLine = ToastTemplateType::ToastTemplateType_ToastText01,
 | 
			
		||||
    TextTwoLines = ToastTemplateType::ToastTemplateType_ToastText02,
 | 
			
		||||
    TextThreeLines = ToastTemplateType::ToastTemplateType_ToastText03,
 | 
			
		||||
    TextFourLines = ToastTemplateType::ToastTemplateType_ToastText04,
 | 
			
		||||
    WinToastTemplateTypeCount
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
        enum WinToastTemplateType {
 | 
			
		||||
            ImageWithOneLine = ToastTemplateType::ToastTemplateType_ToastImageAndText01,
 | 
			
		||||
            ImageWithTwoLines = ToastTemplateType::ToastTemplateType_ToastImageAndText02,
 | 
			
		||||
            ImageWithThreeLines = ToastTemplateType::ToastTemplateType_ToastImageAndText03,
 | 
			
		||||
            ImageWithFourLines = ToastTemplateType::ToastTemplateType_ToastImageAndText04,
 | 
			
		||||
            TextOneLine = ToastTemplateType::ToastTemplateType_ToastText01,
 | 
			
		||||
            TextTwoLines = ToastTemplateType::ToastTemplateType_ToastText02,
 | 
			
		||||
            TextThreeLines = ToastTemplateType::ToastTemplateType_ToastText03,
 | 
			
		||||
            TextFourLines = ToastTemplateType::ToastTemplateType_ToastText04,
 | 
			
		||||
            WinToastTemplateTypeCount
 | 
			
		||||
        };
 | 
			
		||||
  explicit WinToastTemplate(
 | 
			
		||||
      _In_ const WinToastTemplateType& type = ImageWithTwoLines);
 | 
			
		||||
  ~WinToastTemplate();
 | 
			
		||||
 | 
			
		||||
        WinToastTemplate(_In_ const WinToastTemplateType& type = ImageWithTwoLines);
 | 
			
		||||
        ~WinToastTemplate();
 | 
			
		||||
  int textFieldsCount() const { return _textFields.size(); }
 | 
			
		||||
  bool hasImage() const { return _hasImage; }
 | 
			
		||||
  std::vector<std::wstring> textFields() const { return _textFields; }
 | 
			
		||||
  std::wstring textField(_In_ const TextField& pos) const {
 | 
			
		||||
    return _textFields[pos];
 | 
			
		||||
  }
 | 
			
		||||
  std::wstring imagePath() const { return _imagePath; }
 | 
			
		||||
  WinToastTemplateType type() const { return _type; }
 | 
			
		||||
  void setTextField(_In_ const std::wstring& txt, _In_ const TextField& pos);
 | 
			
		||||
  void setImagePath(_In_ const std::wstring& imgPath);
 | 
			
		||||
  void setSilent(bool is_silent);
 | 
			
		||||
  bool isSilent() const { return _silent; }
 | 
			
		||||
 | 
			
		||||
        int                                 textFieldsCount() const { return _textFields.size(); }
 | 
			
		||||
        bool                                hasImage() const { return _hasImage; }
 | 
			
		||||
        std::vector<std::wstring>			textFields() const { return _textFields; }
 | 
			
		||||
        std::wstring                        textField(_In_ const TextField& pos) const { return _textFields[pos]; }
 | 
			
		||||
        std::wstring                        imagePath() const { return _imagePath; }
 | 
			
		||||
        WinToastTemplateType                type() const { return _type; }
 | 
			
		||||
        void                                setTextField(_In_ const std::wstring& txt, _In_ const TextField& pos);
 | 
			
		||||
        void                                setImagePath(_In_ const std::wstring& imgPath);
 | 
			
		||||
        void                                setSilent(bool is_silent);
 | 
			
		||||
        bool                                isSilent() const { return _silent; }
 | 
			
		||||
    private:
 | 
			
		||||
        static int                          TextFieldsCount[WinToastTemplateTypeCount];
 | 
			
		||||
        bool                                _hasImage;
 | 
			
		||||
        bool                                _silent = false;
 | 
			
		||||
        std::vector<std::wstring>			_textFields;
 | 
			
		||||
        std::wstring                        _imagePath;
 | 
			
		||||
        WinToastTemplateType                _type;
 | 
			
		||||
        void initComponentsFromType();
 | 
			
		||||
    };
 | 
			
		||||
 private:
 | 
			
		||||
  static int TextFieldsCount[WinToastTemplateTypeCount];
 | 
			
		||||
  bool _hasImage;
 | 
			
		||||
  bool _silent = false;
 | 
			
		||||
  std::vector<std::wstring> _textFields;
 | 
			
		||||
  std::wstring _imagePath;
 | 
			
		||||
  WinToastTemplateType _type;
 | 
			
		||||
  void initComponentsFromType();
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
    class WinToast {
 | 
			
		||||
    public:
 | 
			
		||||
        static WinToast* instance();
 | 
			
		||||
        static bool isCompatible();
 | 
			
		||||
        static std::wstring configureAUMI(_In_ const std::wstring& company,
 | 
			
		||||
                                                    _In_ const std::wstring& name,
 | 
			
		||||
                                                    _In_ const std::wstring& surname,
 | 
			
		||||
                                                    _In_ const std::wstring& versionInfo
 | 
			
		||||
                                                    );
 | 
			
		||||
        bool                initialize();
 | 
			
		||||
        bool                isInitialized() const { return _isInitialized; }
 | 
			
		||||
        bool                showToast(_In_ const WinToastTemplate& toast, _In_ WinToastHandler* handler);
 | 
			
		||||
        std::wstring        appName() const;
 | 
			
		||||
        std::wstring        appUserModelId() const;
 | 
			
		||||
        void                setAppUserModelId(_In_ const std::wstring& appName);
 | 
			
		||||
        void                setAppName(_In_ const std::wstring& appName);
 | 
			
		||||
    private:
 | 
			
		||||
        bool											_isInitialized;
 | 
			
		||||
        std::wstring                                    _appName;
 | 
			
		||||
        std::wstring                                    _aumi;
 | 
			
		||||
        ComPtr<IXmlDocument>                            _xmlDocument;
 | 
			
		||||
        ComPtr<IToastNotificationManagerStatics>        _notificationManager;
 | 
			
		||||
        ComPtr<IToastNotifier>                          _notifier;
 | 
			
		||||
        ComPtr<IToastNotificationFactory>               _notificationFactory;
 | 
			
		||||
        ComPtr<IToastNotification>                      _notification;
 | 
			
		||||
        static WinToast*								_instance;
 | 
			
		||||
class WinToast {
 | 
			
		||||
 public:
 | 
			
		||||
  static WinToast* instance();
 | 
			
		||||
  static bool isCompatible();
 | 
			
		||||
  static std::wstring configureAUMI(_In_ const std::wstring& company,
 | 
			
		||||
                                    _In_ const std::wstring& name,
 | 
			
		||||
                                    _In_ const std::wstring& surname,
 | 
			
		||||
                                    _In_ const std::wstring& versionInfo);
 | 
			
		||||
  bool initialize();
 | 
			
		||||
  bool isInitialized() const { return _isInitialized; }
 | 
			
		||||
  bool showToast(_In_ const WinToastTemplate& toast,
 | 
			
		||||
                 _In_ WinToastHandler* handler);
 | 
			
		||||
  std::wstring appName() const;
 | 
			
		||||
  std::wstring appUserModelId() const;
 | 
			
		||||
  void setAppUserModelId(_In_ const std::wstring& appName);
 | 
			
		||||
  void setAppName(_In_ const std::wstring& appName);
 | 
			
		||||
 | 
			
		||||
        WinToast(void);
 | 
			
		||||
        IXmlDocument*							xmlDocument() const { return _xmlDocument.Get(); }
 | 
			
		||||
        IToastNotifier*							notifier() const { return _notifier.Get(); }
 | 
			
		||||
        IToastNotificationFactory*				notificationFactory() const { return _notificationFactory.Get(); }
 | 
			
		||||
        IToastNotificationManagerStatics*		notificationManager() const { return _notificationManager.Get(); }
 | 
			
		||||
        IToastNotification*						notification() const { return _notification.Get(); }
 | 
			
		||||
 private:
 | 
			
		||||
  bool _isInitialized;
 | 
			
		||||
  std::wstring _appName;
 | 
			
		||||
  std::wstring _aumi;
 | 
			
		||||
  ComPtr<IXmlDocument> _xmlDocument;
 | 
			
		||||
  ComPtr<IToastNotificationManagerStatics> _notificationManager;
 | 
			
		||||
  ComPtr<IToastNotifier> _notifier;
 | 
			
		||||
  ComPtr<IToastNotificationFactory> _notificationFactory;
 | 
			
		||||
  ComPtr<IToastNotification> _notification;
 | 
			
		||||
  static WinToast* _instance;
 | 
			
		||||
 | 
			
		||||
        HRESULT     validateShellLink();
 | 
			
		||||
        HRESULT		createShellLink();
 | 
			
		||||
        HRESULT		setImageField(_In_ const std::wstring& path);
 | 
			
		||||
        HRESULT     setTextField(_In_ const std::wstring& text, _In_ int pos);
 | 
			
		||||
        bool        makeSilent(bool is_silent);
 | 
			
		||||
    };
 | 
			
		||||
  WinToast(void);
 | 
			
		||||
  IXmlDocument* xmlDocument() const { return _xmlDocument.Get(); }
 | 
			
		||||
  IToastNotifier* notifier() const { return _notifier.Get(); }
 | 
			
		||||
  IToastNotificationFactory* notificationFactory() const {
 | 
			
		||||
    return _notificationFactory.Get();
 | 
			
		||||
  }
 | 
			
		||||
  IToastNotificationManagerStatics* notificationManager() const {
 | 
			
		||||
    return _notificationManager.Get();
 | 
			
		||||
  }
 | 
			
		||||
  IToastNotification* notification() const { return _notification.Get(); }
 | 
			
		||||
 | 
			
		||||
  HRESULT validateShellLink();
 | 
			
		||||
  HRESULT createShellLink();
 | 
			
		||||
  HRESULT setImageField(_In_ const std::wstring& path);
 | 
			
		||||
  HRESULT setTextField(_In_ const std::wstring& text, _In_ int pos);
 | 
			
		||||
  bool makeSilent(bool is_silent);
 | 
			
		||||
};
 | 
			
		||||
}  // namespace WinToastLib
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#endif // WINTOASTLIB_H
 | 
			
		||||
#endif  // ATOM_BROWSER_UI_WIN_TOAST_LIB_H_
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -32,14 +32,14 @@ exports.load = (appUrl) => {
 | 
			
		|||
      title: 'Hello World',
 | 
			
		||||
      body: 'This is the long and complicated body for this notification that just goes on and on and on and never really seems to stop',
 | 
			
		||||
      silent: true,
 | 
			
		||||
      icon: '/Users/samuel/Downloads/ninja.png',
 | 
			
		||||
      icon: path.resolve('C:\\Users\\Samuel\\Downloads\\icon.png'),
 | 
			
		||||
      hasReply: true,
 | 
			
		||||
      replyPlaceholder: 'Type Here!!'
 | 
			
		||||
    });
 | 
			
		||||
    n.on('show', () => console.log('showed'));
 | 
			
		||||
    n.on('click', () => console.info('clicked!!'));
 | 
			
		||||
    n.on('reply', (e, reply) => console.log('Replied:', reply));
 | 
			
		||||
    })
 | 
			
		||||
    n.on('show', () => console.log('showed'))
 | 
			
		||||
    n.on('click', () => console.info('clicked!!'))
 | 
			
		||||
    n.on('reply', (e, reply) => console.log('Replied:', reply))
 | 
			
		||||
 | 
			
		||||
    n.show();
 | 
			
		||||
    n.show()
 | 
			
		||||
  })
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue