Skip to content

Commit de26cd8

Browse files
committed
Merge pull request nwjs#1957 from jtg-gg/badge
SetBadgeLabel Windows Implementation
2 parents 0555ea5 + 1213d73 commit de26cd8

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ Fabrice Weinberg <fabrice@weinberg.me>
2525
Lv Kaiyang <kevinlui598@gmail.com>
2626
Lukas Benes <bender@false.cz>
2727
Lithare Emileit <litharemilit@gmail.com>
28+
Jefry Tedjokusumo <jtg_gg@yahoo.com.sg>

src/browser/native_window_win.cc

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
#include "ui/gfx/native_widget_types.h"
4444
#include "ui/gfx/win/hwnd_util.h"
4545
#include "ui/gfx/path.h"
46+
#include "ui/gfx/canvas.h"
47+
#include "ui/gfx/icon_util.h"
48+
#include "ui/gfx/font_list.h"
49+
#include "ui/gfx/platform_font.h"
4650
#include "ui/gfx/image/image_skia_operations.h"
4751
#include "ui/views/controls/webview/webview.h"
4852
#include "ui/views/layout/box_layout.h"
@@ -470,8 +474,51 @@ void NativeWindowWin::FlashFrame(bool flash) {
470474
window_->FlashFrame(flash);
471475
}
472476

477+
HICON createBadgeIcon(const HWND hWnd, const TCHAR *value, const int sizeX, const int sizeY) {
478+
// canvas for the overlay icon
479+
gfx::Canvas canvas(gfx::Size(sizeX, sizeY), 1, false);
480+
481+
// drawing red circle
482+
SkPaint paint;
483+
paint.setColor(SK_ColorRED);
484+
canvas.DrawCircle(gfx::Point(sizeX / 2, sizeY / 2), sizeX / 2, paint);
485+
486+
// drawing the text
487+
gfx::PlatformFont *platform_font = gfx::PlatformFont::CreateDefault();
488+
const int fontSize = sizeY*0.65f;
489+
gfx::Font font(platform_font->GetFontName(), fontSize);
490+
platform_font->Release();
491+
platform_font = NULL;
492+
const int yMargin = (sizeY - fontSize) / 2;
493+
canvas.DrawStringRectWithFlags(value, gfx::FontList(font), SK_ColorWHITE, gfx::Rect(sizeX, fontSize + yMargin + 1), gfx::Canvas::TEXT_ALIGN_CENTER);
494+
495+
// return the canvas as windows native icon handle
496+
return IconUtil::CreateHICONFromSkBitmap(canvas.ExtractImageRep().sk_bitmap());
497+
}
498+
473499
void NativeWindowWin::SetBadgeLabel(const std::string& badge) {
474-
// TODO
500+
base::win::ScopedComPtr<ITaskbarList3> taskbar;
501+
HRESULT result = taskbar.CreateInstance(CLSID_TaskbarList, NULL,
502+
CLSCTX_INPROC_SERVER);
503+
504+
if (FAILED(result)) {
505+
VLOG(1) << "Failed creating a TaskbarList3 object: " << result;
506+
return;
507+
}
508+
509+
result = taskbar->HrInit();
510+
if (FAILED(result)) {
511+
LOG(ERROR) << "Failed initializing an ITaskbarList3 interface.";
512+
return;
513+
}
514+
515+
HICON icon = NULL;
516+
HWND hWnd = window_->GetNativeWindow();
517+
if (badge.size())
518+
icon = createBadgeIcon(hWnd, UTF8ToUTF16(badge).c_str(), 32, 32);
519+
520+
taskbar->SetOverlayIcon(hWnd, icon, _T("Status"));
521+
DestroyIcon(icon);
475522
}
476523

477524
void NativeWindowWin::SetKiosk(bool kiosk) {

0 commit comments

Comments
 (0)