electron/shell/browser/notifications/win/win32_notification.cc

75 lines
2.1 KiB
C++
Raw Normal View History

// Copyright (c) 2015 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include "shell/browser/notifications/win/win32_notification.h"
#include <windows.h>
#include <string>
#include <utility>
#include <vector>
#include "base/strings/utf_string_conversions.h"
#include "third_party/skia/include/core/SkBitmap.h"
namespace electron {
2017-06-24 11:03:27 +00:00
void Win32Notification::Show(const NotificationOptions& options) {
auto* presenter = static_cast<NotificationPresenterWin7*>(this->presenter());
2018-04-18 01:56:12 +00:00
if (!presenter)
return;
HBITMAP image = NULL;
if (!options.icon.drawsNothing()) {
if (options.icon.colorType() == kBGRA_8888_SkColorType) {
BITMAPINFOHEADER bmi = {sizeof(BITMAPINFOHEADER)};
bmi.biWidth = options.icon.width();
bmi.biHeight = -options.icon.height();
bmi.biPlanes = 1;
bmi.biBitCount = 32;
bmi.biCompression = BI_RGB;
HDC hdcScreen = GetDC(NULL);
image =
CreateDIBitmap(hdcScreen, &bmi, CBM_INIT, options.icon.getPixels(),
reinterpret_cast<BITMAPINFO*>(&bmi), DIB_RGB_COLORS);
ReleaseDC(NULL, hdcScreen);
}
2018-04-18 01:56:12 +00:00
}
2018-04-18 01:56:12 +00:00
Win32Notification* existing = nullptr;
if (!options.tag.empty())
existing = presenter->GetNotificationObjectByTag(options.tag);
if (existing) {
existing->tag_.clear();
2018-04-18 01:56:12 +00:00
this->notification_ref_ = std::move(existing->notification_ref_);
this->notification_ref_.Set(base::UTF16ToWide(options.title),
base::UTF16ToWide(options.msg), image);
// Need to remove the entry in the notifications set that
// NotificationPresenter is holding
existing->Destroy();
2018-04-18 01:56:12 +00:00
} else {
this->notification_ref_ =
presenter->AddNotification(base::UTF16ToWide(options.title),
base::UTF16ToWide(options.msg), image);
2018-04-18 01:56:12 +00:00
}
2018-04-18 01:56:12 +00:00
this->tag_ = options.tag;
2018-04-18 01:56:12 +00:00
if (image)
DeleteObject(image);
}
void Win32Notification::Dismiss() {
2018-04-18 01:56:12 +00:00
notification_ref_.Close();
}
} // namespace electron