Remove OS specific implementations
This commit is contained in:
parent
058bdfbced
commit
c741b584a1
11 changed files with 40 additions and 1038 deletions
|
@ -9,15 +9,21 @@
|
||||||
|
|
||||||
#include "atom/browser/api/atom_api_menu.h"
|
#include "atom/browser/api/atom_api_menu.h"
|
||||||
#include "atom/browser/browser.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/api/atom_api_native_image.h"
|
||||||
#include "atom/common/native_mate_converters/gfx_converter.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/image_converter.h"
|
||||||
#include "atom/common/native_mate_converters/string16_converter.h"
|
#include "atom/common/native_mate_converters/string16_converter.h"
|
||||||
#include "atom/common/node_includes.h"
|
#include "atom/common/node_includes.h"
|
||||||
|
#include "base/strings/utf_string_conversions.h"
|
||||||
#include "base/threading/thread_task_runner_handle.h"
|
#include "base/threading/thread_task_runner_handle.h"
|
||||||
|
#include "common/string_conversion.h"
|
||||||
#include "native_mate/constructor.h"
|
#include "native_mate/constructor.h"
|
||||||
#include "native_mate/dictionary.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 "ui/gfx/image/image.h"
|
||||||
|
#include "url/gurl.h"
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
|
@ -121,6 +127,33 @@ void Notification::OnShown() {
|
||||||
Emit("show");
|
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
|
// static
|
||||||
void Notification::BuildPrototype(v8::Isolate* isolate,
|
void Notification::BuildPrototype(v8::Isolate* isolate,
|
||||||
v8::Local<v8::FunctionTemplate> prototype) {
|
v8::Local<v8::FunctionTemplate> prototype) {
|
||||||
|
|
|
@ -12,6 +12,8 @@
|
||||||
#include "atom/browser/api/trackable_object.h"
|
#include "atom/browser/api/trackable_object.h"
|
||||||
#include "atom/browser/ui/notification_observer.h"
|
#include "atom/browser/ui/notification_observer.h"
|
||||||
#include "base/strings/utf_string_conversions.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 "native_mate/handle.h"
|
||||||
#include "ui/gfx/image/image.h"
|
#include "ui/gfx/image/image.h"
|
||||||
|
|
||||||
|
@ -69,6 +71,7 @@ class Notification : public mate::TrackableObject<Notification>,
|
||||||
bool silent_ = false;
|
bool silent_ = false;
|
||||||
base::string16 reply_placeholder_ = base::UTF8ToUTF16("");
|
base::string16 reply_placeholder_ = base::UTF8ToUTF16("");
|
||||||
bool has_reply_ = false;
|
bool has_reply_ = false;
|
||||||
|
brightray::NotificationPresenter* presenter_;
|
||||||
|
|
||||||
int id_;
|
int id_;
|
||||||
|
|
||||||
|
|
|
@ -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
|
|
|
@ -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
|
|
|
@ -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
|
|
|
@ -1,29 +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/ui/win/toast_handler.h"
|
|
||||||
|
|
||||||
#include "atom/browser/ui/win/toast_lib.h"
|
|
||||||
#include "atom/browser/api/atom_api_notification.h"
|
|
||||||
|
|
||||||
namespace atom {
|
|
||||||
|
|
||||||
AtomToastHandler::AtomToastHandler(atom::api::Notification* target) {
|
|
||||||
observer_ = target;
|
|
||||||
}
|
|
||||||
|
|
||||||
void AtomToastHandler::toastActivated() {
|
|
||||||
observer_->OnClicked();
|
|
||||||
}
|
|
||||||
|
|
||||||
void AtomToastHandler::toastDismissed(
|
|
||||||
WinToastLib::WinToastHandler::WinToastDismissalReason state) {
|
|
||||||
// observer_->OnDismissed();
|
|
||||||
}
|
|
||||||
|
|
||||||
void AtomToastHandler::toastFailed() {
|
|
||||||
// observer_->OnErrored();
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace atom
|
|
|
@ -1,26 +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/ui/win/toast_lib.h"
|
|
||||||
#include "atom/browser/api/atom_api_notification.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_;
|
|
||||||
explicit AtomToastHandler(atom::api::Notification* target);
|
|
||||||
|
|
||||||
void toastActivated() override;
|
|
||||||
void toastDismissed(
|
|
||||||
WinToastLib::WinToastHandler::WinToastDismissalReason state);
|
|
||||||
void toastFailed();
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace atom
|
|
||||||
|
|
||||||
#endif // ATOM_BROWSER_UI_WIN_TOAST_HANDLER_H_
|
|
|
@ -1,485 +0,0 @@
|
||||||
// MIT License
|
|
||||||
|
|
||||||
// Copyright (c) 2016 Mohammed Boujemaoui Boulaghmoudi
|
|
||||||
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
// of this software and associated documentation files (the "Software"), to deal
|
|
||||||
// in the Software without restriction, including without limitation the rights
|
|
||||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
// 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 SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
// SOFTWARE.
|
|
||||||
|
|
||||||
#include "atom/browser/ui/win/toast_lib.h"
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include "brightray/browser/win/scoped_hstring.h"
|
|
||||||
|
|
||||||
#pragma comment(lib, "shlwapi")
|
|
||||||
#pragma comment(lib, "user32")
|
|
||||||
|
|
||||||
using WinToastLib::WinToast;
|
|
||||||
using WinToastLib::WinToastHandler;
|
|
||||||
using WinToastLib::WinToastStringWrapper;
|
|
||||||
using WinToastLib::WinToastTemplate;
|
|
||||||
|
|
||||||
namespace DllImporter {
|
|
||||||
|
|
||||||
// Function load a function from library
|
|
||||||
template <typename Function>
|
|
||||||
HRESULT loadFunctionFromLibrary(HINSTANCE library,
|
|
||||||
LPCSTR name,
|
|
||||||
Function& func) { // NOLINT
|
|
||||||
if (!library)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
func = reinterpret_cast<Function>(GetProcAddress(library, name));
|
|
||||||
return (func != nullptr) ? S_OK : E_FAIL;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef HRESULT(FAR STDAPICALLTYPE* f_SetCurrentProcessExplicitAppUserModelID)(
|
|
||||||
__in PCWSTR AppID);
|
|
||||||
typedef HRESULT(FAR STDAPICALLTYPE* f_PropVariantToString)(
|
|
||||||
_In_ REFPROPVARIANT propvar,
|
|
||||||
_Out_writes_(cch) PWSTR psz,
|
|
||||||
_In_ UINT cch);
|
|
||||||
typedef HRESULT(FAR STDAPICALLTYPE* f_RoGetActivationFactory)(
|
|
||||||
_In_ HSTRING activatableClassId,
|
|
||||||
_In_ REFIID iid,
|
|
||||||
_COM_Outptr_ void** factory);
|
|
||||||
typedef HRESULT(FAR STDAPICALLTYPE* f_WindowsCreateStringReference)(
|
|
||||||
_In_reads_opt_(length + 1) PCWSTR sourceString,
|
|
||||||
UINT32 length,
|
|
||||||
_Out_ HSTRING_HEADER* hstringHeader,
|
|
||||||
_Outptr_result_maybenull_ _Result_nullonfailure_ HSTRING* string);
|
|
||||||
typedef HRESULT(FAR STDAPICALLTYPE* f_WindowsDeleteString)(
|
|
||||||
_In_opt_ HSTRING string);
|
|
||||||
|
|
||||||
f_SetCurrentProcessExplicitAppUserModelID
|
|
||||||
SetCurrentProcessExplicitAppUserModelID;
|
|
||||||
f_PropVariantToString PropVariantToString;
|
|
||||||
f_RoGetActivationFactory RoGetActivationFactory;
|
|
||||||
f_WindowsCreateStringReference WindowsCreateStringReference;
|
|
||||||
f_WindowsDeleteString WindowsDeleteString;
|
|
||||||
|
|
||||||
template <class T>
|
|
||||||
_Check_return_ __inline HRESULT _1_GetActivationFactory(
|
|
||||||
_In_ HSTRING activatableClassId,
|
|
||||||
_COM_Outptr_ T** factory) {
|
|
||||||
return RoGetActivationFactory(activatableClassId, IID_INS_ARGS(factory));
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
inline HRESULT Wrap_GetActivationFactory(
|
|
||||||
_In_ HSTRING activatableClassId,
|
|
||||||
_Inout_ ComPtrRef<T> factory) throw() {
|
|
||||||
return _1_GetActivationFactory(activatableClassId,
|
|
||||||
factory.ReleaseAndGetAddressOf());
|
|
||||||
}
|
|
||||||
|
|
||||||
inline HRESULT initialize() {
|
|
||||||
HINSTANCE LibShell32 = LoadLibrary(L"SHELL32.DLL");
|
|
||||||
HRESULT hr = loadFunctionFromLibrary(
|
|
||||||
LibShell32, "SetCurrentProcessExplicitAppUserModelID",
|
|
||||||
SetCurrentProcessExplicitAppUserModelID);
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
HINSTANCE LibPropSys = LoadLibrary(L"PROPSYS.DLL");
|
|
||||||
hr = loadFunctionFromLibrary(LibPropSys, "PropVariantToString",
|
|
||||||
PropVariantToString);
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
HINSTANCE LibComBase = LoadLibrary(L"COMBASE.DLL");
|
|
||||||
return SUCCEEDED(loadFunctionFromLibrary(LibComBase,
|
|
||||||
"RoGetActivationFactory",
|
|
||||||
RoGetActivationFactory)) &&
|
|
||||||
SUCCEEDED(loadFunctionFromLibrary(LibComBase,
|
|
||||||
"WindowsCreateStringReference",
|
|
||||||
WindowsCreateStringReference)) &&
|
|
||||||
SUCCEEDED(loadFunctionFromLibrary(
|
|
||||||
LibComBase, "WindowsDeleteString", WindowsDeleteString));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return hr;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace DllImporter
|
|
||||||
|
|
||||||
namespace Util {
|
|
||||||
inline HRESULT defaultExecutablePath(_In_ WCHAR* path,
|
|
||||||
_In_ DWORD nSize = MAX_PATH) {
|
|
||||||
DWORD written =
|
|
||||||
GetModuleFileNameEx(GetCurrentProcess(), nullptr, path, nSize);
|
|
||||||
return (written > 0) ? S_OK : E_FAIL;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline HRESULT defaultShellLinksDirectory(_In_ WCHAR* path,
|
|
||||||
_In_ DWORD nSize = MAX_PATH) {
|
|
||||||
DWORD written = GetEnvironmentVariable(L"APPDATA", path, nSize);
|
|
||||||
HRESULT hr = written > 0 ? S_OK : E_INVALIDARG;
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
errno_t result = wcscat_s(path, nSize, DEFAULT_SHELL_LINKS_PATH);
|
|
||||||
hr = (result == 0) ? S_OK : E_INVALIDARG;
|
|
||||||
}
|
|
||||||
return hr;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline HRESULT defaultShellLinkPath(const std::wstring& appname,
|
|
||||||
_In_ WCHAR* path,
|
|
||||||
_In_ DWORD nSize = MAX_PATH) {
|
|
||||||
HRESULT hr = defaultShellLinksDirectory(path, nSize);
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
const std::wstring appLink(appname + DEFAULT_LINK_FORMAT);
|
|
||||||
errno_t result = wcscat_s(path, nSize, appLink.c_str());
|
|
||||||
hr = (result == 0) ? S_OK : E_INVALIDARG;
|
|
||||||
}
|
|
||||||
return hr;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline HRESULT setNodeStringValue(const std::wstring& string,
|
|
||||||
IXmlNode* node,
|
|
||||||
IXmlDocument* xml) {
|
|
||||||
ComPtr<IXmlText> textNode;
|
|
||||||
HRESULT hr =
|
|
||||||
xml->CreateTextNode(WinToastStringWrapper(string).Get(), &textNode);
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
ComPtr<IXmlNode> stringNode;
|
|
||||||
hr = textNode.As(&stringNode);
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
ComPtr<IXmlNode> appendedChild;
|
|
||||||
hr = node->AppendChild(stringNode.Get(), &appendedChild);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return hr;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline HRESULT setEventHandlers(_In_ IToastNotification* notification,
|
|
||||||
_In_ WinToastHandler* eventHandler) {
|
|
||||||
EventRegistrationToken activatedToken, dismissedToken, failedToken;
|
|
||||||
HRESULT hr = notification->add_Activated(
|
|
||||||
Callback<
|
|
||||||
Implements<RuntimeClassFlags<ClassicCom>,
|
|
||||||
ITypedEventHandler<ToastNotification*, IInspectable*>>>(
|
|
||||||
[eventHandler](IToastNotification*, IInspectable*) {
|
|
||||||
eventHandler->toastActivated();
|
|
||||||
return S_OK;
|
|
||||||
})
|
|
||||||
.Get(),
|
|
||||||
&activatedToken);
|
|
||||||
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
hr = notification->add_Dismissed(
|
|
||||||
Callback<Implements<
|
|
||||||
RuntimeClassFlags<ClassicCom>,
|
|
||||||
ITypedEventHandler<ToastNotification*, ToastDismissedEventArgs*>>>(
|
|
||||||
[eventHandler](IToastNotification*, IToastDismissedEventArgs* e) {
|
|
||||||
ToastDismissalReason reason;
|
|
||||||
if (SUCCEEDED(e->get_Reason(&reason))) {
|
|
||||||
eventHandler->toastDismissed(
|
|
||||||
static_cast<WinToastHandler::WinToastDismissalReason>(
|
|
||||||
reason));
|
|
||||||
}
|
|
||||||
return S_OK;
|
|
||||||
})
|
|
||||||
.Get(),
|
|
||||||
&dismissedToken);
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
hr = notification->add_Failed(
|
|
||||||
Callback<Implements<
|
|
||||||
RuntimeClassFlags<ClassicCom>,
|
|
||||||
ITypedEventHandler<ToastNotification*, ToastFailedEventArgs*>>>(
|
|
||||||
[eventHandler](IToastNotification*, IToastFailedEventArgs*) {
|
|
||||||
eventHandler->toastFailed();
|
|
||||||
return S_OK;
|
|
||||||
})
|
|
||||||
.Get(),
|
|
||||||
&failedToken);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return hr;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace Util
|
|
||||||
|
|
||||||
WinToast* WinToast::_instance = nullptr;
|
|
||||||
WinToast* WinToast::instance() {
|
|
||||||
if (_instance == nullptr) {
|
|
||||||
_instance = new WinToast();
|
|
||||||
}
|
|
||||||
return _instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
WinToast::WinToast() : _isInitialized(false) {
|
|
||||||
DllImporter::initialize();
|
|
||||||
}
|
|
||||||
|
|
||||||
void WinToast::setAppName(_In_ const std::wstring& appName) {
|
|
||||||
_appName = appName;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::wstring WinToast::appName() const {
|
|
||||||
return _appName;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::wstring WinToast::appUserModelId() const {
|
|
||||||
return _aumi;
|
|
||||||
}
|
|
||||||
void WinToast::setAppUserModelId(_In_ const std::wstring& aumi) {
|
|
||||||
_aumi = aumi;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool WinToast::isCompatible() {
|
|
||||||
return !((DllImporter::SetCurrentProcessExplicitAppUserModelID == nullptr) ||
|
|
||||||
(DllImporter::PropVariantToString == nullptr) ||
|
|
||||||
(DllImporter::RoGetActivationFactory == nullptr) ||
|
|
||||||
(DllImporter::WindowsCreateStringReference == nullptr) ||
|
|
||||||
(DllImporter::WindowsDeleteString == nullptr));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool WinToast::initialize() {
|
|
||||||
if (_aumi.empty() || _appName.empty()) {
|
|
||||||
return _isInitialized = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isCompatible()) {
|
|
||||||
return _isInitialized = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (FAILED(DllImporter::SetCurrentProcessExplicitAppUserModelID(
|
|
||||||
_aumi.c_str()))) {
|
|
||||||
return _isInitialized = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
HRESULT hr = DllImporter::Wrap_GetActivationFactory(
|
|
||||||
WinToastStringWrapper(
|
|
||||||
RuntimeClass_Windows_UI_Notifications_ToastNotificationManager)
|
|
||||||
.Get(),
|
|
||||||
&_notificationManager);
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
hr = notificationManager()->CreateToastNotifierWithId(
|
|
||||||
WinToastStringWrapper(_aumi).Get(), &_notifier);
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
hr = DllImporter::Wrap_GetActivationFactory(
|
|
||||||
WinToastStringWrapper(
|
|
||||||
RuntimeClass_Windows_UI_Notifications_ToastNotification)
|
|
||||||
.Get(),
|
|
||||||
&_notificationFactory);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return _isInitialized = SUCCEEDED(hr);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool WinToast::showToast(_In_ const WinToastTemplate& toast,
|
|
||||||
_In_ WinToastHandler* handler) {
|
|
||||||
if (!isInitialized()) {
|
|
||||||
return _isInitialized;
|
|
||||||
}
|
|
||||||
|
|
||||||
HRESULT hr = _notificationManager->GetTemplateContent(
|
|
||||||
ToastTemplateType(toast.type()), &_xmlDocument);
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
const int fieldsCount = toast.textFieldsCount();
|
|
||||||
for (int i = 0; i < fieldsCount && SUCCEEDED(hr); i++) {
|
|
||||||
hr = setTextField(toast.textField(WinToastTemplate::TextField(i)), i);
|
|
||||||
}
|
|
||||||
if (makeSilent(toast.isSilent())) {
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
hr = toast.hasImage() ? setImageField(toast.imagePath()) : hr;
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
hr = _notificationFactory->CreateToastNotification(xmlDocument(),
|
|
||||||
&_notification);
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
hr = Util::setEventHandlers(notification(), handler);
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
hr = _notifier->Show(notification());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return SUCCEEDED(hr);
|
|
||||||
}
|
|
||||||
|
|
||||||
HRESULT WinToast::setTextField(_In_ const std::wstring& text, _In_ int pos) {
|
|
||||||
ComPtr<IXmlNodeList> nodeList;
|
|
||||||
HRESULT hr = _xmlDocument->GetElementsByTagName(
|
|
||||||
WinToastStringWrapper(L"text").Get(), &nodeList);
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
ComPtr<IXmlNode> node;
|
|
||||||
hr = nodeList->Item(pos, &node);
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
hr = Util::setNodeStringValue(text, node.Get(), xmlDocument());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return hr;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool WinToast::makeSilent(bool is_silent) {
|
|
||||||
if (!is_silent)
|
|
||||||
return true;
|
|
||||||
ScopedHString tag(L"toast");
|
|
||||||
if (!tag.success())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
ComPtr<IXmlNodeList> node_list;
|
|
||||||
if (FAILED(xmlDocument()->GetElementsByTagName(tag, &node_list)))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
ComPtr<IXmlNode> root;
|
|
||||||
if (FAILED(node_list->Item(0, &root)))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
ComPtr<IXmlElement> audio_element;
|
|
||||||
ScopedHString audio_str(L"audio");
|
|
||||||
if (FAILED(xmlDocument()->CreateElement(audio_str, &audio_element)))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
ComPtr<IXmlNode> audio_node_tmp;
|
|
||||||
if (FAILED(audio_element.As(&audio_node_tmp)))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// Append audio node to toast xml
|
|
||||||
ComPtr<IXmlNode> audio_node;
|
|
||||||
if (FAILED(root->AppendChild(audio_node_tmp.Get(), &audio_node)))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// Create silent attribute
|
|
||||||
ComPtr<IXmlNamedNodeMap> attributes;
|
|
||||||
if (FAILED(audio_node->get_Attributes(&attributes)))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
ComPtr<IXmlAttribute> silent_attribute;
|
|
||||||
ScopedHString silent_str(L"silent");
|
|
||||||
if (FAILED(xmlDocument()->CreateAttribute(silent_str, &silent_attribute)))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
ComPtr<IXmlNode> silent_attribute_node;
|
|
||||||
if (FAILED(silent_attribute.As(&silent_attribute_node)))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// Set silent attribute to true
|
|
||||||
ScopedHString silent_value(L"true");
|
|
||||||
if (!silent_value.success())
|
|
||||||
return false;
|
|
||||||
|
|
||||||
ComPtr<IXmlText> silent_text;
|
|
||||||
if (FAILED(xmlDocument()->CreateTextNode(silent_value, &silent_text)))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
ComPtr<IXmlNode> silent_node;
|
|
||||||
if (FAILED(silent_text.As(&silent_node)))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
ComPtr<IXmlNode> child_node;
|
|
||||||
if (FAILED(
|
|
||||||
silent_attribute_node->AppendChild(silent_node.Get(), &child_node)))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
ComPtr<IXmlNode> silent_attribute_pnode;
|
|
||||||
return SUCCEEDED(attributes.Get()->SetNamedItem(silent_attribute_node.Get(),
|
|
||||||
&silent_attribute_pnode));
|
|
||||||
}
|
|
||||||
|
|
||||||
HRESULT WinToast::setImageField(_In_ const std::wstring& path) {
|
|
||||||
wchar_t imagePath[MAX_PATH] = L"file:///";
|
|
||||||
HRESULT hr = StringCchCat(imagePath, MAX_PATH, path.c_str());
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
ComPtr<IXmlNodeList> nodeList;
|
|
||||||
hr = _xmlDocument->GetElementsByTagName(
|
|
||||||
WinToastStringWrapper(L"image").Get(), &nodeList);
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
ComPtr<IXmlNode> node;
|
|
||||||
hr = nodeList->Item(0, &node);
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
ComPtr<IXmlNamedNodeMap> attributes;
|
|
||||||
hr = node->get_Attributes(&attributes);
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
ComPtr<IXmlNode> editedNode;
|
|
||||||
hr = attributes->GetNamedItem(WinToastStringWrapper(L"src").Get(),
|
|
||||||
&editedNode);
|
|
||||||
if (SUCCEEDED(hr)) {
|
|
||||||
Util::setNodeStringValue(imagePath, editedNode.Get(),
|
|
||||||
xmlDocument());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return hr;
|
|
||||||
}
|
|
||||||
|
|
||||||
WinToastTemplate::WinToastTemplate(const WinToastTemplateType& type)
|
|
||||||
: _type(type) {
|
|
||||||
initComponentsFromType();
|
|
||||||
}
|
|
||||||
|
|
||||||
WinToastTemplate::~WinToastTemplate() {
|
|
||||||
_textFields.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
void WinToastTemplate::setTextField(
|
|
||||||
_In_ const std::wstring& txt,
|
|
||||||
_In_ const WinToastTemplate::TextField& pos) {
|
|
||||||
_textFields[pos] = txt;
|
|
||||||
}
|
|
||||||
void WinToastTemplate::setImagePath(_In_ const std::wstring& imgPath) {
|
|
||||||
if (!_hasImage)
|
|
||||||
return;
|
|
||||||
_imagePath = imgPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
void WinToastTemplate::setSilent(bool is_silent) {
|
|
||||||
_silent = is_silent;
|
|
||||||
}
|
|
||||||
|
|
||||||
int WinToastTemplate::TextFieldsCount[WinToastTemplateTypeCount] = {1, 2, 2, 3,
|
|
||||||
1, 2, 2, 3};
|
|
||||||
void WinToastTemplate::initComponentsFromType() {
|
|
||||||
_hasImage = _type < ToastTemplateType_ToastText01;
|
|
||||||
_textFields = std::vector<std::wstring>(TextFieldsCount[_type], L"");
|
|
||||||
}
|
|
||||||
|
|
||||||
WinToastStringWrapper::WinToastStringWrapper(_In_reads_(length)
|
|
||||||
PCWSTR stringRef,
|
|
||||||
_In_ UINT32 length) throw() {
|
|
||||||
HRESULT hr = DllImporter::WindowsCreateStringReference(stringRef, length,
|
|
||||||
&_header, &_hstring);
|
|
||||||
if (!SUCCEEDED(hr)) {
|
|
||||||
RaiseException(static_cast<DWORD>(STATUS_INVALID_PARAMETER),
|
|
||||||
EXCEPTION_NONCONTINUABLE, 0, nullptr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
WinToastStringWrapper::WinToastStringWrapper(const std::wstring& stringRef) {
|
|
||||||
HRESULT hr = DllImporter::WindowsCreateStringReference(
|
|
||||||
stringRef.c_str(), static_cast<UINT32>(stringRef.length()), &_header,
|
|
||||||
&_hstring);
|
|
||||||
if (FAILED(hr)) {
|
|
||||||
RaiseException(static_cast<DWORD>(STATUS_INVALID_PARAMETER),
|
|
||||||
EXCEPTION_NONCONTINUABLE, 0, nullptr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
WinToastStringWrapper::~WinToastStringWrapper() {
|
|
||||||
DllImporter::WindowsDeleteString(_hstring);
|
|
||||||
}
|
|
||||||
|
|
||||||
HSTRING WinToastStringWrapper::Get() const {
|
|
||||||
return _hstring;
|
|
||||||
}
|
|
|
@ -1,192 +0,0 @@
|
||||||
// MIT License
|
|
||||||
|
|
||||||
// Copyright (c) 2016 Mohammed Boujemaoui Boulaghmoudi
|
|
||||||
|
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
// of this software and associated documentation files (the "Software"), to deal
|
|
||||||
// in the Software without restriction, including without limitation the rights
|
|
||||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
// 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 SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
||||||
// SOFTWARE.
|
|
||||||
|
|
||||||
#ifndef ATOM_BROWSER_UI_WIN_TOAST_LIB_H_
|
|
||||||
#define ATOM_BROWSER_UI_WIN_TOAST_LIB_H_
|
|
||||||
|
|
||||||
#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();
|
|
||||||
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
|
|
||||||
};
|
|
||||||
|
|
||||||
virtual void toastActivated() {}
|
|
||||||
virtual void toastDismissed(WinToastDismissalReason state) {}
|
|
||||||
virtual void toastFailed() {}
|
|
||||||
};
|
|
||||||
|
|
||||||
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
|
|
||||||
};
|
|
||||||
|
|
||||||
explicit 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; }
|
|
||||||
|
|
||||||
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();
|
|
||||||
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;
|
|
||||||
|
|
||||||
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 setImageField(_In_ const std::wstring& path);
|
|
||||||
HRESULT setTextField(_In_ const std::wstring& text, _In_ int pos);
|
|
||||||
bool makeSilent(bool is_silent);
|
|
||||||
};
|
|
||||||
} // namespace WinToastLib
|
|
||||||
|
|
||||||
#endif // ATOM_BROWSER_UI_WIN_TOAST_LIB_H_
|
|
|
@ -33,8 +33,9 @@ exports.load = (appUrl) => {
|
||||||
body: 'Bar',
|
body: 'Bar',
|
||||||
hasReply: true,
|
hasReply: true,
|
||||||
replyPlaceholder: 'foo'
|
replyPlaceholder: 'foo'
|
||||||
});
|
})
|
||||||
n.on('reply', (...args) => console.log(args));
|
n.on('reply', (...args) => console.log('reply', ...args))
|
||||||
n.show();
|
n.on('click', (...args) => console.log('click', ...args))
|
||||||
|
n.show()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -130,9 +130,6 @@
|
||||||
'atom/browser/api/atom_api_net.h',
|
'atom/browser/api/atom_api_net.h',
|
||||||
'atom/browser/api/atom_api_notification.cc',
|
'atom/browser/api/atom_api_notification.cc',
|
||||||
'atom/browser/api/atom_api_notification.h',
|
'atom/browser/api/atom_api_notification.h',
|
||||||
'atom/browser/api/atom_api_notification_mac.mm',
|
|
||||||
'atom/browser/api/atom_api_notification_win.cc',
|
|
||||||
'atom/browser/api/atom_api_notification_linux.cc',
|
|
||||||
'atom/browser/api/atom_api_power_monitor.cc',
|
'atom/browser/api/atom_api_power_monitor.cc',
|
||||||
'atom/browser/api/atom_api_power_monitor.h',
|
'atom/browser/api/atom_api_power_monitor.h',
|
||||||
'atom/browser/api/atom_api_power_save_blocker.cc',
|
'atom/browser/api/atom_api_power_save_blocker.cc',
|
||||||
|
@ -361,10 +358,6 @@
|
||||||
'atom/browser/ui/win/notify_icon.h',
|
'atom/browser/ui/win/notify_icon.h',
|
||||||
'atom/browser/ui/win/taskbar_host.cc',
|
'atom/browser/ui/win/taskbar_host.cc',
|
||||||
'atom/browser/ui/win/taskbar_host.h',
|
'atom/browser/ui/win/taskbar_host.h',
|
||||||
'atom/browser/ui/win/toast_handler.cc',
|
|
||||||
'atom/browser/ui/win/toast_handler.h',
|
|
||||||
'atom/browser/ui/win/toast_lib.cc',
|
|
||||||
'atom/browser/ui/win/toast_lib.h',
|
|
||||||
'atom/browser/ui/x/event_disabler.cc',
|
'atom/browser/ui/x/event_disabler.cc',
|
||||||
'atom/browser/ui/x/event_disabler.h',
|
'atom/browser/ui/x/event_disabler.h',
|
||||||
'atom/browser/ui/x/window_state_watcher.cc',
|
'atom/browser/ui/x/window_state_watcher.cc',
|
||||||
|
|
Loading…
Reference in a new issue