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 "chrome/browser/bitmap_fetcher/bitmap_fetcher.h" | 7 #include "chrome/browser/ui/passwords/manage_passwords_view_utils.h" |
8 #include "grit/theme_resources.h" | 8 #include "grit/theme_resources.h" |
9 #include "net/base/load_flags.h" | |
10 #include "ui/base/resource/resource_bundle.h" | 9 #include "ui/base/resource/resource_bundle.h" |
11 #include "ui/gfx/canvas.h" | 10 #include "ui/gfx/canvas.h" |
12 #include "ui/gfx/image/image.h" | 11 #include "ui/gfx/image/image.h" |
13 #include "ui/gfx/image/image_skia_operations.h" | |
14 #include "ui/gfx/path.h" | 12 #include "ui/gfx/path.h" |
15 #include "ui/views/border.h" | 13 #include "ui/views/border.h" |
16 #include "ui/views/controls/image_view.h" | 14 #include "ui/views/controls/image_view.h" |
17 #include "ui/views/controls/label.h" | 15 #include "ui/views/controls/label.h" |
18 | 16 |
19 namespace { | 17 namespace { |
20 const int kIconSize = 50; | |
21 // The default spacing between the icon and text. | 18 // The default spacing between the icon and text. |
22 const int kSpacing = 5; | 19 const int kSpacing = 5; |
23 | 20 |
24 gfx::Size GetTextLabelsSize(const views::Label* full_name_label, | 21 gfx::Size GetTextLabelsSize(const views::Label* full_name_label, |
25 const views::Label* username_label) { | 22 const views::Label* username_label) { |
26 gfx::Size full_name = full_name_label->GetPreferredSize(); | 23 gfx::Size full_name = full_name_label->GetPreferredSize(); |
27 gfx::Size username = username_label ? username_label->GetPreferredSize() : | 24 gfx::Size username = username_label ? username_label->GetPreferredSize() : |
28 gfx::Size(); | 25 gfx::Size(); |
29 return gfx::Size(std::max(full_name.width(), username.width()), | 26 return gfx::Size(std::max(full_name.width(), username.width()), |
30 full_name.height() + username.height()); | 27 full_name.height() + username.height()); |
31 } | 28 } |
32 | 29 |
33 // Crops and scales |image| to kIconSize | |
34 gfx::ImageSkia ScaleImage(gfx::ImageSkia skia_image) { | |
35 gfx::Size size = skia_image.size(); | |
36 if (size.height() != size.width()) { | |
37 gfx::Rect target(size); | |
38 int side = std::min(size.height(), size.width()); | |
39 target.ClampToCenteredSize(gfx::Size(side, side)); | |
40 skia_image = gfx::ImageSkiaOperations::ExtractSubset(skia_image, target); | |
41 } | |
42 return gfx::ImageSkiaOperations::CreateResizedImage( | |
43 skia_image, | |
44 skia::ImageOperations::RESIZE_BEST, | |
45 gfx::Size(kIconSize, kIconSize)); | |
46 } | |
47 | |
48 class CircularImageView : public views::ImageView { | 30 class CircularImageView : public views::ImageView { |
49 public: | 31 public: |
50 CircularImageView() = default; | 32 CircularImageView() = default; |
51 | 33 |
52 private: | 34 private: |
53 // views::ImageView: | 35 // views::ImageView: |
54 void OnPaint(gfx::Canvas* canvas) override; | 36 void OnPaint(gfx::Canvas* canvas) override; |
55 | 37 |
56 DISALLOW_COPY_AND_ASSIGN(CircularImageView); | 38 DISALLOW_COPY_AND_ASSIGN(CircularImageView); |
57 }; | 39 }; |
58 | 40 |
59 void CircularImageView::OnPaint(gfx::Canvas* canvas) { | 41 void CircularImageView::OnPaint(gfx::Canvas* canvas) { |
60 // Display the avatar picture as a circle. | 42 // Display the avatar picture as a circle. |
61 gfx::Rect bounds(GetImageBounds()); | 43 gfx::Rect bounds(GetImageBounds()); |
62 gfx::Path circular_mask; | 44 gfx::Path circular_mask; |
63 circular_mask.addCircle( | 45 circular_mask.addCircle( |
64 SkIntToScalar(bounds.x() + bounds.right()) / 2, | 46 SkIntToScalar(bounds.x() + bounds.right()) / 2, |
65 SkIntToScalar(bounds.y() + bounds.bottom()) / 2, | 47 SkIntToScalar(bounds.y() + bounds.bottom()) / 2, |
66 SkIntToScalar(std::min(bounds.height(), bounds.width())) / 2); | 48 SkIntToScalar(std::min(bounds.height(), bounds.width())) / 2); |
67 canvas->ClipPath(circular_mask, true); | 49 canvas->ClipPath(circular_mask, true); |
68 ImageView::OnPaint(canvas); | 50 ImageView::OnPaint(canvas); |
69 } | 51 } |
70 | 52 |
71 } // namespace | 53 } // namespace |
72 | 54 |
73 // Helper class to download the avatar. It deletes itself once the request is | |
74 // done. | |
75 class CredentialsItemView::AvatarFetcher | |
76 : public chrome::BitmapFetcherDelegate { | |
77 public: | |
78 AvatarFetcher(const GURL& url, | |
79 const base::WeakPtr<CredentialsItemView>& delegate); | |
80 | |
81 void Start(net::URLRequestContextGetter* request_context); | |
82 | |
83 private: | |
84 ~AvatarFetcher() override; | |
85 | |
86 // chrome::BitmapFetcherDelegate: | |
87 void OnFetchComplete(const GURL url, const SkBitmap* bitmap) override; | |
88 | |
89 chrome::BitmapFetcher fetcher_; | |
90 base::WeakPtr<CredentialsItemView> delegate_; | |
91 | |
92 DISALLOW_COPY_AND_ASSIGN(AvatarFetcher); | |
93 }; | |
94 | |
95 CredentialsItemView::AvatarFetcher::AvatarFetcher( | |
96 const GURL& url, | |
97 const base::WeakPtr<CredentialsItemView>& delegate) | |
98 : fetcher_(url, this), | |
99 delegate_(delegate) { | |
100 } | |
101 | |
102 CredentialsItemView::AvatarFetcher::~AvatarFetcher() = default; | |
103 | |
104 void CredentialsItemView::AvatarFetcher::Start( | |
105 net::URLRequestContextGetter* request_context) { | |
106 fetcher_.Start(request_context, std::string(), | |
107 net::URLRequest::NEVER_CLEAR_REFERRER, | |
108 net::LOAD_DO_NOT_SEND_COOKIES | | |
109 net::LOAD_DO_NOT_SAVE_COOKIES | | |
110 net::LOAD_MAYBE_USER_GESTURE); | |
111 } | |
112 | |
113 void CredentialsItemView::AvatarFetcher::OnFetchComplete( | |
114 const GURL /*url*/, const SkBitmap* bitmap) { | |
115 if (bitmap && delegate_) | |
116 delegate_->UpdateAvatar(gfx::ImageSkia::CreateFrom1xBitmap(*bitmap)); | |
117 | |
118 delete this; | |
119 } | |
120 | |
121 CredentialsItemView::CredentialsItemView( | 55 CredentialsItemView::CredentialsItemView( |
122 views::ButtonListener* button_listener, | 56 views::ButtonListener* button_listener, |
123 const autofill::PasswordForm& form, | 57 const autofill::PasswordForm& form, |
124 password_manager::CredentialType credential_type, | 58 password_manager::CredentialType credential_type, |
125 net::URLRequestContextGetter* request_context) | 59 net::URLRequestContextGetter* request_context) |
126 : LabelButton(button_listener, base::string16()), | 60 : LabelButton(button_listener, base::string16()), |
127 form_(form), | 61 form_(form), |
128 credential_type_(credential_type), | 62 credential_type_(credential_type), |
129 weak_ptr_factory_(this) { | 63 weak_ptr_factory_(this) { |
130 set_notify_enter_exit_on_child(true); | 64 set_notify_enter_exit_on_child(true); |
131 // Create an image-view for the avatar. Make sure it ignores events so that | 65 // Create an image-view for the avatar. Make sure it ignores events so that |
132 // the parent can receive the events instead. | 66 // the parent can receive the events instead. |
133 image_view_ = new CircularImageView; | 67 image_view_ = new CircularImageView; |
134 image_view_->set_interactive(false); | 68 image_view_->set_interactive(false); |
135 gfx::Image image = ResourceBundle::GetSharedInstance().GetImageNamed( | 69 gfx::Image image = ResourceBundle::GetSharedInstance().GetImageNamed( |
136 IDR_PROFILE_AVATAR_PLACEHOLDER_LARGE); | 70 IDR_PROFILE_AVATAR_PLACEHOLDER_LARGE); |
137 DCHECK(image.Width() >= kIconSize && image.Height() >= kIconSize); | 71 DCHECK(image.Width() >= kAvatarImageSize && |
| 72 image.Height() >= kAvatarImageSize); |
138 UpdateAvatar(image.AsImageSkia()); | 73 UpdateAvatar(image.AsImageSkia()); |
139 if (form_.avatar_url.is_valid()) { | 74 if (form_.avatar_url.is_valid()) { |
140 // Fetch the actual avatar. | 75 // Fetch the actual avatar. |
141 AvatarFetcher* fetcher = new AvatarFetcher(form_.avatar_url, | 76 AccountAvatarFetcher* fetcher = new AccountAvatarFetcher( |
142 weak_ptr_factory_.GetWeakPtr()); | 77 form_.avatar_url, weak_ptr_factory_.GetWeakPtr()); |
143 fetcher->Start(request_context); | 78 fetcher->Start(request_context); |
144 } | 79 } |
145 AddChildView(image_view_); | 80 AddChildView(image_view_); |
146 | 81 |
147 // Add a label to show the full name. | 82 // Add a label to show the full name. |
148 // TODO(vasilii): temporarily the label shows username. | 83 // TODO(vasilii): temporarily the label shows username. |
149 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); | 84 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); |
150 full_name_label_ = new views::Label( | 85 full_name_label_ = new views::Label( |
151 form_.username_value, rb->GetFontList(ui::ResourceBundle::BoldFont)); | 86 form_.username_value, rb->GetFontList(ui::ResourceBundle::BoldFont)); |
152 full_name_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 87 full_name_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
153 AddChildView(full_name_label_); | 88 AddChildView(full_name_label_); |
154 | 89 |
155 // Add a label to show the user name. | 90 // Add a label to show the user name. |
156 username_label_ = new views::Label( | 91 username_label_ = new views::Label( |
157 form_.username_value, rb->GetFontList(ui::ResourceBundle::SmallFont)); | 92 form_.username_value, rb->GetFontList(ui::ResourceBundle::SmallFont)); |
158 username_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 93 username_label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
159 username_label_->SetEnabled(false); | 94 username_label_->SetEnabled(false); |
160 AddChildView(username_label_); | 95 AddChildView(username_label_); |
161 | 96 |
162 SetFocusable(true); | 97 SetFocusable(true); |
163 } | 98 } |
164 | 99 |
165 CredentialsItemView::~CredentialsItemView() = default; | 100 CredentialsItemView::~CredentialsItemView() = default; |
166 | 101 |
167 gfx::Size CredentialsItemView::GetPreferredSize() const { | 102 gfx::Size CredentialsItemView::GetPreferredSize() const { |
168 gfx::Size labels_size = GetTextLabelsSize(full_name_label_, username_label_); | 103 gfx::Size labels_size = GetTextLabelsSize(full_name_label_, username_label_); |
169 gfx::Size size = gfx::Size(kIconSize + labels_size.width(), | 104 gfx::Size size = gfx::Size(kAvatarImageSize + labels_size.width(), |
170 std::max(kIconSize, labels_size.height())); | 105 std::max(kAvatarImageSize, labels_size.height())); |
171 const gfx::Insets insets(GetInsets()); | 106 const gfx::Insets insets(GetInsets()); |
172 size.Enlarge(insets.width(), insets.height()); | 107 size.Enlarge(insets.width(), insets.height()); |
173 size.Enlarge(kSpacing, 0); | 108 size.Enlarge(kSpacing, 0); |
174 | 109 |
175 // Make the size at least as large as the minimum size needed by the border. | 110 // Make the size at least as large as the minimum size needed by the border. |
176 size.SetToMax(border() ? border()->GetMinimumSize() : gfx::Size()); | 111 size.SetToMax(border() ? border()->GetMinimumSize() : gfx::Size()); |
177 return size; | 112 return size; |
178 } | 113 } |
179 | 114 |
180 int CredentialsItemView::GetHeightForWidth(int w) const { | 115 int CredentialsItemView::GetHeightForWidth(int w) const { |
(...skipping 18 matching lines...) Expand all Loading... |
199 gfx::Point label_origin(image_origin.x() + image_size.width() + kSpacing, | 134 gfx::Point label_origin(image_origin.x() + image_size.width() + kSpacing, |
200 child_area.origin().y() + y_offset); | 135 child_area.origin().y() + y_offset); |
201 full_name_label_->SetBoundsRect(gfx::Rect(label_origin, full_name_size)); | 136 full_name_label_->SetBoundsRect(gfx::Rect(label_origin, full_name_size)); |
202 if (username_label_) { | 137 if (username_label_) { |
203 label_origin.Offset(0, full_name_size.height()); | 138 label_origin.Offset(0, full_name_size.height()); |
204 username_label_->SetBoundsRect(gfx::Rect(label_origin, username_size)); | 139 username_label_->SetBoundsRect(gfx::Rect(label_origin, username_size)); |
205 } | 140 } |
206 } | 141 } |
207 | 142 |
208 void CredentialsItemView::UpdateAvatar(const gfx::ImageSkia& image) { | 143 void CredentialsItemView::UpdateAvatar(const gfx::ImageSkia& image) { |
209 image_view_->SetImage(ScaleImage(image)); | 144 image_view_->SetImage(ScaleImageForAccountAvatar(image)); |
210 } | 145 } |
OLD | NEW |