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/insets.h"
19 #include "ui/gfx/rect.h"
20 #include "ui/gfx/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;
59 class NoNotificationMessageView : public views::View {
61 NoNotificationMessageView();
62 ~NoNotificationMessageView() override;
64 // Overridden from views::View.
65 gfx::Size GetPreferredSize() const override;
66 int GetHeightForWidth(int width) const override;
67 void Layout() override;
72 DISALLOW_COPY_AND_ASSIGN(NoNotificationMessageView);
75 NoNotificationMessageView::NoNotificationMessageView() {
76 label_ = new views::Label(l10n_util::GetStringUTF16(
77 IDS_MESSAGE_CENTER_NO_MESSAGES));
78 label_->SetAutoColorReadabilityEnabled(false);
79 label_->SetEnabledColor(kNoNotificationsTextColor);
80 // Set transparent background to ensure that subpixel rendering
81 // is disabled. See crbug.com/169056
82 #if defined(OS_LINUX) && defined(OS_CHROMEOS)
83 label_->SetBackgroundColor(kTransparentColor);
88 NoNotificationMessageView::~NoNotificationMessageView() {
91 gfx::Size NoNotificationMessageView::GetPreferredSize() const {
92 return gfx::Size(kMinScrollViewHeight, label_->GetPreferredSize().width());
95 int NoNotificationMessageView::GetHeightForWidth(int width) const {
96 return kMinScrollViewHeight;
99 void NoNotificationMessageView::Layout() {
100 int text_height = label_->GetHeightForWidth(width());
101 int margin = (height() - text_height) / 2;
102 label_->SetBounds(0, margin, width(), text_height);
105 // Displays a list of messages for rich notifications. Functions as an array of
106 // MessageViews and animates them on transitions. It also supports
108 class MessageListView : public views::View,
109 public views::BoundsAnimatorObserver {
111 explicit MessageListView(MessageCenterView* message_center_view,
113 ~MessageListView() override;
115 void AddNotificationAt(MessageView* view, int i);
116 void RemoveNotification(MessageView* view);
117 void UpdateNotification(MessageView* view, const Notification& notification);
118 void SetRepositionTarget(const gfx::Rect& target_rect);
119 void ResetRepositionSession();
120 void ClearAllNotifications(const gfx::Rect& visible_scroll_rect);
123 // Overridden from views::View.
124 void Layout() override;
125 gfx::Size GetPreferredSize() const override;
126 int GetHeightForWidth(int width) const override;
127 void PaintChildren(gfx::Canvas* canvas,
128 const views::CullSet& cull_set) override;
129 void ReorderChildLayers(ui::Layer* parent_layer) override;
131 // Overridden from views::BoundsAnimatorObserver.
132 void OnBoundsAnimatorProgressed(views::BoundsAnimator* animator) override;
133 void OnBoundsAnimatorDone(views::BoundsAnimator* animator) override;
136 bool IsValidChild(const views::View* child) const;
137 void DoUpdateIfPossible();
139 // Animates all notifications below target upwards to align with the top of
140 // the last closed notification.
141 void AnimateNotificationsBelowTarget();
142 // Animates all notifications above target downwards to align with the top of
143 // the last closed notification.
144 void AnimateNotificationsAboveTarget();
146 // Schedules animation for a child to the specified position. Returns false
147 // if |child| will disappear after the animation.
148 bool AnimateChild(views::View* child, int top, int height);
150 // Animate clearing one notification.
151 void AnimateClearingOneNotification();
152 MessageCenterView* message_center_view() const {
153 return message_center_view_;
156 MessageCenterView* message_center_view_; // Weak reference.
157 // The top position of the reposition target rectangle.
160 bool has_deferred_task_;
161 bool clear_all_started_;
163 std::set<views::View*> adding_views_;
164 std::set<views::View*> deleting_views_;
165 std::set<views::View*> deleted_when_done_;
166 std::list<views::View*> clearing_all_views_;
167 scoped_ptr<views::BoundsAnimator> animator_;
168 base::WeakPtrFactory<MessageListView> weak_ptr_factory_;
170 DISALLOW_COPY_AND_ASSIGN(MessageListView);
173 MessageListView::MessageListView(MessageCenterView* message_center_view,
175 : message_center_view_(message_center_view),
178 has_deferred_task_(false),
179 clear_all_started_(false),
181 weak_ptr_factory_(this) {
182 views::BoxLayout* layout =
183 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 1);
184 layout->SetDefaultFlex(1);
185 SetLayoutManager(layout);
187 // Set the margin to 0 for the layout. BoxLayout assumes the same margin
188 // for top and bottom, but the bottom margin here should be smaller
189 // because of the shadow of message view. Use an empty border instead
190 // to provide this margin.
191 gfx::Insets shadow_insets = MessageView::GetShadowInsets();
192 set_background(views::Background::CreateSolidBackground(
193 kMessageCenterBackgroundColor));
194 SetBorder(views::Border::CreateEmptyBorder(
195 top_down ? 0 : kMarginBetweenItems - shadow_insets.top(), /* top */
196 kMarginBetweenItems - shadow_insets.left(), /* left */
197 top_down ? kMarginBetweenItems - shadow_insets.bottom() : 0, /* bottom */
198 kMarginBetweenItems - shadow_insets.right() /* right */));
201 MessageListView::~MessageListView() {
203 animator_->RemoveObserver(this);
206 void MessageListView::Layout() {
210 gfx::Rect child_area = GetContentsBounds();
211 int top = child_area.y();
213 kMarginBetweenItems - MessageView::GetShadowInsets().bottom();
215 for (int i = 0; i < child_count(); ++i) {
216 views::View* child = child_at(i);
217 if (!child->visible())
219 int height = child->GetHeightForWidth(child_area.width());
220 child->SetBounds(child_area.x(), top, child_area.width(), height);
221 top += height + between_items;
225 void MessageListView::AddNotificationAt(MessageView* view, int index) {
226 // |index| refers to a position in a subset of valid children. |real_index|
227 // in a list includes the invalid children, so we compute the real index by
228 // walking the list until |index| number of valid children are encountered,
229 // or to the end of the list.
231 while (real_index < child_count()) {
232 if (IsValidChild(child_at(real_index))) {
240 AddChildViewAt(view, real_index);
241 if (GetContentsBounds().IsEmpty())
244 adding_views_.insert(view);
245 DoUpdateIfPossible();
248 void MessageListView::RemoveNotification(MessageView* view) {
249 DCHECK_EQ(view->parent(), this);
250 if (GetContentsBounds().IsEmpty()) {
254 deleting_views_.insert(view);
257 animator_->StopAnimatingView(view);
260 DoUpdateIfPossible();
264 void MessageListView::UpdateNotification(MessageView* view,
265 const Notification& notification) {
266 int index = GetIndexOf(view);
267 DCHECK_LE(0, index); // GetIndexOf is negative if not a child.
270 animator_->StopAnimatingView(view);
271 if (deleting_views_.find(view) != deleting_views_.end())
272 deleting_views_.erase(view);
273 if (deleted_when_done_.find(view) != deleted_when_done_.end())
274 deleted_when_done_.erase(view);
275 view->UpdateWithNotification(notification);
276 DoUpdateIfPossible();
279 gfx::Size MessageListView::GetPreferredSize() const {
281 for (int i = 0; i < child_count(); i++) {
282 const views::View* child = child_at(i);
283 if (IsValidChild(child))
284 width = std::max(width, child->GetPreferredSize().width());
287 return gfx::Size(width + GetInsets().width(),
288 GetHeightForWidth(width + GetInsets().width()));
291 int MessageListView::GetHeightForWidth(int width) const {
292 if (fixed_height_ > 0)
293 return fixed_height_;
295 width -= GetInsets().width();
298 for (int i = 0; i < child_count(); ++i) {
299 const views::View* child = child_at(i);
300 if (!IsValidChild(child))
302 height += child->GetHeightForWidth(width) + padding;
303 padding = kMarginBetweenItems - MessageView::GetShadowInsets().bottom();
306 return height + GetInsets().height();
309 void MessageListView::PaintChildren(gfx::Canvas* canvas,
310 const views::CullSet& cull_set) {
311 // Paint in the inversed order. Otherwise upper notification may be
312 // hidden by the lower one.
313 for (int i = child_count() - 1; i >= 0; --i) {
314 if (!child_at(i)->layer())
315 child_at(i)->Paint(canvas, cull_set);
319 void MessageListView::ReorderChildLayers(ui::Layer* parent_layer) {
320 // Reorder children to stack the last child layer at the top. Otherwise
321 // upper notification may be hidden by the lower one.
322 for (int i = 0; i < child_count(); ++i) {
323 if (child_at(i)->layer())
324 parent_layer->StackAtBottom(child_at(i)->layer());
328 void MessageListView::SetRepositionTarget(const gfx::Rect& target) {
329 reposition_top_ = target.y();
330 fixed_height_ = GetHeightForWidth(width());
333 void MessageListView::ResetRepositionSession() {
334 // Don't call DoUpdateIfPossible(), but let Layout() do the task without
335 // animation. Reset will cause the change of the bubble size itself, and
336 // animation from the old location will look weird.
337 if (reposition_top_ >= 0 && animator_.get()) {
338 has_deferred_task_ = false;
339 // cancel cause OnBoundsAnimatorDone which deletes |deleted_when_done_|.
341 STLDeleteContainerPointers(deleting_views_.begin(), deleting_views_.end());
342 deleting_views_.clear();
343 adding_views_.clear();
347 reposition_top_ = -1;
351 void MessageListView::ClearAllNotifications(
352 const gfx::Rect& visible_scroll_rect) {
353 for (int i = 0; i < child_count(); ++i) {
354 views::View* child = child_at(i);
355 if (!child->visible())
357 if (gfx::IntersectRects(child->bounds(), visible_scroll_rect).IsEmpty())
359 clearing_all_views_.push_back(child);
361 DoUpdateIfPossible();
364 void MessageListView::OnBoundsAnimatorProgressed(
365 views::BoundsAnimator* animator) {
366 DCHECK_EQ(animator_.get(), animator);
367 for (std::set<views::View*>::iterator iter = deleted_when_done_.begin();
368 iter != deleted_when_done_.end(); ++iter) {
369 const gfx::SlideAnimation* animation = animator->GetAnimationForView(*iter);
371 (*iter)->layer()->SetOpacity(animation->CurrentValueBetween(1.0, 0.0));
375 void MessageListView::OnBoundsAnimatorDone(views::BoundsAnimator* animator) {
376 STLDeleteContainerPointers(
377 deleted_when_done_.begin(), deleted_when_done_.end());
378 deleted_when_done_.clear();
380 if (clear_all_started_) {
381 clear_all_started_ = false;
382 message_center_view()->OnAllNotificationsCleared();
385 if (has_deferred_task_) {
386 has_deferred_task_ = false;
387 DoUpdateIfPossible();
391 GetWidget()->SynthesizeMouseMoveEvent();
394 bool MessageListView::IsValidChild(const views::View* child) const {
395 return child->visible() &&
396 deleting_views_.find(const_cast<views::View*>(child)) ==
397 deleting_views_.end() &&
398 deleted_when_done_.find(const_cast<views::View*>(child)) ==
399 deleted_when_done_.end();
402 void MessageListView::DoUpdateIfPossible() {
403 gfx::Rect child_area = GetContentsBounds();
404 if (child_area.IsEmpty())
407 if (animator_.get() && animator_->IsAnimating()) {
408 has_deferred_task_ = true;
412 if (!animator_.get()) {
413 animator_.reset(new views::BoundsAnimator(this));
414 animator_->AddObserver(this);
417 if (!clearing_all_views_.empty()) {
418 AnimateClearingOneNotification();
423 CommandLine::ForCurrentProcess()->HasSwitch(
424 switches::kEnableMessageCenterAlwaysScrollUpUponNotificationRemoval))
425 AnimateNotificationsBelowTarget();
427 AnimateNotificationsAboveTarget();
429 adding_views_.clear();
430 deleting_views_.clear();
433 void MessageListView::AnimateNotificationsBelowTarget() {
435 for (int i = 0; i < child_count(); ++i) {
436 views::View* child = child_at(i);
437 if (!IsValidChild(child)) {
438 AnimateChild(child, child->y(), child->height());
439 } else if (reposition_top_ < 0 || child->y() > reposition_top_) {
440 // Find first notification below target (or all notifications if no
446 if (last_index > 0) {
448 kMarginBetweenItems - MessageView::GetShadowInsets().bottom();
449 int top = (reposition_top_ > 0) ? reposition_top_ : GetInsets().top();
451 for (int i = last_index; i < child_count(); ++i) {
452 // Animate notifications below target upwards.
453 views::View* child = child_at(i);
454 if (AnimateChild(child, top, child->height()))
455 top += child->height() + between_items;
460 void MessageListView::AnimateNotificationsAboveTarget() {
462 for (int i = child_count() - 1; i >= 0; --i) {
463 views::View* child = child_at(i);
464 if (!IsValidChild(child)) {
465 AnimateChild(child, child->y(), child->height());
466 } else if (reposition_top_ < 0 || child->y() < reposition_top_) {
467 // Find first notification above target (or all notifications if no
473 if (last_index >= 0) {
475 kMarginBetweenItems - MessageView::GetShadowInsets().bottom();
476 int bottom = (reposition_top_ > 0)
477 ? reposition_top_ + child_at(last_index)->height()
478 : GetHeightForWidth(width()) - GetInsets().bottom();
479 for (int i = last_index; i >= 0; --i) {
480 // Animate notifications above target downwards.
481 views::View* child = child_at(i);
482 if (AnimateChild(child, bottom - child->height(), child->height()))
483 bottom -= child->height() + between_items;
488 bool MessageListView::AnimateChild(views::View* child, int top, int height) {
489 gfx::Rect child_area = GetContentsBounds();
490 if (adding_views_.find(child) != adding_views_.end()) {
491 child->SetBounds(child_area.right(), top, child_area.width(), height);
492 animator_->AnimateViewTo(
493 child, gfx::Rect(child_area.x(), top, child_area.width(), height));
494 } else if (deleting_views_.find(child) != deleting_views_.end()) {
495 DCHECK(child->layer());
496 // No moves, but animate to fade-out.
497 animator_->AnimateViewTo(child, child->bounds());
498 deleted_when_done_.insert(child);
501 gfx::Rect target(child_area.x(), top, child_area.width(), height);
502 if (child->bounds().origin() != target.origin())
503 animator_->AnimateViewTo(child, target);
505 child->SetBoundsRect(target);
510 void MessageListView::AnimateClearingOneNotification() {
511 DCHECK(!clearing_all_views_.empty());
513 clear_all_started_ = true;
515 views::View* child = clearing_all_views_.front();
516 clearing_all_views_.pop_front();
518 // Slide from left to right.
519 gfx::Rect new_bounds = child->bounds();
520 new_bounds.set_x(new_bounds.right() + kMarginBetweenItems);
521 animator_->AnimateViewTo(child, new_bounds);
523 // Schedule to start sliding out next notification after a short delay.
524 if (!clearing_all_views_.empty()) {
525 base::MessageLoop::current()->PostDelayedTask(
527 base::Bind(&MessageListView::AnimateClearingOneNotification,
528 weak_ptr_factory_.GetWeakPtr()),
529 base::TimeDelta::FromMilliseconds(
530 kAnimateClearingNextNotificationDelayMS));
534 // MessageCenterView ///////////////////////////////////////////////////////////
536 MessageCenterView::MessageCenterView(MessageCenter* message_center,
537 MessageCenterTray* tray,
539 bool initially_settings_visible,
541 const base::string16& title)
542 : message_center_(message_center),
545 settings_view_(NULL),
548 settings_visible_(initially_settings_visible),
554 context_menu_controller_(new MessageViewContextMenuController(this)) {
555 message_center_->AddObserver(this);
556 set_notify_enter_exit_on_child(true);
557 set_background(views::Background::CreateSolidBackground(
558 kMessageCenterBackgroundColor));
560 NotifierSettingsProvider* notifier_settings_provider =
561 message_center_->GetNotifierSettingsProvider();
562 button_bar_ = new MessageCenterButtonBar(this,
564 notifier_settings_provider,
565 initially_settings_visible,
568 const int button_height = button_bar_->GetPreferredSize().height();
570 scroller_ = new views::ScrollView();
571 scroller_->ClipHeightTo(kMinScrollViewHeight, max_height - button_height);
572 scroller_->SetVerticalScrollBar(new views::OverlayScrollBar(false));
573 scroller_->set_background(
574 views::Background::CreateSolidBackground(kMessageCenterBackgroundColor));
576 scroller_->SetPaintToLayer(true);
577 scroller_->SetFillsBoundsOpaquely(false);
578 scroller_->layer()->SetMasksToBounds(true);
580 empty_list_view_.reset(new NoNotificationMessageView);
581 empty_list_view_->set_owned_by_client();
582 message_list_view_.reset(new MessageListView(this, top_down));
583 message_list_view_->set_owned_by_client();
585 // We want to swap the contents of the scroll view between the empty list
586 // view and the message list view, without constructing them afresh each
587 // time. So, since the scroll view deletes old contents each time you
588 // set the contents (regardless of the |owned_by_client_| setting) we need
589 // an intermediate view for the contents whose children we can swap in and
591 views::View* scroller_contents = new views::View();
592 scroller_contents->SetLayoutManager(new views::FillLayout());
593 scroller_contents->AddChildView(empty_list_view_.get());
594 scroller_->SetContents(scroller_contents);
596 settings_view_ = new NotifierSettingsView(notifier_settings_provider);
598 if (initially_settings_visible)
599 scroller_->SetVisible(false);
601 settings_view_->SetVisible(false);
603 AddChildView(scroller_);
604 AddChildView(settings_view_);
605 AddChildView(button_bar_);
608 MessageCenterView::~MessageCenterView() {
610 message_center_->RemoveObserver(this);
613 void MessageCenterView::SetNotifications(
614 const NotificationList::Notifications& notifications) {
618 notification_views_.clear();
621 for (NotificationList::Notifications::const_iterator iter =
622 notifications.begin(); iter != notifications.end(); ++iter) {
623 AddNotificationAt(*(*iter), index++);
625 message_center_->DisplayedNotification(
626 (*iter)->id(), message_center::DISPLAY_SOURCE_MESSAGE_CENTER);
627 if (notification_views_.size() >= kMaxVisibleMessageCenterNotifications)
631 NotificationsChanged();
632 scroller_->RequestFocus();
635 void MessageCenterView::SetSettingsVisible(bool visible) {
639 if (visible == settings_visible_)
642 settings_visible_ = visible;
645 source_view_ = scroller_;
646 target_view_ = settings_view_;
648 source_view_ = settings_view_;
649 target_view_ = scroller_;
651 source_height_ = source_view_->GetHeightForWidth(width());
652 target_height_ = target_view_->GetHeightForWidth(width());
654 gfx::MultiAnimation::Parts parts;
655 // First part: slide resize animation.
656 parts.push_back(gfx::MultiAnimation::Part(
657 (source_height_ == target_height_) ? 0 : kDefaultAnimationDurationMs,
658 gfx::Tween::EASE_OUT));
659 // Second part: fade-out the source_view.
660 if (source_view_->layer()) {
661 parts.push_back(gfx::MultiAnimation::Part(
662 kDefaultAnimationDurationMs, gfx::Tween::LINEAR));
664 parts.push_back(gfx::MultiAnimation::Part());
666 // Third part: fade-in the target_view.
667 if (target_view_->layer()) {
668 parts.push_back(gfx::MultiAnimation::Part(
669 kDefaultAnimationDurationMs, gfx::Tween::LINEAR));
670 target_view_->layer()->SetOpacity(0);
671 target_view_->SetVisible(true);
673 parts.push_back(gfx::MultiAnimation::Part());
675 settings_transition_animation_.reset(new gfx::MultiAnimation(
676 parts, base::TimeDelta::FromMicroseconds(1000000 / kDefaultFrameRateHz)));
677 settings_transition_animation_->set_delegate(this);
678 settings_transition_animation_->set_continuous(false);
679 settings_transition_animation_->Start();
681 button_bar_->SetBackArrowVisible(visible);
684 void MessageCenterView::ClearAllNotifications() {
688 scroller_->SetEnabled(false);
689 button_bar_->SetAllButtonsEnabled(false);
690 message_list_view_->ClearAllNotifications(scroller_->GetVisibleRect());
693 void MessageCenterView::OnAllNotificationsCleared() {
694 scroller_->SetEnabled(true);
695 button_bar_->SetAllButtonsEnabled(true);
696 button_bar_->SetCloseAllButtonEnabled(false);
697 message_center_->RemoveAllVisibleNotifications(true); // Action by user.
700 size_t MessageCenterView::NumMessageViewsForTest() const {
701 return message_list_view_->child_count();
704 void MessageCenterView::OnSettingsChanged() {
705 scroller_->InvalidateLayout();
706 PreferredSizeChanged();
710 void MessageCenterView::SetIsClosing(bool is_closing) {
711 is_closing_ = is_closing;
713 message_center_->RemoveObserver(this);
715 message_center_->AddObserver(this);
718 void MessageCenterView::Layout() {
722 int button_height = button_bar_->GetHeightForWidth(width()) +
723 button_bar_->GetInsets().height();
724 // Skip unnecessary re-layout of contents during the resize animation.
725 bool animating = settings_transition_animation_ &&
726 settings_transition_animation_->is_animating();
727 if (animating && settings_transition_animation_->current_part_index() == 0) {
729 button_bar_->SetBounds(
730 0, height() - button_height, width(), button_height);
735 scroller_->SetBounds(0,
736 top_down_ ? button_height : 0,
738 height() - button_height);
739 settings_view_->SetBounds(0,
740 top_down_ ? button_height : 0,
742 height() - button_height);
744 bool is_scrollable = false;
745 if (scroller_->visible())
746 is_scrollable = scroller_->height() < message_list_view_->height();
748 is_scrollable = settings_view_->IsScrollable();
752 // Draw separator line on the top of the button bar if it is on the bottom
753 // or draw it at the bottom if the bar is on the top.
754 button_bar_->SetBorder(views::Border::CreateSolidSidedBorder(
755 top_down_ ? 0 : 1, 0, top_down_ ? 1 : 0, 0, kFooterDelimiterColor));
757 button_bar_->SetBorder(views::Border::CreateEmptyBorder(
758 top_down_ ? 0 : 1, 0, top_down_ ? 1 : 0, 0));
760 button_bar_->SchedulePaint();
762 button_bar_->SetBounds(0,
763 top_down_ ? 0 : height() - button_height,
767 GetWidget()->GetRootView()->SchedulePaint();
770 gfx::Size MessageCenterView::GetPreferredSize() const {
771 if (settings_transition_animation_ &&
772 settings_transition_animation_->is_animating()) {
773 int content_width = std::max(source_view_->GetPreferredSize().width(),
774 target_view_->GetPreferredSize().width());
775 int width = std::max(content_width,
776 button_bar_->GetPreferredSize().width());
777 return gfx::Size(width, GetHeightForWidth(width));
781 for (int i = 0; i < child_count(); ++i) {
782 const views::View* child = child_at(0);
783 if (child->visible())
784 width = std::max(width, child->GetPreferredSize().width());
786 return gfx::Size(width, GetHeightForWidth(width));
789 int MessageCenterView::GetHeightForWidth(int width) const {
790 if (settings_transition_animation_ &&
791 settings_transition_animation_->is_animating()) {
792 int content_height = target_height_;
793 if (settings_transition_animation_->current_part_index() == 0) {
794 content_height = settings_transition_animation_->CurrentValueBetween(
795 source_height_, target_height_);
797 return button_bar_->GetHeightForWidth(width) + content_height;
800 int content_height = 0;
801 if (scroller_->visible())
802 content_height += scroller_->GetHeightForWidth(width);
804 content_height += settings_view_->GetHeightForWidth(width);
805 return button_bar_->GetHeightForWidth(width) +
806 button_bar_->GetInsets().height() + content_height;
809 bool MessageCenterView::OnMouseWheel(const ui::MouseWheelEvent& event) {
810 // Do not rely on the default scroll event handler of ScrollView because
811 // the scroll happens only when the focus is on the ScrollView. The
812 // notification center will allow the scrolling even when the focus is on
814 if (scroller_->bounds().Contains(event.location()))
815 return scroller_->OnMouseWheel(event);
816 return views::View::OnMouseWheel(event);
819 void MessageCenterView::OnMouseExited(const ui::MouseEvent& event) {
823 message_list_view_->ResetRepositionSession();
824 NotificationsChanged();
827 void MessageCenterView::OnNotificationAdded(const std::string& id) {
829 const NotificationList::Notifications& notifications =
830 message_center_->GetVisibleNotifications();
831 for (NotificationList::Notifications::const_iterator iter =
832 notifications.begin(); iter != notifications.end();
834 if ((*iter)->id() == id) {
835 AddNotificationAt(*(*iter), index);
838 if (notification_views_.size() >= kMaxVisibleMessageCenterNotifications)
841 NotificationsChanged();
844 void MessageCenterView::OnNotificationRemoved(const std::string& id,
846 NotificationViewsMap::iterator view_iter = notification_views_.find(id);
847 if (view_iter == notification_views_.end())
849 NotificationView* view = view_iter->second;
850 int index = message_list_view_->GetIndexOf(view);
853 message_list_view_->SetRepositionTarget(view->bounds());
854 // Moves the keyboard focus to the next notification if the removed
855 // notification is focused so that the user can dismiss notifications
856 // without re-focusing by tab key.
857 if (view->IsCloseButtonFocused() ||
858 view == GetFocusManager()->GetFocusedView()) {
859 views::View* next_focused_view = NULL;
860 if (message_list_view_->child_count() > index + 1)
861 next_focused_view = message_list_view_->child_at(index + 1);
863 next_focused_view = message_list_view_->child_at(index - 1);
865 if (next_focused_view) {
866 if (view->IsCloseButtonFocused())
867 // Safe cast since all views in MessageListView are MessageViews.
868 static_cast<MessageView*>(
869 next_focused_view)->RequestFocusOnCloseButton();
871 next_focused_view->RequestFocus();
875 message_list_view_->RemoveNotification(view);
876 notification_views_.erase(view_iter);
877 NotificationsChanged();
880 void MessageCenterView::OnNotificationUpdated(const std::string& id) {
881 NotificationViewsMap::const_iterator view_iter = notification_views_.find(id);
882 if (view_iter == notification_views_.end())
884 NotificationView* view = view_iter->second;
885 // TODO(dimich): add MessageCenter::GetVisibleNotificationById(id)
886 const NotificationList::Notifications& notifications =
887 message_center_->GetVisibleNotifications();
888 for (NotificationList::Notifications::const_iterator iter =
889 notifications.begin(); iter != notifications.end(); ++iter) {
890 if ((*iter)->id() == id) {
891 int old_width = view->width();
892 int old_height = view->GetHeightForWidth(old_width);
893 message_list_view_->UpdateNotification(view, **iter);
894 if (view->GetHeightForWidth(old_width) != old_height)
895 NotificationsChanged();
901 void MessageCenterView::ClickOnNotification(
902 const std::string& notification_id) {
903 message_center_->ClickOnNotification(notification_id);
906 void MessageCenterView::RemoveNotification(const std::string& notification_id,
908 message_center_->RemoveNotification(notification_id, by_user);
911 scoped_ptr<ui::MenuModel> MessageCenterView::CreateMenuModel(
912 const NotifierId& notifier_id,
913 const base::string16& display_source) {
914 return tray_->CreateNotificationMenuModel(notifier_id, display_source);
917 bool MessageCenterView::HasClickedListener(const std::string& notification_id) {
918 return message_center_->HasClickedListener(notification_id);
921 void MessageCenterView::ClickOnNotificationButton(
922 const std::string& notification_id,
924 message_center_->ClickOnNotificationButton(notification_id, button_index);
927 void MessageCenterView::AnimationEnded(const gfx::Animation* animation) {
928 DCHECK_EQ(animation, settings_transition_animation_.get());
930 Visibility visibility = target_view_ == settings_view_
931 ? VISIBILITY_SETTINGS
932 : VISIBILITY_MESSAGE_CENTER;
933 message_center_->SetVisibility(visibility);
935 source_view_->SetVisible(false);
936 target_view_->SetVisible(true);
937 if (source_view_->layer())
938 source_view_->layer()->SetOpacity(1.0);
939 if (target_view_->layer())
940 target_view_->layer()->SetOpacity(1.0);
941 settings_transition_animation_.reset();
942 PreferredSizeChanged();
946 void MessageCenterView::AnimationProgressed(const gfx::Animation* animation) {
947 DCHECK_EQ(animation, settings_transition_animation_.get());
948 PreferredSizeChanged();
949 if (settings_transition_animation_->current_part_index() == 1 &&
950 source_view_->layer()) {
951 source_view_->layer()->SetOpacity(
952 1.0 - settings_transition_animation_->GetCurrentValue());
954 } else if (settings_transition_animation_->current_part_index() == 2 &&
955 target_view_->layer()) {
956 target_view_->layer()->SetOpacity(
957 settings_transition_animation_->GetCurrentValue());
962 void MessageCenterView::AnimationCanceled(const gfx::Animation* animation) {
963 DCHECK_EQ(animation, settings_transition_animation_.get());
964 AnimationEnded(animation);
967 void MessageCenterView::AddNotificationAt(const Notification& notification,
969 NotificationView* view =
970 NotificationView::Create(this, notification, false); // Not top-level.
971 view->set_context_menu_controller(context_menu_controller_.get());
972 notification_views_[notification.id()] = view;
973 view->set_scroller(scroller_);
974 message_list_view_->AddNotificationAt(view, index);
977 void MessageCenterView::NotificationsChanged() {
978 bool no_message_views = notification_views_.empty();
980 // When the child view is removed from the hierarchy, its focus is cleared.
981 // In this case we want to save which view has focus so that the user can
982 // continue to interact with notifications in the order they were expecting.
983 views::FocusManager* focus_manager = scroller_->GetFocusManager();
984 View* focused_view = NULL;
985 // |focus_manager| can be NULL in tests.
987 focused_view = focus_manager->GetFocusedView();
989 // All the children of this view are owned by |this|.
990 scroller_->contents()->RemoveAllChildViews(/*delete_children=*/false);
991 scroller_->contents()->AddChildView(
992 no_message_views ? empty_list_view_.get() : message_list_view_.get());
994 button_bar_->SetCloseAllButtonEnabled(!no_message_views);
995 scroller_->SetFocusable(!no_message_views);
997 if (focus_manager && focused_view)
998 focus_manager->SetFocusedView(focused_view);
1000 scroller_->InvalidateLayout();
1001 PreferredSizeChanged();
1005 void MessageCenterView::SetNotificationViewForTest(MessageView* view) {
1006 message_list_view_->AddNotificationAt(view, 0);
1009 } // namespace message_center