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/passwords/credentials_item_view.h" | 5 #include "chrome/browser/ui/views/passwords/credentials_item_view.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/ui/passwords/manage_passwords_view_utils.h" | 9 #include "chrome/browser/ui/passwords/manage_passwords_view_utils.h" |
10 #include "chrome/grit/generated_resources.h" | 10 #include "chrome/grit/generated_resources.h" |
11 #include "components/autofill/core/common/password_form.h" | 11 #include "components/autofill/core/common/password_form.h" |
12 #include "grit/theme_resources.h" | 12 #include "grit/theme_resources.h" |
13 #include "ui/base/l10n/l10n_util.h" | 13 #include "ui/base/l10n/l10n_util.h" |
14 #include "ui/base/resource/resource_bundle.h" | 14 #include "ui/base/resource/resource_bundle.h" |
15 #include "ui/gfx/canvas.h" | 15 #include "ui/gfx/canvas.h" |
16 #include "ui/gfx/image/image.h" | 16 #include "ui/gfx/image/image.h" |
17 #include "ui/gfx/path.h" | 17 #include "ui/gfx/path.h" |
18 #include "ui/views/border.h" | 18 #include "ui/views/border.h" |
19 #include "ui/views/controls/image_view.h" | 19 #include "ui/views/controls/image_view.h" |
20 #include "ui/views/controls/label.h" | 20 #include "ui/views/controls/label.h" |
| 21 #include "ui/views/layout/layout_constants.h" |
21 | 22 |
22 namespace { | 23 namespace { |
23 // The default spacing between the icon and text. | 24 // The default spacing between the icon and text. |
24 const int kSpacing = 5; | 25 const int kSpacing = 12; |
25 | 26 |
26 gfx::Size GetTextLabelsSize(const views::Label* upper_label, | 27 gfx::Size GetTextLabelsSize(const views::Label* upper_label, |
27 const views::Label* lower_label) { | 28 const views::Label* lower_label) { |
28 gfx::Size upper_label_size = upper_label ? upper_label->GetPreferredSize() | 29 gfx::Size upper_label_size = upper_label ? upper_label->GetPreferredSize() |
29 : gfx::Size(); | 30 : gfx::Size(); |
30 gfx::Size lower_label_size = lower_label ? lower_label->GetPreferredSize() | 31 gfx::Size lower_label_size = lower_label ? lower_label->GetPreferredSize() |
31 : gfx::Size(); | 32 : gfx::Size(); |
32 return gfx::Size(std::max(upper_label_size.width(), lower_label_size.width()), | 33 return gfx::Size(std::max(upper_label_size.width(), lower_label_size.width()), |
33 upper_label_size.height() + lower_label_size.height()); | 34 upper_label_size.height() + lower_label_size.height()); |
34 } | 35 } |
(...skipping 18 matching lines...) Expand all Loading... |
53 SkIntToScalar(bounds.y() + bounds.bottom()) / 2, | 54 SkIntToScalar(bounds.y() + bounds.bottom()) / 2, |
54 SkIntToScalar(std::min(bounds.height(), bounds.width())) / 2); | 55 SkIntToScalar(std::min(bounds.height(), bounds.width())) / 2); |
55 canvas->ClipPath(circular_mask, true); | 56 canvas->ClipPath(circular_mask, true); |
56 ImageView::OnPaint(canvas); | 57 ImageView::OnPaint(canvas); |
57 } | 58 } |
58 | 59 |
59 } // namespace | 60 } // namespace |
60 | 61 |
61 CredentialsItemView::CredentialsItemView( | 62 CredentialsItemView::CredentialsItemView( |
62 views::ButtonListener* button_listener, | 63 views::ButtonListener* button_listener, |
63 const autofill::PasswordForm* form, | |
64 password_manager::CredentialType credential_type, | |
65 const base::string16& upper_text, | 64 const base::string16& upper_text, |
66 const base::string16& lower_text, | 65 const base::string16& lower_text, |
| 66 SkColor hover_color, |
| 67 const autofill::PasswordForm* form, |
67 net::URLRequestContextGetter* request_context) | 68 net::URLRequestContextGetter* request_context) |
68 : LabelButton(button_listener, base::string16()), | 69 : LabelButton(button_listener, base::string16()), |
69 form_(form), | 70 form_(form), |
70 credential_type_(credential_type), | |
71 upper_label_(nullptr), | 71 upper_label_(nullptr), |
72 lower_label_(nullptr), | 72 lower_label_(nullptr), |
| 73 hover_color_(hover_color), |
73 weak_ptr_factory_(this) { | 74 weak_ptr_factory_(this) { |
74 set_notify_enter_exit_on_child(true); | 75 set_notify_enter_exit_on_child(true); |
75 // Create an image-view for the avatar. Make sure it ignores events so that | 76 // Create an image-view for the avatar. Make sure it ignores events so that |
76 // the parent can receive the events instead. | 77 // the parent can receive the events instead. |
77 image_view_ = new CircularImageView; | 78 image_view_ = new CircularImageView; |
78 image_view_->set_interactive(false); | 79 image_view_->set_interactive(false); |
79 gfx::Image image = ResourceBundle::GetSharedInstance().GetImageNamed( | 80 gfx::Image image = ResourceBundle::GetSharedInstance().GetImageNamed( |
80 IDR_PROFILE_AVATAR_PLACEHOLDER_LARGE); | 81 IDR_PROFILE_AVATAR_PLACEHOLDER_LARGE); |
81 DCHECK(image.Width() >= kAvatarImageSize && | 82 DCHECK(image.Width() >= kAvatarImageSize && |
82 image.Height() >= kAvatarImageSize); | 83 image.Height() >= kAvatarImageSize); |
83 UpdateAvatar(image.AsImageSkia()); | 84 UpdateAvatar(image.AsImageSkia()); |
84 if (form_->icon_url.is_valid()) { | 85 if (form_->icon_url.is_valid()) { |
85 // Fetch the actual avatar. | 86 // Fetch the actual avatar. |
86 AccountAvatarFetcher* fetcher = new AccountAvatarFetcher( | 87 AccountAvatarFetcher* fetcher = new AccountAvatarFetcher( |
87 form_->icon_url, weak_ptr_factory_.GetWeakPtr()); | 88 form_->icon_url, weak_ptr_factory_.GetWeakPtr()); |
88 fetcher->Start(request_context); | 89 fetcher->Start(request_context); |
89 } | 90 } |
90 AddChildView(image_view_); | 91 AddChildView(image_view_); |
91 | 92 |
92 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); | 93 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
93 if (!upper_text.empty()) { | 94 if (!upper_text.empty()) { |
94 upper_label_ = new views::Label( | 95 upper_label_ = new views::Label( |
95 upper_text, rb->GetFontList(ui::ResourceBundle::BoldFont)); | 96 upper_text, rb->GetFontList(ui::ResourceBundle::SmallFont)); |
96 upper_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 97 upper_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
97 AddChildView(upper_label_); | 98 AddChildView(upper_label_); |
98 } | 99 } |
99 | 100 |
100 if (!lower_text.empty()) { | 101 if (!lower_text.empty()) { |
101 lower_label_ = new views::Label( | 102 lower_label_ = new views::Label( |
102 lower_text, rb->GetFontList(ui::ResourceBundle::SmallFont)); | 103 lower_text, rb->GetFontList(ui::ResourceBundle::SmallFont)); |
103 lower_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 104 lower_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
104 AddChildView(lower_label_); | 105 AddChildView(lower_label_); |
105 } | 106 } |
106 | 107 |
107 SetFocusable(true); | 108 SetFocusable(true); |
108 } | 109 } |
109 | 110 |
110 CredentialsItemView::~CredentialsItemView() = default; | 111 CredentialsItemView::~CredentialsItemView() = default; |
111 | 112 |
| 113 void CredentialsItemView::UpdateAvatar(const gfx::ImageSkia& image) { |
| 114 image_view_->SetImage(ScaleImageForAccountAvatar(image)); |
| 115 } |
| 116 |
| 117 void CredentialsItemView::SetLowerLabelColor(SkColor color) { |
| 118 if (lower_label_) |
| 119 lower_label_->SetEnabledColor(color); |
| 120 } |
| 121 |
| 122 void CredentialsItemView::SetHoverColor(SkColor color) { |
| 123 hover_color_ = color; |
| 124 } |
| 125 |
112 gfx::Size CredentialsItemView::GetPreferredSize() const { | 126 gfx::Size CredentialsItemView::GetPreferredSize() const { |
113 gfx::Size labels_size = GetTextLabelsSize(upper_label_, lower_label_); | 127 gfx::Size labels_size = GetTextLabelsSize(upper_label_, lower_label_); |
114 gfx::Size size = gfx::Size(kAvatarImageSize + labels_size.width(), | 128 gfx::Size size = gfx::Size(kAvatarImageSize + labels_size.width(), |
115 std::max(kAvatarImageSize, labels_size.height())); | 129 std::max(kAvatarImageSize, labels_size.height())); |
116 const gfx::Insets insets(GetInsets()); | 130 const gfx::Insets insets(GetInsets()); |
117 size.Enlarge(insets.width(), insets.height()); | 131 size.Enlarge(insets.width(), insets.height()); |
118 size.Enlarge(kSpacing, 0); | 132 size.Enlarge(kSpacing, 0); |
119 | 133 |
120 // Make the size at least as large as the minimum size needed by the border. | 134 // Make the size at least as large as the minimum size needed by the border. |
121 size.SetToMax(border() ? border()->GetMinimumSize() : gfx::Size()); | 135 size.SetToMax(border() ? border()->GetMinimumSize() : gfx::Size()); |
(...skipping 23 matching lines...) Expand all Loading... |
145 gfx::Point label_origin(image_origin.x() + image_size.width() + kSpacing, | 159 gfx::Point label_origin(image_origin.x() + image_size.width() + kSpacing, |
146 child_area.origin().y() + y_offset); | 160 child_area.origin().y() + y_offset); |
147 if (upper_label_) | 161 if (upper_label_) |
148 upper_label_->SetBoundsRect(gfx::Rect(label_origin, upper_size)); | 162 upper_label_->SetBoundsRect(gfx::Rect(label_origin, upper_size)); |
149 if (lower_label_) { | 163 if (lower_label_) { |
150 label_origin.Offset(0, upper_size.height()); | 164 label_origin.Offset(0, upper_size.height()); |
151 lower_label_->SetBoundsRect(gfx::Rect(label_origin, lower_size)); | 165 lower_label_->SetBoundsRect(gfx::Rect(label_origin, lower_size)); |
152 } | 166 } |
153 } | 167 } |
154 | 168 |
155 void CredentialsItemView::UpdateAvatar(const gfx::ImageSkia& image) { | 169 void CredentialsItemView::OnPaint(gfx::Canvas* canvas) { |
156 image_view_->SetImage(ScaleImageForAccountAvatar(image)); | 170 if (state() == STATE_PRESSED || state() == STATE_HOVERED) |
| 171 canvas->DrawColor(hover_color_); |
| 172 |
| 173 LabelButton::OnPaint(canvas); |
157 } | 174 } |
OLD | NEW |