Chromium Code Reviews| 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/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "grit/ui_resources.h" | 9 #include "grit/ui_resources.h" |
| 10 #include "ui/base/accessibility/accessible_view_state.h" | 10 #include "ui/base/accessibility/accessible_view_state.h" |
| 11 #include "ui/base/layout.h" | |
| 11 #include "ui/base/resource/resource_bundle.h" | 12 #include "ui/base/resource/resource_bundle.h" |
| 12 #include "ui/base/text/text_elider.h" | 13 #include "ui/base/text/text_elider.h" |
| 13 #include "ui/gfx/canvas.h" | 14 #include "ui/gfx/canvas.h" |
| 14 #include "ui/gfx/size.h" | 15 #include "ui/gfx/size.h" |
| 15 #include "ui/message_center/message_center_constants.h" | 16 #include "ui/message_center/message_center_constants.h" |
| 16 #include "ui/message_center/message_center_switches.h" | 17 #include "ui/message_center/message_center_switches.h" |
| 17 #include "ui/message_center/message_center_util.h" | 18 #include "ui/message_center/message_center_util.h" |
| 18 #include "ui/message_center/notification.h" | 19 #include "ui/message_center/notification.h" |
| 19 #include "ui/message_center/notification_change_observer.h" | 20 #include "ui/message_center/notification_change_observer.h" |
| 20 #include "ui/message_center/notification_types.h" | 21 #include "ui/message_center/notification_types.h" |
| 21 #include "ui/message_center/views/bounded_label.h" | 22 #include "ui/message_center/views/bounded_label.h" |
| 22 #include "ui/message_center/views/message_simple_view.h" | 23 #include "ui/message_center/views/message_simple_view.h" |
| 23 #include "ui/native_theme/native_theme.h" | 24 #include "ui/native_theme/native_theme.h" |
| 24 #include "ui/views/controls/button/image_button.h" | 25 #include "ui/views/controls/button/image_button.h" |
| 25 #include "ui/views/controls/image_view.h" | 26 #include "ui/views/controls/image_view.h" |
| 26 #include "ui/views/controls/label.h" | 27 #include "ui/views/controls/label.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" |
| 30 #include "ui/views/widget/widget.h" | |
| 29 | 31 |
| 30 namespace { | 32 namespace { |
| 31 | 33 |
| 32 // Dimensions. | 34 // Dimensions. |
| 33 const int kIconColumnWidth = message_center::kNotificationIconSize; | 35 const int kIconSize = message_center::kNotificationIconSize; |
| 34 const int kLegacyIconSize = 40; | 36 const int kLegacyIconSize = 40; |
| 35 const int kTextLeftPadding = kIconColumnWidth + | 37 const int kTextLeftPadding = kIconSize + message_center::kIconToTextPadding; |
| 36 message_center::kIconToTextPadding; | |
| 37 const int kTextBottomPadding = 12; | 38 const int kTextBottomPadding = 12; |
| 38 const int kTextRightPadding = 23; | 39 const int kTextRightPadding = 23; |
| 39 const int kItemTitleToMessagePadding = 3; | 40 const int kItemTitleToMessagePadding = 3; |
| 40 const int kButtonHeight = 38; | 41 const int kButtonHeight = 38; |
| 41 const int kButtonHorizontalPadding = 16; | 42 const int kButtonHorizontalPadding = 16; |
| 42 const int kButtonVecticalPadding = 0; | 43 const int kButtonVecticalPadding = 0; |
| 43 const int kButtonIconTopPadding = 11; | 44 const int kButtonIconTopPadding = 11; |
| 44 const int kButtonIconToTitlePadding = 16; | 45 const int kButtonIconToTitlePadding = 16; |
| 45 const int kButtonTitleTopPadding = 0; | 46 const int kButtonTitleTopPadding = 0; |
| 46 | 47 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 80 // static | 81 // static |
| 81 views::Border* MakeTextBorder(int top, int bottom) { | 82 views::Border* MakeTextBorder(int top, int bottom) { |
| 82 return MakeEmptyBorder(top, kTextLeftPadding, bottom, kTextRightPadding); | 83 return MakeEmptyBorder(top, kTextLeftPadding, bottom, kTextRightPadding); |
| 83 } | 84 } |
| 84 | 85 |
| 85 // static | 86 // static |
| 86 views::Border* MakeSeparatorBorder(int top, int left, SkColor color) { | 87 views::Border* MakeSeparatorBorder(int top, int left, SkColor color) { |
| 87 return views::Border::CreateSolidSidedBorder(top, left, 0, 0, color); | 88 return views::Border::CreateSolidSidedBorder(top, left, 0, 0, color); |
| 88 } | 89 } |
| 89 | 90 |
| 91 // static | |
| 92 // Return true if and only if the image is null or has alpha. | |
| 93 bool HasAlpha(gfx::ImageSkia& image, views::Widget* widget) { | |
| 94 // Determine which bitmap to use. | |
| 95 ui::ScaleFactor factor = ui::SCALE_FACTOR_100P; | |
| 96 if (widget) { | |
| 97 factor = ui::GetScaleFactorForNativeView(widget->GetNativeView()); | |
| 98 if (factor == ui::SCALE_FACTOR_NONE) | |
| 99 factor = ui::SCALE_FACTOR_100P; | |
| 100 } | |
| 101 | |
| 102 // Extract that bitmap's alpha and look for a non-opaque pixel there. | |
| 103 SkBitmap bitmap = image.GetRepresentation(factor).sk_bitmap(); | |
| 104 if (!bitmap.isNull()) { | |
| 105 SkBitmap alpha; | |
| 106 alpha.setConfig(SkBitmap::kA1_Config, bitmap.width(), bitmap.height(), 0); | |
| 107 bitmap.extractAlpha(&alpha); | |
| 108 for (int y = 0; y < bitmap.height(); ++y) { | |
| 109 for (int x = 0; x < bitmap.width(); ++x) { | |
| 110 if (alpha.getColor(x, y) != SK_ColorBLACK) { | |
| 111 return true; | |
| 112 } | |
| 113 } | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 // If no opaque pixel was found, return false unless the bitmap is empty. | |
| 118 return bitmap.isNull(); | |
| 119 } | |
| 120 | |
| 90 // ItemView //////////////////////////////////////////////////////////////////// | 121 // ItemView //////////////////////////////////////////////////////////////////// |
| 91 | 122 |
| 92 // ItemViews are responsible for drawing each list notification item's title and | 123 // ItemViews are responsible for drawing each list notification item's title and |
| 93 // message next to each other within a single column. | 124 // message next to each other within a single column. |
| 94 class ItemView : public views::View { | 125 class ItemView : public views::View { |
| 95 public: | 126 public: |
| 96 ItemView(const message_center::NotificationItem& item); | 127 ItemView(const message_center::NotificationItem& item); |
| 97 virtual ~ItemView(); | 128 virtual ~ItemView(); |
| 98 | 129 |
| 99 virtual void SetVisible(bool visible) OVERRIDE; | 130 virtual void SetVisible(bool visible) OVERRIDE; |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 378 std::vector<NotificationItem> items = notification.items(); | 409 std::vector<NotificationItem> items = notification.items(); |
| 379 for (size_t i = 0; i < items.size() && i < kNotificationMaximumItems; ++i) { | 410 for (size_t i = 0; i < items.size() && i < kNotificationMaximumItems; ++i) { |
| 380 ItemView* item_view = new ItemView(items[i]); | 411 ItemView* item_view = new ItemView(items[i]); |
| 381 item_view->SetVisible(is_expanded()); | 412 item_view->SetVisible(is_expanded()); |
| 382 item_view->set_border(MakeTextBorder(i > 0 ? 0 : 4, 1)); | 413 item_view->set_border(MakeTextBorder(i > 0 ? 0 : 4, 1)); |
| 383 item_views_.push_back(item_view); | 414 item_views_.push_back(item_view); |
| 384 top_view_->AddChildView(item_view); | 415 top_view_->AddChildView(item_view); |
| 385 } | 416 } |
| 386 | 417 |
| 387 // Create the notification icon view. | 418 // Create the notification icon view. |
| 388 if (notification.type() == NOTIFICATION_TYPE_SIMPLE) { | 419 gfx::ImageSkia icon = notification.icon().AsImageSkia(); |
| 420 if (notification.type() == NOTIFICATION_TYPE_SIMPLE && | |
| 421 (icon.width() != kIconSize || | |
| 422 icon.height() != kIconSize || | |
| 423 HasAlpha(icon, GetWidget()))) { | |
|
Jun Mukai
2013/04/12 18:50:21
nit: align the indent
dharcourt
2013/04/12 19:21:07
Should this be aligned this way (#1):
if (not
Jun Mukai
2013/04/12 19:34:38
#1 is in my mind.
dharcourt
2013/04/12 20:09:56
Lucky me, that's what I uploaded :-).
| |
| 389 views::ImageView* icon_view = new views::ImageView(); | 424 views::ImageView* icon_view = new views::ImageView(); |
| 390 icon_view->SetImage(notification.icon().AsImageSkia()); | 425 icon_view->SetImage(icon); |
| 391 icon_view->SetImageSize(gfx::Size(kLegacyIconSize, kLegacyIconSize)); | 426 icon_view->SetImageSize(gfx::Size(kLegacyIconSize, kLegacyIconSize)); |
| 392 icon_view->SetHorizontalAlignment(views::ImageView::CENTER); | 427 icon_view->SetHorizontalAlignment(views::ImageView::CENTER); |
| 393 icon_view->SetVerticalAlignment(views::ImageView::CENTER); | 428 icon_view->SetVerticalAlignment(views::ImageView::CENTER); |
| 394 icon_view->set_background(MakeBackground(kLegacyIconBackgroundColor)); | 429 icon_view->set_background(MakeBackground(kLegacyIconBackgroundColor)); |
| 395 icon_view_ = icon_view; | 430 icon_view_ = icon_view; |
| 396 } else { | 431 } else { |
| 397 icon_view_ = new ProportionalImageView(notification.icon().AsImageSkia()); | 432 icon_view_ = new ProportionalImageView(icon); |
| 398 } | 433 } |
| 399 | 434 |
| 400 // Create the bottom_view_, which collects into a vertical box all content | 435 // Create the bottom_view_, which collects into a vertical box all content |
| 401 // below the notification icon except for the expand button. | 436 // below the notification icon except for the expand button. |
| 402 bottom_view_ = new views::View(); | 437 bottom_view_ = new views::View(); |
| 403 bottom_view_->SetLayoutManager( | 438 bottom_view_->SetLayoutManager( |
| 404 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0)); | 439 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0)); |
| 405 | 440 |
| 406 // Create the image view if appropriate. | 441 // Create the image view if appropriate. |
| 407 image_view_ = NULL; | 442 image_view_ = NULL; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 444 int top_width = top_view_->GetPreferredSize().width(); | 479 int top_width = top_view_->GetPreferredSize().width(); |
| 445 int bottom_width = bottom_view_->GetPreferredSize().width(); | 480 int bottom_width = bottom_view_->GetPreferredSize().width(); |
| 446 int preferred_width = std::max(top_width, bottom_width) + GetInsets().width(); | 481 int preferred_width = std::max(top_width, bottom_width) + GetInsets().width(); |
| 447 return gfx::Size(preferred_width, GetHeightForWidth(preferred_width)); | 482 return gfx::Size(preferred_width, GetHeightForWidth(preferred_width)); |
| 448 } | 483 } |
| 449 | 484 |
| 450 int NotificationView::GetHeightForWidth(int width) { | 485 int NotificationView::GetHeightForWidth(int width) { |
| 451 gfx::Insets insets = GetInsets(); | 486 gfx::Insets insets = GetInsets(); |
| 452 int top_height = top_view_->GetHeightForWidth(width - insets.width()); | 487 int top_height = top_view_->GetHeightForWidth(width - insets.width()); |
| 453 int bottom_height = bottom_view_->GetHeightForWidth(width - insets.width()); | 488 int bottom_height = bottom_view_->GetHeightForWidth(width - insets.width()); |
| 454 int icon_size = message_center::kNotificationIconSize; | 489 return std::max(top_height, kIconSize) + bottom_height + insets.height(); |
| 455 return std::max(top_height, icon_size) + bottom_height + insets.height(); | |
| 456 } | 490 } |
| 457 | 491 |
| 458 void NotificationView::Layout() { | 492 void NotificationView::Layout() { |
| 459 gfx::Insets insets = GetInsets(); | 493 gfx::Insets insets = GetInsets(); |
| 460 int content_width = width() - insets.width(); | 494 int content_width = width() - insets.width(); |
| 461 int content_right = width() - insets.right(); | 495 int content_right = width() - insets.right(); |
| 462 | 496 |
| 463 // Before any resizing, set or adjust the number of message lines. | 497 // Before any resizing, set or adjust the number of message lines. |
| 464 if (message_view_) | 498 if (message_view_) |
| 465 message_view_->SetLineLimit(GetMessageLineLimit()); | 499 message_view_->SetLineLimit(GetMessageLineLimit()); |
| 466 | 500 |
| 467 // Background. | 501 // Background. |
| 468 background_view_->SetBounds(insets.left(), insets.top(), | 502 background_view_->SetBounds(insets.left(), insets.top(), |
| 469 content_width, height() - insets.height()); | 503 content_width, height() - insets.height()); |
| 470 | 504 |
| 471 // Top views. | 505 // Top views. |
| 472 int top_height = top_view_->GetHeightForWidth(content_width); | 506 int top_height = top_view_->GetHeightForWidth(content_width); |
| 473 top_view_->SetBounds(insets.left(), insets.top(), content_width, top_height); | 507 top_view_->SetBounds(insets.left(), insets.top(), content_width, top_height); |
| 474 | 508 |
| 475 // Icon. | 509 // Icon. |
| 476 int icon_size = message_center::kNotificationIconSize; | 510 icon_view_->SetBounds(insets.left(), insets.top(), kIconSize, kIconSize); |
| 477 icon_view_->SetBounds(insets.left(), insets.top(), icon_size, icon_size); | |
| 478 | 511 |
| 479 // Bottom views. | 512 // Bottom views. |
| 480 int bottom_y = insets.top() + std::max(top_height, icon_size); | 513 int bottom_y = insets.top() + std::max(top_height, kIconSize); |
| 481 int bottom_height = bottom_view_->GetHeightForWidth(content_width); | 514 int bottom_height = bottom_view_->GetHeightForWidth(content_width); |
| 482 bottom_view_->SetBounds(insets.left(), bottom_y, | 515 bottom_view_->SetBounds(insets.left(), bottom_y, |
| 483 content_width, bottom_height); | 516 content_width, bottom_height); |
| 484 | 517 |
| 485 // Close button. | 518 // Close button. |
| 486 gfx::Size close_size(close_button()->GetPreferredSize()); | 519 gfx::Size close_size(close_button()->GetPreferredSize()); |
| 487 close_button()->SetBounds(content_right - close_size.width(), insets.top(), | 520 close_button()->SetBounds(content_right - close_size.width(), insets.top(), |
| 488 close_size.width(), close_size.height()); | 521 close_size.width(), close_size.height()); |
| 489 | 522 |
| 490 // Expand button. | 523 // Expand button. |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 545 | 578 |
| 546 // If there's a title ensure title + message lines <= collapsed line limit. | 579 // If there's a title ensure title + message lines <= collapsed line limit. |
| 547 if (title_view_) | 580 if (title_view_) |
| 548 return kMessageCollapsedLineLimit - title_view_->GetLinesForWidth(width()); | 581 return kMessageCollapsedLineLimit - title_view_->GetLinesForWidth(width()); |
| 549 | 582 |
| 550 // If there's no title we get an extra line because message lines are shorter. | 583 // If there's no title we get an extra line because message lines are shorter. |
| 551 return kMessageCollapsedLineLimit + 1; | 584 return kMessageCollapsedLineLimit + 1; |
| 552 } | 585 } |
| 553 | 586 |
| 554 } // namespace message_center | 587 } // namespace message_center |
| OLD | NEW |