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