| 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/ui/views/profiles/avatar_menu_bubble_view.h" | 5 #include "chrome/browser/ui/views/profiles/avatar_menu_bubble_view.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 private: | 223 private: |
| 224 gfx::ImageSkia GetBadgedIcon(const gfx::ImageSkia& icon); | 224 gfx::ImageSkia GetBadgedIcon(const gfx::ImageSkia& icon); |
| 225 | 225 |
| 226 bool IsHighlighted(); | 226 bool IsHighlighted(); |
| 227 | 227 |
| 228 AvatarMenu::Item item_; | 228 AvatarMenu::Item item_; |
| 229 AvatarMenuBubbleView* parent_; | 229 AvatarMenuBubbleView* parent_; |
| 230 AvatarMenu* menu_; | 230 AvatarMenu* menu_; |
| 231 views::ImageView* image_view_; | 231 views::ImageView* image_view_; |
| 232 views::Label* name_label_; | 232 views::Label* name_label_; |
| 233 views::Label* sync_state_label_; | 233 views::Label* username_label_; |
| 234 EditProfileLink* edit_link_; | 234 EditProfileLink* edit_link_; |
| 235 | 235 |
| 236 DISALLOW_COPY_AND_ASSIGN(ProfileItemView); | 236 DISALLOW_COPY_AND_ASSIGN(ProfileItemView); |
| 237 }; | 237 }; |
| 238 | 238 |
| 239 ProfileItemView::ProfileItemView(const AvatarMenu::Item& item, | 239 ProfileItemView::ProfileItemView(const AvatarMenu::Item& item, |
| 240 AvatarMenuBubbleView* parent, | 240 AvatarMenuBubbleView* parent, |
| 241 AvatarMenu* menu) | 241 AvatarMenu* menu) |
| 242 : views::CustomButton(parent), | 242 : views::CustomButton(parent), |
| 243 item_(item), | 243 item_(item), |
| (...skipping 17 matching lines...) Expand all Loading... |
| 261 | 261 |
| 262 // Add a label to show the profile name. | 262 // Add a label to show the profile name. |
| 263 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); | 263 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
| 264 name_label_ = new views::Label(item_.name, | 264 name_label_ = new views::Label(item_.name, |
| 265 rb->GetFontList(item_.active ? | 265 rb->GetFontList(item_.active ? |
| 266 ui::ResourceBundle::BoldFont : | 266 ui::ResourceBundle::BoldFont : |
| 267 ui::ResourceBundle::BaseFont)); | 267 ui::ResourceBundle::BaseFont)); |
| 268 name_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 268 name_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 269 AddChildView(name_label_); | 269 AddChildView(name_label_); |
| 270 | 270 |
| 271 // Add a label to show the sync state. | 271 // Add a label to show the username. |
| 272 sync_state_label_ = new views::Label(item_.sync_state); | 272 username_label_ = new views::Label(item_.username); |
| 273 if (item_.signed_in) | 273 if (item_.signed_in) |
| 274 sync_state_label_->SetElideBehavior(gfx::ELIDE_EMAIL); | 274 username_label_->SetElideBehavior(gfx::ELIDE_EMAIL); |
| 275 sync_state_label_->SetFontList( | 275 username_label_->SetFontList( |
| 276 rb->GetFontList(ui::ResourceBundle::SmallFont)); | 276 rb->GetFontList(ui::ResourceBundle::SmallFont)); |
| 277 sync_state_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 277 username_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 278 sync_state_label_->SetEnabled(false); | 278 username_label_->SetEnabled(false); |
| 279 AddChildView(sync_state_label_); | 279 AddChildView(username_label_); |
| 280 | 280 |
| 281 // Add an edit profile link. | 281 // Add an edit profile link. |
| 282 edit_link_ = new EditProfileLink( | 282 edit_link_ = new EditProfileLink( |
| 283 l10n_util::GetStringUTF16(IDS_PROFILES_EDIT_PROFILE_LINK), this); | 283 l10n_util::GetStringUTF16(IDS_PROFILES_EDIT_PROFILE_LINK), this); |
| 284 edit_link_->set_listener(parent); | 284 edit_link_->set_listener(parent); |
| 285 edit_link_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 285 edit_link_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 286 AddChildView(edit_link_); | 286 AddChildView(edit_link_); |
| 287 | 287 |
| 288 OnHighlightStateChanged(); | 288 OnHighlightStateChanged(); |
| 289 } | 289 } |
| 290 | 290 |
| 291 gfx::Size ProfileItemView::GetPreferredSize() const { | 291 gfx::Size ProfileItemView::GetPreferredSize() const { |
| 292 int text_width = std::max(name_label_->GetPreferredSize().width(), | 292 int text_width = std::max(name_label_->GetPreferredSize().width(), |
| 293 sync_state_label_->GetPreferredSize().width()); | 293 username_label_->GetPreferredSize().width()); |
| 294 text_width = std::max(edit_link_->GetPreferredSize().width(), text_width); | 294 text_width = std::max(edit_link_->GetPreferredSize().width(), text_width); |
| 295 text_width = std::min(kMaxItemTextWidth, text_width); | 295 text_width = std::min(kMaxItemTextWidth, text_width); |
| 296 return gfx::Size(profiles::kAvatarIconWidth + kIconMarginX + text_width, | 296 return gfx::Size(profiles::kAvatarIconWidth + kIconMarginX + text_width, |
| 297 kItemHeight); | 297 kItemHeight); |
| 298 } | 298 } |
| 299 | 299 |
| 300 void ProfileItemView::Layout() { | 300 void ProfileItemView::Layout() { |
| 301 // Profile icon. | 301 // Profile icon. |
| 302 gfx::Rect icon_rect; | 302 gfx::Rect icon_rect; |
| 303 if (item_.active) { | 303 if (item_.active) { |
| 304 // If this is the active item then the icon is already scaled and so | 304 // If this is the active item then the icon is already scaled and so |
| 305 // just use the preferred size. | 305 // just use the preferred size. |
| 306 icon_rect.set_size(image_view_->GetPreferredSize()); | 306 icon_rect.set_size(image_view_->GetPreferredSize()); |
| 307 icon_rect.set_y((height() - icon_rect.height()) / 2); | 307 icon_rect.set_y((height() - icon_rect.height()) / 2); |
| 308 } else { | 308 } else { |
| 309 const gfx::ImageSkia& icon = image_view_->GetImage(); | 309 const gfx::ImageSkia& icon = image_view_->GetImage(); |
| 310 icon_rect = GetCenteredAndScaledRect(icon.width(), icon.height(), 0, 0, | 310 icon_rect = GetCenteredAndScaledRect(icon.width(), icon.height(), 0, 0, |
| 311 profiles::kAvatarIconWidth, height()); | 311 profiles::kAvatarIconWidth, height()); |
| 312 } | 312 } |
| 313 image_view_->SetBoundsRect(icon_rect); | 313 image_view_->SetBoundsRect(icon_rect); |
| 314 | 314 |
| 315 int label_x = profiles::kAvatarIconWidth + kIconMarginX; | 315 int label_x = profiles::kAvatarIconWidth + kIconMarginX; |
| 316 int max_label_width = std::max(width() - label_x, 0); | 316 int max_label_width = std::max(width() - label_x, 0); |
| 317 gfx::Size name_size = name_label_->GetPreferredSize(); | 317 gfx::Size name_size = name_label_->GetPreferredSize(); |
| 318 name_size.set_width(std::min(name_size.width(), max_label_width)); | 318 name_size.set_width(std::min(name_size.width(), max_label_width)); |
| 319 gfx::Size state_size = sync_state_label_->GetPreferredSize(); | 319 gfx::Size state_size = username_label_->GetPreferredSize(); |
| 320 state_size.set_width(std::min(state_size.width(), max_label_width)); | 320 state_size.set_width(std::min(state_size.width(), max_label_width)); |
| 321 gfx::Size edit_size = edit_link_->GetPreferredSize(); | 321 gfx::Size edit_size = edit_link_->GetPreferredSize(); |
| 322 edit_size.set_width(std::min(edit_size.width(), max_label_width)); | 322 edit_size.set_width(std::min(edit_size.width(), max_label_width)); |
| 323 | 323 |
| 324 const int kNameStatePaddingY = 2; | 324 const int kNameStatePaddingY = 2; |
| 325 int labels_height = name_size.height() + kNameStatePaddingY + | 325 int labels_height = name_size.height() + kNameStatePaddingY + |
| 326 std::max(state_size.height(), edit_size.height()); | 326 std::max(state_size.height(), edit_size.height()); |
| 327 int y = (height() - labels_height) / 2; | 327 int y = (height() - labels_height) / 2; |
| 328 name_label_->SetBounds(label_x, y, name_size.width(), name_size.height()); | 328 name_label_->SetBounds(label_x, y, name_size.width(), name_size.height()); |
| 329 | 329 |
| 330 int bottom = y + labels_height; | 330 int bottom = y + labels_height; |
| 331 sync_state_label_->SetBounds(label_x, bottom - state_size.height(), | 331 username_label_->SetBounds(label_x, bottom - state_size.height(), |
| 332 state_size.width(), state_size.height()); | 332 state_size.width(), state_size.height()); |
| 333 // The edit link overlaps the sync state label. | 333 // The edit link overlaps the sync state label. |
| 334 edit_link_->SetBounds(label_x, bottom - edit_size.height(), | 334 edit_link_->SetBounds(label_x, bottom - edit_size.height(), |
| 335 edit_size.width(), edit_size.height()); | 335 edit_size.width(), edit_size.height()); |
| 336 } | 336 } |
| 337 | 337 |
| 338 void ProfileItemView::OnMouseEntered(const ui::MouseEvent& event) { | 338 void ProfileItemView::OnMouseEntered(const ui::MouseEvent& event) { |
| 339 views::CustomButton::OnMouseEntered(event); | 339 views::CustomButton::OnMouseEntered(event); |
| 340 OnHighlightStateChanged(); | 340 OnHighlightStateChanged(); |
| 341 } | 341 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 352 | 352 |
| 353 void ProfileItemView::OnBlur() { | 353 void ProfileItemView::OnBlur() { |
| 354 views::CustomButton::OnBlur(); | 354 views::CustomButton::OnBlur(); |
| 355 OnFocusStateChanged(false); | 355 OnFocusStateChanged(false); |
| 356 } | 356 } |
| 357 | 357 |
| 358 void ProfileItemView::OnHighlightStateChanged() { | 358 void ProfileItemView::OnHighlightStateChanged() { |
| 359 const SkColor color = IsHighlighted() ? kHighlightColor : parent_->color(); | 359 const SkColor color = IsHighlighted() ? kHighlightColor : parent_->color(); |
| 360 set_background(views::Background::CreateSolidBackground(color)); | 360 set_background(views::Background::CreateSolidBackground(color)); |
| 361 name_label_->SetBackgroundColor(color); | 361 name_label_->SetBackgroundColor(color); |
| 362 sync_state_label_->SetBackgroundColor(color); | 362 username_label_->SetBackgroundColor(color); |
| 363 edit_link_->SetBackgroundColor(color); | 363 edit_link_->SetBackgroundColor(color); |
| 364 | 364 |
| 365 bool show_edit = IsHighlighted() && item_.active && | 365 bool show_edit = IsHighlighted() && item_.active && |
| 366 menu_->ShouldShowEditProfileLink(); | 366 menu_->ShouldShowEditProfileLink(); |
| 367 sync_state_label_->SetVisible(!show_edit); | 367 username_label_->SetVisible(!show_edit); |
| 368 edit_link_->SetVisible(show_edit); | 368 edit_link_->SetVisible(show_edit); |
| 369 SchedulePaint(); | 369 SchedulePaint(); |
| 370 } | 370 } |
| 371 | 371 |
| 372 void ProfileItemView::OnFocusStateChanged(bool has_focus) { | 372 void ProfileItemView::OnFocusStateChanged(bool has_focus) { |
| 373 if (!has_focus && state() != views::CustomButton::STATE_DISABLED) | 373 if (!has_focus && state() != views::CustomButton::STATE_DISABLED) |
| 374 SetState(views::CustomButton::STATE_NORMAL); | 374 SetState(views::CustomButton::STATE_NORMAL); |
| 375 OnHighlightStateChanged(); | 375 OnHighlightStateChanged(); |
| 376 } | 376 } |
| 377 | 377 |
| (...skipping 419 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 797 Layout(); | 797 Layout(); |
| 798 if (GetBubbleFrameView()) | 798 if (GetBubbleFrameView()) |
| 799 SizeToContents(); | 799 SizeToContents(); |
| 800 } | 800 } |
| 801 | 801 |
| 802 void AvatarMenuBubbleView::SetBackgroundColors() { | 802 void AvatarMenuBubbleView::SetBackgroundColors() { |
| 803 for (size_t i = 0; i < item_views_.size(); ++i) { | 803 for (size_t i = 0; i < item_views_.size(); ++i) { |
| 804 item_views_[i]->OnHighlightStateChanged(); | 804 item_views_[i]->OnHighlightStateChanged(); |
| 805 } | 805 } |
| 806 } | 806 } |
| OLD | NEW |