diff options
author | atwilson@chromium.org <atwilson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-23 19:20:08 +0000 |
---|---|---|
committer | atwilson@chromium.org <atwilson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-23 19:20:08 +0000 |
commit | ead68fa16bb57be280d19b30e6b592fd8f6192a0 (patch) | |
tree | b07766252903cb7bffde4d96c232edce56da6471 | |
parent | 722ec18ab0d520ff1b029bb961a92618c39f9f2d (diff) | |
download | chromium_src-ead68fa16bb57be280d19b30e6b592fd8f6192a0.zip chromium_src-ead68fa16bb57be280d19b30e6b592fd8f6192a0.tar.gz chromium_src-ead68fa16bb57be280d19b30e6b592fd8f6192a0.tar.bz2 |
Add dynamic badge listing the # background pages on the wrench menu.
Add badge_util::DrawBadgeIconOverlay() which draws a dynamic text overlay over
a badge icon.
Add Windows support for rendering a dynamic icon showing the # background pages
to the wrench menu.
BUG=64144
TEST=Enable background page, look at wrench menu
Review URL: http://codereview.chromium.org/5299001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@67130 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/app/generated_resources.grd | 3 | ||||
-rw-r--r-- | chrome/browser/ui/views/toolbar_view.cc | 27 | ||||
-rw-r--r-- | chrome/browser/wrench_menu_model.cc | 37 | ||||
-rw-r--r-- | chrome/common/badge_util.cc | 48 | ||||
-rw-r--r-- | chrome/common/badge_util.h | 11 |
5 files changed, 112 insertions, 14 deletions
diff --git a/chrome/app/generated_resources.grd b/chrome/app/generated_resources.grd index 74f3d62..bd9659d 100644 --- a/chrome/app/generated_resources.grd +++ b/chrome/app/generated_resources.grd @@ -1081,6 +1081,9 @@ each locale. --> E&xit </message> </if> + <message name="IDS_BACKGROUND_PAGE_BADGE_OVERFLOW" desc="The character displayed in the background page badge, if the count of background pages is too large to fit in the badge. Should be only a single character to ensure it will always fit in the badge."> + ! + </message> <if expr="pp_ifdef('chromeos')"> <message name="IDS_SIGN_OUT" desc="The text label of the Sign Out menu item"> Sign out diff --git a/chrome/browser/ui/views/toolbar_view.cc b/chrome/browser/ui/views/toolbar_view.cc index 76e08b5..a692dff 100644 --- a/chrome/browser/ui/views/toolbar_view.cc +++ b/chrome/browser/ui/views/toolbar_view.cc @@ -6,6 +6,7 @@ #include "app/l10n_util.h" #include "app/resource_bundle.h" +#include "base/i18n/number_formatting.h" #include "chrome/app/chrome_command_ids.h" #include "chrome/browser/accessibility/browser_accessibility_state.h" #include "chrome/browser/background_page_tracker.h" @@ -20,6 +21,7 @@ #include "chrome/browser/views/event_utils.h" #include "chrome/browser/views/frame/browser_view.h" #include "chrome/browser/wrench_menu_model.h" +#include "chrome/common/badge_util.h" #include "chrome/common/notification_service.h" #include "chrome/common/pref_names.h" #include "gfx/canvas.h" @@ -70,6 +72,15 @@ static const int kPopupTopSpacingNonGlass = 3; static const int kPopupBottomSpacingNonGlass = 2; static const int kPopupBottomSpacingGlass = 1; +// The size of the font to use in the text overlay for the background page +// badge. +static const float kBadgeTextFontSize = 9.0; + +// Margins for the wrench menu badges (badge is placed in the upper right +// corner of the wrench menu with the specified margins). +static const int kBadgeRightMargin = 2; +static const int kBadgeTopMargin = 2; + static SkBitmap* kPopupBackgroundEdge = NULL; //////////////////////////////////////////////////////////////////////////////// @@ -726,7 +737,8 @@ SkBitmap ToolbarView::GetAppMenuIcon(views::CustomButton::ButtonState state) { SkBitmap badge; // Only one badge can be active at any given time. The Upgrade notification - // is deemed most important, then the DLL conflict badge. + // is deemed most important, then the temporary background page badge, + // then the DLL conflict badge. if (IsUpgradeRecommended()) { badge = *tp->GetBitmapNamed(IDR_UPDATE_BADGE); } else if (ShouldShowBackgroundPageBadge()) { @@ -741,8 +753,6 @@ SkBitmap ToolbarView::GetAppMenuIcon(views::CustomButton::ButtonState state) { NOTREACHED(); } - static const int kBadgeRightMargin = 2; - static const int kBadgeTopMargin = 2; canvas->DrawBitmapInt(badge, icon.width() - badge.width() - kBadgeRightMargin, kBadgeTopMargin); @@ -752,6 +762,13 @@ SkBitmap ToolbarView::GetAppMenuIcon(views::CustomButton::ButtonState state) { SkBitmap ToolbarView::GetBackgroundPageBadge() { ThemeProvider* tp = GetThemeProvider(); - // TODO(atwilson): Add code to display current number of pages in badge. - return *tp->GetBitmapNamed(IDR_BACKGROUND_BADGE); + SkBitmap* badge = tp->GetBitmapNamed(IDR_BACKGROUND_BADGE); + string16 badge_text = base::FormatNumber( + BackgroundPageTracker::GetSingleton()->GetBackgroundPageCount()); + return badge_util::DrawBadgeIconOverlay( + *badge, + kBadgeTextFontSize, + badge_text, + l10n_util::GetStringUTF16(IDS_BACKGROUND_PAGE_BADGE_OVERFLOW)); } + diff --git a/chrome/browser/wrench_menu_model.cc b/chrome/browser/wrench_menu_model.cc index 87ed089..9c84667 100644 --- a/chrome/browser/wrench_menu_model.cc +++ b/chrome/browser/wrench_menu_model.cc @@ -28,6 +28,7 @@ #include "chrome/browser/tabs/tab_strip_model.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/upgrade_detector.h" +#include "chrome/common/badge_util.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/notification_service.h" #include "chrome/common/notification_source.h" @@ -55,6 +56,22 @@ #include "chrome/browser/enumerate_modules_model_win.h" #endif +// The size of the font used for dynamic text overlays on menu items. +const float kMenuBadgeFontSize = 12.0; + +namespace { +SkBitmap GetBackgroundPageIcon() { + string16 pages = base::FormatNumber( + BackgroundPageTracker::GetSingleton()->GetBackgroundPageCount()); + return badge_util::DrawBadgeIconOverlay( + *ResourceBundle::GetSharedInstance().GetBitmapNamed(IDR_BACKGROUND_MENU), + kMenuBadgeFontSize, + pages, + l10n_util::GetStringUTF16(IDS_BACKGROUND_PAGE_BADGE_OVERFLOW)); +} + +} // namespace + //////////////////////////////////////////////////////////////////////////////// // EncodingMenuModel @@ -334,11 +351,15 @@ void WrenchMenuModel::Observe(NotificationType type, const NotificationDetails& details) { switch (type.value) { case NotificationType::BACKGROUND_PAGE_TRACKER_CHANGED: { - bool show_badge = BackgroundPageTracker::GetSingleton()-> - GetUnacknowledgedBackgroundPageCount() > 0; - SetIcon(GetIndexOfCommandId(IDC_VIEW_BACKGROUND_PAGES), - show_badge ? *ResourceBundle::GetSharedInstance().GetBitmapNamed( - IDR_BACKGROUND_MENU) : SkBitmap()); + int num_pages = BackgroundPageTracker::GetSingleton()-> + GetUnacknowledgedBackgroundPageCount(); + if (num_pages > 0) { + SetIcon(GetIndexOfCommandId(IDC_VIEW_BACKGROUND_PAGES), + GetBackgroundPageIcon()); + } else { + // Just set a blank icon (no icon). + SetIcon(GetIndexOfCommandId(IDC_VIEW_BACKGROUND_PAGES), SkBitmap()); + } break; } case NotificationType::ZOOM_LEVEL_CHANGED: @@ -461,12 +482,12 @@ void WrenchMenuModel::Build() { *rb.GetBitmapNamed(IDR_CONFLICT_MENU)); #endif - // Add an icon to the View Background Pages item if t + // Add an icon to the View Background Pages item if there are unacknowledged + // pages. if (BackgroundPageTracker::GetSingleton()-> GetUnacknowledgedBackgroundPageCount() > 0) { - // TODO(atwilson): Add code to display current number of pages in badge. SetIcon(GetIndexOfCommandId(IDC_VIEW_BACKGROUND_PAGES), - *rb.GetBitmapNamed(IDR_BACKGROUND_MENU)); + GetBackgroundPageIcon()); } AddItemWithStringId(IDC_HELP_PAGE, IDS_HELP_PAGE); diff --git a/chrome/common/badge_util.cc b/chrome/common/badge_util.cc index 2e175df..bd2067c 100644 --- a/chrome/common/badge_util.cc +++ b/chrome/common/badge_util.cc @@ -6,6 +6,7 @@ #include "app/resource_bundle.h" #include "base/utf_string_conversions.h" +#include "gfx/canvas_skia.h" #include "gfx/font.h" #include "third_party/skia/include/core/SkPaint.h" #include "third_party/skia/include/core/SkTypeface.h" @@ -23,7 +24,6 @@ SkPaint* GetBadgeTextPaintSingleton() { if (!text_paint) { text_paint = new SkPaint; text_paint->setAntiAlias(true); - text_paint->setTextAlign(SkPaint::kLeft_Align); SkTypeface* typeface = SkTypeface::CreateFromName( @@ -50,4 +50,50 @@ SkPaint* GetBadgeTextPaintSingleton() { return text_paint; } +SkBitmap DrawBadgeIconOverlay(const SkBitmap& icon, + float font_size, + const string16& text, + const string16& fallback) { + const int kMinPadding = 1; + + // Calculate the proper style/text overlay to render on the badge. + SkPaint* paint = badge_util::GetBadgeTextPaintSingleton(); + paint->setTextSize(SkFloatToScalar(font_size)); + paint->setColor(SK_ColorWHITE); + + std::string badge_text = UTF16ToUTF8(text); + + // See if the text will fit - otherwise use a default. + SkScalar text_width = paint->measureText(badge_text.c_str(), + badge_text.size()); + + if (SkScalarRound(text_width) > (icon.width() - kMinPadding * 2)) { + // String is too large - use the alternate text. + badge_text = UTF16ToUTF8(fallback); + text_width = paint->measureText(badge_text.c_str(), badge_text.size()); + } + + // When centering the text, we need to make sure there are an equal number + // of pixels on each side as otherwise the text looks off-center. So if the + // padding would be uneven, clip one pixel off the right side. + int badge_width = icon.width(); + if ((SkScalarRound(text_width) % 1) != (badge_width % 1)) + badge_width--; + + // Render the badge bitmap and overlay into a canvas. + scoped_ptr<gfx::CanvasSkia> canvas( + new gfx::CanvasSkia(badge_width, icon.height(), false)); + canvas->DrawBitmapInt(icon, 0, 0); + + // Draw the text overlay centered horizontally and vertically. Skia expects + // us to specify the lower left coordinate of the text box, which is why we + // add 'font_size - 1' to the height. + SkScalar x = (badge_width - text_width)/2; + SkScalar y = (icon.height() - font_size)/2 + font_size - 1; + canvas->drawText(badge_text.c_str(), badge_text.size(), x, y, *paint); + + // Return the generated image. + return canvas->ExtractBitmap(); +} + } // namespace badge_util diff --git a/chrome/common/badge_util.h b/chrome/common/badge_util.h index 606008e..57180ef 100644 --- a/chrome/common/badge_util.h +++ b/chrome/common/badge_util.h @@ -6,6 +6,9 @@ #define CHROME_COMMON_BADGE_UTIL_H_ #pragma once +#include "base/string16.h" +#include "third_party/skia/include/core/SkBitmap.h" + class SkPaint; // badge_util provides a set of helper routines for rendering dynamically @@ -16,6 +19,14 @@ namespace badge_util { // rendering badge overlay text (correct font, typeface, etc). SkPaint* GetBadgeTextPaintSingleton(); +// Given an |icon|, renders the |text| centered on the |icon|. If |text| is +// too large to fit within the bounds of the image, the |fallback| string is +// rendered instead (or nothing, if |fallback| is empty). +SkBitmap DrawBadgeIconOverlay(const SkBitmap& icon, + float font_size_in_pixels, + const string16& text, + const string16& fallback); + } // namespace badge_util; #endif // CHROME_COMMON_BADGE_UTIL_H_ |