Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/ui/views/avatar_menu_bubble_view.h" | 5 #include "chrome/browser/ui/views/avatar_menu_bubble_view.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/profiles/avatar_menu_model.h" | 11 #include "chrome/browser/profiles/avatar_menu_model.h" |
| 12 #include "chrome/browser/profiles/profile_info_cache.h" | 12 #include "chrome/browser/profiles/profile_info_cache.h" |
| 13 #include "chrome/browser/profiles/profile_info_util.h" | |
| 13 #include "chrome/browser/profiles/profile_manager.h" | 14 #include "chrome/browser/profiles/profile_manager.h" |
| 14 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
| 15 #include "grit/generated_resources.h" | 16 #include "grit/generated_resources.h" |
| 16 #include "grit/theme_resources.h" | 17 #include "grit/theme_resources.h" |
| 17 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
| 18 #include "ui/base/resource/resource_bundle.h" | 19 #include "ui/base/resource/resource_bundle.h" |
| 19 #include "ui/gfx/canvas.h" | 20 #include "ui/gfx/canvas.h" |
| 20 #include "ui/gfx/canvas_skia.h" | 21 #include "ui/gfx/canvas_skia.h" |
| 21 #include "ui/gfx/font.h" | 22 #include "ui/gfx/font.h" |
| 22 #include "ui/gfx/image/image.h" | 23 #include "ui/gfx/image/image.h" |
| 23 #include "views/controls/button/custom_button.h" | 24 #include "views/controls/button/custom_button.h" |
| 24 #include "views/controls/button/image_button.h" | 25 #include "views/controls/button/image_button.h" |
| 25 #include "views/controls/image_view.h" | 26 #include "views/controls/image_view.h" |
| 26 #include "views/controls/label.h" | 27 #include "views/controls/label.h" |
| 27 #include "views/controls/link.h" | 28 #include "views/controls/link.h" |
| 28 #include "views/controls/separator.h" | 29 #include "views/controls/separator.h" |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 // TODO(msw): Get color from theme/window color. | 33 // TODO(msw): Get color from theme/window color. |
| 33 const SkColor kColor = SK_ColorWHITE; | 34 const SkColor kColor = SK_ColorWHITE; |
| 34 | 35 |
| 35 const int kItemHeight = 44; | 36 const int kItemHeight = 44; |
| 36 const int kItemMarginY = 4; | 37 const int kItemMarginY = 4; |
| 37 const int kIconWidth = 38; | |
| 38 const int kIconMarginX = 6; | 38 const int kIconMarginX = 6; |
| 39 const int kSeparatorPaddingY = 5; | 39 const int kSeparatorPaddingY = 5; |
| 40 | 40 |
| 41 inline int Round(double x) { | 41 inline int Round(double x) { |
| 42 return static_cast<int>(x + 0.5); | 42 return static_cast<int>(x + 0.5); |
| 43 } | 43 } |
| 44 | 44 |
| 45 gfx::Rect GetCenteredAndScaledRect(int src_width, int src_height, | 45 gfx::Rect GetCenteredAndScaledRect(int src_width, int src_height, |
| 46 int dst_x, int dst_y, | 46 int dst_x, int dst_y, |
| 47 int dst_width, int dst_height) { | 47 int dst_width, int dst_height) { |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 219 edit_link_->SetHasFocusBorder(true); | 219 edit_link_->SetHasFocusBorder(true); |
| 220 AddChildView(edit_link_); | 220 AddChildView(edit_link_); |
| 221 | 221 |
| 222 OnHighlightStateChanged(); | 222 OnHighlightStateChanged(); |
| 223 } | 223 } |
| 224 | 224 |
| 225 gfx::Size ProfileItemView::GetPreferredSize() { | 225 gfx::Size ProfileItemView::GetPreferredSize() { |
| 226 int width = std::max(name_label_->GetPreferredSize().width(), | 226 int width = std::max(name_label_->GetPreferredSize().width(), |
| 227 sync_state_label_->GetPreferredSize().width()); | 227 sync_state_label_->GetPreferredSize().width()); |
| 228 width = std::max(edit_link_->GetPreferredSize().width(), width); | 228 width = std::max(edit_link_->GetPreferredSize().width(), width); |
| 229 return gfx::Size(kIconWidth + kIconMarginX + width, kItemHeight); | 229 return gfx::Size(profiles::kAvatarIconWidth + kIconMarginX + width, |
| 230 kItemHeight); | |
| 230 } | 231 } |
| 231 | 232 |
| 232 void ProfileItemView::Layout() { | 233 void ProfileItemView::Layout() { |
| 233 // Profile icon. | 234 // Profile icon. |
| 234 const SkBitmap& icon = image_view_->GetImage(); | 235 gfx::Rect icon_rect; |
| 235 gfx::Rect icon_rect = GetCenteredAndScaledRect( | 236 if (item_.active) { |
| 236 icon.width(), icon.height(), 0, 0, kIconWidth, height()); | 237 // If this is the active item then the icon is already scaled and so |
|
sail
2011/11/26 06:59:04
This old code had a bug in it. It assumed that the
| |
| 238 // just use the preferred size. | |
| 239 icon_rect.set_size(image_view_->GetPreferredSize()); | |
| 240 icon_rect.set_y((height() - icon_rect.height()) / 2); | |
| 241 } else { | |
| 242 const SkBitmap& icon = image_view_->GetImage(); | |
| 243 icon_rect = GetCenteredAndScaledRect(icon.width(), icon.height(), 0, 0, | |
| 244 profiles::kAvatarIconWidth, height()); | |
| 245 } | |
| 237 image_view_->SetBoundsRect(icon_rect); | 246 image_view_->SetBoundsRect(icon_rect); |
| 238 | 247 |
| 239 int label_x = icon_rect.right() + kIconMarginX; | 248 int label_x = profiles::kAvatarIconWidth + kIconMarginX; |
| 240 int max_label_width = std::max(width() - label_x, 0); | 249 int max_label_width = std::max(width() - label_x, 0); |
| 241 gfx::Size name_size = name_label_->GetPreferredSize(); | 250 gfx::Size name_size = name_label_->GetPreferredSize(); |
| 242 name_size.set_width(std::min(name_size.width(), max_label_width)); | 251 name_size.set_width(std::min(name_size.width(), max_label_width)); |
| 243 gfx::Size state_size = sync_state_label_->GetPreferredSize(); | 252 gfx::Size state_size = sync_state_label_->GetPreferredSize(); |
| 244 state_size.set_width(std::min(state_size.width(), max_label_width)); | 253 state_size.set_width(std::min(state_size.width(), max_label_width)); |
| 245 gfx::Size edit_size = edit_link_->GetPreferredSize(); | 254 gfx::Size edit_size = edit_link_->GetPreferredSize(); |
| 246 edit_size.set_width(std::min(edit_size.width(), max_label_width)); | 255 edit_size.set_width(std::min(edit_size.width(), max_label_width)); |
| 247 | 256 |
| 248 const int kNameStatePaddingY = 2; | 257 const int kNameStatePaddingY = 2; |
| 249 int labels_height = name_size.height() + kNameStatePaddingY + | 258 int labels_height = name_size.height() + kNameStatePaddingY + |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 290 } | 299 } |
| 291 | 300 |
| 292 void ProfileItemView::OnFocusStateChanged(bool has_focus) { | 301 void ProfileItemView::OnFocusStateChanged(bool has_focus) { |
| 293 if (!has_focus && state() != views::CustomButton::BS_DISABLED) | 302 if (!has_focus && state() != views::CustomButton::BS_DISABLED) |
| 294 SetState(views::CustomButton::BS_NORMAL); | 303 SetState(views::CustomButton::BS_NORMAL); |
| 295 OnHighlightStateChanged(); | 304 OnHighlightStateChanged(); |
| 296 } | 305 } |
| 297 | 306 |
| 298 // static | 307 // static |
| 299 SkBitmap ProfileItemView::GetBadgedIcon(const SkBitmap& icon) { | 308 SkBitmap ProfileItemView::GetBadgedIcon(const SkBitmap& icon) { |
| 300 gfx::Rect icon_rect = GetCenteredAndScaledRect( | 309 gfx::Rect icon_rect = GetCenteredAndScaledRect(icon.width(), icon.height(), |
| 301 icon.width(), icon.height(), 0, 0, kIconWidth, kItemHeight); | 310 0, 0, profiles::kAvatarIconWidth, kItemHeight); |
| 302 | 311 |
| 303 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 312 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 304 SkBitmap badge = rb.GetImageNamed(IDR_PROFILE_SELECTED); | 313 SkBitmap badge = rb.GetImageNamed(IDR_PROFILE_SELECTED); |
| 305 const float kBadgeOverlapRatioX = 1.0f / 5.0f; | 314 const float kBadgeOverlapRatioX = 1.0f / 5.0f; |
| 306 int width = icon_rect.width() + badge.width() * kBadgeOverlapRatioX; | 315 int width = icon_rect.width() + badge.width() * kBadgeOverlapRatioX; |
| 307 const float kBadgeOverlapRatioY = 1.0f / 3.0f; | 316 const float kBadgeOverlapRatioY = 1.0f / 3.0f; |
| 308 int height = icon_rect.height() + badge.height() * kBadgeOverlapRatioY; | 317 int height = icon_rect.height() + badge.height() * kBadgeOverlapRatioY; |
| 309 | 318 |
| 310 gfx::CanvasSkia canvas(width, height, false); | 319 gfx::CanvasSkia canvas(width, height, false); |
| 311 canvas.DrawBitmapInt(icon, 0, 0, icon.width(), icon.height(), 0, 0, | 320 canvas.DrawBitmapInt(icon, 0, 0, icon.width(), icon.height(), 0, 0, |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 354 gfx::Size size = item_views_[i]->GetPreferredSize(); | 363 gfx::Size size = item_views_[i]->GetPreferredSize(); |
| 355 max_width = std::max(max_width, size.width()); | 364 max_width = std::max(max_width, size.width()); |
| 356 total_height += size.height() + kItemMarginY; | 365 total_height += size.height() + kItemMarginY; |
| 357 } | 366 } |
| 358 | 367 |
| 359 total_height += kSeparatorPaddingY * 2 + | 368 total_height += kSeparatorPaddingY * 2 + |
| 360 separator_->GetPreferredSize().height(); | 369 separator_->GetPreferredSize().height(); |
| 361 | 370 |
| 362 gfx::Size add_profile_size = add_profile_link_->GetPreferredSize(); | 371 gfx::Size add_profile_size = add_profile_link_->GetPreferredSize(); |
| 363 max_width = std::max(max_width, | 372 max_width = std::max(max_width, |
| 364 add_profile_size.width() + kIconWidth + kIconMarginX); | 373 add_profile_size.width() + profiles::kAvatarIconWidth + kIconMarginX); |
| 365 total_height += add_profile_link_->GetPreferredSize().height(); | 374 total_height += add_profile_link_->GetPreferredSize().height(); |
| 366 | 375 |
| 367 const int kBubbleViewMaxWidth = 800; | 376 const int kBubbleViewMaxWidth = 800; |
| 368 const int kBubbleViewMinWidth = 175; | 377 const int kBubbleViewMinWidth = 175; |
| 369 int total_width = std::min(std::max(max_width, kBubbleViewMinWidth), | 378 int total_width = std::min(std::max(max_width, kBubbleViewMinWidth), |
| 370 kBubbleViewMaxWidth); | 379 kBubbleViewMaxWidth); |
| 371 return gfx::Size(total_width, total_height); | 380 return gfx::Size(total_width, total_height); |
| 372 } | 381 } |
| 373 | 382 |
| 374 void AvatarMenuBubbleView::Layout() { | 383 void AvatarMenuBubbleView::Layout() { |
| 375 int y = 0; | 384 int y = 0; |
| 376 for (size_t i = 0; i < item_views_.size(); ++i) { | 385 for (size_t i = 0; i < item_views_.size(); ++i) { |
| 377 views::CustomButton* item_view = item_views_[i]; | 386 views::CustomButton* item_view = item_views_[i]; |
| 378 int item_height = item_view->GetPreferredSize().height(); | 387 int item_height = item_view->GetPreferredSize().height(); |
| 379 int item_width = width(); | 388 int item_width = width(); |
| 380 item_view->SetBounds(0, y, item_width, item_height); | 389 item_view->SetBounds(0, y, item_width, item_height); |
| 381 y += item_height + kItemMarginY; | 390 y += item_height + kItemMarginY; |
| 382 } | 391 } |
| 383 | 392 |
| 384 y += kSeparatorPaddingY; | 393 y += kSeparatorPaddingY; |
| 385 int separator_height = separator_->GetPreferredSize().height(); | 394 int separator_height = separator_->GetPreferredSize().height(); |
| 386 separator_->SetBounds(0, y, width(), separator_height); | 395 separator_->SetBounds(0, y, width(), separator_height); |
| 387 y += kSeparatorPaddingY + separator_height; | 396 y += kSeparatorPaddingY + separator_height; |
| 388 | 397 |
| 389 add_profile_link_->SetBounds(kIconWidth + kIconMarginX, y, width(), | 398 add_profile_link_->SetBounds(profiles::kAvatarIconWidth + kIconMarginX, y, |
| 390 add_profile_link_->GetPreferredSize().height()); | 399 width(), add_profile_link_->GetPreferredSize().height()); |
| 391 } | 400 } |
| 392 | 401 |
| 393 bool AvatarMenuBubbleView::AcceleratorPressed( | 402 bool AvatarMenuBubbleView::AcceleratorPressed( |
| 394 const ui::Accelerator& accelerator) { | 403 const ui::Accelerator& accelerator) { |
| 395 if (accelerator.key_code() != ui::VKEY_DOWN && | 404 if (accelerator.key_code() != ui::VKEY_DOWN && |
| 396 accelerator.key_code() != ui::VKEY_UP) { | 405 accelerator.key_code() != ui::VKEY_UP) { |
| 397 return false; | 406 return false; |
| 398 } | 407 } |
| 399 | 408 |
| 400 if (item_views_.empty()) | 409 if (item_views_.empty()) |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 480 | 489 |
| 481 add_profile_link_ = new views::Link( | 490 add_profile_link_ = new views::Link( |
| 482 l10n_util::GetStringUTF16(IDS_PROFILES_CREATE_NEW_PROFILE_LINK)); | 491 l10n_util::GetStringUTF16(IDS_PROFILES_CREATE_NEW_PROFILE_LINK)); |
| 483 add_profile_link_->set_listener(this); | 492 add_profile_link_->set_listener(this); |
| 484 add_profile_link_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | 493 add_profile_link_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
| 485 add_profile_link_->SetEnabledColor(SkColorSetRGB(0xe3, 0xed, 0xf6)); | 494 add_profile_link_->SetEnabledColor(SkColorSetRGB(0xe3, 0xed, 0xf6)); |
| 486 AddChildView(add_profile_link_); | 495 AddChildView(add_profile_link_); |
| 487 | 496 |
| 488 PreferredSizeChanged(); | 497 PreferredSizeChanged(); |
| 489 } | 498 } |
| OLD | NEW |