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
3 // found in the LICENSE file.
5 #include "ui/message_center/views/message_center_bubble.h"
7 #include "ui/base/l10n/l10n_util.h"
8 #include "ui/message_center/message_center_style.h"
9 #include "ui/message_center/views/message_center_view.h"
10 #include "ui/strings/grit/ui_strings.h"
11 #include "ui/views/layout/fill_layout.h"
12 #include "ui/views/widget/widget.h"
14 namespace message_center
{
16 // ContentsView ////////////////////////////////////////////////////////////////
18 // Handles changes in MessageCenterView sizes.
19 class ContentsView
: public views::View
{
21 explicit ContentsView(MessageCenterBubble
* bubble
, views::View
* contents
);
22 ~ContentsView() override
;
24 // Overridden from views::View:
25 int GetHeightForWidth(int width
) const override
;
28 // Overridden from views::View:
29 void ChildPreferredSizeChanged(View
* child
) override
;
32 base::WeakPtr
<MessageCenterBubble
> bubble_
;
34 DISALLOW_COPY_AND_ASSIGN(ContentsView
);
37 ContentsView::ContentsView(MessageCenterBubble
* bubble
, views::View
* contents
)
38 : bubble_(bubble
->AsWeakPtr()) {
39 SetLayoutManager(new views::FillLayout());
40 AddChildView(contents
);
43 ContentsView::~ContentsView() {
46 int ContentsView::GetHeightForWidth(int width
) const {
47 DCHECK_EQ(1, child_count());
48 int contents_width
= std::max(width
- GetInsets().width(), 0);
49 int contents_height
= child_at(0)->GetHeightForWidth(contents_width
);
50 return contents_height
+ GetInsets().height();
53 void ContentsView::ChildPreferredSizeChanged(View
* child
) {
54 // TODO(dharcourt): Reduce the amount of updating this requires.
56 bubble_
->bubble_view()->UpdateBubble();
59 // MessageCenterBubble /////////////////////////////////////////////////////////
61 MessageCenterBubble::MessageCenterBubble(MessageCenter
* message_center
,
62 MessageCenterTray
* tray
,
63 bool first_item_has_no_margin
)
64 : MessageBubbleBase(message_center
, tray
),
65 message_center_view_(NULL
),
66 initially_settings_visible_(false),
67 first_item_has_no_margin_(first_item_has_no_margin
),
68 title_(l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_FOOTER_TITLE
)) {
71 MessageCenterBubble::~MessageCenterBubble() {
74 void MessageCenterBubble::SetSettingsVisible() {
75 if (message_center_view_
)
76 message_center_view_
->SetSettingsVisible(true);
78 initially_settings_visible_
= true;
81 views::TrayBubbleView::InitParams
MessageCenterBubble::GetInitParams(
82 views::TrayBubbleView::AnchorAlignment anchor_alignment
) {
83 views::TrayBubbleView::InitParams init_params
=
84 GetDefaultInitParams(anchor_alignment
);
85 init_params
.min_width
+= kMarginBetweenItems
* 2;
86 init_params
.max_width
+= kMarginBetweenItems
* 2;
87 init_params
.max_height
= max_height();
88 init_params
.can_activate
= true;
89 init_params
.first_item_has_no_margin
= first_item_has_no_margin_
;
93 void MessageCenterBubble::InitializeContents(
94 views::TrayBubbleView
* new_bubble_view
) {
95 set_bubble_view(new_bubble_view
);
96 bubble_view()->GetWidget()->AddObserver(this);
97 message_center_view_
= new MessageCenterView(
101 initially_settings_visible_
,
102 false, /* MessageCenterBubble should be used only on ChromeOS.
103 Message center is never shown top down in ChromeOS. */
105 bubble_view()->AddChildView(new ContentsView(this, message_center_view_
));
106 // Resize the content of the bubble view to the given bubble size. This is
107 // necessary in case of the bubble border forcing a bigger size then the
108 // |new_bubble_view| actually wants. See crbug.com/169390.
109 bubble_view()->Layout();
113 void MessageCenterBubble::OnBubbleViewDestroyed() {
114 message_center_view_
= NULL
;
117 void MessageCenterBubble::UpdateBubbleView() {
119 return; // Could get called after view is closed
120 const NotificationList::Notifications
& notifications
=
121 message_center()->GetVisibleNotifications();
122 message_center_view_
->SetNotifications(notifications
);
123 bubble_view()->GetWidget()->Show();
124 bubble_view()->UpdateBubble();
127 void MessageCenterBubble::OnWidgetClosing(views::Widget
* widget
) {
128 bubble_view()->GetWidget()->RemoveObserver(this);
129 if (message_center_view_
)
130 message_center_view_
->SetIsClosing(true);
133 void MessageCenterBubble::OnMouseEnteredView() {
136 void MessageCenterBubble::OnMouseExitedView() {
139 size_t MessageCenterBubble::NumMessageViewsForTest() const {
140 return message_center_view_
->NumMessageViewsForTest();
143 } // namespace message_center