1 // Copyright (c) 2013 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_view.h"
10 #include "base/command_line.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/stl_util.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/gfx/animation/multi_animation.h"
16 #include "ui/gfx/animation/slide_animation.h"
17 #include "ui/gfx/canvas.h"
18 #include "ui/gfx/geometry/insets.h"
19 #include "ui/gfx/geometry/rect.h"
20 #include "ui/gfx/geometry/size.h"
21 #include "ui/message_center/message_center.h"
22 #include "ui/message_center/message_center_style.h"
23 #include "ui/message_center/message_center_switches.h"
24 #include "ui/message_center/message_center_tray.h"
25 #include "ui/message_center/message_center_types.h"
26 #include "ui/message_center/views/message_center_button_bar.h"
27 #include "ui/message_center/views/message_view.h"
28 #include "ui/message_center/views/message_view_context_menu_controller.h"
29 #include "ui/message_center/views/notification_view.h"
30 #include "ui/message_center/views/notifier_settings_view.h"
31 #include "ui/resources/grit/ui_resources.h"
32 #include "ui/strings/grit/ui_strings.h"
33 #include "ui/views/animation/bounds_animator.h"
34 #include "ui/views/animation/bounds_animator_observer.h"
35 #include "ui/views/background.h"
36 #include "ui/views/border.h"
37 #include "ui/views/controls/button/button.h"
38 #include "ui/views/controls/label.h"
39 #include "ui/views/controls/scroll_view.h"
40 #include "ui/views/controls/scrollbar/overlay_scroll_bar.h"
41 #include "ui/views/layout/box_layout.h"
42 #include "ui/views/layout/fill_layout.h"
43 #include "ui/views/widget/widget.h"
45 namespace message_center
{
49 const SkColor kNoNotificationsTextColor
= SkColorSetRGB(0xb4, 0xb4, 0xb4);
50 #if defined(OS_LINUX) && defined(OS_CHROMEOS)
51 const SkColor kTransparentColor
= SkColorSetARGB(0, 0, 0, 0);
53 const int kAnimateClearingNextNotificationDelayMS
= 40;
55 const int kDefaultAnimationDurationMs
= 120;
56 const int kDefaultFrameRateHz
= 60;
58 void SetViewHierarchyEnabled(views::View
* view
, bool enabled
) {
59 for (int i
= 0; i
< view
->child_count(); i
++)
60 SetViewHierarchyEnabled(view
->child_at(i
), enabled
);
61 view
->SetEnabled(enabled
);
66 class NoNotificationMessageView
: public views::View
{
68 NoNotificationMessageView();
69 ~NoNotificationMessageView() override
;
71 // Overridden from views::View.
72 gfx::Size
GetPreferredSize() const override
;
73 int GetHeightForWidth(int width
) const override
;
74 void Layout() override
;
79 DISALLOW_COPY_AND_ASSIGN(NoNotificationMessageView
);
82 NoNotificationMessageView::NoNotificationMessageView() {
83 label_
= new views::Label(l10n_util::GetStringUTF16(
84 IDS_MESSAGE_CENTER_NO_MESSAGES
));
85 label_
->SetAutoColorReadabilityEnabled(false);
86 label_
->SetEnabledColor(kNoNotificationsTextColor
);
87 // Set transparent background to ensure that subpixel rendering
88 // is disabled. See crbug.com/169056
89 #if defined(OS_LINUX) && defined(OS_CHROMEOS)
90 label_
->SetBackgroundColor(kTransparentColor
);
95 NoNotificationMessageView::~NoNotificationMessageView() {
98 gfx::Size
NoNotificationMessageView::GetPreferredSize() const {
99 return gfx::Size(kMinScrollViewHeight
, label_
->GetPreferredSize().width());
102 int NoNotificationMessageView::GetHeightForWidth(int width
) const {
103 return kMinScrollViewHeight
;
106 void NoNotificationMessageView::Layout() {
107 int text_height
= label_
->GetHeightForWidth(width());
108 int margin
= (height() - text_height
) / 2;
109 label_
->SetBounds(0, margin
, width(), text_height
);
112 // Displays a list of messages for rich notifications. Functions as an array of
113 // MessageViews and animates them on transitions. It also supports
115 class MessageListView
: public views::View
,
116 public views::BoundsAnimatorObserver
{
118 explicit MessageListView(MessageCenterView
* message_center_view
,
120 ~MessageListView() override
;
122 void AddNotificationAt(MessageView
* view
, int i
);
123 void RemoveNotification(MessageView
* view
);
124 void UpdateNotification(MessageView
* view
, const Notification
& notification
);
125 void SetRepositionTarget(const gfx::Rect
& target_rect
);
126 void ResetRepositionSession();
127 void ClearAllNotifications(const gfx::Rect
& visible_scroll_rect
);
130 // Overridden from views::View.
131 void Layout() override
;
132 gfx::Size
GetPreferredSize() const override
;
133 int GetHeightForWidth(int width
) const override
;
134 void PaintChildren(const ui::PaintContext
& context
) override
;
135 void ReorderChildLayers(ui::Layer
* parent_layer
) override
;
137 // Overridden from views::BoundsAnimatorObserver.
138 void OnBoundsAnimatorProgressed(views::BoundsAnimator
* animator
) override
;
139 void OnBoundsAnimatorDone(views::BoundsAnimator
* animator
) override
;
142 bool IsValidChild(const views::View
* child
) const;
143 void DoUpdateIfPossible();
145 // Animates all notifications below target upwards to align with the top of
146 // the last closed notification.
147 void AnimateNotificationsBelowTarget();
148 // Animates all notifications above target downwards to align with the top of
149 // the last closed notification.
150 void AnimateNotificationsAboveTarget();
152 // Schedules animation for a child to the specified position. Returns false
153 // if |child| will disappear after the animation.
154 bool AnimateChild(views::View
* child
, int top
, int height
);
156 // Animate clearing one notification.
157 void AnimateClearingOneNotification();
158 MessageCenterView
* message_center_view() const {
159 return message_center_view_
;
162 MessageCenterView
* message_center_view_
; // Weak reference.
163 // The top position of the reposition target rectangle.
166 bool has_deferred_task_
;
167 bool clear_all_started_
;
169 std::set
<views::View
*> adding_views_
;
170 std::set
<views::View
*> deleting_views_
;
171 std::set
<views::View
*> deleted_when_done_
;
172 std::list
<views::View
*> clearing_all_views_
;
173 scoped_ptr
<views::BoundsAnimator
> animator_
;
174 base::WeakPtrFactory
<MessageListView
> weak_ptr_factory_
;
176 DISALLOW_COPY_AND_ASSIGN(MessageListView
);
179 MessageListView::MessageListView(MessageCenterView
* message_center_view
,
181 : message_center_view_(message_center_view
),
184 has_deferred_task_(false),
185 clear_all_started_(false),
187 weak_ptr_factory_(this) {
188 views::BoxLayout
* layout
=
189 new views::BoxLayout(views::BoxLayout::kVertical
, 0, 0, 1);
190 layout
->SetDefaultFlex(1);
191 SetLayoutManager(layout
);
193 // Set the margin to 0 for the layout. BoxLayout assumes the same margin
194 // for top and bottom, but the bottom margin here should be smaller
195 // because of the shadow of message view. Use an empty border instead
196 // to provide this margin.
197 gfx::Insets shadow_insets
= MessageView::GetShadowInsets();
198 set_background(views::Background::CreateSolidBackground(
199 kMessageCenterBackgroundColor
));
200 SetBorder(views::Border::CreateEmptyBorder(
201 top_down
? 0 : kMarginBetweenItems
- shadow_insets
.top(), /* top */
202 kMarginBetweenItems
- shadow_insets
.left(), /* left */
203 top_down
? kMarginBetweenItems
- shadow_insets
.bottom() : 0, /* bottom */
204 kMarginBetweenItems
- shadow_insets
.right() /* right */));
207 MessageListView::~MessageListView() {
209 animator_
->RemoveObserver(this);
212 void MessageListView::Layout() {
216 gfx::Rect child_area
= GetContentsBounds();
217 int top
= child_area
.y();
219 kMarginBetweenItems
- MessageView::GetShadowInsets().bottom();
221 for (int i
= 0; i
< child_count(); ++i
) {
222 views::View
* child
= child_at(i
);
223 if (!child
->visible())
225 int height
= child
->GetHeightForWidth(child_area
.width());
226 child
->SetBounds(child_area
.x(), top
, child_area
.width(), height
);
227 top
+= height
+ between_items
;
231 void MessageListView::AddNotificationAt(MessageView
* view
, int index
) {
232 // |index| refers to a position in a subset of valid children. |real_index|
233 // in a list includes the invalid children, so we compute the real index by
234 // walking the list until |index| number of valid children are encountered,
235 // or to the end of the list.
237 while (real_index
< child_count()) {
238 if (IsValidChild(child_at(real_index
))) {
246 AddChildViewAt(view
, real_index
);
247 if (GetContentsBounds().IsEmpty())
250 adding_views_
.insert(view
);
251 DoUpdateIfPossible();
254 void MessageListView::RemoveNotification(MessageView
* view
) {
255 DCHECK_EQ(view
->parent(), this);
256 if (GetContentsBounds().IsEmpty()) {
260 deleting_views_
.insert(view
);
263 animator_
->StopAnimatingView(view
);
266 DoUpdateIfPossible();
270 void MessageListView::UpdateNotification(MessageView
* view
,
271 const Notification
& notification
) {
272 int index
= GetIndexOf(view
);
273 DCHECK_LE(0, index
); // GetIndexOf is negative if not a child.
276 animator_
->StopAnimatingView(view
);
277 if (deleting_views_
.find(view
) != deleting_views_
.end())
278 deleting_views_
.erase(view
);
279 if (deleted_when_done_
.find(view
) != deleted_when_done_
.end())
280 deleted_when_done_
.erase(view
);
281 view
->UpdateWithNotification(notification
);
282 DoUpdateIfPossible();
285 gfx::Size
MessageListView::GetPreferredSize() const {
287 for (int i
= 0; i
< child_count(); i
++) {
288 const views::View
* child
= child_at(i
);
289 if (IsValidChild(child
))
290 width
= std::max(width
, child
->GetPreferredSize().width());
293 return gfx::Size(width
+ GetInsets().width(),
294 GetHeightForWidth(width
+ GetInsets().width()));
297 int MessageListView::GetHeightForWidth(int width
) const {
298 if (fixed_height_
> 0)
299 return fixed_height_
;
301 width
-= GetInsets().width();
304 for (int i
= 0; i
< child_count(); ++i
) {
305 const views::View
* child
= child_at(i
);
306 if (!IsValidChild(child
))
308 height
+= child
->GetHeightForWidth(width
) + padding
;
309 padding
= kMarginBetweenItems
- MessageView::GetShadowInsets().bottom();
312 return height
+ GetInsets().height();
315 void MessageListView::PaintChildren(const ui::PaintContext
& context
) {
316 // Paint in the inversed order. Otherwise upper notification may be
317 // hidden by the lower one.
318 for (int i
= child_count() - 1; i
>= 0; --i
) {
319 if (!child_at(i
)->layer())
320 child_at(i
)->Paint(context
);
324 void MessageListView::ReorderChildLayers(ui::Layer
* parent_layer
) {
325 // Reorder children to stack the last child layer at the top. Otherwise
326 // upper notification may be hidden by the lower one.
327 for (int i
= 0; i
< child_count(); ++i
) {
328 if (child_at(i
)->layer())
329 parent_layer
->StackAtBottom(child_at(i
)->layer());
333 void MessageListView::SetRepositionTarget(const gfx::Rect
& target
) {
334 reposition_top_
= target
.y();
335 fixed_height_
= GetHeightForWidth(width());
338 void MessageListView::ResetRepositionSession() {
339 // Don't call DoUpdateIfPossible(), but let Layout() do the task without
340 // animation. Reset will cause the change of the bubble size itself, and
341 // animation from the old location will look weird.
342 if (reposition_top_
>= 0 && animator_
.get()) {
343 has_deferred_task_
= false;
344 // cancel cause OnBoundsAnimatorDone which deletes |deleted_when_done_|.
346 STLDeleteContainerPointers(deleting_views_
.begin(), deleting_views_
.end());
347 deleting_views_
.clear();
348 adding_views_
.clear();
352 reposition_top_
= -1;
356 void MessageListView::ClearAllNotifications(
357 const gfx::Rect
& visible_scroll_rect
) {
358 for (int i
= 0; i
< child_count(); ++i
) {
359 views::View
* child
= child_at(i
);
360 if (!child
->visible())
362 if (gfx::IntersectRects(child
->bounds(), visible_scroll_rect
).IsEmpty())
364 clearing_all_views_
.push_back(child
);
366 DoUpdateIfPossible();
369 void MessageListView::OnBoundsAnimatorProgressed(
370 views::BoundsAnimator
* animator
) {
371 DCHECK_EQ(animator_
.get(), animator
);
372 for (std::set
<views::View
*>::iterator iter
= deleted_when_done_
.begin();
373 iter
!= deleted_when_done_
.end(); ++iter
) {
374 const gfx::SlideAnimation
* animation
= animator
->GetAnimationForView(*iter
);
376 (*iter
)->layer()->SetOpacity(animation
->CurrentValueBetween(1.0, 0.0));
380 void MessageListView::OnBoundsAnimatorDone(views::BoundsAnimator
* animator
) {
381 STLDeleteContainerPointers(
382 deleted_when_done_
.begin(), deleted_when_done_
.end());
383 deleted_when_done_
.clear();
385 if (clear_all_started_
) {
386 clear_all_started_
= false;
387 message_center_view()->OnAllNotificationsCleared();
390 if (has_deferred_task_
) {
391 has_deferred_task_
= false;
392 DoUpdateIfPossible();
396 GetWidget()->SynthesizeMouseMoveEvent();
399 bool MessageListView::IsValidChild(const views::View
* child
) const {
400 return child
->visible() &&
401 deleting_views_
.find(const_cast<views::View
*>(child
)) ==
402 deleting_views_
.end() &&
403 deleted_when_done_
.find(const_cast<views::View
*>(child
)) ==
404 deleted_when_done_
.end();
407 void MessageListView::DoUpdateIfPossible() {
408 gfx::Rect child_area
= GetContentsBounds();
409 if (child_area
.IsEmpty())
412 if (animator_
.get() && animator_
->IsAnimating()) {
413 has_deferred_task_
= true;
417 if (!animator_
.get()) {
418 animator_
.reset(new views::BoundsAnimator(this));
419 animator_
->AddObserver(this);
422 if (!clearing_all_views_
.empty()) {
423 AnimateClearingOneNotification();
428 base::CommandLine::ForCurrentProcess()->HasSwitch(
429 switches::kEnableMessageCenterAlwaysScrollUpUponNotificationRemoval
))
430 AnimateNotificationsBelowTarget();
432 AnimateNotificationsAboveTarget();
434 adding_views_
.clear();
435 deleting_views_
.clear();
438 void MessageListView::AnimateNotificationsBelowTarget() {
440 for (int i
= 0; i
< child_count(); ++i
) {
441 views::View
* child
= child_at(i
);
442 if (!IsValidChild(child
)) {
443 AnimateChild(child
, child
->y(), child
->height());
444 } else if (reposition_top_
< 0 || child
->y() > reposition_top_
) {
445 // Find first notification below target (or all notifications if no
451 if (last_index
> 0) {
453 kMarginBetweenItems
- MessageView::GetShadowInsets().bottom();
454 int top
= (reposition_top_
> 0) ? reposition_top_
: GetInsets().top();
456 for (int i
= last_index
; i
< child_count(); ++i
) {
457 // Animate notifications below target upwards.
458 views::View
* child
= child_at(i
);
459 if (AnimateChild(child
, top
, child
->height()))
460 top
+= child
->height() + between_items
;
465 void MessageListView::AnimateNotificationsAboveTarget() {
467 for (int i
= child_count() - 1; i
>= 0; --i
) {
468 views::View
* child
= child_at(i
);
469 if (!IsValidChild(child
)) {
470 AnimateChild(child
, child
->y(), child
->height());
471 } else if (reposition_top_
< 0 || child
->y() < reposition_top_
) {
472 // Find first notification above target (or all notifications if no
478 if (last_index
>= 0) {
480 kMarginBetweenItems
- MessageView::GetShadowInsets().bottom();
481 int bottom
= (reposition_top_
> 0)
482 ? reposition_top_
+ child_at(last_index
)->height()
483 : GetHeightForWidth(width()) - GetInsets().bottom();
484 for (int i
= last_index
; i
>= 0; --i
) {
485 // Animate notifications above target downwards.
486 views::View
* child
= child_at(i
);
487 if (AnimateChild(child
, bottom
- child
->height(), child
->height()))
488 bottom
-= child
->height() + between_items
;
493 bool MessageListView::AnimateChild(views::View
* child
, int top
, int height
) {
494 gfx::Rect child_area
= GetContentsBounds();
495 if (adding_views_
.find(child
) != adding_views_
.end()) {
496 child
->SetBounds(child_area
.right(), top
, child_area
.width(), height
);
497 animator_
->AnimateViewTo(
498 child
, gfx::Rect(child_area
.x(), top
, child_area
.width(), height
));
499 } else if (deleting_views_
.find(child
) != deleting_views_
.end()) {
500 DCHECK(child
->layer());
501 // No moves, but animate to fade-out.
502 animator_
->AnimateViewTo(child
, child
->bounds());
503 deleted_when_done_
.insert(child
);
506 gfx::Rect
target(child_area
.x(), top
, child_area
.width(), height
);
507 if (child
->bounds().origin() != target
.origin())
508 animator_
->AnimateViewTo(child
, target
);
510 child
->SetBoundsRect(target
);
515 void MessageListView::AnimateClearingOneNotification() {
516 DCHECK(!clearing_all_views_
.empty());
518 clear_all_started_
= true;
520 views::View
* child
= clearing_all_views_
.front();
521 clearing_all_views_
.pop_front();
523 // Slide from left to right.
524 gfx::Rect new_bounds
= child
->bounds();
525 new_bounds
.set_x(new_bounds
.right() + kMarginBetweenItems
);
526 animator_
->AnimateViewTo(child
, new_bounds
);
528 // Schedule to start sliding out next notification after a short delay.
529 if (!clearing_all_views_
.empty()) {
530 base::MessageLoop::current()->PostDelayedTask(
532 base::Bind(&MessageListView::AnimateClearingOneNotification
,
533 weak_ptr_factory_
.GetWeakPtr()),
534 base::TimeDelta::FromMilliseconds(
535 kAnimateClearingNextNotificationDelayMS
));
539 // MessageCenterView ///////////////////////////////////////////////////////////
541 MessageCenterView::MessageCenterView(MessageCenter
* message_center
,
542 MessageCenterTray
* tray
,
544 bool initially_settings_visible
,
546 const base::string16
& title
)
547 : message_center_(message_center
),
550 settings_view_(NULL
),
553 settings_visible_(initially_settings_visible
),
559 context_menu_controller_(new MessageViewContextMenuController(this)) {
560 message_center_
->AddObserver(this);
561 set_notify_enter_exit_on_child(true);
562 set_background(views::Background::CreateSolidBackground(
563 kMessageCenterBackgroundColor
));
565 NotifierSettingsProvider
* notifier_settings_provider
=
566 message_center_
->GetNotifierSettingsProvider();
567 button_bar_
= new MessageCenterButtonBar(this,
569 notifier_settings_provider
,
570 initially_settings_visible
,
573 const int button_height
= button_bar_
->GetPreferredSize().height();
575 scroller_
= new views::ScrollView();
576 scroller_
->ClipHeightTo(kMinScrollViewHeight
, max_height
- button_height
);
577 scroller_
->SetVerticalScrollBar(new views::OverlayScrollBar(false));
578 scroller_
->set_background(
579 views::Background::CreateSolidBackground(kMessageCenterBackgroundColor
));
581 scroller_
->SetPaintToLayer(true);
582 scroller_
->SetFillsBoundsOpaquely(false);
583 scroller_
->layer()->SetMasksToBounds(true);
585 empty_list_view_
.reset(new NoNotificationMessageView
);
586 empty_list_view_
->set_owned_by_client();
587 message_list_view_
.reset(new MessageListView(this, top_down
));
588 message_list_view_
->set_owned_by_client();
590 // We want to swap the contents of the scroll view between the empty list
591 // view and the message list view, without constructing them afresh each
592 // time. So, since the scroll view deletes old contents each time you
593 // set the contents (regardless of the |owned_by_client_| setting) we need
594 // an intermediate view for the contents whose children we can swap in and
596 views::View
* scroller_contents
= new views::View();
597 scroller_contents
->SetLayoutManager(new views::FillLayout());
598 scroller_contents
->AddChildView(empty_list_view_
.get());
599 scroller_
->SetContents(scroller_contents
);
601 settings_view_
= new NotifierSettingsView(notifier_settings_provider
);
603 if (initially_settings_visible
)
604 scroller_
->SetVisible(false);
606 settings_view_
->SetVisible(false);
608 AddChildView(scroller_
);
609 AddChildView(settings_view_
);
610 AddChildView(button_bar_
);
613 MessageCenterView::~MessageCenterView() {
615 message_center_
->RemoveObserver(this);
618 void MessageCenterView::SetNotifications(
619 const NotificationList::Notifications
& notifications
) {
623 notification_views_
.clear();
626 for (NotificationList::Notifications::const_iterator iter
=
627 notifications
.begin(); iter
!= notifications
.end(); ++iter
) {
628 AddNotificationAt(*(*iter
), index
++);
630 message_center_
->DisplayedNotification(
631 (*iter
)->id(), message_center::DISPLAY_SOURCE_MESSAGE_CENTER
);
632 if (notification_views_
.size() >= kMaxVisibleMessageCenterNotifications
)
636 NotificationsChanged();
637 scroller_
->RequestFocus();
640 void MessageCenterView::SetSettingsVisible(bool visible
) {
644 if (visible
== settings_visible_
)
647 settings_visible_
= visible
;
650 source_view_
= scroller_
;
651 target_view_
= settings_view_
;
653 source_view_
= settings_view_
;
654 target_view_
= scroller_
;
656 source_height_
= source_view_
->GetHeightForWidth(width());
657 target_height_
= target_view_
->GetHeightForWidth(width());
659 gfx::MultiAnimation::Parts parts
;
660 // First part: slide resize animation.
661 parts
.push_back(gfx::MultiAnimation::Part(
662 (source_height_
== target_height_
) ? 0 : kDefaultAnimationDurationMs
,
663 gfx::Tween::EASE_OUT
));
664 // Second part: fade-out the source_view.
665 if (source_view_
->layer()) {
666 parts
.push_back(gfx::MultiAnimation::Part(
667 kDefaultAnimationDurationMs
, gfx::Tween::LINEAR
));
669 parts
.push_back(gfx::MultiAnimation::Part());
671 // Third part: fade-in the target_view.
672 if (target_view_
->layer()) {
673 parts
.push_back(gfx::MultiAnimation::Part(
674 kDefaultAnimationDurationMs
, gfx::Tween::LINEAR
));
675 target_view_
->layer()->SetOpacity(0);
676 target_view_
->SetVisible(true);
678 parts
.push_back(gfx::MultiAnimation::Part());
680 settings_transition_animation_
.reset(new gfx::MultiAnimation(
681 parts
, base::TimeDelta::FromMicroseconds(1000000 / kDefaultFrameRateHz
)));
682 settings_transition_animation_
->set_delegate(this);
683 settings_transition_animation_
->set_continuous(false);
684 settings_transition_animation_
->Start();
686 button_bar_
->SetBackArrowVisible(visible
);
689 void MessageCenterView::ClearAllNotifications() {
693 SetViewHierarchyEnabled(scroller_
, false);
694 button_bar_
->SetAllButtonsEnabled(false);
695 message_list_view_
->ClearAllNotifications(scroller_
->GetVisibleRect());
698 void MessageCenterView::OnAllNotificationsCleared() {
699 SetViewHierarchyEnabled(scroller_
, true);
700 button_bar_
->SetAllButtonsEnabled(true);
701 button_bar_
->SetCloseAllButtonEnabled(false);
702 message_center_
->RemoveAllVisibleNotifications(true); // Action by user.
705 size_t MessageCenterView::NumMessageViewsForTest() const {
706 return message_list_view_
->child_count();
709 void MessageCenterView::OnSettingsChanged() {
710 scroller_
->InvalidateLayout();
711 PreferredSizeChanged();
715 void MessageCenterView::SetIsClosing(bool is_closing
) {
716 is_closing_
= is_closing
;
718 message_center_
->RemoveObserver(this);
720 message_center_
->AddObserver(this);
723 void MessageCenterView::Layout() {
727 int button_height
= button_bar_
->GetHeightForWidth(width()) +
728 button_bar_
->GetInsets().height();
729 // Skip unnecessary re-layout of contents during the resize animation.
730 bool animating
= settings_transition_animation_
&&
731 settings_transition_animation_
->is_animating();
732 if (animating
&& settings_transition_animation_
->current_part_index() == 0) {
734 button_bar_
->SetBounds(
735 0, height() - button_height
, width(), button_height
);
740 scroller_
->SetBounds(0,
741 top_down_
? button_height
: 0,
743 height() - button_height
);
744 settings_view_
->SetBounds(0,
745 top_down_
? button_height
: 0,
747 height() - button_height
);
749 bool is_scrollable
= false;
750 if (scroller_
->visible())
751 is_scrollable
= scroller_
->height() < message_list_view_
->height();
753 is_scrollable
= settings_view_
->IsScrollable();
757 // Draw separator line on the top of the button bar if it is on the bottom
758 // or draw it at the bottom if the bar is on the top.
759 button_bar_
->SetBorder(views::Border::CreateSolidSidedBorder(
760 top_down_
? 0 : 1, 0, top_down_
? 1 : 0, 0, kFooterDelimiterColor
));
762 button_bar_
->SetBorder(views::Border::CreateEmptyBorder(
763 top_down_
? 0 : 1, 0, top_down_
? 1 : 0, 0));
765 button_bar_
->SchedulePaint();
767 button_bar_
->SetBounds(0,
768 top_down_
? 0 : height() - button_height
,
772 GetWidget()->GetRootView()->SchedulePaint();
775 gfx::Size
MessageCenterView::GetPreferredSize() const {
776 if (settings_transition_animation_
&&
777 settings_transition_animation_
->is_animating()) {
778 int content_width
= std::max(source_view_
->GetPreferredSize().width(),
779 target_view_
->GetPreferredSize().width());
780 int width
= std::max(content_width
,
781 button_bar_
->GetPreferredSize().width());
782 return gfx::Size(width
, GetHeightForWidth(width
));
786 for (int i
= 0; i
< child_count(); ++i
) {
787 const views::View
* child
= child_at(0);
788 if (child
->visible())
789 width
= std::max(width
, child
->GetPreferredSize().width());
791 return gfx::Size(width
, GetHeightForWidth(width
));
794 int MessageCenterView::GetHeightForWidth(int width
) const {
795 if (settings_transition_animation_
&&
796 settings_transition_animation_
->is_animating()) {
797 int content_height
= target_height_
;
798 if (settings_transition_animation_
->current_part_index() == 0) {
799 content_height
= settings_transition_animation_
->CurrentValueBetween(
800 source_height_
, target_height_
);
802 return button_bar_
->GetHeightForWidth(width
) + content_height
;
805 int content_height
= 0;
806 if (scroller_
->visible())
807 content_height
+= scroller_
->GetHeightForWidth(width
);
809 content_height
+= settings_view_
->GetHeightForWidth(width
);
810 return button_bar_
->GetHeightForWidth(width
) +
811 button_bar_
->GetInsets().height() + content_height
;
814 bool MessageCenterView::OnMouseWheel(const ui::MouseWheelEvent
& event
) {
815 // Do not rely on the default scroll event handler of ScrollView because
816 // the scroll happens only when the focus is on the ScrollView. The
817 // notification center will allow the scrolling even when the focus is on
819 if (scroller_
->bounds().Contains(event
.location()))
820 return scroller_
->OnMouseWheel(event
);
821 return views::View::OnMouseWheel(event
);
824 void MessageCenterView::OnMouseExited(const ui::MouseEvent
& event
) {
828 message_list_view_
->ResetRepositionSession();
829 NotificationsChanged();
832 void MessageCenterView::OnNotificationAdded(const std::string
& id
) {
834 const NotificationList::Notifications
& notifications
=
835 message_center_
->GetVisibleNotifications();
836 for (NotificationList::Notifications::const_iterator iter
=
837 notifications
.begin(); iter
!= notifications
.end();
839 if ((*iter
)->id() == id
) {
840 AddNotificationAt(*(*iter
), index
);
843 if (notification_views_
.size() >= kMaxVisibleMessageCenterNotifications
)
846 NotificationsChanged();
849 void MessageCenterView::OnNotificationRemoved(const std::string
& id
,
851 NotificationViewsMap::iterator view_iter
= notification_views_
.find(id
);
852 if (view_iter
== notification_views_
.end())
854 NotificationView
* view
= view_iter
->second
;
855 int index
= message_list_view_
->GetIndexOf(view
);
858 message_list_view_
->SetRepositionTarget(view
->bounds());
859 // Moves the keyboard focus to the next notification if the removed
860 // notification is focused so that the user can dismiss notifications
861 // without re-focusing by tab key.
862 if (view
->IsCloseButtonFocused() ||
863 view
== GetFocusManager()->GetFocusedView()) {
864 views::View
* next_focused_view
= NULL
;
865 if (message_list_view_
->child_count() > index
+ 1)
866 next_focused_view
= message_list_view_
->child_at(index
+ 1);
868 next_focused_view
= message_list_view_
->child_at(index
- 1);
870 if (next_focused_view
) {
871 if (view
->IsCloseButtonFocused()) {
872 // Safe cast since all views in MessageListView are MessageViews.
873 static_cast<MessageView
*>(
874 next_focused_view
)->RequestFocusOnCloseButton();
876 next_focused_view
->RequestFocus();
881 message_list_view_
->RemoveNotification(view
);
882 notification_views_
.erase(view_iter
);
883 NotificationsChanged();
886 void MessageCenterView::OnNotificationUpdated(const std::string
& id
) {
887 NotificationViewsMap::const_iterator view_iter
= notification_views_
.find(id
);
888 if (view_iter
== notification_views_
.end())
890 NotificationView
* view
= view_iter
->second
;
891 // TODO(dimich): add MessageCenter::GetVisibleNotificationById(id)
892 const NotificationList::Notifications
& notifications
=
893 message_center_
->GetVisibleNotifications();
894 for (NotificationList::Notifications::const_iterator iter
=
895 notifications
.begin(); iter
!= notifications
.end(); ++iter
) {
896 if ((*iter
)->id() == id
) {
897 int old_width
= view
->width();
898 int old_height
= view
->GetHeightForWidth(old_width
);
899 message_list_view_
->UpdateNotification(view
, **iter
);
900 if (view
->GetHeightForWidth(old_width
) != old_height
)
901 NotificationsChanged();
907 void MessageCenterView::ClickOnNotification(
908 const std::string
& notification_id
) {
909 message_center_
->ClickOnNotification(notification_id
);
912 void MessageCenterView::RemoveNotification(const std::string
& notification_id
,
914 message_center_
->RemoveNotification(notification_id
, by_user
);
917 scoped_ptr
<ui::MenuModel
> MessageCenterView::CreateMenuModel(
918 const NotifierId
& notifier_id
,
919 const base::string16
& display_source
) {
920 return tray_
->CreateNotificationMenuModel(notifier_id
, display_source
);
923 bool MessageCenterView::HasClickedListener(const std::string
& notification_id
) {
924 return message_center_
->HasClickedListener(notification_id
);
927 void MessageCenterView::ClickOnNotificationButton(
928 const std::string
& notification_id
,
930 message_center_
->ClickOnNotificationButton(notification_id
, button_index
);
933 void MessageCenterView::AnimationEnded(const gfx::Animation
* animation
) {
934 DCHECK_EQ(animation
, settings_transition_animation_
.get());
936 Visibility visibility
= target_view_
== settings_view_
937 ? VISIBILITY_SETTINGS
938 : VISIBILITY_MESSAGE_CENTER
;
939 message_center_
->SetVisibility(visibility
);
941 source_view_
->SetVisible(false);
942 target_view_
->SetVisible(true);
943 if (source_view_
->layer())
944 source_view_
->layer()->SetOpacity(1.0);
945 if (target_view_
->layer())
946 target_view_
->layer()->SetOpacity(1.0);
947 settings_transition_animation_
.reset();
948 PreferredSizeChanged();
952 void MessageCenterView::AnimationProgressed(const gfx::Animation
* animation
) {
953 DCHECK_EQ(animation
, settings_transition_animation_
.get());
954 PreferredSizeChanged();
955 if (settings_transition_animation_
->current_part_index() == 1 &&
956 source_view_
->layer()) {
957 source_view_
->layer()->SetOpacity(
958 1.0 - settings_transition_animation_
->GetCurrentValue());
960 } else if (settings_transition_animation_
->current_part_index() == 2 &&
961 target_view_
->layer()) {
962 target_view_
->layer()->SetOpacity(
963 settings_transition_animation_
->GetCurrentValue());
968 void MessageCenterView::AnimationCanceled(const gfx::Animation
* animation
) {
969 DCHECK_EQ(animation
, settings_transition_animation_
.get());
970 AnimationEnded(animation
);
973 void MessageCenterView::AddNotificationAt(const Notification
& notification
,
975 NotificationView
* view
=
976 NotificationView::Create(this, notification
, false); // Not top-level.
977 view
->set_context_menu_controller(context_menu_controller_
.get());
978 notification_views_
[notification
.id()] = view
;
979 view
->set_scroller(scroller_
);
980 message_list_view_
->AddNotificationAt(view
, index
);
983 void MessageCenterView::NotificationsChanged() {
984 bool no_message_views
= notification_views_
.empty();
986 // When the child view is removed from the hierarchy, its focus is cleared.
987 // In this case we want to save which view has focus so that the user can
988 // continue to interact with notifications in the order they were expecting.
989 views::FocusManager
* focus_manager
= scroller_
->GetFocusManager();
990 View
* focused_view
= NULL
;
991 // |focus_manager| can be NULL in tests.
993 focused_view
= focus_manager
->GetFocusedView();
995 // All the children of this view are owned by |this|.
996 scroller_
->contents()->RemoveAllChildViews(/*delete_children=*/false);
997 scroller_
->contents()->AddChildView(
998 no_message_views
? empty_list_view_
.get() : message_list_view_
.get());
1000 button_bar_
->SetCloseAllButtonEnabled(!no_message_views
);
1001 scroller_
->SetFocusable(!no_message_views
);
1003 if (focus_manager
&& focused_view
)
1004 focus_manager
->SetFocusedView(focused_view
);
1006 scroller_
->InvalidateLayout();
1007 PreferredSizeChanged();
1011 void MessageCenterView::SetNotificationViewForTest(MessageView
* view
) {
1012 message_list_view_
->AddNotificationAt(view
, 0);
1015 } // namespace message_center