OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ash/system/user/tray_user.h" | 5 #include "ash/system/user/tray_user.h" |
6 | 6 |
7 #include <algorithm> | |
8 #include <climits> | |
9 #include <vector> | |
10 | |
7 #include "ash/shell.h" | 11 #include "ash/shell.h" |
12 #include "ash/system/tray/system_tray.h" | |
8 #include "ash/system/tray/system_tray_delegate.h" | 13 #include "ash/system/tray/system_tray_delegate.h" |
9 #include "ash/system/tray/tray_constants.h" | 14 #include "ash/system/tray/tray_constants.h" |
10 #include "ash/system/tray/tray_item_view.h" | 15 #include "ash/system/tray/tray_item_view.h" |
11 #include "ash/system/tray/tray_views.h" | 16 #include "ash/system/tray/tray_views.h" |
17 #include "base/i18n/rtl.h" | |
18 #include "base/logging.h" | |
19 #include "base/memory/scoped_vector.h" | |
20 #include "base/string16.h" | |
21 #include "base/string_util.h" | |
12 #include "base/utf_string_conversions.h" | 22 #include "base/utf_string_conversions.h" |
23 #include "grit/ash_resources.h" | |
13 #include "grit/ash_strings.h" | 24 #include "grit/ash_strings.h" |
14 #include "skia/ext/image_operations.h" | 25 #include "skia/ext/image_operations.h" |
15 #include "third_party/skia/include/core/SkCanvas.h" | 26 #include "third_party/skia/include/core/SkCanvas.h" |
16 #include "third_party/skia/include/core/SkPaint.h" | 27 #include "third_party/skia/include/core/SkPaint.h" |
17 #include "third_party/skia/include/core/SkPath.h" | 28 #include "third_party/skia/include/core/SkPath.h" |
29 #include "ui/base/l10n/l10n_util.h" | |
30 #include "ui/base/range/range.h" | |
18 #include "ui/base/resource/resource_bundle.h" | 31 #include "ui/base/resource/resource_bundle.h" |
32 #include "ui/base/text/text_elider.h" | |
19 #include "ui/gfx/canvas.h" | 33 #include "ui/gfx/canvas.h" |
34 #include "ui/gfx/font.h" | |
20 #include "ui/gfx/image/image.h" | 35 #include "ui/gfx/image/image.h" |
21 #include "ui/gfx/image/image_skia_operations.h" | 36 #include "ui/gfx/image/image_skia_operations.h" |
37 #include "ui/gfx/insets.h" | |
38 #include "ui/gfx/rect.h" | |
39 #include "ui/gfx/render_text.h" | |
22 #include "ui/gfx/size.h" | 40 #include "ui/gfx/size.h" |
23 #include "ui/gfx/skia_util.h" | 41 #include "ui/gfx/skia_util.h" |
42 #include "ui/views/border.h" | |
43 #include "ui/views/bubble/tray_bubble_view.h" | |
44 #include "ui/views/controls/button/border_images.h" | |
24 #include "ui/views/controls/button/button.h" | 45 #include "ui/views/controls/button/button.h" |
25 #include "ui/views/controls/button/text_button.h" | 46 #include "ui/views/controls/button/custom_button.h" |
26 #include "ui/views/controls/image_view.h" | 47 #include "ui/views/controls/image_view.h" |
27 #include "ui/views/controls/label.h" | 48 #include "ui/views/controls/label.h" |
49 #include "ui/views/controls/link.h" | |
50 #include "ui/views/controls/link_listener.h" | |
28 #include "ui/views/layout/box_layout.h" | 51 #include "ui/views/layout/box_layout.h" |
29 #include "ui/views/view.h" | 52 #include "ui/views/view.h" |
30 #include "ui/views/widget/widget.h" | 53 #include "ui/views/widget/widget.h" |
31 | 54 |
32 namespace { | 55 namespace { |
33 | 56 |
34 const int kUserInfoVerticalPadding = 10; | 57 const int kUserDetailsVerticalPadding = 5; |
58 const int kUserCardVerticalPadding = 10; | |
59 const int kProfileRoundedCornerRadius = 2; | |
35 const int kUserIconSize = 27; | 60 const int kUserIconSize = 27; |
36 const int kProfileRoundedCornerRadius = 2; | 61 |
62 // The invisible word joiner character, used as a marker to indicate the start | |
63 // and end of the user's display name in the public account user card's text. | |
64 const char16 kDisplayNameMark[] = { 0x2060 }; | |
65 | |
66 const int kPublicAccountLogoutButtonBorderImagesNormal[] = { | |
67 IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER, | |
68 IDR_AURA_TRAY_POPUP_LABEL_BUTTON_NORMAL_BACKGROUND, | |
69 IDR_AURA_TRAY_POPUP_LABEL_BUTTON_NORMAL_BACKGROUND, | |
70 IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER, | |
71 IDR_AURA_TRAY_POPUP_LABEL_BUTTON_NORMAL_BACKGROUND, | |
72 IDR_AURA_TRAY_POPUP_LABEL_BUTTON_NORMAL_BACKGROUND, | |
73 IDR_AURA_TRAY_POPUP_PUBLIC_ACCOUNT_LOGOUT_BUTTON_BORDER, | |
74 IDR_AURA_TRAY_POPUP_LABEL_BUTTON_NORMAL_BACKGROUND, | |
75 IDR_AURA_TRAY_POPUP_LABEL_BUTTON_NORMAL_BACKGROUND, | |
76 }; | |
37 | 77 |
38 } // namespace | 78 } // namespace |
39 | 79 |
40 namespace ash { | 80 namespace ash { |
41 namespace internal { | 81 namespace internal { |
42 | 82 |
43 namespace tray { | 83 namespace tray { |
44 | 84 |
45 // A custom image view with rounded edges. | 85 // A custom image view with rounded edges. |
46 class RoundedImageView : public views::View { | 86 class RoundedImageView : public views::View { |
47 public: | 87 public: |
48 // Constructs a new rounded image view with rounded corners of radius | 88 // Constructs a new rounded image view with rounded corners of radius |
49 // |corner_radius|. | 89 // |corner_radius|. |
50 explicit RoundedImageView(int corner_radius) : corner_radius_(corner_radius) { | 90 explicit RoundedImageView(int corner_radius); |
51 } | 91 virtual ~RoundedImageView(); |
52 | |
53 virtual ~RoundedImageView() { | |
54 } | |
55 | 92 |
56 // Set the image that should be displayed from a pointer. The pointer | 93 // Set the image that should be displayed from a pointer. The pointer |
57 // contents is copied in the receiver's image. | 94 // contents is copied in the receiver's image. |
58 void SetImage(const gfx::ImageSkia& img, const gfx::Size& size) { | 95 void SetImage(const gfx::ImageSkia& img, const gfx::Size& size); |
59 image_ = img; | |
60 image_size_ = size; | |
61 | |
62 // Try to get the best image quality for the avatar. | |
63 resized_ = gfx::ImageSkiaOperations::CreateResizedImage(image_, | |
64 skia::ImageOperations::RESIZE_BEST, size); | |
65 if (GetWidget() && visible()) { | |
66 PreferredSizeChanged(); | |
67 SchedulePaint(); | |
68 } | |
69 } | |
70 | |
71 // Overridden from views::View. | |
72 virtual gfx::Size GetPreferredSize() OVERRIDE { | |
73 return gfx::Size(image_size_.width() + GetInsets().width(), | |
74 image_size_.height() + GetInsets().height()); | |
75 } | |
76 | |
77 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE { | |
78 View::OnPaint(canvas); | |
79 gfx::Rect image_bounds(size()); | |
80 image_bounds.ClampToCenteredSize(GetPreferredSize()); | |
81 image_bounds.Inset(GetInsets()); | |
82 const SkScalar kRadius = SkIntToScalar(corner_radius_); | |
83 SkPath path; | |
84 path.addRoundRect(gfx::RectToSkRect(image_bounds), kRadius, kRadius); | |
85 SkPaint paint; | |
86 paint.setAntiAlias(true); | |
87 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); | |
88 canvas->DrawImageInPath(resized_, image_bounds.x(), image_bounds.y(), | |
89 path, paint); | |
90 } | |
91 | 96 |
92 private: | 97 private: |
98 // Overridden from views::View. | |
99 virtual gfx::Size GetPreferredSize() OVERRIDE; | |
100 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; | |
101 | |
93 gfx::ImageSkia image_; | 102 gfx::ImageSkia image_; |
94 gfx::ImageSkia resized_; | 103 gfx::ImageSkia resized_; |
95 gfx::Size image_size_; | 104 gfx::Size image_size_; |
96 int corner_radius_; | 105 int corner_radius_; |
97 | 106 |
98 DISALLOW_COPY_AND_ASSIGN(RoundedImageView); | 107 DISALLOW_COPY_AND_ASSIGN(RoundedImageView); |
99 }; | 108 }; |
100 | 109 |
110 // The user details shown in public account mode. This is essentially a label | |
111 // but with custom painting code as the text is styled with multiple colors and | |
112 // contains a link. | |
113 class PublicAccountUserDetails : public views::View, | |
114 public views::LinkListener { | |
115 public: | |
116 PublicAccountUserDetails(SystemTrayItem* owner, int used_width); | |
117 virtual ~PublicAccountUserDetails(); | |
118 | |
119 private: | |
120 // Overridden from views::LinkListener. | |
121 virtual void LinkClicked(views::Link* source, int event_flags) OVERRIDE; | |
122 | |
123 // Overridden from views::View. | |
124 virtual void Layout() OVERRIDE; | |
125 virtual gfx::Size GetPreferredSize() OVERRIDE { return preferred_size_; }; | |
126 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; | |
127 | |
128 // Calculate a preferred size that ensures the label text and the following | |
129 // link do not wrap over more than three lines in total for aesthetic reasons | |
130 // if possible. | |
131 void CalculatePreferredSize(SystemTrayItem* owner, int used_width); | |
132 | |
133 string16 text_; | |
134 views::Link* learn_more_; | |
135 gfx::Font font_; | |
136 gfx::Size preferred_size_; | |
137 ScopedVector<gfx::RenderText> lines_; | |
138 | |
139 DISALLOW_COPY_AND_ASSIGN(PublicAccountUserDetails); | |
140 }; | |
141 | |
101 class UserView : public views::View, | 142 class UserView : public views::View, |
102 public views::ButtonListener { | 143 public views::ButtonListener { |
103 public: | 144 public: |
104 explicit UserView(ash::user::LoginStatus login) | 145 explicit UserView(SystemTrayItem* owner, ash::user::LoginStatus login); |
105 : login_(login), | 146 virtual ~UserView(); |
106 user_info_(NULL), | |
107 username_(NULL), | |
108 email_(NULL), | |
109 signout_(NULL) { | |
110 CHECK(login_ != ash::user::LOGGED_IN_NONE); | |
111 | |
112 bool public_account = login_ == ash::user::LOGGED_IN_PUBLIC; | |
113 bool guest = login_ == ash::user::LOGGED_IN_GUEST; | |
114 bool locked = login_ == ash::user::LOGGED_IN_LOCKED; | |
115 | |
116 set_background(views::Background::CreateSolidBackground( | |
117 public_account ? kPublicAccountBackgroundColor : kBackgroundColor)); | |
118 | |
119 if (!guest) | |
120 AddUserInfo(); | |
121 | |
122 // A user should not be able to modify logged in state when screen is | |
123 // locked. | |
124 if (!locked) | |
125 AddButtonContainer(); | |
126 } | |
127 | |
128 virtual ~UserView() {} | |
129 | |
130 // Create container for buttons. | |
131 void AddButtonContainer() { | |
132 TrayPopupLabelButton* button = new TrayPopupLabelButton(this, | |
133 ash::user::GetLocalizedSignOutStringForStatus(login_, true)); | |
134 AddChildView(button); | |
135 signout_ = button; | |
136 } | |
137 | 147 |
138 private: | 148 private: |
139 void AddUserInfo() { | 149 // Overridden from views::View. |
140 user_info_ = new views::View; | 150 virtual gfx::Size GetPreferredSize() OVERRIDE; |
141 user_info_->SetLayoutManager(new views::BoxLayout( | 151 virtual void Layout() OVERRIDE; |
142 views::BoxLayout::kHorizontal, kTrayPopupPaddingHorizontal, | |
143 kUserInfoVerticalPadding, kTrayPopupPaddingBetweenItems)); | |
144 AddChildView(user_info_); | |
145 | |
146 if (login_ == ash::user::LOGGED_IN_KIOSK) { | |
147 views::Label* label = new views::Label; | |
148 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | |
149 label->SetText( | |
150 bundle.GetLocalizedString(IDS_ASH_STATUS_TRAY_KIOSK_LABEL)); | |
151 label->set_border(views::Border::CreateEmptyBorder( | |
152 0, 4, 0, 1)); | |
153 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
154 user_info_->AddChildView(label); | |
155 return; | |
156 } | |
157 | |
158 RoundedImageView* image = new RoundedImageView(kProfileRoundedCornerRadius); | |
159 image->SetImage(ash::Shell::GetInstance()->tray_delegate()->GetUserImage(), | |
160 gfx::Size(kUserIconSize, kUserIconSize)); | |
161 user_info_->AddChildView(image); | |
162 | |
163 views::View* user = new views::View; | |
164 user->SetLayoutManager(new views::BoxLayout( | |
165 views::BoxLayout::kVertical, 0, 5, 0)); | |
166 ash::SystemTrayDelegate* tray = | |
167 ash::Shell::GetInstance()->tray_delegate(); | |
168 username_ = new views::Label(tray->GetUserDisplayName()); | |
169 username_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
170 user->AddChildView(username_); | |
171 | |
172 email_ = new views::Label(UTF8ToUTF16(tray->GetUserEmail())); | |
173 email_->SetFont(username_->font().DeriveFont(-1)); | |
174 email_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
175 email_->SetEnabled(false); | |
176 user->AddChildView(email_); | |
177 | |
178 user_info_->AddChildView(user); | |
179 } | |
180 | 152 |
181 // Overridden from views::ButtonListener. | 153 // Overridden from views::ButtonListener. |
182 virtual void ButtonPressed(views::Button* sender, | 154 virtual void ButtonPressed(views::Button* sender, |
183 const ui::Event& event) OVERRIDE { | 155 const ui::Event& event) OVERRIDE; |
184 CHECK(sender == signout_); | 156 |
185 ash::SystemTrayDelegate* tray = ash::Shell::GetInstance()->tray_delegate(); | 157 void AddLogoutButton(ash::user::LoginStatus login); |
186 tray->SignOut(); | 158 void AddUserCard(SystemTrayItem* owner, ash::user::LoginStatus login); |
187 } | 159 |
188 | 160 views::View* user_card_; |
189 // Overridden from views::View. | 161 views::View* logout_button_; |
190 virtual gfx::Size GetPreferredSize() OVERRIDE { | |
191 gfx::Size size; | |
192 if (user_info_) | |
193 size = user_info_->GetPreferredSize(); | |
194 if (signout_) { | |
195 gfx::Size signout_size = signout_->GetPreferredSize(); | |
196 // Make sure the user default view item at least as tall as the other | |
197 // tray popup items. | |
198 if (size.height() == 0) | |
199 size.set_height(kTrayPopupItemHeight); | |
200 size.set_height(std::max(size.height(), signout_size.height())); | |
201 size.set_width(size.width() + signout_size.width() + | |
202 kTrayPopupPaddingHorizontal * 2 + kTrayPopupPaddingBetweenItems); | |
203 } | |
204 return size; | |
205 } | |
206 | |
207 virtual void Layout() OVERRIDE { | |
208 views::View::Layout(); | |
209 if (bounds().IsEmpty()) | |
210 return; | |
211 | |
212 if (signout_ && user_info_) { | |
213 gfx::Rect signout_bounds(bounds()); | |
214 signout_bounds.ClampToCenteredSize(signout_->GetPreferredSize()); | |
215 signout_bounds.set_x(width() - signout_bounds.width() - | |
216 kTrayPopupPaddingHorizontal); | |
217 signout_->SetBoundsRect(signout_bounds); | |
218 | |
219 gfx::Rect usercard_bounds(user_info_->GetPreferredSize()); | |
220 usercard_bounds.set_width(signout_bounds.x()); | |
221 user_info_->SetBoundsRect(usercard_bounds); | |
222 } else if (signout_) { | |
223 signout_->SetBoundsRect(gfx::Rect(size())); | |
224 } else if (user_info_) { | |
225 user_info_->SetBoundsRect(gfx::Rect(size())); | |
226 } | |
227 } | |
228 | |
229 user::LoginStatus login_; | |
230 | |
231 views::View* user_info_; | |
232 views::Label* username_; | |
233 views::Label* email_; | |
234 | |
235 views::Button* signout_; | |
236 | 162 |
237 DISALLOW_COPY_AND_ASSIGN(UserView); | 163 DISALLOW_COPY_AND_ASSIGN(UserView); |
238 }; | 164 }; |
239 | 165 |
166 RoundedImageView::RoundedImageView(int corner_radius) | |
167 : corner_radius_(corner_radius) {} | |
168 | |
169 RoundedImageView::~RoundedImageView() {} | |
170 | |
171 void RoundedImageView::SetImage(const gfx::ImageSkia& img, | |
172 const gfx::Size& size) { | |
173 image_ = img; | |
174 image_size_ = size; | |
175 | |
176 // Try to get the best image quality for the avatar. | |
177 resized_ = gfx::ImageSkiaOperations::CreateResizedImage(image_, | |
178 skia::ImageOperations::RESIZE_BEST, size); | |
179 if (GetWidget() && visible()) { | |
180 PreferredSizeChanged(); | |
181 SchedulePaint(); | |
182 } | |
183 } | |
184 | |
185 gfx::Size RoundedImageView::GetPreferredSize() { | |
186 return gfx::Size(image_size_.width() + GetInsets().width(), | |
187 image_size_.height() + GetInsets().height()); | |
188 } | |
189 | |
190 void RoundedImageView::OnPaint(gfx::Canvas* canvas) { | |
191 View::OnPaint(canvas); | |
192 gfx::Rect image_bounds(size()); | |
193 image_bounds.ClampToCenteredSize(GetPreferredSize()); | |
194 image_bounds.Inset(GetInsets()); | |
195 const SkScalar kRadius = SkIntToScalar(corner_radius_); | |
196 SkPath path; | |
197 path.addRoundRect(gfx::RectToSkRect(image_bounds), kRadius, kRadius); | |
198 SkPaint paint; | |
199 paint.setAntiAlias(true); | |
200 paint.setXfermodeMode(SkXfermode::kSrcOver_Mode); | |
201 canvas->DrawImageInPath(resized_, image_bounds.x(), image_bounds.y(), | |
202 path, paint); | |
203 } | |
204 | |
205 PublicAccountUserDetails::PublicAccountUserDetails(SystemTrayItem* owner, | |
206 int used_width) | |
207 : learn_more_(NULL), | |
208 font_(ResourceBundle::GetSharedInstance().GetFont( | |
209 ResourceBundle::BaseFont)) { | |
210 const int inner_padding = | |
211 kTrayPopupPaddingHorizontal - kTrayPopupPaddingBetweenItems; | |
212 const bool rtl = base::i18n::IsRTL(); | |
213 set_border(views::Border::CreateEmptyBorder( | |
214 kUserDetailsVerticalPadding, rtl ? 0 : inner_padding, | |
215 kUserDetailsVerticalPadding, rtl ? inner_padding : 0)); | |
216 | |
217 ash::SystemTrayDelegate* delegate = | |
218 ash::Shell::GetInstance()->tray_delegate(); | |
219 // Retrieve the user's display name and wrap it with markers. | |
220 string16 display_name = delegate->GetUserDisplayName(); | |
221 ReplaceChars(display_name, kDisplayNameMark, string16(), &display_name); | |
stevenjb
2012/11/19 18:36:53
Wouldn't RemoveChars work here and be mroe clear?
bartfab (slow)
2012/11/19 18:44:09
Done.
| |
222 display_name.insert(0U, 1U, kDisplayNameMark[0]); | |
223 display_name.push_back(kDisplayNameMark[0]); | |
stevenjb
2012/11/19 18:36:53
I think the above two lines would be easier to rea
bartfab (slow)
2012/11/19 18:44:09
Done.
| |
224 // Retrieve the domain managing the device and wrap it with markers. | |
225 string16 domain = UTF8ToUTF16(delegate->GetEnterpriseDomain()); | |
226 ReplaceChars(domain, kDisplayNameMark, string16(), &domain); | |
227 base::i18n::WrapStringWithLTRFormatting(&domain); | |
228 // Retrieve the label text, inserting the display name and domain. | |
229 text_ = l10n_util::GetStringFUTF16(IDS_ASH_STATUS_TRAY_PUBLIC_LABEL, | |
230 display_name, domain); | |
231 | |
232 learn_more_ = new views::Link(l10n_util::GetStringUTF16(IDS_ASH_LEARN_MORE)); | |
233 learn_more_->SetUnderline(false); | |
234 learn_more_->set_listener(this); | |
235 AddChildView(learn_more_); | |
236 | |
237 CalculatePreferredSize(owner, used_width); | |
238 } | |
239 | |
240 PublicAccountUserDetails::~PublicAccountUserDetails() {} | |
241 | |
242 void PublicAccountUserDetails::LinkClicked(views::Link* source, | |
243 int event_flags) { | |
244 DCHECK_EQ(source, learn_more_); | |
245 ash::Shell::GetInstance()->tray_delegate()->ShowPublicAccountInfo(); | |
246 } | |
247 | |
248 void PublicAccountUserDetails::Layout() { | |
249 lines_.clear(); | |
250 const gfx::Rect contents_area = GetContentsBounds(); | |
251 if (contents_area.IsEmpty()) | |
252 return; | |
253 | |
254 // Word-wrap the label text. | |
255 std::vector<string16> lines; | |
256 ui::ElideRectangleText(text_, font_, contents_area.width(), | |
257 contents_area.height(), ui::ELIDE_LONG_WORDS, &lines); | |
258 // Loop through the lines, creating a renderer for each. | |
259 gfx::Point position = contents_area.origin(); | |
260 ui::Range display_name(ui::Range::InvalidRange()); | |
261 for (std::vector<string16>::const_iterator it = lines.begin(); | |
262 it != lines.end(); ++it) { | |
263 gfx::RenderText* line = gfx::RenderText::CreateInstance(); | |
264 line->SetDirectionalityMode(gfx::DIRECTIONALITY_FROM_UI); | |
265 line->SetText(*it); | |
266 const gfx::Size size(contents_area.width(), line->GetStringSize().height()); | |
267 line->SetDisplayRect(gfx::Rect(position, size)); | |
268 position.set_y(position.y() + size.height()); | |
269 | |
270 // Set the default text color for the line. | |
271 gfx::StyleRange default_style(line->default_style()); | |
272 default_style.foreground = kPublicAccountUserCardTextColor; | |
273 line->set_default_style(default_style); | |
274 line->ApplyDefaultStyle(); | |
275 | |
276 // If a range of the line contains the user's display name, apply a custom | |
277 // text color to it. | |
278 if (display_name.is_empty()) | |
279 display_name.set_start(it->find(kDisplayNameMark)); | |
280 if (!display_name.is_empty()) { | |
281 display_name.set_end( | |
282 it->find(kDisplayNameMark, display_name.start() + 1)); | |
283 gfx::StyleRange display_name_style(line->default_style()); | |
284 display_name_style.foreground = kPublicAccountUserCardNameColor; | |
285 ui::Range line_range(0, it->size()); | |
286 display_name_style.range = display_name.Intersect(line_range); | |
287 line->ApplyStyleRange(display_name_style); | |
288 // Update the range for the next line. | |
289 if (display_name.end() >= line_range.end()) | |
290 display_name.set_start(0); | |
291 else | |
292 display_name = ui::Range::InvalidRange(); | |
293 } | |
294 | |
295 lines_.push_back(line); | |
296 } | |
297 | |
298 // Position link after the label text, separated by a space. If it does not | |
299 // fit onto the last line of the text, wrap the link onto its own line. | |
300 const gfx::Size last_line_size = lines_.back()->GetStringSize(); | |
301 const int space_width = font_.GetStringWidth(ASCIIToUTF16(" ")); | |
302 const gfx::Size link_size = learn_more_->GetPreferredSize(); | |
303 if (contents_area.width() - last_line_size.width() >= | |
304 space_width + link_size.width()) { | |
305 position.set_x(position.x() + last_line_size.width() + space_width); | |
306 position.set_y(position.y() - last_line_size.height()); | |
307 } | |
308 position.set_y(position.y() - learn_more_->GetInsets().top()); | |
309 gfx::Rect learn_more_bounds(position, link_size); | |
310 learn_more_bounds.Intersect(contents_area); | |
311 if (base::i18n::IsRTL()) { | |
312 const gfx::Insets insets = GetInsets(); | |
313 learn_more_bounds.Offset(insets.right() - insets.left(), 0); | |
314 } | |
315 learn_more_->SetBoundsRect(learn_more_bounds); | |
316 } | |
317 | |
318 void PublicAccountUserDetails::OnPaint(gfx::Canvas* canvas) { | |
319 for (ScopedVector<gfx::RenderText>::const_iterator it = lines_.begin(); | |
320 it != lines_.end(); ++it) { | |
321 (*it)->Draw(canvas); | |
322 } | |
323 views::View::OnPaint(canvas); | |
324 } | |
325 | |
326 void PublicAccountUserDetails::CalculatePreferredSize(SystemTrayItem* owner, | |
327 int used_width) { | |
328 | |
329 const gfx::Size link_size = learn_more_->GetPreferredSize(); | |
330 const int space_width = font_.GetStringWidth(ASCIIToUTF16(" ")); | |
331 const gfx::Insets insets = GetInsets(); | |
332 views::TrayBubbleView* bubble_view = | |
333 owner->system_tray()->GetSystemBubble()->bubble_view(); | |
334 int min_width = std::max( | |
335 link_size.width(), | |
336 bubble_view->GetPreferredSize().width() - (used_width + insets.width())); | |
337 int max_width = std::min( | |
338 font_.GetStringWidth(text_) + space_width + link_size.width(), | |
339 bubble_view->GetMaximumSize().width() - (used_width + insets.width())); | |
340 // Do a binary search for the minimum width that ensures no more than three | |
341 // lines are needed. The lower bound is the minimum of the current bubble | |
342 // width and the width of the link (as no wrapping is permitted inside the | |
343 // link). The upper bound is the maximum of the largest allowed bubble width | |
344 // and the sum of the label text and link widths when put on a single line. | |
345 std::vector<string16> lines; | |
346 while (min_width < max_width) { | |
347 lines.clear(); | |
348 const int width = (min_width + max_width) / 2; | |
349 const bool too_narrow = ui::ElideRectangleText( | |
350 text_, font_, width, INT_MAX, ui::TRUNCATE_LONG_WORDS, &lines); | |
351 int line_count = lines.size(); | |
352 if (!too_narrow && line_count == 3 && | |
353 width - font_.GetStringWidth(lines.back()) <= | |
354 space_width + link_size.width()) { | |
355 ++line_count; | |
356 } | |
357 if (too_narrow || line_count > 3) | |
358 min_width = width + 1; | |
359 else | |
360 max_width = width; | |
361 } | |
362 | |
363 // Calculate the corresponding height and set the preferred size. | |
364 lines.clear(); | |
365 ui::ElideRectangleText( | |
366 text_, font_, min_width, INT_MAX, ui::TRUNCATE_LONG_WORDS, &lines); | |
367 int line_count = lines.size(); | |
368 if (min_width - font_.GetStringWidth(lines.back()) <= | |
369 space_width + link_size.width()) { | |
370 ++line_count; | |
371 } | |
372 const int line_height = font_.GetHeight(); | |
373 const int link_extra_height = std::max( | |
374 link_size.height() - learn_more_->GetInsets().top() - line_height, 0); | |
375 preferred_size_ = gfx::Size( | |
376 min_width + insets.width(), | |
377 line_count * line_height + link_extra_height + insets.height()); | |
378 | |
379 bubble_view->SetWidth(preferred_size_.width() + used_width); | |
380 } | |
381 | |
382 UserView::UserView(SystemTrayItem* owner, ash::user::LoginStatus login) | |
383 : user_card_(NULL), | |
384 logout_button_(NULL) { | |
385 CHECK(login != ash::user::LOGGED_IN_NONE); | |
386 set_background(views::Background::CreateSolidBackground( | |
387 login == ash::user::LOGGED_IN_PUBLIC ? kPublicAccountBackgroundColor : | |
388 kBackgroundColor)); | |
389 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal, 0, 0, | |
390 kTrayPopupPaddingBetweenItems)); | |
391 AddLogoutButton(login); | |
392 AddUserCard(owner, login); | |
393 } | |
394 | |
395 UserView::~UserView() {} | |
396 | |
397 gfx::Size UserView::GetPreferredSize() { | |
398 gfx::Size size = views::View::GetPreferredSize(); | |
399 if (!user_card_) { | |
400 // Make sure the default user default view item is at least as tall as the | |
401 // other items. | |
402 size.set_height(std::max(size.height(), | |
403 kTrayPopupItemHeight + GetInsets().height())); | |
404 } | |
405 return size; | |
406 } | |
407 | |
408 void UserView::Layout() { | |
409 gfx::Rect contents_area(GetContentsBounds()); | |
410 if (user_card_ && logout_button_) { | |
411 // Give the logout button the space it requests. | |
412 gfx::Rect logout_area = contents_area; | |
413 logout_area.ClampToCenteredSize(logout_button_->GetPreferredSize()); | |
414 logout_area.set_x(contents_area.right() - logout_area.width()); | |
415 logout_button_->SetBoundsRect(logout_area); | |
416 | |
417 // Give the remaining space to the user card. | |
418 gfx::Rect user_card_area = contents_area; | |
419 user_card_area.set_width(contents_area.width() - | |
420 (logout_area.width() + kTrayPopupPaddingBetweenItems)); | |
421 user_card_->SetBoundsRect(user_card_area); | |
422 } else if (user_card_) { | |
423 user_card_->SetBoundsRect(contents_area); | |
424 } else if (logout_button_) { | |
425 logout_button_->SetBoundsRect(contents_area); | |
426 } | |
427 } | |
428 | |
429 void UserView::ButtonPressed(views::Button* sender, const ui::Event& event) { | |
430 DCHECK(sender == logout_button_); | |
431 ash::Shell::GetInstance()->tray_delegate()->SignOut(); | |
432 } | |
433 | |
434 void UserView::AddLogoutButton(ash::user::LoginStatus login) { | |
435 // The logout button must be added before the user card. | |
436 DCHECK(!user_card_); | |
437 | |
438 // A user should not be able to modify logged-in state when screen is | |
439 // locked. | |
440 if (login == ash::user::LOGGED_IN_LOCKED) | |
441 return; | |
442 | |
443 TrayPopupLabelButton* logout_button = new TrayPopupLabelButton( | |
444 this, ash::user::GetLocalizedSignOutStringForStatus(login, true)); | |
445 // In public account mode, the vertical separator painted by the logout | |
446 // button's border has a custom color. | |
447 if (login == ash::user::LOGGED_IN_PUBLIC) { | |
448 static_cast<TrayPopupLabelButtonBorder*>(logout_button->border())-> | |
449 SetImages(views::CustomButton::STATE_NORMAL, | |
450 views::BorderImages(kPublicAccountLogoutButtonBorderImagesNormal)); | |
451 } | |
452 logout_button_ = logout_button; | |
453 AddChildView(logout_button); | |
454 } | |
455 | |
456 void UserView::AddUserCard(SystemTrayItem* owner, | |
457 ash::user::LoginStatus login) { | |
458 if (login == ash::user::LOGGED_IN_GUEST) | |
459 return; | |
460 | |
461 set_border(views::Border::CreateEmptyBorder(0, kTrayPopupPaddingHorizontal, | |
462 0, kTrayPopupPaddingHorizontal)); | |
463 | |
464 user_card_ = new views::View(); | |
465 user_card_->SetLayoutManager(new views::BoxLayout( | |
466 views::BoxLayout::kHorizontal, 0, kUserCardVerticalPadding, | |
467 kTrayPopupPaddingBetweenItems)); | |
468 AddChildViewAt(user_card_, 0); | |
469 | |
470 if (login == ash::user::LOGGED_IN_KIOSK) { | |
471 views::Label* details = new views::Label; | |
472 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance(); | |
473 details->SetText( | |
474 bundle.GetLocalizedString(IDS_ASH_STATUS_TRAY_KIOSK_LABEL)); | |
475 details->set_border(views::Border::CreateEmptyBorder(0, 4, 0, 1)); | |
476 details->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
477 user_card_->AddChildView(details); | |
478 return; | |
479 } | |
480 | |
481 RoundedImageView* avatar = new RoundedImageView(kProfileRoundedCornerRadius); | |
482 avatar->SetImage(ash::Shell::GetInstance()->tray_delegate()->GetUserImage(), | |
483 gfx::Size(kUserIconSize, kUserIconSize)); | |
484 user_card_->AddChildView(avatar); | |
485 | |
486 if (login == ash::user::LOGGED_IN_PUBLIC) { | |
487 user_card_->AddChildView(new PublicAccountUserDetails( | |
488 owner, GetPreferredSize().width() + kTrayPopupPaddingBetweenItems)); | |
489 return; | |
490 } | |
491 | |
492 ash::SystemTrayDelegate* delegate = | |
493 ash::Shell::GetInstance()->tray_delegate(); | |
494 views::View* details = new views::View; | |
495 details->SetLayoutManager(new views::BoxLayout( | |
496 views::BoxLayout::kVertical, 0, kUserDetailsVerticalPadding, 0)); | |
497 views::Label* username = new views::Label(delegate->GetUserDisplayName()); | |
498 username->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
499 details->AddChildView(username); | |
500 | |
501 views::Label* email = new views::Label(UTF8ToUTF16(delegate->GetUserEmail())); | |
502 email->SetFont(username->font().DeriveFont(-1)); | |
503 email->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
504 email->SetEnabled(false); | |
505 details->AddChildView(email); | |
506 user_card_->AddChildView(details); | |
507 } | |
508 | |
240 } // namespace tray | 509 } // namespace tray |
241 | 510 |
242 TrayUser::TrayUser(SystemTray* system_tray) | 511 TrayUser::TrayUser(SystemTray* system_tray) |
243 : SystemTrayItem(system_tray), | 512 : SystemTrayItem(system_tray), |
244 user_(NULL), | 513 user_(NULL), |
245 avatar_(NULL), | 514 avatar_(NULL), |
246 label_(NULL) { | 515 label_(NULL) { |
247 } | 516 } |
248 | 517 |
249 TrayUser::~TrayUser() { | 518 TrayUser::~TrayUser() { |
(...skipping 13 matching lines...) Expand all Loading... | |
263 UpdateAfterLoginStatusChange(status); | 532 UpdateAfterLoginStatusChange(status); |
264 return avatar_ ? static_cast<views::View*>(avatar_) | 533 return avatar_ ? static_cast<views::View*>(avatar_) |
265 : static_cast<views::View*>(label_); | 534 : static_cast<views::View*>(label_); |
266 } | 535 } |
267 | 536 |
268 views::View* TrayUser::CreateDefaultView(user::LoginStatus status) { | 537 views::View* TrayUser::CreateDefaultView(user::LoginStatus status) { |
269 if (status == user::LOGGED_IN_NONE) | 538 if (status == user::LOGGED_IN_NONE) |
270 return NULL; | 539 return NULL; |
271 | 540 |
272 CHECK(user_ == NULL); | 541 CHECK(user_ == NULL); |
273 user_ = new tray::UserView(status); | 542 user_ = new tray::UserView(this, status); |
274 return user_; | 543 return user_; |
275 } | 544 } |
276 | 545 |
277 views::View* TrayUser::CreateDetailedView(user::LoginStatus status) { | 546 views::View* TrayUser::CreateDetailedView(user::LoginStatus status) { |
278 return NULL; | 547 return NULL; |
279 } | 548 } |
280 | 549 |
281 void TrayUser::DestroyTrayView() { | 550 void TrayUser::DestroyTrayView() { |
282 avatar_ = NULL; | 551 avatar_ = NULL; |
283 label_ = NULL; | 552 label_ = NULL; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
341 // Check for null to avoid crbug.com/150944. | 610 // Check for null to avoid crbug.com/150944. |
342 if (avatar_) { | 611 if (avatar_) { |
343 avatar_->SetImage( | 612 avatar_->SetImage( |
344 ash::Shell::GetInstance()->tray_delegate()->GetUserImage(), | 613 ash::Shell::GetInstance()->tray_delegate()->GetUserImage(), |
345 gfx::Size(kUserIconSize, kUserIconSize)); | 614 gfx::Size(kUserIconSize, kUserIconSize)); |
346 } | 615 } |
347 } | 616 } |
348 | 617 |
349 } // namespace internal | 618 } // namespace internal |
350 } // namespace ash | 619 } // namespace ash |
OLD | NEW |