Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/profiles/profile_avatar_icon_util.h" | 5 #include "chrome/browser/profiles/profile_avatar_icon_util.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 24 #include "third_party/skia/include/core/SkPath.h" | 24 #include "third_party/skia/include/core/SkPath.h" |
| 25 #include "third_party/skia/include/core/SkScalar.h" | 25 #include "third_party/skia/include/core/SkScalar.h" |
| 26 #include "third_party/skia/include/core/SkXfermode.h" | 26 #include "third_party/skia/include/core/SkXfermode.h" |
| 27 #include "ui/base/l10n/l10n_util.h" | 27 #include "ui/base/l10n/l10n_util.h" |
| 28 #include "ui/base/resource/resource_bundle.h" | 28 #include "ui/base/resource/resource_bundle.h" |
| 29 #include "ui/gfx/canvas.h" | 29 #include "ui/gfx/canvas.h" |
| 30 #include "ui/gfx/geometry/rect.h" | 30 #include "ui/gfx/geometry/rect.h" |
| 31 #include "ui/gfx/image/canvas_image_source.h" | 31 #include "ui/gfx/image/canvas_image_source.h" |
| 32 #include "ui/gfx/image/image.h" | 32 #include "ui/gfx/image/image.h" |
| 33 #include "ui/gfx/image/image_skia_operations.h" | 33 #include "ui/gfx/image/image_skia_operations.h" |
| 34 #include "ui/gfx/path.h" | |
| 34 #include "ui/gfx/skia_util.h" | 35 #include "ui/gfx/skia_util.h" |
| 35 #include "url/gurl.h" | 36 #include "url/gurl.h" |
| 36 #include "url/url_canon.h" | 37 #include "url/url_canon.h" |
| 37 | 38 |
| 38 // Helper methods for transforming and drawing avatar icons. | 39 // Helper methods for transforming and drawing avatar icons. |
| 39 namespace { | 40 namespace { |
| 40 | 41 |
| 41 // Path format for specifying thumbnail's size. | 42 // Path format for specifying thumbnail's size. |
| 42 const char kThumbnailSizeFormat[] = "s%d-c"; | 43 const char kThumbnailSizeFormat[] = "s%d-c"; |
| 43 // Default thumbnail size. | 44 // Default thumbnail size. |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 67 enum AvatarBorder { | 68 enum AvatarBorder { |
| 68 BORDER_NONE, | 69 BORDER_NONE, |
| 69 BORDER_NORMAL, | 70 BORDER_NORMAL, |
| 70 BORDER_ETCHED, | 71 BORDER_ETCHED, |
| 71 }; | 72 }; |
| 72 | 73 |
| 73 AvatarImageSource(gfx::ImageSkia avatar, | 74 AvatarImageSource(gfx::ImageSkia avatar, |
| 74 const gfx::Size& canvas_size, | 75 const gfx::Size& canvas_size, |
| 75 int width, | 76 int width, |
| 76 AvatarPosition position, | 77 AvatarPosition position, |
| 77 AvatarBorder border); | 78 AvatarBorder border, |
| 79 profiles::AvatarShape shape=profiles::SHAPE_SQUARE); | |
|
anthonyvd
2016/06/28 14:50:46
nit: spaces here too
Jane
2016/06/28 16:32:53
Changed this to function overloads.
| |
| 80 | |
| 78 ~AvatarImageSource() override; | 81 ~AvatarImageSource() override; |
| 79 | 82 |
| 80 // CanvasImageSource override: | 83 // CanvasImageSource override: |
| 81 void Draw(gfx::Canvas* canvas) override; | 84 void Draw(gfx::Canvas* canvas) override; |
| 82 | 85 |
| 83 private: | 86 private: |
| 84 gfx::ImageSkia avatar_; | 87 gfx::ImageSkia avatar_; |
| 85 const gfx::Size canvas_size_; | 88 const gfx::Size canvas_size_; |
| 86 const int width_; | 89 const int width_; |
| 87 const int height_; | 90 const int height_; |
| 88 const AvatarPosition position_; | 91 const AvatarPosition position_; |
| 89 const AvatarBorder border_; | 92 const AvatarBorder border_; |
| 93 const profiles::AvatarShape shape_; | |
| 90 | 94 |
| 91 DISALLOW_COPY_AND_ASSIGN(AvatarImageSource); | 95 DISALLOW_COPY_AND_ASSIGN(AvatarImageSource); |
| 92 }; | 96 }; |
| 93 | 97 |
| 94 AvatarImageSource::AvatarImageSource(gfx::ImageSkia avatar, | 98 AvatarImageSource::AvatarImageSource(gfx::ImageSkia avatar, |
| 95 const gfx::Size& canvas_size, | 99 const gfx::Size& canvas_size, |
| 96 int width, | 100 int width, |
| 97 AvatarPosition position, | 101 AvatarPosition position, |
| 98 AvatarBorder border) | 102 AvatarBorder border, |
| 103 profiles::AvatarShape shape) | |
| 99 : gfx::CanvasImageSource(canvas_size, false), | 104 : gfx::CanvasImageSource(canvas_size, false), |
| 100 canvas_size_(canvas_size), | 105 canvas_size_(canvas_size), |
| 101 width_(width), | 106 width_(width), |
| 102 height_(GetScaledAvatarHeightForWidth(width, avatar)), | 107 height_(GetScaledAvatarHeightForWidth(width, avatar)), |
| 103 position_(position), | 108 position_(position), |
| 104 border_(border) { | 109 border_(border), |
| 110 shape_(shape) { | |
| 105 avatar_ = gfx::ImageSkiaOperations::CreateResizedImage( | 111 avatar_ = gfx::ImageSkiaOperations::CreateResizedImage( |
| 106 avatar, skia::ImageOperations::RESIZE_BEST, | 112 avatar, skia::ImageOperations::RESIZE_BEST, |
| 107 gfx::Size(width_, height_)); | 113 gfx::Size(width_, height_)); |
| 108 } | 114 } |
| 109 | 115 |
| 110 AvatarImageSource::~AvatarImageSource() { | 116 AvatarImageSource::~AvatarImageSource() { |
| 111 } | 117 } |
| 112 | 118 |
| 113 void AvatarImageSource::Draw(gfx::Canvas* canvas) { | 119 void AvatarImageSource::Draw(gfx::Canvas* canvas) { |
| 114 // Center the avatar horizontally. | 120 // Center the avatar horizontally. |
| 115 int x = (canvas_size_.width() - width_) / 2; | 121 int x = (canvas_size_.width() - width_) / 2; |
| 116 int y; | 122 int y; |
| 117 | 123 |
| 118 if (position_ == POSITION_CENTER) { | 124 if (position_ == POSITION_CENTER) { |
| 119 // Draw the avatar centered on the canvas. | 125 // Draw the avatar centered on the canvas. |
| 120 y = (canvas_size_.height() - height_) / 2; | 126 y = (canvas_size_.height() - height_) / 2; |
| 121 } else { | 127 } else { |
| 122 // Draw the avatar on the bottom center of the canvas, leaving 1px below. | 128 // Draw the avatar on the bottom center of the canvas, leaving 1px below. |
| 123 y = canvas_size_.height() - height_ - 1; | 129 y = canvas_size_.height() - height_ - 1; |
| 124 } | 130 } |
| 125 | 131 |
| 132 #if defined(OS_ANDROID) | |
| 133 // Circular shape is only available on desktop platforms. | |
| 134 DCHECK(shape_ != profiles::SHAPE_CIRCLE); | |
| 135 #else | |
| 136 if (shape_ == profiles::SHAPE_CIRCLE) { | |
| 137 // Draw the avatar on the bottom center of the canvas; overrides the | |
|
anthonyvd
2016/06/28 14:50:46
Could you explain why in this comment please?
Jane
2016/06/28 16:32:53
Done. Does this make sense?
anthonyvd
2016/06/28 19:41:33
Yep, perfect :)
| |
| 138 // previous position specification. | |
| 139 y = canvas_size_.height() - height_; | |
| 140 | |
| 141 // Calculate the circular mask that will be used to display the avatar | |
| 142 // image. | |
| 143 gfx::Path circular_mask; | |
| 144 circular_mask.addCircle(SkIntToScalar(canvas_size_.width() / 2), | |
| 145 SkIntToScalar(canvas_size_.height() / 2), | |
| 146 SkIntToScalar(canvas_size_.width() / 2)); | |
| 147 canvas->ClipPath(circular_mask, true); | |
| 148 } | |
| 149 #endif | |
| 150 | |
| 126 canvas->DrawImageInt(avatar_, x, y); | 151 canvas->DrawImageInt(avatar_, x, y); |
| 127 | 152 |
| 128 // The border should be square. | 153 // The border should be square. |
| 129 int border_size = std::max(width_, height_); | 154 int border_size = std::max(width_, height_); |
| 130 // Reset the x and y for the square border. | 155 // Reset the x and y for the square border. |
| 131 x = (canvas_size_.width() - border_size) / 2; | 156 x = (canvas_size_.width() - border_size) / 2; |
| 132 y = (canvas_size_.height() - border_size) / 2; | 157 y = (canvas_size_.height() - border_size) / 2; |
| 133 | 158 |
| 134 if (border_ == BORDER_NORMAL) { | 159 if (border_ == BORDER_NORMAL) { |
| 135 // Draw a gray border on the inside of the avatar. | 160 // Draw a gray border on the inside of the avatar. |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 221 const size_t kDefaultAvatarIconsCount = 27; | 246 const size_t kDefaultAvatarIconsCount = 27; |
| 222 | 247 |
| 223 // The first 8 icons are generic. | 248 // The first 8 icons are generic. |
| 224 const size_t kGenericAvatarIconsCount = 8; | 249 const size_t kGenericAvatarIconsCount = 8; |
| 225 | 250 |
| 226 // The avatar used as a placeholder (grey silhouette). | 251 // The avatar used as a placeholder (grey silhouette). |
| 227 const size_t kPlaceholderAvatarIndex = 26; | 252 const size_t kPlaceholderAvatarIndex = 26; |
| 228 | 253 |
| 229 gfx::Image GetSizedAvatarIcon(const gfx::Image& image, | 254 gfx::Image GetSizedAvatarIcon(const gfx::Image& image, |
| 230 bool is_rectangle, | 255 bool is_rectangle, |
| 231 int width, int height) { | 256 int width, |
| 257 int height, | |
| 258 AvatarShape shape) { | |
| 232 if (!is_rectangle && image.Height() <= height) | 259 if (!is_rectangle && image.Height() <= height) |
| 233 return image; | 260 return image; |
| 234 | 261 |
| 235 gfx::Size size(width, height); | 262 gfx::Size size(width, height); |
| 236 | 263 |
| 237 // Source for a centered, sized icon. GAIA images get a border. | 264 // Source for a centered, sized icon. GAIA images get a border. |
| 238 std::unique_ptr<gfx::ImageSkiaSource> source(new AvatarImageSource( | 265 std::unique_ptr<gfx::ImageSkiaSource> source( |
| 239 *image.ToImageSkia(), size, std::min(width, height), | 266 new AvatarImageSource(*image.ToImageSkia(), size, std::min(width, height), |
| 240 AvatarImageSource::POSITION_CENTER, AvatarImageSource::BORDER_NONE)); | 267 AvatarImageSource::POSITION_CENTER, |
| 268 AvatarImageSource::BORDER_NONE, shape)); | |
| 241 | 269 |
| 242 return gfx::Image(gfx::ImageSkia(source.release(), size)); | 270 return gfx::Image(gfx::ImageSkia(source.release(), size)); |
| 243 } | 271 } |
| 244 | 272 |
| 245 gfx::Image GetAvatarIconForMenu(const gfx::Image& image, | 273 gfx::Image GetAvatarIconForMenu(const gfx::Image& image, |
| 246 bool is_rectangle) { | 274 bool is_rectangle) { |
| 247 return GetSizedAvatarIcon( | 275 return GetSizedAvatarIcon( |
| 248 image, is_rectangle, kAvatarIconWidth, kAvatarIconHeight); | 276 image, is_rectangle, kAvatarIconWidth, kAvatarIconHeight); |
| 249 } | 277 } |
| 250 | 278 |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 508 avatar_info->SetString( | 536 avatar_info->SetString( |
| 509 "label", l10n_util::GetStringUTF16( | 537 "label", l10n_util::GetStringUTF16( |
| 510 profiles::GetDefaultAvatarLabelResourceIDAtIndex(i))); | 538 profiles::GetDefaultAvatarLabelResourceIDAtIndex(i))); |
| 511 | 539 |
| 512 avatars->Append(std::move(avatar_info)); | 540 avatars->Append(std::move(avatar_info)); |
| 513 } | 541 } |
| 514 return avatars; | 542 return avatars; |
| 515 } | 543 } |
| 516 | 544 |
| 517 } // namespace profiles | 545 } // namespace profiles |
| OLD | NEW |