Commit 16f6e742 authored by Avi Drissman's avatar Avi Drissman Committed by Chromium LUCI CQ

Don't cache the New tag image

The New tag image's colors depend on whether the Mac is in
light or dark mode. Don't cache the image, so that if the
user switches modes, it will draw correctly.

Fixed: 1154805
Change-Id: Ia28e6177a2691b261c6c8bcd8c7bf33239cbbb5a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2568686
Commit-Queue: Elly Fong-Jones <ellyjones@chromium.org>
Auto-Submit: Avi Drissman <avi@chromium.org>
Reviewed-by: default avatarElly Fong-Jones <ellyjones@chromium.org>
Cr-Commit-Position: refs/heads/master@{#833030}
parent b99f1ec1
...@@ -27,85 +27,80 @@ ...@@ -27,85 +27,80 @@
namespace { namespace {
NSImage* NewTagImage() { NSImage* NewTagImage() {
static NSImage* new_tag = []() { // 1. Make the attributed string.
// 1. Make the attributed string.
NSString* badge_text = l10n_util::GetNSString(IDS_NEW_BADGE);
NSString* badge_text = l10n_util::GetNSString(IDS_NEW_BADGE);
// The preferred font is slightly smaller and slightly more bold than the
// The preferred font is slightly smaller and slightly more bold than the // menu font. The size change is required to make it look correct in the
// menu font. The size change is required to make it look correct in the // badge; we add a small degree of bold to prevent color smearing/blurring
// badge; we add a small degree of bold to prevent color smearing/blurring // due to font smoothing. This ensures readability on all platforms and in
// due to font smoothing. This ensures readability on all platforms and in // both light and dark modes.
// both light and dark modes. gfx::Font badge_font = gfx::Font(
gfx::Font badge_font = gfx::Font( new gfx::PlatformFontMac(gfx::PlatformFontMac::SystemFontType::kMenu));
new gfx::PlatformFontMac(gfx::PlatformFontMac::SystemFontType::kMenu)); badge_font = badge_font.Derive(views::NewBadge::kNewBadgeFontSizeAdjustment,
badge_font = gfx::Font::NORMAL, gfx::Font::Weight::MEDIUM);
badge_font.Derive(views::NewBadge::kNewBadgeFontSizeAdjustment,
gfx::Font::NORMAL, gfx::Font::Weight::MEDIUM); NSColor* badge_text_color = skia::SkColorToSRGBNSColor(
ui::NativeTheme::GetInstanceForNativeUi()->GetSystemColor(
NSColor* badge_text_color = skia::SkColorToSRGBNSColor( ui::NativeTheme::kColorId_TextOnProminentButtonColor));
ui::NativeTheme::GetInstanceForNativeUi()->GetSystemColor(
ui::NativeTheme::kColorId_TextOnProminentButtonColor)); NSDictionary* badge_attrs = @{
NSFontAttributeName : badge_font.GetNativeFont(),
NSDictionary* badge_attrs = @{ NSForegroundColorAttributeName : badge_text_color,
NSFontAttributeName : badge_font.GetNativeFont(), };
NSForegroundColorAttributeName : badge_text_color,
}; NSMutableAttributedString* badge_attr_string =
[[NSMutableAttributedString alloc] initWithString:badge_text
NSMutableAttributedString* badge_attr_string = attributes:badge_attrs];
[[NSMutableAttributedString alloc] initWithString:badge_text
attributes:badge_attrs]; if (base::mac::IsOS10_10()) {
// The system font for 10.10 is Helvetica Neue, and when used for this
if (base::mac::IsOS10_10()) { // "new tag" the letters look cramped. Track it out so that there's some
// The system font for 10.10 is Helvetica Neue, and when used for this // breathing room. There is no tracking attribute, so instead add kerning
// "new tag" the letters look cramped. Track it out so that there's some // to all but the last character.
// breathing room. There is no tracking attribute, so instead add kerning [badge_attr_string
// to all but the last character. addAttribute:NSKernAttributeName
[badge_attr_string value:@0.4
addAttribute:NSKernAttributeName range:NSMakeRange(0, [badge_attr_string length] - 1)];
value:@0.4 }
range:NSMakeRange(0, [badge_attr_string length] - 1)];
}
// 2. Calculate the size required. // 2. Calculate the size required.
NSSize badge_size = [badge_attr_string size]; NSSize badge_size = [badge_attr_string size];
badge_size.width = trunc(badge_size.width); badge_size.width = trunc(badge_size.width);
badge_size.height = trunc(badge_size.height); badge_size.height = trunc(badge_size.height);
badge_size.width += 2 * views::NewBadge::kNewBadgeInternalPadding + badge_size.width += 2 * views::NewBadge::kNewBadgeInternalPadding +
2 * views::NewBadge::kNewBadgeHorizontalMargin; 2 * views::NewBadge::kNewBadgeHorizontalMargin;
badge_size.height += views::NewBadge::kNewBadgeInternalPaddingTopMac; badge_size.height += views::NewBadge::kNewBadgeInternalPaddingTopMac;
// 3. Craft the image. // 3. Craft the image.
return [[NSImage return [NSImage
imageWithSize:badge_size imageWithSize:badge_size
flipped:NO flipped:NO
drawingHandler:^(NSRect dest_rect) { drawingHandler:^(NSRect dest_rect) {
NSRect badge_frame = NSInsetRect( NSRect badge_frame = NSInsetRect(
dest_rect, views::NewBadge::kNewBadgeHorizontalMargin, 0); dest_rect, views::NewBadge::kNewBadgeHorizontalMargin, 0);
NSBezierPath* rounded_badge_rect = [NSBezierPath NSBezierPath* rounded_badge_rect = [NSBezierPath
bezierPathWithRoundedRect:badge_frame bezierPathWithRoundedRect:badge_frame
xRadius:views::NewBadge::kNewBadgeCornerRadius xRadius:views::NewBadge::kNewBadgeCornerRadius
yRadius:views::NewBadge::kNewBadgeCornerRadius]; yRadius:views::NewBadge::kNewBadgeCornerRadius];
NSColor* badge_color = skia::SkColorToSRGBNSColor( NSColor* badge_color = skia::SkColorToSRGBNSColor(
ui::NativeTheme::GetInstanceForNativeUi()->GetSystemColor( ui::NativeTheme::GetInstanceForNativeUi()->GetSystemColor(
ui::NativeTheme::kColorId_ProminentButtonColor)); ui::NativeTheme::kColorId_ProminentButtonColor));
[badge_color set]; [badge_color set];
[rounded_badge_rect fill]; [rounded_badge_rect fill];
NSPoint badge_text_location = NSMakePoint( NSPoint badge_text_location = NSMakePoint(
NSMinX(badge_frame) + views::NewBadge::kNewBadgeInternalPadding, NSMinX(badge_frame) + views::NewBadge::kNewBadgeInternalPadding,
NSMinY(badge_frame) + NSMinY(badge_frame) +
views::NewBadge::kNewBadgeInternalPaddingTopMac); views::NewBadge::kNewBadgeInternalPaddingTopMac);
[badge_attr_string drawAtPoint:badge_text_location]; [badge_attr_string drawAtPoint:badge_text_location];
return YES; return YES;
}] retain]; }];
}();
return new_tag;
} }
} // namespace } // namespace
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment