Skip to content

Commit

Permalink
fix: missing badge text on Windows (#41629)
Browse files Browse the repository at this point in the history
https://chromium-review.googlesource.com/c/chromium/src/+/5053607

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
  • Loading branch information
trop[bot] and codebytere committed Mar 19, 2024
1 parent a47b724 commit 417e16c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
3 changes: 2 additions & 1 deletion shell/browser/browser_win.cc
Expand Up @@ -40,6 +40,7 @@
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/skia_util.h"
#include "shell/common/thread_restrictions.h"
#include "skia/ext/font_utils.h"
#include "skia/ext/legacy_display_globals.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkFont.h"
Expand Down Expand Up @@ -591,7 +592,7 @@ void Browser::UpdateBadgeContents(
paint.reset();
paint.setColor(kForegroundColor);

SkFont font;
SkFont font = skia::DefaultFont();

SkRect bounds;
int text_size = kMaxTextSize;
Expand Down
50 changes: 48 additions & 2 deletions shell/browser/ui/win/taskbar_host.cc
Expand Up @@ -11,7 +11,10 @@
#include "base/strings/utf_string_conversions.h"
#include "base/win/scoped_gdi_object.h"
#include "shell/browser/native_window.h"
#include "skia/ext/image_operations.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkCanvas.h"
#include "third_party/skia/include/core/SkRRect.h"
#include "ui/display/win/screen_win.h"
#include "ui/gfx/icon_util.h"

Expand Down Expand Up @@ -166,13 +169,56 @@ bool TaskbarHost::SetProgressBar(HWND window,
return success;
}

// Adapted from SetOverlayIcon in
// chrome/browser/taskbar/taskbar_decorator_win.cc.
bool TaskbarHost::SetOverlayIcon(HWND window,
const SkBitmap& overlay,
const SkBitmap& bitmap,
const std::string& text) {
if (!InitializeTaskbar())
return false;

base::win::ScopedHICON icon(IconUtil::CreateHICONFromSkBitmap(overlay));
base::win::ScopedGDIObject<HICON> icon;
if (!bitmap.isNull()) {
DCHECK_GE(bitmap.width(), bitmap.height());

constexpr int kOverlayIconSize = 16;

// Maintain aspect ratio on resize, but prefer more square.
// (We used to round down here, but rounding up produces nicer results.)
const int resized_height =
base::ClampCeil(kOverlayIconSize *
(static_cast<float>(bitmap.height()) / bitmap.width()));

DCHECK_GE(kOverlayIconSize, resized_height);
// Since the target size is so small, we use our best resizer.
SkBitmap sk_icon = skia::ImageOperations::Resize(
bitmap, skia::ImageOperations::RESIZE_LANCZOS3, kOverlayIconSize,
resized_height);

// Paint the resized icon onto a 16x16 canvas otherwise Windows will badly
// hammer it to 16x16. We'll use a circular clip to be consistent with the
// way profile icons are rendered in the profile switcher.
SkBitmap offscreen_bitmap;
offscreen_bitmap.allocN32Pixels(kOverlayIconSize, kOverlayIconSize);
SkCanvas offscreen_canvas(offscreen_bitmap, SkSurfaceProps{});
offscreen_canvas.clear(SK_ColorTRANSPARENT);

static const SkRRect overlay_icon_clip =
SkRRect::MakeOval(SkRect::MakeWH(kOverlayIconSize, kOverlayIconSize));
offscreen_canvas.clipRRect(overlay_icon_clip, true);

// Note: the original code used kOverlayIconSize - resized_height, but in
// order to center the icon in the circle clip area, we're going to center
// it in the paintable region instead, rounding up to the closest pixel to
// avoid smearing.
const int y_offset = std::ceilf((kOverlayIconSize - resized_height) / 2.0f);
offscreen_canvas.drawImage(sk_icon.asImage(), 0, y_offset);

icon = IconUtil::CreateHICONFromSkBitmap(offscreen_bitmap);
if (!icon.is_valid())
return false;
}

return SUCCEEDED(taskbar_->SetOverlayIcon(window, icon.get(),
base::UTF8ToWide(text).c_str()));
}
Expand Down
2 changes: 1 addition & 1 deletion shell/browser/ui/win/taskbar_host.h
Expand Up @@ -52,7 +52,7 @@ class TaskbarHost {

// Set the overlay icon in taskbar.
bool SetOverlayIcon(HWND window,
const SkBitmap& overlay,
const SkBitmap& bitmap,
const std::string& text);

// Set the region of the window to show as a thumbnail in taskbar.
Expand Down

0 comments on commit 417e16c

Please sign in to comment.