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 "ui/message_center/views/notification_view.h" | 5 #include "ui/message_center/views/notification_view.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "grit/ui_resources.h" | 10 #include "grit/ui_resources.h" |
11 #include "ui/base/layout.h" | 11 #include "ui/base/layout.h" |
12 #include "ui/base/resource/resource_bundle.h" | 12 #include "ui/base/resource/resource_bundle.h" |
13 #include "ui/base/text/text_elider.h" | 13 #include "ui/base/text/text_elider.h" |
14 #include "ui/gfx/canvas.h" | 14 #include "ui/gfx/canvas.h" |
15 #include "ui/gfx/size.h" | 15 #include "ui/gfx/size.h" |
16 #include "ui/message_center/message_center.h" | 16 #include "ui/message_center/message_center.h" |
17 #include "ui/message_center/message_center_style.h" | 17 #include "ui/message_center/message_center_style.h" |
18 #include "ui/message_center/message_center_switches.h" | 18 #include "ui/message_center/message_center_switches.h" |
19 #include "ui/message_center/message_center_util.h" | 19 #include "ui/message_center/message_center_util.h" |
20 #include "ui/message_center/notification.h" | 20 #include "ui/message_center/notification.h" |
21 #include "ui/message_center/notification_types.h" | 21 #include "ui/message_center/notification_types.h" |
22 #include "ui/message_center/views/bounded_label.h" | 22 #include "ui/message_center/views/bounded_label.h" |
23 #include "ui/native_theme/native_theme.h" | 23 #include "ui/native_theme/native_theme.h" |
24 #include "ui/views/controls/button/image_button.h" | 24 #include "ui/views/controls/button/image_button.h" |
25 #include "ui/views/controls/image_view.h" | 25 #include "ui/views/controls/image_view.h" |
26 #include "ui/views/controls/label.h" | 26 #include "ui/views/controls/label.h" |
27 #include "ui/views/controls/progress_bar.h" | |
27 #include "ui/views/layout/box_layout.h" | 28 #include "ui/views/layout/box_layout.h" |
28 #include "ui/views/layout/fill_layout.h" | 29 #include "ui/views/layout/fill_layout.h" |
29 #include "ui/views/widget/widget.h" | 30 #include "ui/views/widget/widget.h" |
30 | 31 |
31 namespace { | 32 namespace { |
32 | 33 |
33 // Dimensions. | 34 // Dimensions. |
34 const int kIconSize = message_center::kNotificationIconSize; | 35 const int kIconSize = message_center::kNotificationIconSize; |
35 const int kLegacyIconSize = 40; | 36 const int kLegacyIconSize = 40; |
36 const int kIconBottomPadding = 16; | 37 const int kIconBottomPadding = 16; |
37 const int kTextLeftPadding = kIconSize + message_center::kIconToTextPadding; | 38 const int kTextLeftPadding = kIconSize + message_center::kIconToTextPadding; |
38 const int kTextBottomPadding = 12; | 39 const int kTextBottomPadding = 12; |
39 const int kTextRightPadding = 23; | 40 const int kTextRightPadding = 23; |
40 const int kItemTitleToMessagePadding = 3; | 41 const int kItemTitleToMessagePadding = 3; |
42 const int kProgressBarWidth = message_center::kNotificationWidth - | |
43 kTextLeftPadding - kTextRightPadding; | |
44 const int kProgressBarHeight = 8; | |
41 const int kButtonVecticalPadding = 0; | 45 const int kButtonVecticalPadding = 0; |
42 const int kButtonTitleTopPadding = 0; | 46 const int kButtonTitleTopPadding = 0; |
43 | 47 |
44 // Character limits: Displayed text will be subject to the line limits above, | 48 // Character limits: Displayed text will be subject to the line limits above, |
45 // but we also remove trailing characters from text to reduce processing cost. | 49 // but we also remove trailing characters from text to reduce processing cost. |
46 // Character limit = pixels per line * line limit / min. pixels per character. | 50 // Character limit = pixels per line * line limit / min. pixels per character. |
47 const size_t kTitleCharacterLimit = | 51 const size_t kTitleCharacterLimit = |
48 message_center::kNotificationWidth * message_center::kTitleLineLimit / 4; | 52 message_center::kNotificationWidth * message_center::kTitleLineLimit / 4; |
49 const size_t kMessageCharacterLimit = | 53 const size_t kMessageCharacterLimit = |
50 message_center::kNotificationWidth * | 54 message_center::kNotificationWidth * |
(...skipping 17 matching lines...) Expand all Loading... | |
68 } | 72 } |
69 | 73 |
70 // static | 74 // static |
71 views::Border* MakeTextBorder(int padding, int top, int bottom) { | 75 views::Border* MakeTextBorder(int padding, int top, int bottom) { |
72 // Split the padding between the top and the bottom, then add the extra space. | 76 // Split the padding between the top and the bottom, then add the extra space. |
73 return MakeEmptyBorder(padding / 2 + top, kTextLeftPadding, | 77 return MakeEmptyBorder(padding / 2 + top, kTextLeftPadding, |
74 (padding + 1) / 2 + bottom, kTextRightPadding); | 78 (padding + 1) / 2 + bottom, kTextRightPadding); |
75 } | 79 } |
76 | 80 |
77 // static | 81 // static |
82 views::Border* MakeProgressBarBorder(int top, int bottom) { | |
83 return MakeEmptyBorder(top, kTextLeftPadding, bottom, kTextRightPadding); | |
84 } | |
85 | |
86 // static | |
78 views::Border* MakeSeparatorBorder(int top, int left, SkColor color) { | 87 views::Border* MakeSeparatorBorder(int top, int left, SkColor color) { |
79 return views::Border::CreateSolidSidedBorder(top, left, 0, 0, color); | 88 return views::Border::CreateSolidSidedBorder(top, left, 0, 0, color); |
80 } | 89 } |
81 | 90 |
82 // static | 91 // static |
83 // Return true if and only if the image is null or has alpha. | 92 // Return true if and only if the image is null or has alpha. |
84 bool HasAlpha(gfx::ImageSkia& image, views::Widget* widget) { | 93 bool HasAlpha(gfx::ImageSkia& image, views::Widget* widget) { |
85 // Determine which bitmap to use. | 94 // Determine which bitmap to use. |
86 ui::ScaleFactor factor = ui::SCALE_FACTOR_100P; | 95 ui::ScaleFactor factor = ui::SCALE_FACTOR_100P; |
87 if (widget) { | 96 if (widget) { |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
216 true, paint); | 225 true, paint); |
217 } | 226 } |
218 } | 227 } |
219 } | 228 } |
220 | 229 |
221 gfx::Size ProportionalImageView::GetImageSizeForWidth(int width) { | 230 gfx::Size ProportionalImageView::GetImageSizeForWidth(int width) { |
222 gfx::Size size = visible() ? image_.size() : gfx::Size(); | 231 gfx::Size size = visible() ? image_.size() : gfx::Size(); |
223 return message_center::GetImageSizeForWidth(width, size); | 232 return message_center::GetImageSizeForWidth(width, size); |
224 } | 233 } |
225 | 234 |
235 // NotificationProgressBar ///////////////////////////////////////////////////// | |
236 | |
237 class NotificationProgressBar : public views::ProgressBar { | |
238 public: | |
239 NotificationProgressBar(); | |
240 virtual ~NotificationProgressBar(); | |
241 | |
242 private: | |
243 // Overriden from View | |
244 virtual gfx::Size GetPreferredSize() OVERRIDE; | |
245 | |
246 DISALLOW_COPY_AND_ASSIGN(NotificationProgressBar); | |
247 }; | |
248 | |
249 NotificationProgressBar::NotificationProgressBar() { | |
250 } | |
251 | |
252 NotificationProgressBar::~NotificationProgressBar() { | |
253 } | |
254 | |
255 gfx::Size NotificationProgressBar::GetPreferredSize() { | |
256 gfx::Size pref_size(kProgressBarWidth, kProgressBarHeight); | |
257 gfx::Insets insets = GetInsets(); | |
258 pref_size.Enlarge(insets.width(), insets.height()); | |
259 return pref_size; | |
260 } | |
261 | |
226 // NotificationButton ////////////////////////////////////////////////////////// | 262 // NotificationButton ////////////////////////////////////////////////////////// |
227 | 263 |
228 // NotificationButtons render the action buttons of notifications. | 264 // NotificationButtons render the action buttons of notifications. |
229 class NotificationButton : public views::CustomButton { | 265 class NotificationButton : public views::CustomButton { |
230 public: | 266 public: |
231 NotificationButton(views::ButtonListener* listener); | 267 NotificationButton(views::ButtonListener* listener); |
232 virtual ~NotificationButton(); | 268 virtual ~NotificationButton(); |
233 | 269 |
234 void SetIcon(const gfx::ImageSkia& icon); | 270 void SetIcon(const gfx::ImageSkia& icon); |
235 void SetTitle(const string16& title); | 271 void SetTitle(const string16& title); |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
338 MessageView* NotificationView::Create(const Notification& notification, | 374 MessageView* NotificationView::Create(const Notification& notification, |
339 MessageCenter* message_center, | 375 MessageCenter* message_center, |
340 MessageCenterTray* tray, | 376 MessageCenterTray* tray, |
341 bool expanded, | 377 bool expanded, |
342 bool top_level) { | 378 bool top_level) { |
343 switch (notification.type()) { | 379 switch (notification.type()) { |
344 case NOTIFICATION_TYPE_BASE_FORMAT: | 380 case NOTIFICATION_TYPE_BASE_FORMAT: |
345 case NOTIFICATION_TYPE_IMAGE: | 381 case NOTIFICATION_TYPE_IMAGE: |
346 case NOTIFICATION_TYPE_MULTIPLE: | 382 case NOTIFICATION_TYPE_MULTIPLE: |
347 case NOTIFICATION_TYPE_SIMPLE: | 383 case NOTIFICATION_TYPE_SIMPLE: |
384 case NOTIFICATION_TYPE_PROGRESS: | |
348 break; | 385 break; |
349 default: | 386 default: |
350 // If the caller asks for an unrecognized kind of view (entirely possible | 387 // If the caller asks for an unrecognized kind of view (entirely possible |
351 // if an application is running on an older version of this code that | 388 // if an application is running on an older version of this code that |
352 // doesn't have the requested kind of notification template), we'll fall | 389 // doesn't have the requested kind of notification template), we'll fall |
353 // back to a notification instance that will provide at least basic | 390 // back to a notification instance that will provide at least basic |
354 // functionality. | 391 // functionality. |
355 LOG(WARNING) << "Unable to fulfill request for unrecognized " | 392 LOG(WARNING) << "Unable to fulfill request for unrecognized " |
356 << "notification type " << notification.type() << ". " | 393 << "notification type " << notification.type() << ". " |
357 << "Falling back to simple notification type."; | 394 << "Falling back to simple notification type."; |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
415 ui::TruncateString(notification.message(), kMessageCharacterLimit)); | 452 ui::TruncateString(notification.message(), kMessageCharacterLimit)); |
416 message_view_->SetLineHeight(kMessageLineHeight); | 453 message_view_->SetLineHeight(kMessageLineHeight); |
417 message_view_->SetVisible(!is_expanded() || !notification.items().size()); | 454 message_view_->SetVisible(!is_expanded() || !notification.items().size()); |
418 message_view_->SetColors(message_center::kDimTextColor, | 455 message_view_->SetColors(message_center::kDimTextColor, |
419 kDimTextBackgroundColor); | 456 kDimTextBackgroundColor); |
420 message_view_->set_border(MakeTextBorder(padding, 4, 0)); | 457 message_view_->set_border(MakeTextBorder(padding, 4, 0)); |
421 top_view_->AddChildView(message_view_); | 458 top_view_->AddChildView(message_view_); |
422 accessible_lines.push_back(notification.message()); | 459 accessible_lines.push_back(notification.message()); |
423 } | 460 } |
424 | 461 |
462 // Create the progress bar view. | |
463 progress_bar_view_ = NULL; | |
464 if (notification.type() == NOTIFICATION_TYPE_PROGRESS) { | |
465 progress_bar_view_ = new NotificationProgressBar(); | |
466 progress_bar_view_->set_border(MakeProgressBarBorder(12, 2)); | |
dewittj
2013/07/15 15:32:15
What are these constants? Can we name them?
jianli
2013/07/23 19:04:53
Done.
| |
467 progress_bar_view_->SetValue(notification.progress() / 100.0); | |
468 top_view_->AddChildView(progress_bar_view_); | |
469 } | |
470 | |
425 // Create the list item views (up to a maximum). | 471 // Create the list item views (up to a maximum). |
426 int padding = kMessageLineHeight - views::Label().font().GetHeight(); | 472 int padding = kMessageLineHeight - views::Label().font().GetHeight(); |
427 std::vector<NotificationItem> items = notification.items(); | 473 std::vector<NotificationItem> items = notification.items(); |
428 for (size_t i = 0; i < items.size() && i < kNotificationMaximumItems; ++i) { | 474 for (size_t i = 0; i < items.size() && i < kNotificationMaximumItems; ++i) { |
429 ItemView* item_view = new ItemView(items[i]); | 475 ItemView* item_view = new ItemView(items[i]); |
430 item_view->SetVisible(is_expanded()); | 476 item_view->SetVisible(is_expanded()); |
431 item_view->set_border(MakeTextBorder(padding, i ? 0 : 4, 0)); | 477 item_view->set_border(MakeTextBorder(padding, i ? 0 : 4, 0)); |
432 item_views_.push_back(item_view); | 478 item_views_.push_back(item_view); |
433 top_view_->AddChildView(item_view); | 479 top_view_->AddChildView(item_view); |
434 accessible_lines.push_back( | 480 accessible_lines.push_back( |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
675 return message_view_ ? | 721 return message_view_ ? |
676 message_view_->GetLinesForWidthAndLimit(width, limit) : 0; | 722 message_view_->GetLinesForWidthAndLimit(width, limit) : 0; |
677 } | 723 } |
678 | 724 |
679 int NotificationView::GetMessageHeight(int width, int limit) { | 725 int NotificationView::GetMessageHeight(int width, int limit) { |
680 return message_view_ ? | 726 return message_view_ ? |
681 message_view_->GetSizeForWidthAndLines(width, limit).height() : 0; | 727 message_view_->GetSizeForWidthAndLines(width, limit).height() : 0; |
682 } | 728 } |
683 | 729 |
684 } // namespace message_center | 730 } // namespace message_center |
OLD | NEW |