diff options
author | atwilson@chromium.org <atwilson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-22 19:23:06 +0000 |
---|---|---|
committer | atwilson@chromium.org <atwilson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-22 19:23:06 +0000 |
commit | cdb8bc2837fcff4f54d9caa611fa8420eace1262 (patch) | |
tree | f41ef1aee3e3cdd4b299093d73a99c623dcbf9dc /chrome | |
parent | 28fbe1fcd1f7dfe84e4b74116d8fbd347cbd7536 (diff) | |
download | chromium_src-cdb8bc2837fcff4f54d9caa611fa8420eace1262.zip chromium_src-cdb8bc2837fcff4f54d9caa611fa8420eace1262.tar.gz chromium_src-cdb8bc2837fcff4f54d9caa611fa8420eace1262.tar.bz2 |
Refactored GetTextPaint() into common/badge_util.h
Refactored GetTextPaint() into common/badge_util.h so it can be used by other
toolbar widgets other than ExtensionAction.
BUG=none
TEST=check that browser action badges still work
Review URL: http://codereview.chromium.org/5144008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@66983 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/chrome_common.gypi | 74 | ||||
-rw-r--r-- | chrome/common/badge_util.cc | 53 | ||||
-rw-r--r-- | chrome/common/badge_util.h | 21 | ||||
-rw-r--r-- | chrome/common/extensions/extension_action.cc | 45 |
4 files changed, 115 insertions, 78 deletions
diff --git a/chrome/chrome_common.gypi b/chrome/chrome_common.gypi index 6d4d4c6..9bc47ff 100644 --- a/chrome/chrome_common.gypi +++ b/chrome/chrome_common.gypi @@ -188,8 +188,38 @@ 'sources': [ # .cc, .h, and .mm files under chrome/common that are not required for # building 64-bit Windows targets. Test files are not included. - 'common/desktop_notifications/active_notification_tracker.h', + 'common/appcache/appcache_backend_proxy.cc', + 'common/appcache/appcache_backend_proxy.h', + 'common/appcache/appcache_dispatcher.cc', + 'common/appcache/appcache_dispatcher.h', + 'common/automation_constants.cc', + 'common/automation_constants.h', + 'common/automation_messages.cc', + 'common/automation_messages.h', + 'common/automation_messages_internal.h', + 'common/badge_util.cc', + 'common/badge_util.h', + 'common/child_process_host.cc', + 'common/child_process_host.h', + 'common/chrome_descriptors.h', + 'common/chrome_plugin_api.h', + 'common/chrome_plugin_lib.cc', + 'common/chrome_plugin_lib.h', + 'common/chrome_plugin_util.cc', + 'common/chrome_plugin_util.h', + 'common/common_glue.cc', + 'common/css_colors.h', + 'common/database_util.cc', + 'common/database_util.h', + 'common/db_message_filter.cc', + 'common/db_message_filter.h', + 'common/default_plugin.cc', + 'common/default_plugin.h', + 'common/deprecated/event_sys-inl.h', + 'common/deprecated/event_sys.h', 'common/desktop_notifications/active_notification_tracker.cc', + 'common/desktop_notifications/active_notification_tracker.h', + 'common/dom_storage_common.h', 'common/extensions/extension.cc', 'common/extensions/extension.h', 'common/extensions/extension_action.cc', @@ -220,36 +250,6 @@ 'common/extensions/url_pattern.h', 'common/extensions/user_script.cc', 'common/extensions/user_script.h', - 'common/web_resource/web_resource_unpacker.cc', - 'common/web_resource/web_resource_unpacker.h', - 'common/appcache/appcache_backend_proxy.cc', - 'common/appcache/appcache_backend_proxy.h', - 'common/appcache/appcache_dispatcher.cc', - 'common/appcache/appcache_dispatcher.h', - 'common/automation_constants.cc', - 'common/automation_constants.h', - 'common/automation_messages.cc', - 'common/automation_messages.h', - 'common/automation_messages_internal.h', - 'common/child_process_host.cc', - 'common/child_process_host.h', - 'common/chrome_descriptors.h', - 'common/chrome_plugin_api.h', - 'common/chrome_plugin_lib.cc', - 'common/chrome_plugin_lib.h', - 'common/chrome_plugin_util.cc', - 'common/chrome_plugin_util.h', - 'common/common_glue.cc', - 'common/css_colors.h', - 'common/database_util.cc', - 'common/database_util.h', - 'common/db_message_filter.cc', - 'common/db_message_filter.h', - 'common/default_plugin.cc', - 'common/default_plugin.h', - 'common/deprecated/event_sys-inl.h', - 'common/deprecated/event_sys.h', - 'common/dom_storage_common.h', 'common/file_system/file_system_dispatcher.cc', 'common/file_system/file_system_dispatcher.h', 'common/font_loader_mac.h', @@ -268,21 +268,21 @@ 'common/libxml_utils.cc', 'common/libxml_utils.h', 'common/mru_cache.h', - 'common/navigation_gesture.h', - 'common/navigation_types.h', 'common/native_web_keyboard_event.h', 'common/native_web_keyboard_event_linux.cc', 'common/native_web_keyboard_event_mac.mm', 'common/native_web_keyboard_event_win.cc', 'common/native_window_notification_source.h', - 'common/page_type.h', + 'common/navigation_gesture.h', + 'common/navigation_types.h', 'common/page_transition_types.cc', 'common/page_transition_types.h', + 'common/page_type.h', 'common/page_zoom.h', 'common/pepper_plugin_registry.cc', 'common/pepper_plugin_registry.h', - 'common/plugin_carbon_interpose_constants_mac.h', 'common/plugin_carbon_interpose_constants_mac.cc', + 'common/plugin_carbon_interpose_constants_mac.h', 'common/plugin_messages.cc', 'common/plugin_messages.h', 'common/plugin_messages_internal.h', @@ -290,9 +290,9 @@ 'common/pref_store.h', 'common/render_messages.cc', 'common/render_messages.h', + 'common/render_messages_internal.h', 'common/render_messages_params.cc', 'common/render_messages_params.h', - 'common/render_messages_internal.h', 'common/renderer_preferences.cc', 'common/renderer_preferences.h', 'common/resource_dispatcher.cc', @@ -330,6 +330,8 @@ 'common/web_apps.h', 'common/web_database_observer_impl.cc', 'common/web_database_observer_impl.h', + 'common/web_resource/web_resource_unpacker.cc', + 'common/web_resource/web_resource_unpacker.h', 'common/webkit_param_traits.cc', 'common/webkit_param_traits.h', 'common/webmessageportchannel_impl.cc', diff --git a/chrome/common/badge_util.cc b/chrome/common/badge_util.cc new file mode 100644 index 0000000..2e175df --- /dev/null +++ b/chrome/common/badge_util.cc @@ -0,0 +1,53 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/common/badge_util.h" + +#include "app/resource_bundle.h" +#include "base/utf_string_conversions.h" +#include "gfx/font.h" +#include "third_party/skia/include/core/SkPaint.h" +#include "third_party/skia/include/core/SkTypeface.h" + +namespace badge_util { + +SkPaint* GetBadgeTextPaintSingleton() { +#if defined(OS_MACOSX) + const char kPreferredTypeface[] = "Helvetica Bold"; +#else + const char kPreferredTypeface[] = "Arial"; +#endif + + static SkPaint* text_paint = NULL; + if (!text_paint) { + text_paint = new SkPaint; + text_paint->setAntiAlias(true); + + text_paint->setTextAlign(SkPaint::kLeft_Align); + + SkTypeface* typeface = SkTypeface::CreateFromName( + kPreferredTypeface, SkTypeface::kBold); + // Skia doesn't do any font fallback---if the user is missing the font then + // typeface will be NULL. If we don't do manual fallback then we'll crash. + if (typeface) { + text_paint->setFakeBoldText(true); + } else { + // Fall back to the system font. We don't bold it because we aren't sure + // how it will look. + // For the most part this code path will only be hit on Linux systems + // that don't have Arial. + ResourceBundle& rb = ResourceBundle::GetSharedInstance(); + const gfx::Font& base_font = rb.GetFont(ResourceBundle::BaseFont); + typeface = SkTypeface::CreateFromName( + WideToUTF8(base_font.GetFontName()).c_str(), SkTypeface::kNormal); + } + + text_paint->setTypeface(typeface); + // |text_paint| adds its own ref. Release the ref from CreateFontName. + typeface->unref(); + } + return text_paint; +} + +} // namespace badge_util diff --git a/chrome/common/badge_util.h b/chrome/common/badge_util.h new file mode 100644 index 0000000..606008e --- /dev/null +++ b/chrome/common/badge_util.h @@ -0,0 +1,21 @@ +// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_COMMON_BADGE_UTIL_H_ +#define CHROME_COMMON_BADGE_UTIL_H_ +#pragma once + +class SkPaint; + +// badge_util provides a set of helper routines for rendering dynamically +// generated text overlays ("badges") on toolbar icons. +namespace badge_util { + +// Helper routine that returns a singleton SkPaint object configured for +// rendering badge overlay text (correct font, typeface, etc). +SkPaint* GetBadgeTextPaintSingleton(); + +} // namespace badge_util; + +#endif // CHROME_COMMON_BADGE_UTIL_H_ diff --git a/chrome/common/extensions/extension_action.cc b/chrome/common/extensions/extension_action.cc index 6186d79..7139fb2 100644 --- a/chrome/common/extensions/extension_action.cc +++ b/chrome/common/extensions/extension_action.cc @@ -8,14 +8,12 @@ #include "app/resource_bundle.h" #include "base/logging.h" -#include "base/utf_string_conversions.h" +#include "chrome/common/badge_util.h" #include "gfx/canvas_skia.h" -#include "gfx/font.h" #include "gfx/rect.h" #include "googleurl/src/gurl.h" #include "grit/app_resources.h" #include "third_party/skia/include/core/SkBitmap.h" -#include "third_party/skia/include/core/SkTypeface.h" #include "third_party/skia/include/effects/SkGradientShader.h" namespace { @@ -49,44 +47,6 @@ const int kMaxTextWidth = 23; // The minimum width for center-aligning the badge. const int kCenterAlignThreshold = 20; -#if defined(OS_MACOSX) -const char kPreferredTypeface[] = "Helvetica Bold"; -#else -const char kPreferredTypeface[] = "Arial"; -#endif - -SkPaint* GetTextPaint() { - static SkPaint* text_paint = NULL; - if (!text_paint) { - text_paint = new SkPaint; - text_paint->setAntiAlias(true); - - text_paint->setTextAlign(SkPaint::kLeft_Align); - text_paint->setTextSize(SkFloatToScalar(kTextSize)); - - SkTypeface* typeface = SkTypeface::CreateFromName( - kPreferredTypeface, SkTypeface::kBold); - // Skia doesn't do any font fallback---if the user is missing the font then - // typeface will be NULL. If we don't do manual fallback then we'll crash. - if (typeface) { - text_paint->setFakeBoldText(true); - } else { - // Fall back to the system font. We don't bold it because we aren't sure - // how it will look. - // For the most part this code path will only be hit on Linux systems - // that don't have Arial. - ResourceBundle& rb = ResourceBundle::GetSharedInstance(); - const gfx::Font& base_font = rb.GetFont(ResourceBundle::BaseFont); - typeface = SkTypeface::CreateFromName( - WideToUTF8(base_font.GetFontName()).c_str(), SkTypeface::kNormal); - } - - text_paint->setTypeface(typeface); - // |text_paint| adds its own ref. Release the ref from CreateFontName. - typeface->unref(); - } - return text_paint; -} } // namespace @@ -160,7 +120,8 @@ void ExtensionAction::PaintBadge(gfx::Canvas* canvas, canvas->Save(); - SkPaint* text_paint = GetTextPaint(); + SkPaint* text_paint = badge_util::GetBadgeTextPaintSingleton(); + text_paint->setTextSize(SkFloatToScalar(kTextSize)); text_paint->setColor(text_color); // Calculate text width. We clamp it to a max size. |