| 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" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "ui/gfx/image/image.h" | 22 #include "ui/gfx/image/image.h" |
| 23 #include "views/controls/button/custom_button.h" | 23 #include "views/controls/button/custom_button.h" |
| 24 #include "views/controls/button/image_button.h" | 24 #include "views/controls/button/image_button.h" |
| 25 #include "views/controls/image_view.h" | 25 #include "views/controls/image_view.h" |
| 26 #include "views/controls/label.h" | 26 #include "views/controls/label.h" |
| 27 #include "views/controls/link.h" | 27 #include "views/controls/link.h" |
| 28 #include "views/controls/separator.h" | 28 #include "views/controls/separator.h" |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 // TODO(msw): Get color from theme/window color. |
| 33 const SkColor kColor = SK_ColorWHITE; |
| 34 |
| 32 const int kItemHeight = 44; | 35 const int kItemHeight = 44; |
| 33 const int kItemMarginY = 4; | 36 const int kItemMarginY = 4; |
| 34 const int kIconWidth = 38; | 37 const int kIconWidth = 38; |
| 35 const int kIconMarginX = 6; | 38 const int kIconMarginX = 6; |
| 36 const int kSeparatorPaddingY = 5; | 39 const int kSeparatorPaddingY = 5; |
| 37 | 40 |
| 38 inline int Round(double x) { | 41 inline int Round(double x) { |
| 39 return static_cast<int>(x + 0.5); | 42 return static_cast<int>(x + 0.5); |
| 40 } | 43 } |
| 41 | 44 |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 } | 230 } |
| 228 | 231 |
| 229 void ProfileItemView::Layout() { | 232 void ProfileItemView::Layout() { |
| 230 // Profile icon. | 233 // Profile icon. |
| 231 const SkBitmap& icon = image_view_->GetImage(); | 234 const SkBitmap& icon = image_view_->GetImage(); |
| 232 gfx::Rect icon_rect = GetCenteredAndScaledRect( | 235 gfx::Rect icon_rect = GetCenteredAndScaledRect( |
| 233 icon.width(), icon.height(), 0, 0, kIconWidth, height()); | 236 icon.width(), icon.height(), 0, 0, kIconWidth, height()); |
| 234 image_view_->SetBoundsRect(icon_rect); | 237 image_view_->SetBoundsRect(icon_rect); |
| 235 | 238 |
| 236 int label_x = icon_rect.right() + kIconMarginX; | 239 int label_x = icon_rect.right() + kIconMarginX; |
| 237 int max_label_width = width() - label_x; | 240 int max_label_width = std::max(width() - label_x, 0); |
| 238 gfx::Size name_size = name_label_->GetPreferredSize(); | 241 gfx::Size name_size = name_label_->GetPreferredSize(); |
| 239 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)); |
| 240 gfx::Size state_size = sync_state_label_->GetPreferredSize(); | 243 gfx::Size state_size = sync_state_label_->GetPreferredSize(); |
| 241 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)); |
| 242 gfx::Size edit_size = edit_link_->GetPreferredSize(); | 245 gfx::Size edit_size = edit_link_->GetPreferredSize(); |
| 243 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)); |
| 244 | 247 |
| 245 const int kNameStatePaddingY = 2; | 248 const int kNameStatePaddingY = 2; |
| 246 int labels_height = name_size.height() + kNameStatePaddingY + | 249 int labels_height = name_size.height() + kNameStatePaddingY + |
| 247 std::max(state_size.height(), edit_size.height()); | 250 std::max(state_size.height(), edit_size.height()); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 272 } | 275 } |
| 273 | 276 |
| 274 void ProfileItemView::OnBlur() { | 277 void ProfileItemView::OnBlur() { |
| 275 views::CustomButton::OnBlur(); | 278 views::CustomButton::OnBlur(); |
| 276 OnFocusStateChanged(false); | 279 OnFocusStateChanged(false); |
| 277 } | 280 } |
| 278 | 281 |
| 279 void ProfileItemView::OnHighlightStateChanged() { | 282 void ProfileItemView::OnHighlightStateChanged() { |
| 280 set_background(IsHighlighted() ? views::Background::CreateSolidBackground( | 283 set_background(IsHighlighted() ? views::Background::CreateSolidBackground( |
| 281 SkColorSetRGB(0xe3, 0xed, 0xf6)) : NULL); | 284 SkColorSetRGB(0xe3, 0xed, 0xf6)) : NULL); |
| 282 SkColor background_color = background() ? | |
| 283 background()->get_color() : Bubble::kBackgroundColor; | |
| 284 name_label_->SetBackgroundColor(background_color); | |
| 285 sync_state_label_->SetBackgroundColor(background_color); | |
| 286 edit_link_->SetBackgroundColor(background_color); | |
| 287 | 285 |
| 288 bool show_edit = IsHighlighted() && item_.active; | 286 bool show_edit = IsHighlighted() && item_.active; |
| 289 sync_state_label_->SetVisible(!show_edit); | 287 sync_state_label_->SetVisible(!show_edit); |
| 290 edit_link_->SetVisible(show_edit); | 288 edit_link_->SetVisible(show_edit); |
| 291 SchedulePaint(); | 289 SchedulePaint(); |
| 292 } | 290 } |
| 293 | 291 |
| 294 void ProfileItemView::OnFocusStateChanged(bool has_focus) { | 292 void ProfileItemView::OnFocusStateChanged(bool has_focus) { |
| 295 if (!has_focus && state() != views::CustomButton::BS_DISABLED) | 293 if (!has_focus && state() != views::CustomButton::BS_DISABLED) |
| 296 SetState(views::CustomButton::BS_NORMAL); | 294 SetState(views::CustomButton::BS_NORMAL); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 323 edit_link_->state() == views::CustomButton::BS_HOT || | 321 edit_link_->state() == views::CustomButton::BS_HOT || |
| 324 HasFocus() || | 322 HasFocus() || |
| 325 edit_link_->HasFocus(); | 323 edit_link_->HasFocus(); |
| 326 } | 324 } |
| 327 | 325 |
| 328 } // namespace | 326 } // namespace |
| 329 | 327 |
| 330 | 328 |
| 331 // AvatarMenuBubbleView ------------------------------------------------------- | 329 // AvatarMenuBubbleView ------------------------------------------------------- |
| 332 | 330 |
| 333 AvatarMenuBubbleView::AvatarMenuBubbleView(Browser* browser) | 331 AvatarMenuBubbleView::AvatarMenuBubbleView( |
| 334 : add_profile_link_(NULL), | 332 views::View* anchor_view, |
| 333 views::BubbleBorder::ArrowLocation arrow_location, |
| 334 const gfx::Rect& anchor_rect, |
| 335 Browser* browser) |
| 336 : BubbleDelegateView(anchor_view, arrow_location, kColor), |
| 337 add_profile_link_(NULL), |
| 338 anchor_rect_(anchor_rect), |
| 335 browser_(browser) { | 339 browser_(browser) { |
| 336 avatar_menu_model_.reset(new AvatarMenuModel( | 340 avatar_menu_model_.reset(new AvatarMenuModel( |
| 337 &g_browser_process->profile_manager()->GetProfileInfoCache(), | 341 &g_browser_process->profile_manager()->GetProfileInfoCache(), |
| 338 this, browser_)); | 342 this, browser_)); |
| 339 // Build the menu for the first time. | 343 // Build the menu for the first time. |
| 340 OnAvatarMenuModelChanged(avatar_menu_model_.get()); | 344 OnAvatarMenuModelChanged(avatar_menu_model_.get()); |
| 341 } | 345 } |
| 342 | 346 |
| 343 AvatarMenuBubbleView::~AvatarMenuBubbleView() { | 347 AvatarMenuBubbleView::~AvatarMenuBubbleView() { |
| 344 } | 348 } |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 | 441 |
| 438 for (size_t i = 0; i < item_views_.size(); ++i) { | 442 for (size_t i = 0; i < item_views_.size(); ++i) { |
| 439 ProfileItemView* item_view = static_cast<ProfileItemView*>(item_views_[i]); | 443 ProfileItemView* item_view = static_cast<ProfileItemView*>(item_views_[i]); |
| 440 if (source == item_view->edit_link()) { | 444 if (source == item_view->edit_link()) { |
| 441 avatar_menu_model_->EditProfile(i); | 445 avatar_menu_model_->EditProfile(i); |
| 442 return; | 446 return; |
| 443 } | 447 } |
| 444 } | 448 } |
| 445 } | 449 } |
| 446 | 450 |
| 447 void AvatarMenuBubbleView::BubbleShown() { | 451 gfx::Point AvatarMenuBubbleView::GetAnchorPoint() { |
| 448 GetFocusManager()->RegisterAccelerator( | 452 return gfx::Point(anchor_rect_.CenterPoint().x(), anchor_rect_.bottom()); |
| 449 ui::Accelerator(ui::VKEY_DOWN, false, false, false), this); | |
| 450 GetFocusManager()->RegisterAccelerator( | |
| 451 ui::Accelerator(ui::VKEY_UP, false, false, false), this); | |
| 452 } | 453 } |
| 453 | 454 |
| 454 void AvatarMenuBubbleView::BubbleClosing(Bubble* bubble, | 455 void AvatarMenuBubbleView::Init() { |
| 455 bool closed_by_escape) { | 456 AddAccelerator(ui::Accelerator(ui::VKEY_DOWN, 0)); |
| 456 } | 457 AddAccelerator(ui::Accelerator(ui::VKEY_UP, 0)); |
| 457 | |
| 458 bool AvatarMenuBubbleView::CloseOnEscape() { | |
| 459 return true; | |
| 460 } | |
| 461 | |
| 462 bool AvatarMenuBubbleView::FadeInOnShow() { | |
| 463 return false; | |
| 464 } | 458 } |
| 465 | 459 |
| 466 void AvatarMenuBubbleView::OnAvatarMenuModelChanged( | 460 void AvatarMenuBubbleView::OnAvatarMenuModelChanged( |
| 467 AvatarMenuModel* avatar_menu_model) { | 461 AvatarMenuModel* avatar_menu_model) { |
| 468 // Unset all our child view references and call RemoveAllChildViews() which | 462 // Unset all our child view references and call RemoveAllChildViews() which |
| 469 // will actually delete them. | 463 // will actually delete them. |
| 470 add_profile_link_ = NULL; | 464 add_profile_link_ = NULL; |
| 471 item_views_.clear(); | 465 item_views_.clear(); |
| 472 RemoveAllChildViews(true); | 466 RemoveAllChildViews(true); |
| 473 | 467 |
| 474 for (size_t i = 0; i < avatar_menu_model->GetNumberOfItems(); ++i) { | 468 for (size_t i = 0; i < avatar_menu_model->GetNumberOfItems(); ++i) { |
| 475 const AvatarMenuModel::Item& item = avatar_menu_model->GetItemAt(i); | 469 const AvatarMenuModel::Item& item = avatar_menu_model->GetItemAt(i); |
| 476 ProfileItemView* item_view = new ProfileItemView(item, this, this); | 470 ProfileItemView* item_view = new ProfileItemView(item, this, this); |
| 477 item_view->SetAccessibleName(l10n_util::GetStringFUTF16( | 471 item_view->SetAccessibleName(l10n_util::GetStringFUTF16( |
| 478 IDS_PROFILES_SWITCH_TO_PROFILE_ACCESSIBLE_NAME, item.name)); | 472 IDS_PROFILES_SWITCH_TO_PROFILE_ACCESSIBLE_NAME, item.name)); |
| 479 item_view->set_focusable(true); | 473 item_view->set_focusable(true); |
| 480 AddChildView(item_view); | 474 AddChildView(item_view); |
| 481 item_views_.push_back(item_view); | 475 item_views_.push_back(item_view); |
| 482 } | 476 } |
| 483 | 477 |
| 484 separator_ = new views::Separator(); | 478 separator_ = new views::Separator(); |
| 485 AddChildView(separator_); | 479 AddChildView(separator_); |
| 486 | 480 |
| 487 add_profile_link_ = new views::Link( | 481 add_profile_link_ = new views::Link( |
| 488 l10n_util::GetStringUTF16(IDS_PROFILES_CREATE_NEW_PROFILE_LINK)); | 482 l10n_util::GetStringUTF16(IDS_PROFILES_CREATE_NEW_PROFILE_LINK)); |
| 489 add_profile_link_->set_listener(this); | 483 add_profile_link_->set_listener(this); |
| 490 add_profile_link_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); | 484 add_profile_link_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |
| 491 add_profile_link_->SetBackgroundColor(Bubble::kBackgroundColor); | |
| 492 add_profile_link_->SetEnabledColor(SkColorSetRGB(0xe3, 0xed, 0xf6)); | 485 add_profile_link_->SetEnabledColor(SkColorSetRGB(0xe3, 0xed, 0xf6)); |
| 493 AddChildView(add_profile_link_); | 486 AddChildView(add_profile_link_); |
| 494 | 487 |
| 495 PreferredSizeChanged(); | 488 PreferredSizeChanged(); |
| 496 } | 489 } |
| OLD | NEW |