diff options
author | sail@chromium.org <sail@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-30 08:09:44 +0000 |
---|---|---|
committer | sail@chromium.org <sail@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-11-30 08:09:44 +0000 |
commit | c6e1f9e6bfb167569afbec108cb7dba07adc3110 (patch) | |
tree | 0d8e67aabb49086ddb9b91856215a58c52e0ba43 | |
parent | f3b1060590b2f1effb29424b8ae2d72c430521e9 (diff) | |
download | chromium_src-c6e1f9e6bfb167569afbec108cb7dba07adc3110.zip chromium_src-c6e1f9e6bfb167569afbec108cb7dba07adc3110.tar.gz chromium_src-c6e1f9e6bfb167569afbec108cb7dba07adc3110.tar.bz2 |
Utility to draw GAIA pictures
This CL adds common utility functions to draw GAIA pictures in the avatar menu bubble and in the title bar.
BUG=91241
TEST=
Review URL: http://codereview.chromium.org/8700014
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@112159 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/profiles/avatar_menu_model.cc | 10 | ||||
-rw-r--r-- | chrome/browser/profiles/avatar_menu_model.h | 7 | ||||
-rw-r--r-- | chrome/browser/profiles/profile_info_util.cc | 74 | ||||
-rw-r--r-- | chrome/browser/profiles/profile_info_util.h | 29 | ||||
-rw-r--r-- | chrome/browser/profiles/profile_info_util_unittest.cc | 47 | ||||
-rw-r--r-- | chrome/chrome_browser.gypi | 2 | ||||
-rw-r--r-- | chrome/chrome_tests.gypi | 1 |
7 files changed, 163 insertions, 7 deletions
diff --git a/chrome/browser/profiles/avatar_menu_model.cc b/chrome/browser/profiles/avatar_menu_model.cc index 5ff3e8c..592f6ec 100644 --- a/chrome/browser/profiles/avatar_menu_model.cc +++ b/chrome/browser/profiles/avatar_menu_model.cc @@ -10,6 +10,7 @@ #include "chrome/browser/profiles/avatar_menu_model_observer.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile_info_cache.h" +#include "chrome/browser/profiles/profile_info_util.h" #include "chrome/browser/profiles/profile_manager.h" #include "chrome/browser/profiles/profile_metrics.h" #include "chrome/browser/ui/browser.h" @@ -21,7 +22,6 @@ #include "content/public/browser/notification_service.h" #include "grit/generated_resources.h" #include "ui/base/l10n/l10n_util.h" -#include "ui/gfx/image/image.h" using content::BrowserThread; @@ -151,7 +151,13 @@ void AvatarMenuModel::RebuildMenu() { const size_t count = profile_info_->GetNumberOfProfiles(); for (size_t i = 0; i < count; ++i) { - Item* item = new Item(i, profile_info_->GetAvatarIconOfProfileAtIndex(i)); + bool is_gaia_picture = + profile_info_->IsUsingGAIAPictureOfProfileAtIndex(i) && + profile_info_->GetGAIAPictureOfProfileAtIndex(i); + gfx::Image icon = profiles::GetAvatarIconForMenu( + profile_info_->GetAvatarIconOfProfileAtIndex(i), is_gaia_picture); + + Item* item = new Item(i, icon); item->name = profile_info_->GetNameOfProfileAtIndex(i); item->sync_state = profile_info_->GetUserNameOfProfileAtIndex(i); if (item->sync_state.empty()) { diff --git a/chrome/browser/profiles/avatar_menu_model.h b/chrome/browser/profiles/avatar_menu_model.h index 73f75de2..85abe85 100644 --- a/chrome/browser/profiles/avatar_menu_model.h +++ b/chrome/browser/profiles/avatar_menu_model.h @@ -12,15 +12,12 @@ #include "base/string16.h" #include "content/public/browser/notification_observer.h" #include "content/public/browser/notification_registrar.h" +#include "ui/gfx/image/image.h" class AvatarMenuModelObserver; class Browser; class ProfileInfoInterface; -namespace gfx { -class Image; -} - // This class is the model for the menu-like interface that appears when the // avatar icon is clicked in the browser window frame. This class will notify // its observer when the backend data changes, and the controller/view for this @@ -33,7 +30,7 @@ class AvatarMenuModel : public content::NotificationObserver { ~Item(); // The icon to be displayed next to the item. - const gfx::Image& icon; + gfx::Image icon; // Whether or not the current browser is using this profile. bool active; diff --git a/chrome/browser/profiles/profile_info_util.cc b/chrome/browser/profiles/profile_info_util.cc new file mode 100644 index 0000000..9956d0e --- /dev/null +++ b/chrome/browser/profiles/profile_info_util.cc @@ -0,0 +1,74 @@ +// Copyright (c) 2011 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/browser/profiles/profile_info_util.h" + +#include "skia/ext/image_operations.h" +#include "ui/gfx/canvas_skia.h" +#include "ui/gfx/rect.h" + +namespace profiles { + +const int kAvatarIconWidth = 38; +const int kAvatarIconHeight = 31; + +gfx::Image GetAvatarIconForMenu(const gfx::Image& image, + bool is_gaia_picture) { + if (!is_gaia_picture) + return image; + + int length = std::min(kAvatarIconWidth, kAvatarIconHeight) - 2; + SkBitmap bmp = skia::ImageOperations::Resize( + image, skia::ImageOperations::RESIZE_BEST, length, length); + gfx::CanvasSkia canvas(kAvatarIconWidth, kAvatarIconHeight, false); + + // Draw the icon centered on the canvas. + int x = (kAvatarIconWidth - length) / 2; + int y = (kAvatarIconHeight - length) / 2; + canvas.DrawBitmapInt(bmp, x, y); + + // Draw a gray border on the inside of the icon. + SkColor color = SkColorSetARGB(83, 0, 0, 0); + canvas.DrawRectInt(color, x, y, length - 1, length - 1); + + return gfx::Image(new SkBitmap(canvas.ExtractBitmap())); +} + +gfx::Image GetAvatarIconForTitleBar(const gfx::Image& image, + bool is_gaia_picture, + int dst_width, + int dst_height) { + if (!is_gaia_picture) + return image; + + int length = std::min(std::min(kAvatarIconWidth, kAvatarIconHeight), + std::min(dst_width, dst_height)) - 2; + SkBitmap bmp = skia::ImageOperations::Resize( + image, skia::ImageOperations::RESIZE_BEST, length, length); + gfx::CanvasSkia canvas(dst_width, dst_height, false); + + // Draw the icon on the bottom center of the canvas. + int x1 = (dst_width - length) / 2; + int x2 = x1 + length; + int y1 = dst_height - length - 1; + int y2 = y1 + length; + canvas.DrawBitmapInt(bmp, x1, y1); + + // Give the icon an etched look by drawing a highlight on the bottom edge + // and a shadow on the remaining edges. + SkColor highlight_color = SkColorSetARGB(128, 255, 255, 255); + SkColor shadow_color = SkColorSetARGB(83, 0, 0, 0); + // Bottom highlight. + canvas.DrawLineInt(highlight_color, x1, y2 - 1, x2, y2 - 1); + // Top shadow. + canvas.DrawLineInt(shadow_color, x1, y1, x2, y1); + // Left shadow. + canvas.DrawLineInt(shadow_color, x1, y1 + 1, x1, y2 - 1); + // Right shadow. + canvas.DrawLineInt(shadow_color, x2 - 1, y1 + 1, x2 - 1, y2 - 1); + + return gfx::Image(new SkBitmap(canvas.ExtractBitmap())); +} + +} // namespace diff --git a/chrome/browser/profiles/profile_info_util.h b/chrome/browser/profiles/profile_info_util.h new file mode 100644 index 0000000..52819ce --- /dev/null +++ b/chrome/browser/profiles/profile_info_util.h @@ -0,0 +1,29 @@ +// Copyright (c) 2011 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_BROWSER_PROFILES_PROFILE_INFO_UTIL_H_ +#define CHROME_BROWSER_PROFILES_PROFILE_INFO_UTIL_H_ +#pragma once + +#include "ui/gfx/image/image.h" + +namespace profiles { + +extern const int kAvatarIconWidth; +extern const int kAvatarIconHeight; + +// Returns a version of |image| suitable for use in menus. +gfx::Image GetAvatarIconForMenu(const gfx::Image& image, + bool is_gaia_picture); + +// Returns a version of |image| suitable for use in title bars. The returned +// image is scaled to fit |dst_width| and |dst_height|. +gfx::Image GetAvatarIconForTitleBar(const gfx::Image& image, + bool is_gaia_picture, + int dst_width, + int dst_height); + +} + +#endif // CHROME_BROWSER_PROFILES_PROFILE_INFO_UTIL_H_ diff --git a/chrome/browser/profiles/profile_info_util_unittest.cc b/chrome/browser/profiles/profile_info_util_unittest.cc new file mode 100644 index 0000000..edb0e45 --- /dev/null +++ b/chrome/browser/profiles/profile_info_util_unittest.cc @@ -0,0 +1,47 @@ +// Copyright (c) 2011 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/browser/profiles/profile_info_util.h" + +#include "grit/generated_resources.h" +#include "grit/theme_resources.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "ui/base/resource/resource_bundle.h" +#include "ui/gfx/image/image_unittest_util.h" + +namespace { + +TEST(ProfileInfoUtilTest, MenuIcon) { + // Test that an avatar icon isn't changed. + const gfx::Image& profile_image( + ResourceBundle::GetSharedInstance().GetImageNamed(IDR_PROFILE_AVATAR_0)); + gfx::Image result = profiles::GetAvatarIconForMenu(profile_image, false); + EXPECT_FALSE(gfx::test::IsEmpty(result)); + EXPECT_TRUE(gfx::test::IsEqual(profile_image, result)); + + // Test that a GAIA picture is changed. + gfx::Image gaia_picture(gfx::test::CreateImage()); + gfx::Image result2 = profiles::GetAvatarIconForMenu(gaia_picture, true); + EXPECT_FALSE(gfx::test::IsEmpty(result2)); + EXPECT_FALSE(gfx::test::IsEqual(gaia_picture, result2)); +} + +TEST(ProfileInfoUtilTest, TitleBarIcon) { + // Test that an avatar icon isn't changed. + const gfx::Image& profile_image( + ResourceBundle::GetSharedInstance().GetImageNamed(IDR_PROFILE_AVATAR_0)); + gfx::Image result = profiles::GetAvatarIconForTitleBar( + profile_image, false, 100, 40); + EXPECT_FALSE(gfx::test::IsEmpty(result)); + EXPECT_TRUE(gfx::test::IsEqual(profile_image, result)); + + // Test that a GAIA picture is changed. + gfx::Image gaia_picture(gfx::test::CreateImage()); + gfx::Image result2 = profiles::GetAvatarIconForTitleBar( + gaia_picture, true, 100, 40); + EXPECT_FALSE(gfx::test::IsEmpty(result2)); + EXPECT_FALSE(gfx::test::IsEqual(gaia_picture, result2)); +} + +} // namespace diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index d57429d..3792350 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1970,6 +1970,8 @@ 'browser/profiles/profile_info_cache.h', 'browser/profiles/profile_info_cache_observer.h', 'browser/profiles/profile_info_interface.h', + 'browser/profiles/profile_info_util.cc', + 'browser/profiles/profile_info_util.h', 'browser/profiles/profile_io_data.cc', 'browser/profiles/profile_io_data.h', 'browser/profiles/profile_keyed_service_factory.cc', diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 174b03a..48e21e8 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1570,6 +1570,7 @@ 'browser/profiles/profile_dependency_manager_unittest.cc', 'browser/profiles/profile_info_cache_unittest.cc', 'browser/profiles/profile_info_cache_unittest.h', + 'browser/profiles/profile_info_util_unittest.cc', 'browser/profiles/profile_manager_unittest.cc', 'browser/renderer_host/web_cache_manager_unittest.cc', 'browser/resources/print_preview/print_preview_utils.js', |