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/stl_util.h" |
8 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
9 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
10 #include "grit/ui_resources.h" | 11 #include "grit/ui_resources.h" |
11 #include "grit/ui_strings.h" | 12 #include "grit/ui_strings.h" |
12 #include "ui/base/cursor/cursor.h" | 13 #include "ui/base/cursor/cursor.h" |
13 #include "ui/base/l10n/l10n_util.h" | 14 #include "ui/base/l10n/l10n_util.h" |
14 #include "ui/base/layout.h" | 15 #include "ui/base/layout.h" |
15 #include "ui/base/resource/resource_bundle.h" | 16 #include "ui/base/resource/resource_bundle.h" |
16 #include "ui/gfx/canvas.h" | 17 #include "ui/gfx/canvas.h" |
17 #include "ui/gfx/size.h" | 18 #include "ui/gfx/size.h" |
(...skipping 422 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 } | 441 } |
441 | 442 |
442 views::View* NotificationView::GetEventHandlerForRect(const gfx::Rect& rect) { | 443 views::View* NotificationView::GetEventHandlerForRect(const gfx::Rect& rect) { |
443 // TODO(tdanderson): Modify this function to support rect-based event | 444 // TODO(tdanderson): Modify this function to support rect-based event |
444 // targeting. Using the center point of |rect| preserves this function's | 445 // targeting. Using the center point of |rect| preserves this function's |
445 // expected behavior for the time being. | 446 // expected behavior for the time being. |
446 gfx::Point point = rect.CenterPoint(); | 447 gfx::Point point = rect.CenterPoint(); |
447 | 448 |
448 // Want to return this for underlying views, otherwise GetCursor is not | 449 // Want to return this for underlying views, otherwise GetCursor is not |
449 // called. But buttons are exceptions, they'll have their own event handlings. | 450 // called. But buttons are exceptions, they'll have their own event handlings. |
450 std::vector<views::View*> buttons(action_buttons_); | 451 std::vector<views::View*> buttons(action_buttons_.begin(), |
| 452 action_buttons_.end()); |
451 buttons.push_back(close_button()); | 453 buttons.push_back(close_button()); |
452 | 454 |
453 for (size_t i = 0; i < buttons.size(); ++i) { | 455 for (size_t i = 0; i < buttons.size(); ++i) { |
454 gfx::Point point_in_child = point; | 456 gfx::Point point_in_child = point; |
455 ConvertPointToTarget(this, buttons[i], &point_in_child); | 457 ConvertPointToTarget(this, buttons[i], &point_in_child); |
456 if (buttons[i]->HitTestPoint(point_in_child)) | 458 if (buttons[i]->HitTestPoint(point_in_child)) |
457 return buttons[i]->GetEventHandlerForPoint(point_in_child); | 459 return buttons[i]->GetEventHandlerForPoint(point_in_child); |
458 } | 460 } |
459 | 461 |
460 return this; | 462 return this; |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
691 if (!notification.image().IsEmpty()) { | 693 if (!notification.image().IsEmpty()) { |
692 gfx::Size image_size(kNotificationPreferredImageWidth, | 694 gfx::Size image_size(kNotificationPreferredImageWidth, |
693 kNotificationPreferredImageHeight); | 695 kNotificationPreferredImageHeight); |
694 image_view_ = MakeNotificationImage(notification.image(), image_size); | 696 image_view_ = MakeNotificationImage(notification.image(), image_size); |
695 bottom_view_->AddChildView(image_view_); | 697 bottom_view_->AddChildView(image_view_); |
696 } | 698 } |
697 } | 699 } |
698 | 700 |
699 void NotificationView::CreateOrUpdateActionButtonViews( | 701 void NotificationView::CreateOrUpdateActionButtonViews( |
700 const Notification& notification) { | 702 const Notification& notification) { |
701 for (size_t i = 0; i < separators_.size(); ++i) | 703 std::vector<ButtonInfo> buttons = notification.buttons(); |
702 delete separators_[i]; | 704 bool new_buttons = action_buttons_.size() != buttons.size(); |
703 separators_.clear(); | |
704 | 705 |
705 for (size_t i = 0; i < action_buttons_.size(); ++i) | 706 if (new_buttons || buttons.size() == 0) { |
706 delete action_buttons_[i]; | 707 // STLDeleteElements also clears the container. |
707 action_buttons_.clear(); | 708 STLDeleteElements(&separators_); |
| 709 STLDeleteElements(&action_buttons_); |
| 710 } |
708 | 711 |
709 DCHECK(bottom_view_); | 712 DCHECK(bottom_view_); |
710 DCHECK_EQ(this, bottom_view_->parent()); | 713 DCHECK_EQ(this, bottom_view_->parent()); |
711 | 714 |
712 std::vector<ButtonInfo> buttons = notification.buttons(); | |
713 for (size_t i = 0; i < buttons.size(); ++i) { | 715 for (size_t i = 0; i < buttons.size(); ++i) { |
714 views::View* separator = new views::ImageView(); | |
715 separator->SetBorder(MakeSeparatorBorder(1, 0, kButtonSeparatorColor)); | |
716 separators_.push_back(separator); | |
717 bottom_view_->AddChildView(separator); | |
718 NotificationButton* button = new NotificationButton(this); | |
719 ButtonInfo button_info = buttons[i]; | 716 ButtonInfo button_info = buttons[i]; |
720 button->SetTitle(button_info.title); | 717 if (new_buttons) { |
721 button->SetIcon(button_info.icon.AsImageSkia()); | 718 views::View* separator = new views::ImageView(); |
722 action_buttons_.push_back(button); | 719 separator->SetBorder(MakeSeparatorBorder(1, 0, kButtonSeparatorColor)); |
723 bottom_view_->AddChildView(button); | 720 separators_.push_back(separator); |
| 721 bottom_view_->AddChildView(separator); |
| 722 NotificationButton* button = new NotificationButton(this); |
| 723 button->SetTitle(button_info.title); |
| 724 button->SetIcon(button_info.icon.AsImageSkia()); |
| 725 action_buttons_.push_back(button); |
| 726 bottom_view_->AddChildView(button); |
| 727 } else { |
| 728 action_buttons_[i]->SetTitle(button_info.title); |
| 729 action_buttons_[i]->SetIcon(button_info.icon.AsImageSkia()); |
| 730 action_buttons_[i]->SchedulePaint(); |
| 731 action_buttons_[i]->Layout(); |
| 732 } |
| 733 } |
| 734 |
| 735 if (new_buttons) { |
| 736 Layout(); |
| 737 views::Widget* widget = GetWidget(); |
| 738 if (widget != NULL) { |
| 739 widget->SetSize(widget->GetContentsView()->GetPreferredSize()); |
| 740 GetWidget()->SynthesizeMouseMoveEvent(); |
| 741 } |
724 } | 742 } |
725 } | 743 } |
726 | 744 |
727 int NotificationView::GetMessageLineLimit(int title_lines, int width) const { | 745 int NotificationView::GetMessageLineLimit(int title_lines, int width) const { |
728 // Image notifications require that the image must be kept flush against | 746 // Image notifications require that the image must be kept flush against |
729 // their icons, but we can allow more text if no image. | 747 // their icons, but we can allow more text if no image. |
730 int effective_title_lines = std::max(0, title_lines - 1); | 748 int effective_title_lines = std::max(0, title_lines - 1); |
731 int line_reduction_from_title = (image_view_ ? 1 : 2) * effective_title_lines; | 749 int line_reduction_from_title = (image_view_ ? 1 : 2) * effective_title_lines; |
732 if (!image_view_) { | 750 if (!image_view_) { |
733 // Title lines are counted as twice as big as message lines for the purpose | 751 // Title lines are counted as twice as big as message lines for the purpose |
(...skipping 25 matching lines...) Expand all Loading... |
759 | 777 |
760 return message_line_limit; | 778 return message_line_limit; |
761 } | 779 } |
762 | 780 |
763 int NotificationView::GetMessageHeight(int width, int limit) const { | 781 int NotificationView::GetMessageHeight(int width, int limit) const { |
764 return message_view_ ? | 782 return message_view_ ? |
765 message_view_->GetSizeForWidthAndLines(width, limit).height() : 0; | 783 message_view_->GetSizeForWidthAndLines(width, limit).height() : 0; |
766 } | 784 } |
767 | 785 |
768 } // namespace message_center | 786 } // namespace message_center |
OLD | NEW |