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/message_center/message_center_style.h"
8 #include "ui/message_center/message_center_util.h"
9 #include "ui/message_center/views/message_center_view.h"
10 #include "ui/views/layout/fill_layout.h"
11 #include "ui/views/widget/widget.h"
13 namespace message_center
{
15 // ContentsView ////////////////////////////////////////////////////////////////
17 // Handles changes in MessageCenterView sizes.
18 class ContentsView
: public views::View
{
20 explicit ContentsView(MessageCenterBubble
* bubble
, views::View
* contents
);
21 virtual ~ContentsView();
23 // Overridden from views::View:
24 virtual int GetHeightForWidth(int width
) OVERRIDE
;
27 // Overridden from views::View:
28 virtual void ChildPreferredSizeChanged(View
* child
) OVERRIDE
;
31 base::WeakPtr
<MessageCenterBubble
> bubble_
;
33 DISALLOW_COPY_AND_ASSIGN(ContentsView
);
36 ContentsView::ContentsView(MessageCenterBubble
* bubble
, views::View
* contents
)
37 : bubble_(bubble
->AsWeakPtr()) {
38 SetLayoutManager(new views::FillLayout());
39 AddChildView(contents
);
42 ContentsView::~ContentsView() {
45 int ContentsView::GetHeightForWidth(int width
) {
46 DCHECK_EQ(1, child_count());
47 int contents_width
= std::max(width
- GetInsets().width(), 0);
48 int contents_height
= child_at(0)->GetHeightForWidth(contents_width
);
49 return contents_height
+ GetInsets().height();
52 void ContentsView::ChildPreferredSizeChanged(View
* child
) {
53 // TODO(dharcourt): Reduce the amount of updating this requires.
55 bubble_
->bubble_view()->UpdateBubble();
58 // MessageCenterBubble /////////////////////////////////////////////////////////
60 MessageCenterBubble::MessageCenterBubble(MessageCenter
* message_center
,
61 MessageCenterTray
* tray
,
62 bool first_item_has_no_margin
)
63 : MessageBubbleBase(message_center
, tray
),
64 message_center_view_(NULL
),
65 initially_settings_visible_(false),
66 first_item_has_no_margin_(first_item_has_no_margin
) {
69 MessageCenterBubble::~MessageCenterBubble() {
72 void MessageCenterBubble::SetSettingsVisible() {
73 if (message_center_view_
)
74 message_center_view_
->SetSettingsVisible(true);
76 initially_settings_visible_
= true;
79 views::TrayBubbleView::InitParams
MessageCenterBubble::GetInitParams(
80 views::TrayBubbleView::AnchorAlignment anchor_alignment
) {
81 views::TrayBubbleView::InitParams init_params
=
82 GetDefaultInitParams(anchor_alignment
);
83 init_params
.min_width
+= kMarginBetweenItems
* 2;
84 init_params
.max_width
+= kMarginBetweenItems
* 2;
85 init_params
.max_height
= max_height();
86 init_params
.can_activate
= true;
87 init_params
.first_item_has_no_margin
= first_item_has_no_margin_
;
91 void MessageCenterBubble::InitializeContents(
92 views::TrayBubbleView
* new_bubble_view
) {
93 set_bubble_view(new_bubble_view
);
94 bubble_view()->GetWidget()->AddObserver(this);
95 message_center_view_
= new MessageCenterView(
99 initially_settings_visible_
,
100 false /* MessageCenterBubble should be used only on ChromeOS.
101 Message center is never shown top down in ChromeOS. */);
102 bubble_view()->AddChildView(new ContentsView(this, message_center_view_
));
103 // Resize the content of the bubble view to the given bubble size. This is
104 // necessary in case of the bubble border forcing a bigger size then the
105 // |new_bubble_view| actually wants. See crbug.com/169390.
106 bubble_view()->Layout();
110 void MessageCenterBubble::OnBubbleViewDestroyed() {
111 message_center_view_
= NULL
;
114 void MessageCenterBubble::UpdateBubbleView() {
116 return; // Could get called after view is closed
117 const NotificationList::Notifications
& notifications
=
118 message_center()->GetNotifications();
119 message_center_view_
->SetNotifications(notifications
);
120 bubble_view()->GetWidget()->Show();
121 bubble_view()->UpdateBubble();
124 void MessageCenterBubble::OnWidgetClosing(views::Widget
* widget
) {
125 bubble_view()->GetWidget()->RemoveObserver(this);
126 if (message_center_view_
)
127 message_center_view_
->SetIsClosing(true);
130 void MessageCenterBubble::OnMouseEnteredView() {
133 void MessageCenterBubble::OnMouseExitedView() {
136 size_t MessageCenterBubble::NumMessageViewsForTest() const {
137 return message_center_view_
->NumMessageViewsForTest();
140 } // namespace message_center