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