forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_center_view.cc
775 lines (658 loc) · 26.2 KB
/
message_center_view.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/message_center/message_center_view.h"
#include <list>
#include <map>
#include "ash/message_center/message_center_button_bar.h"
#include "ash/message_center/message_center_style.h"
#include "ash/message_center/notifier_settings_view.h"
#include "ash/public/cpp/ash_features.h"
#include "ash/public/cpp/ash_switches.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/session/session_controller.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/stl_util.h"
#include "build/build_config.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/compositor/paint_recorder.h"
#include "ui/gfx/animation/slide_animation.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/skia_paint_util.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/message_center_types.h"
#include "ui/message_center/public/cpp/message_center_constants.h"
#include "ui/message_center/views/message_view.h"
#include "ui/message_center/views/message_view_factory.h"
#include "ui/message_center/views/notification_control_buttons_view.h"
#include "ui/views/background.h"
#include "ui/views/border.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/controls/scrollbar/overlay_scroll_bar.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/widget/widget.h"
using message_center::MessageCenter;
using message_center::MessageView;
using message_center::Notification;
using message_center::NotificationList;
namespace ash {
// static
const size_t MessageCenterView::kMaxVisibleNotifications = 100;
// static
bool MessageCenterView::disable_animation_for_testing = false;
namespace {
constexpr int kMinScrollViewHeight = 77;
constexpr int kEmptyViewHeight = 96;
constexpr gfx::Insets kEmptyViewPadding(0, 0, 24, 0);
constexpr int kScrollShadowOffsetY = -2;
constexpr int kScrollShadowBlur = 2;
constexpr SkColor kScrollShadowColor = SkColorSetA(SK_ColorBLACK, 0x24);
void SetViewHierarchyEnabled(views::View* view, bool enabled) {
for (int i = 0; i < view->child_count(); i++)
SetViewHierarchyEnabled(view->child_at(i), enabled);
view->SetEnabled(enabled);
}
// Create a view that is shown when there are no notifications.
views::View* CreateEmptyNotificationView() {
auto* view = new views::View;
auto layout = std::make_unique<views::BoxLayout>(views::BoxLayout::kVertical,
kEmptyViewPadding, 0);
layout->set_main_axis_alignment(views::BoxLayout::MAIN_AXIS_ALIGNMENT_END);
layout->set_cross_axis_alignment(
views::BoxLayout::CROSS_AXIS_ALIGNMENT_CENTER);
view->SetLayoutManager(std::move(layout));
views::ImageView* icon = new views::ImageView();
icon->SetImage(gfx::CreateVectorIcon(kNotificationCenterAllDoneIcon,
message_center_style::kEmptyIconSize,
message_center_style::kEmptyViewColor));
icon->SetBorder(
views::CreateEmptyBorder(message_center_style::kEmptyIconPadding));
view->AddChildView(icon);
views::Label* label = new views::Label(
l10n_util::GetStringUTF16(IDS_ASH_MESSAGE_CENTER_NO_MESSAGES));
label->SetEnabledColor(message_center_style::kEmptyViewColor);
// "Roboto-Medium, 12sp" is specified in the mock.
label->SetFontList(
gfx::FontList().DeriveWithWeight(gfx::Font::Weight::MEDIUM));
label->SetHorizontalAlignment(gfx::ALIGN_CENTER);
label->SetSubpixelRenderingEnabled(false);
view->AddChildView(label);
view->SetPaintToLayer();
view->layer()->SetFillsBoundsOpaquely(false);
return view;
}
class MessageCenterScrollView : public views::ScrollView {
public:
MessageCenterScrollView(MessageCenterView* owner) : owner_(owner) {}
~MessageCenterScrollView() override = default;
private:
// views::View:
void GetAccessibleNodeData(ui::AXNodeData* node_data) override {
node_data->role = ax::mojom::Role::kDialog;
node_data->SetName(
l10n_util::GetStringUTF16(IDS_ASH_MESSAGE_CENTER_FOOTER_TITLE));
}
// views::ScrollBarController:
void ScrollToPosition(views::ScrollBar* source, int position) override {
views::ScrollView::ScrollToPosition(source, position);
owner_->UpdateScrollerShadowVisibility();
}
MessageCenterView* const owner_;
DISALLOW_COPY_AND_ASSIGN(MessageCenterScrollView);
};
// A view that displays a shadow at the bottom when |scroller_| is bounded.
class ScrollShadowView : public views::View {
public:
ScrollShadowView(int max_scroll_view_height, int button_height)
: max_scroll_view_height_(max_scroll_view_height),
button_height_(button_height) {
SetPaintToLayer();
layer()->SetFillsBoundsOpaquely(false);
set_can_process_events_within_subtree(false);
}
~ScrollShadowView() override = default;
protected:
void PaintChildren(const views::PaintInfo& paint_info) override {
views::View::PaintChildren(paint_info);
if (height() != max_scroll_view_height_)
return;
// Draw a shadow at the bottom of the viewport when scrolled.
DrawShadow(paint_info.context(),
gfx::Rect(0, height(), width(), button_height_));
}
private:
// Draws a drop shadow above |shadowed_area|.
void DrawShadow(const ui::PaintContext& context,
const gfx::Rect& shadowed_area) {
ui::PaintRecorder recorder(context, size());
gfx::Canvas* canvas = recorder.canvas();
cc::PaintFlags flags;
gfx::ShadowValues shadow;
shadow.emplace_back(gfx::Vector2d(0, kScrollShadowOffsetY),
kScrollShadowBlur, kScrollShadowColor);
flags.setLooper(gfx::CreateShadowDrawLooper(shadow));
flags.setAntiAlias(true);
canvas->ClipRect(shadowed_area, SkClipOp::kDifference);
canvas->DrawRect(shadowed_area, flags);
}
const int max_scroll_view_height_;
const int button_height_;
DISALLOW_COPY_AND_ASSIGN(ScrollShadowView);
};
} // namespace
// MessageCenterView ///////////////////////////////////////////////////////////
MessageCenterView::MessageCenterView(
MessageCenter* message_center,
int max_height,
bool initially_settings_visible)
: message_center_(message_center),
settings_visible_(initially_settings_visible),
is_locked_(Shell::Get()->session_controller()->IsScreenLocked()) {
if (is_locked_ && !features::IsLockScreenNotificationsEnabled())
mode_ = Mode::LOCKED;
else if (initially_settings_visible)
mode_ = Mode::SETTINGS;
message_center_->AddObserver(this);
set_notify_enter_exit_on_child(true);
SetFocusBehavior(views::View::FocusBehavior::NEVER);
button_bar_ = new MessageCenterButtonBar(
this, message_center, initially_settings_visible, is_locked_);
button_bar_->SetCloseAllButtonEnabled(false);
const int button_height = button_bar_->GetPreferredSize().height();
const int max_scroll_view_height = max_height - button_height;
scroller_shadow_ =
new ScrollShadowView(max_scroll_view_height, button_height);
scroller_ = new MessageCenterScrollView(this);
// Need to set the transparent background explicitly, since ScrollView has
// set the default opaque background color.
scroller_->SetBackgroundColor(SK_ColorTRANSPARENT);
scroller_->ClipHeightTo(kMinScrollViewHeight, max_scroll_view_height);
scroller_->SetVerticalScrollBar(new views::OverlayScrollBar(false));
scroller_->SetHorizontalScrollBar(new views::OverlayScrollBar(true));
message_list_view_.reset(new MessageListView());
message_list_view_->SetBorderPadding();
message_list_view_->set_scroller(scroller_);
message_list_view_->set_owned_by_client();
message_list_view_->AddObserver(this);
// We want to swap the contents of the scroll view between the empty list
// view and the message list view, without constructing them afresh each
// time. So, since the scroll view deletes old contents each time you
// set the contents (regardless of the |owned_by_client_| setting) we need
// an intermediate view for the contents whose children we can swap in and
// out.
views::View* scroller_contents = new views::View();
scroller_contents->SetLayoutManager(std::make_unique<views::FillLayout>());
scroller_contents->AddChildView(message_list_view_.get());
scroller_->SetContents(scroller_contents);
settings_view_ = new NotifierSettingsView();
settings_view_->SetBackground(
views::CreateSolidBackground(message_center_style::kBackgroundColor));
no_notifications_view_ = CreateEmptyNotificationView();
scroller_->SetVisible(false); // Because it has no notifications at first.
settings_view_->SetVisible(mode_ == Mode::SETTINGS);
no_notifications_view_->SetVisible(mode_ == Mode::NO_NOTIFICATIONS);
AddChildView(no_notifications_view_);
AddChildView(scroller_);
AddChildView(scroller_shadow_);
AddChildView(settings_view_);
AddChildView(button_bar_);
}
MessageCenterView::~MessageCenterView() {
message_list_view_->RemoveObserver(this);
if (!is_closing_)
message_center_->RemoveObserver(this);
if (focus_manager_)
focus_manager_->RemoveFocusChangeListener(this);
}
void MessageCenterView::Init() {
focus_manager_ = GetFocusManager();
if (focus_manager_)
focus_manager_->AddFocusChangeListener(this);
}
void MessageCenterView::SetNotifications(
const NotificationList::Notifications& notifications) {
if (is_closing_)
return;
int index = 0;
for (NotificationList::Notifications::const_iterator iter =
notifications.begin();
iter != notifications.end(); ++iter) {
AddNotificationAt(*(*iter), index++);
message_center_->DisplayedNotification(
(*iter)->id(), message_center::DISPLAY_SOURCE_MESSAGE_CENTER);
if (message_list_view_->GetNotificationCount() >=
kMaxVisibleNotifications) {
break;
}
}
Update(false /* animate */);
scroller_->RequestFocus();
}
void MessageCenterView::SetSettingsVisible(bool visible) {
settings_visible_ = visible;
Update(true /* animate */);
}
void MessageCenterView::ClearAllClosableNotifications() {
if (is_closing_)
return;
is_clearing_all_notifications_ = true;
UpdateButtonBarStatus();
SetViewHierarchyEnabled(scroller_, false);
message_list_view_->ClearAllClosableNotifications(
scroller_->GetVisibleRect());
}
void MessageCenterView::OnLockStateChanged(bool locked) {
is_locked_ = locked;
Update(true /* animate */);
// Refresh a11y information, because accessible name of the view changes.
NotifyAccessibilityEvent(ax::mojom::Event::kAriaAttributeChanged, true);
}
void MessageCenterView::OnAllNotificationsCleared() {
SetViewHierarchyEnabled(scroller_, true);
button_bar_->SetCloseAllButtonEnabled(false);
// The status of buttons will be updated after removing all notifications.
// Action by user.
message_center_->RemoveAllNotifications(
true /* by_user */, MessageCenter::RemoveType::NON_PINNED);
is_clearing_all_notifications_ = false;
}
size_t MessageCenterView::NumMessageViewsForTest() const {
return message_list_view_->GetNotificationCount();
}
void MessageCenterView::OnSettingsChanged() {
scroller_->InvalidateLayout();
PreferredSizeChanged();
Layout();
}
void MessageCenterView::SetIsClosing(bool is_closing) {
is_closing_ = is_closing;
if (is_closing)
message_center_->RemoveObserver(this);
else
message_center_->AddObserver(this);
}
void MessageCenterView::OnDidChangeFocus(views::View* before,
views::View* now) {
// Update the button visibility when the focus state is changed.
size_t count = message_list_view_->GetNotificationCount();
for (size_t i = 0; i < count; ++i) {
MessageView* view = message_list_view_->GetNotificationAt(i);
// ControlButtonsView is not in the same view hierarchy on ARC++
// notifications, so check it separately.
if (view->Contains(before) || view->Contains(now) ||
(view->GetControlButtonsView() &&
(view->GetControlButtonsView()->Contains(before) ||
view->GetControlButtonsView()->Contains(now)))) {
view->UpdateControlButtonsVisibility();
}
// Ensure that a notification is not removed or added during iteration.
DCHECK_EQ(count, message_list_view_->GetNotificationCount());
}
}
void MessageCenterView::UpdateScrollerShadowVisibility() {
// |scroller_shadow_| is visible only if |scroller_| is not all scrolled.
scroller_shadow_->SetVisible(scroller_->contents()->height() +
scroller_->contents()->y() !=
scroller_shadow_->height());
}
void MessageCenterView::Layout() {
if (is_closing_)
return;
int button_height = button_bar_->GetHeightForWidth(width());
int settings_height =
std::min(GetSettingsHeightForWidth(width()), height() - button_height);
// In order to keep the fix for https://crbug.com/767805 working,
// we have to always call SetBounds of scroller_.
// TODO(tetsui): Fix the bug above without calling SetBounds, as SetBounds
// invokes Layout() which is a heavy operation.
scroller_->SetBounds(0, 0, width(), height() - button_height);
scroller_shadow_->SetBounds(0, 0, width(), height() - button_height);
if (settings_view_->visible()) {
settings_view_->SetBounds(0, height() - settings_height, width(),
settings_height);
}
if (no_notifications_view_->visible())
no_notifications_view_->SetBounds(0, 0, width(), kEmptyViewHeight);
button_bar_->SetBounds(0, height() - button_height - settings_height, width(),
button_height);
if (GetWidget())
GetWidget()->GetRootView()->SchedulePaint();
}
gfx::Size MessageCenterView::CalculatePreferredSize() const {
int width = 0;
for (int i = 0; i < child_count(); ++i) {
const views::View* child = child_at(i);
if (child->visible())
width = std::max(width, child->GetPreferredSize().width());
}
return gfx::Size(width, GetHeightForWidth(width));
}
int MessageCenterView::GetHeightForWidth(int width) const {
if (settings_transition_animation_ &&
settings_transition_animation_->is_animating()) {
return button_bar_->GetHeightForWidth(width) +
GetContentHeightDuringAnimation();
}
return button_bar_->GetHeightForWidth(width) +
GetContentHeightForMode(mode_, width);
}
bool MessageCenterView::OnMouseWheel(const ui::MouseWheelEvent& event) {
// Do not rely on the default scroll event handler of ScrollView because
// the scroll happens only when the focus is on the ScrollView. The
// notification center will allow the scrolling even when the focus is on
// the buttons.
if (scroller_->bounds().Contains(event.location()))
return scroller_->OnMouseWheel(event);
return views::View::OnMouseWheel(event);
}
void MessageCenterView::OnMouseExited(const ui::MouseEvent& event) {
if (is_closing_)
return;
message_list_view_->ResetRepositionSession();
Update(true /* animate */);
}
void MessageCenterView::OnNotificationAdded(const std::string& id) {
int index = 0;
const NotificationList::Notifications& notifications =
message_center_->GetVisibleNotifications();
for (NotificationList::Notifications::const_iterator
iter = notifications.begin();
iter != notifications.end(); ++iter, ++index) {
if ((*iter)->id() == id) {
AddNotificationAt(*(*iter), index);
break;
}
if (message_list_view_->GetNotificationCount() >=
kMaxVisibleNotifications) {
break;
}
}
Update(true /* animate */);
}
void MessageCenterView::OnNotificationRemoved(const std::string& id,
bool by_user) {
auto view_pair = message_list_view_->GetNotificationById(id);
MessageView* view = view_pair.second;
if (!view)
return;
size_t index = view_pair.first;
// We skip repositioning during clear-all anomation, since we don't need keep
// positions.
if (by_user && !is_clearing_all_notifications_) {
message_list_view_->SetRepositionTarget(view->bounds());
// Moves the keyboard focus to the next notification if the removed
// notification is focused so that the user can dismiss notifications
// without re-focusing by tab key.
if (view->IsCloseButtonFocused() || view->HasFocus()) {
views::View* next_focused_view = nullptr;
if (message_list_view_->GetNotificationCount() > index + 1)
next_focused_view = message_list_view_->GetNotificationAt(index + 1);
else if (index > 0)
next_focused_view = message_list_view_->GetNotificationAt(index - 1);
if (next_focused_view) {
if (view->IsCloseButtonFocused()) {
// Safe cast since all views in MessageListView are MessageViews.
static_cast<MessageView*>(next_focused_view)
->RequestFocusOnCloseButton();
} else {
next_focused_view->RequestFocus();
}
}
}
}
message_list_view_->RemoveNotification(view);
Update(true /* animate */);
}
// This is a separate function so we can override it in tests.
bool MessageCenterView::SetRepositionTarget() {
// Set the item on the mouse cursor as the reposition target so that it
// should stick to the current position over the update.
if (message_list_view_->IsMouseHovered()) {
size_t count = message_list_view_->GetNotificationCount();
for (size_t i = 0; i < count; ++i) {
const views::View* hover_view = message_list_view_->GetNotificationAt(i);
if (hover_view->IsMouseHovered()) {
message_list_view_->SetRepositionTarget(hover_view->bounds());
return true;
}
}
}
return false;
}
void MessageCenterView::OnNotificationUpdated(const std::string& id) {
// If there is no reposition target anymore, make sure to reset the reposition
// session.
if (!SetRepositionTarget())
message_list_view_->ResetRepositionSession();
UpdateNotification(id);
}
void MessageCenterView::OnQuietModeChanged(bool is_quiet_mode) {
settings_view_->SetQuietModeState(is_quiet_mode);
button_bar_->SetQuietModeState(is_quiet_mode);
}
void MessageCenterView::AnimationEnded(const gfx::Animation* animation) {
DCHECK_EQ(animation, settings_transition_animation_.get());
message_center::Visibility visibility =
mode_ == Mode::SETTINGS ? message_center::VISIBILITY_SETTINGS
: message_center::VISIBILITY_MESSAGE_CENTER;
message_center_->SetVisibility(visibility);
if (source_view_) {
source_view_->SetVisible(false);
}
if (target_view_)
target_view_->SetVisible(true);
if (settings_transition_animation_)
NotifyAnimationState(false /* animating */);
settings_transition_animation_.reset();
PreferredSizeChanged();
Layout();
// We should update minimum fixed height based on new |scroller_| height.
// This is required when switching between message list and settings panel.
if (!scroller_->visible())
message_list_view_->ResetRepositionSession();
}
void MessageCenterView::AnimationProgressed(const gfx::Animation* animation) {
DCHECK_EQ(animation, settings_transition_animation_.get());
PreferredSizeChanged();
Layout();
SchedulePaint();
}
void MessageCenterView::AnimationCanceled(const gfx::Animation* animation) {
DCHECK_EQ(animation, settings_transition_animation_.get());
AnimationEnded(animation);
}
void MessageCenterView::OnViewPreferredSizeChanged(views::View* observed_view) {
DCHECK_EQ(std::string(MessageView::kViewClassName),
observed_view->GetClassName());
UpdateNotification(
static_cast<MessageView*>(observed_view)->notification_id());
}
void MessageCenterView::AddNotificationAt(const Notification& notification,
int index) {
MessageView* view = message_center::MessageViewFactory::Create(
notification, false); // Not top-level.
view->AddObserver(this);
view->set_scroller(scroller_);
message_list_view_->AddNotificationAt(view, index);
}
void MessageCenterView::Update(bool animate) {
bool no_message_views = (message_list_view_->GetNotificationCount() == 0);
if (is_locked_ && !features::IsLockScreenNotificationsEnabled())
SetVisibilityMode(Mode::LOCKED, animate);
else if (settings_visible_)
SetVisibilityMode(Mode::SETTINGS, animate);
else if (no_message_views)
SetVisibilityMode(Mode::NO_NOTIFICATIONS, animate);
else
SetVisibilityMode(Mode::NOTIFICATIONS, animate);
UpdateButtonBarStatus();
if (scroller_->visible())
scroller_->InvalidateLayout();
PreferredSizeChanged();
Layout();
}
void MessageCenterView::SetVisibilityMode(Mode mode, bool animate) {
if (is_closing_)
return;
if (mode == mode_)
return;
switch (mode_) {
case Mode::NOTIFICATIONS:
source_view_ = scroller_;
break;
case Mode::SETTINGS:
source_view_ = settings_view_;
break;
case Mode::LOCKED:
source_view_ = nullptr;
break;
case Mode::NO_NOTIFICATIONS:
source_view_ = no_notifications_view_;
break;
}
switch (mode) {
case Mode::NOTIFICATIONS:
target_view_ = scroller_;
break;
case Mode::SETTINGS:
target_view_ = settings_view_;
break;
case Mode::LOCKED:
target_view_ = nullptr;
break;
case Mode::NO_NOTIFICATIONS:
target_view_ = no_notifications_view_;
break;
}
source_height_ = GetContentHeightForMode(mode_, width());
target_height_ = GetContentHeightForMode(mode, width());
mode_ = mode;
int contents_max_height =
max_height_ - button_bar_->GetPreferredSize().height();
source_height_ = std::min(contents_max_height, source_height_);
target_height_ = std::min(contents_max_height, target_height_);
if (source_view_)
source_view_->SetVisible(true);
if (target_view_)
target_view_->SetVisible(true);
if (!animate || disable_animation_for_testing) {
AnimationEnded(nullptr);
return;
}
NotifyAnimationState(true /* animating */);
settings_transition_animation_ = std::make_unique<gfx::SlideAnimation>(this);
settings_transition_animation_->SetSlideDuration(
message_center_style::kSettingsTransitionDurationMs);
settings_transition_animation_->SetTweenType(gfx::Tween::EASE_IN_OUT);
settings_transition_animation_->Show();
}
void MessageCenterView::UpdateButtonBarStatus() {
// Disables all buttons during animation of cleaning of all notifications.
if (is_clearing_all_notifications_) {
button_bar_->SetSettingsAndQuietModeButtonsEnabled(false);
button_bar_->SetCloseAllButtonEnabled(false);
return;
}
button_bar_->SetBackArrowVisible(mode_ == Mode::SETTINGS);
button_bar_->SetIsLocked(is_locked_);
EnableCloseAllIfAppropriate();
}
void MessageCenterView::EnableCloseAllIfAppropriate() {
if (mode_ == Mode::NOTIFICATIONS) {
bool no_closable_views = true;
size_t count = message_list_view_->GetNotificationCount();
for (size_t i = 0; i < count; ++i) {
if (message_list_view_->GetNotificationAt(i)->GetMode() ==
MessageView::Mode::NORMAL) {
no_closable_views = false;
break;
}
}
button_bar_->SetCloseAllButtonEnabled(!no_closable_views);
} else {
// Disable the close-all button since no notification is visible.
button_bar_->SetCloseAllButtonEnabled(false);
}
}
void MessageCenterView::SetNotificationViewForTest(MessageView* view) {
message_list_view_->AddNotificationAt(view, 0);
}
void MessageCenterView::UpdateNotification(const std::string& id) {
MessageView* view = message_list_view_->GetNotificationById(id).second;
if (!view)
return;
Notification* notification = message_center_->FindVisibleNotificationById(id);
if (notification) {
int old_width = view->width();
int old_height = view->height();
MessageView::Mode old_mode = view->GetMode();
message_list_view_->UpdateNotification(view, *notification);
if (view->GetHeightForWidth(old_width) != old_height) {
Update(true /* animate */);
} else if (view->GetMode() != old_mode) {
// Animate flag is false, since the pinned flag transition doesn't need
// animation.
Update(false /* animate */);
}
}
// Notify accessibility that the contents have changed.
view->NotifyAccessibilityEvent(ax::mojom::Event::kChildrenChanged, false);
}
void MessageCenterView::NotifyAnimationState(bool animating) {
size_t count = message_list_view_->GetNotificationCount();
for (size_t i = 0; i < count; ++i) {
MessageView* view = message_list_view_->GetNotificationAt(i);
if (animating)
view->OnContainerAnimationStarted();
else
view->OnContainerAnimationEnded();
// Ensure that a notification is not removed or added during iteration.
DCHECK_EQ(count, message_list_view_->GetNotificationCount());
}
}
int MessageCenterView::GetSettingsHeightForWidth(int width) const {
if (settings_transition_animation_ &&
settings_transition_animation_->is_animating() &&
(source_view_ == settings_view_ || target_view_ == settings_view_)) {
return settings_transition_animation_->CurrentValueBetween(
target_view_ == settings_view_ ? 0 : source_height_,
source_view_ == settings_view_ ? 0 : target_height_);
} else {
return mode_ == Mode::SETTINGS ? settings_view_->GetHeightForWidth(width)
: 0;
}
}
int MessageCenterView::GetContentHeightDuringAnimation() const {
DCHECK(settings_transition_animation_);
int contents_height = settings_transition_animation_->CurrentValueBetween(
target_view_ == settings_view_ ? 0 : source_height_,
source_view_ == settings_view_ ? 0 : target_height_);
if (target_view_ == settings_view_)
contents_height = std::max(source_height_, contents_height);
if (source_view_ == settings_view_)
contents_height = std::max(target_height_, contents_height);
return contents_height;
}
int MessageCenterView::GetContentHeightForMode(Mode mode, int width) const {
switch (mode) {
case Mode::NOTIFICATIONS:
return scroller_->GetHeightForWidth(width);
case Mode::SETTINGS:
return settings_view_->GetHeightForWidth(width);
case Mode::LOCKED:
return 0;
case Mode::NO_NOTIFICATIONS:
return kEmptyViewHeight;
}
}
} // namespace ash