Commit 483681a8 authored by Tetsui Ohkubo's avatar Tetsui Ohkubo Committed by Commit Bot

NewMessageListView: Add Clear All button

This CL adds Clear All button to NewUnifiedMessageCenterView.

NewUnifiedMessageCenterView will replace UnifiedMessageCenterView.
It's behind a flag: --enable-features=NewMessageListView

Design doc: go/chrome-popup-refactoring

TEST=NewUnifiedMessageCenterViewTest
BUG=769219

Change-Id: Ibadc6e5a3d799fcbeb6c94199d68656d169295d4
Reviewed-on: https://chromium-review.googlesource.com/c/1260530Reviewed-by: default avatarYoshiki Iguchi <yoshiki@chromium.org>
Commit-Queue: Tetsui Ohkubo <tetsui@chromium.org>
Cr-Commit-Position: refs/heads/master@{#597005}
parent dcbec5e8
......@@ -1850,6 +1850,7 @@ test("ash_unittests") {
"system/message_center/arc/arc_notification_view_unittest.cc",
"system/message_center/ash_popup_alignment_delegate_unittest.cc",
"system/message_center/inactive_user_notification_blocker_unittest.cc",
"system/message_center/new_unified_message_center_view_unittest.cc",
"system/message_center/notification_tray_unittest.cc",
"system/message_center/session_state_notification_blocker_unittest.cc",
"system/message_center/unified_message_list_view_unittest.cc",
......
......@@ -5,9 +5,19 @@
#include "ash/system/message_center/new_unified_message_center_view.h"
#include "ash/message_center/message_center_scroll_bar.h"
#include "ash/session/session_controller.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/message_center/unified_message_list_view.h"
#include "ash/system/tray/tray_constants.h"
#include "ash/system/unified/sign_out_button.h"
#include "ash/system/unified/unified_system_tray_controller.h"
#include "base/metrics/user_metrics.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/views/message_view.h"
#include "ui/views/controls/scroll_view.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/widget/widget.h"
......@@ -18,15 +28,15 @@ NewUnifiedMessageCenterView::NewUnifiedMessageCenterView()
message_list_view_(new UnifiedMessageListView(this)) {
message_list_view_->Init();
SetLayoutManager(std::make_unique<views::FillLayout>());
// Need to set the transparent background explicitly, since ScrollView has
// set the default opaque background color.
scroller_->SetContents(message_list_view_);
scroller_->SetContents(CreateScrollerContents());
scroller_->SetBackgroundColor(SK_ColorTRANSPARENT);
scroller_->SetVerticalScrollBar(new MessageCenterScrollBar(this));
scroller_->set_draw_overflow_indicator(false);
AddChildView(scroller_);
UpdateVisibility();
}
NewUnifiedMessageCenterView::~NewUnifiedMessageCenterView() = default;
......@@ -36,7 +46,7 @@ void NewUnifiedMessageCenterView::SetMaxHeight(int max_height) {
}
void NewUnifiedMessageCenterView::ListPreferredSizeChanged() {
SetVisible(message_list_view_->child_count() > 0);
UpdateVisibility();
PreferredSizeChanged();
if (GetWidget())
......@@ -48,6 +58,63 @@ void NewUnifiedMessageCenterView::ConfigureMessageView(
message_view->set_scroller(scroller_);
}
void NewUnifiedMessageCenterView::Layout() {
// We have to manually layout because we want to override
// CalculatePreferredSize().
scroller_->SetBoundsRect(GetContentsBounds());
}
gfx::Size NewUnifiedMessageCenterView::CalculatePreferredSize() const {
gfx::Size preferred_size = scroller_->GetPreferredSize();
// Hide Clear All button at the buttom from initial viewport.
preferred_size.set_height(preferred_size.height() -
3 * kUnifiedNotificationCenterSpacing);
return preferred_size;
}
void NewUnifiedMessageCenterView::OnMessageCenterScrolled() {}
void NewUnifiedMessageCenterView::ButtonPressed(views::Button* sender,
const ui::Event& event) {
base::RecordAction(
base::UserMetricsAction("StatusArea_Notifications_ClearAll"));
message_center::MessageCenter::Get()->RemoveAllNotifications(
true /* by_user */,
message_center::MessageCenter::RemoveType::NON_PINNED);
}
void NewUnifiedMessageCenterView::UpdateVisibility() {
SessionController* session_controller = Shell::Get()->session_controller();
SetVisible(message_list_view_->child_count() > 0 &&
session_controller->ShouldShowNotificationTray() &&
!session_controller->IsScreenLocked());
}
views::View* NewUnifiedMessageCenterView::CreateScrollerContents() {
views::View* scroller_contents = new views::View;
auto* contents_layout = scroller_contents->SetLayoutManager(
std::make_unique<views::BoxLayout>(views::BoxLayout::kVertical));
contents_layout->set_cross_axis_alignment(
views::BoxLayout::CROSS_AXIS_ALIGNMENT_STRETCH);
scroller_contents->AddChildView(message_list_view_);
views::View* button_container = new views::View;
auto* button_layout =
button_container->SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::kHorizontal,
gfx::Insets(kUnifiedNotificationCenterSpacing), 0));
button_layout->set_main_axis_alignment(
views::BoxLayout::MAIN_AXIS_ALIGNMENT_END);
auto* clear_all_button = new RoundedLabelButton(
this,
l10n_util::GetStringUTF16(IDS_ASH_MESSAGE_CENTER_CLEAR_ALL_BUTTON_LABEL));
clear_all_button->SetTooltipText(l10n_util::GetStringUTF16(
IDS_ASH_MESSAGE_CENTER_CLEAR_ALL_BUTTON_TOOLTIP));
button_container->AddChildView(clear_all_button);
scroller_contents->AddChildView(button_container);
return scroller_contents;
}
} // namespace ash
......@@ -24,7 +24,8 @@ namespace ash {
// TODO(tetsui): Rename to UnifiedMessageCenterView after old code is removed.
class ASH_EXPORT NewUnifiedMessageCenterView
: public views::View,
public MessageCenterScrollBar::Observer {
public MessageCenterScrollBar::Observer,
public views::ButtonListener {
public:
NewUnifiedMessageCenterView();
~NewUnifiedMessageCenterView() override;
......@@ -39,10 +40,22 @@ class ASH_EXPORT NewUnifiedMessageCenterView
// UnifiedMessageListView.
void ConfigureMessageView(message_center::MessageView* message_view);
// views::View:
void Layout() override;
gfx::Size CalculatePreferredSize() const override;
// MessageCenterScrollBar::Observer:
void OnMessageCenterScrolled() override;
// views::ButtonListener:
void ButtonPressed(views::Button* sender, const ui::Event& event) override;
private:
friend class NewUnifiedMessageCenterViewTest;
void UpdateVisibility();
views::View* CreateScrollerContents();
views::ScrollView* const scroller_;
UnifiedMessageListView* const message_list_view_;
......
// Copyright 2018 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/system/message_center/new_unified_message_center_view.h"
#include "ash/system/tray/tray_constants.h"
#include "ash/system/unified/unified_system_tray_controller.h"
#include "ash/test/ash_test_base.h"
#include "base/macros.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/views/message_view.h"
#include "ui/views/controls/scroll_view.h"
using message_center::MessageCenter;
using message_center::MessageView;
using message_center::Notification;
namespace ash {
namespace {
class DummyEvent : public ui::Event {
public:
DummyEvent() : Event(ui::ET_UNKNOWN, base::TimeTicks(), 0) {}
~DummyEvent() override = default;
};
} // namespace
class NewUnifiedMessageCenterViewTest : public AshTestBase,
public views::ViewObserver {
public:
NewUnifiedMessageCenterViewTest() = default;
~NewUnifiedMessageCenterViewTest() override = default;
// AshTestBase:
void TearDown() override {
message_center_view_.reset();
AshTestBase::TearDown();
}
// views::ViewObserver:
void OnViewPreferredSizeChanged(views::View* view) override {
view->SetBoundsRect(gfx::Rect(view->GetPreferredSize()));
view->Layout();
++size_changed_count_;
}
protected:
std::string AddNotification() {
std::string id = base::IntToString(id_++);
MessageCenter::Get()->AddNotification(std::make_unique<Notification>(
message_center::NOTIFICATION_TYPE_BASE_FORMAT, id,
base::UTF8ToUTF16("test title"), base::UTF8ToUTF16("test message"),
gfx::Image(), base::string16() /* display_source */, GURL(),
message_center::NotifierId(), message_center::RichNotificationData(),
new message_center::NotificationDelegate()));
return id;
}
void CreateMessageCenterView() {
message_center_view_ = std::make_unique<NewUnifiedMessageCenterView>();
message_center_view_->AddObserver(this);
message_center_view_->SetMaxHeight(500);
OnViewPreferredSizeChanged(message_center_view_.get());
size_changed_count_ = 0;
}
UnifiedMessageListView* GetMessageListView() {
return message_center_view()->message_list_view_;
}
views::ScrollView* GetScroller() { return message_center_view()->scroller_; }
views::View* GetScrollerContents() {
return message_center_view()->scroller_->contents();
}
NewUnifiedMessageCenterView* message_center_view() {
return message_center_view_.get();
}
int size_changed_count() const { return size_changed_count_; }
private:
int id_ = 0;
int size_changed_count_ = 0;
std::unique_ptr<NewUnifiedMessageCenterView> message_center_view_;
DISALLOW_COPY_AND_ASSIGN(NewUnifiedMessageCenterViewTest);
};
TEST_F(NewUnifiedMessageCenterViewTest, AddAndRemoveNotification) {
CreateMessageCenterView();
EXPECT_FALSE(message_center_view()->visible());
auto id0 = AddNotification();
EXPECT_TRUE(message_center_view()->visible());
MessageCenter::Get()->RemoveNotification(id0, true /* by_user */);
EXPECT_FALSE(message_center_view()->visible());
}
TEST_F(NewUnifiedMessageCenterViewTest, NotVisibleWhenLocked) {
AddNotification();
AddNotification();
BlockUserSession(BLOCKED_BY_LOCK_SCREEN);
CreateMessageCenterView();
EXPECT_FALSE(message_center_view()->visible());
}
TEST_F(NewUnifiedMessageCenterViewTest, ClearAllPressed) {
AddNotification();
AddNotification();
CreateMessageCenterView();
EXPECT_TRUE(message_center_view()->visible());
// ScrollView fills MessageCenterView.
EXPECT_EQ(message_center_view()->bounds(), GetScroller()->bounds());
EXPECT_EQ(GetMessageListView()->GetPreferredSize().width(),
message_center_view()->GetPreferredSize().width());
// MessageCenterView returns smaller height to hide Clear All button.
EXPECT_EQ(kUnifiedNotificationCenterSpacing,
message_center_view()->GetPreferredSize().height() -
GetMessageListView()->GetPreferredSize().height());
// ScrollView has larger height than MessageListView because it has Clear All
// button.
EXPECT_EQ(4 * kUnifiedNotificationCenterSpacing,
GetScrollerContents()->GetPreferredSize().height() -
GetMessageListView()->GetPreferredSize().height());
// When Clear All button is pressed, all notifications are removed and the
// view becomes invisible.
message_center_view()->ButtonPressed(nullptr, DummyEvent());
EXPECT_FALSE(message_center_view()->visible());
}
} // namespace ash
......@@ -245,14 +245,14 @@ UnifiedSystemTrayView::UnifiedSystemTrayView(
SessionController* session_controller = Shell::Get()->session_controller();
views::View* message_center_view;
if (features::IsNewMessageListViewEnabled())
if (features::IsNewMessageListViewEnabled()) {
message_center_view = new_message_center_view_;
else
} else {
message_center_view = message_center_view_;
message_center_view->SetVisible(
session_controller->ShouldShowNotificationTray() &&
!session_controller->IsScreenLocked());
message_center_view->SetVisible(
session_controller->ShouldShowNotificationTray() &&
!session_controller->IsScreenLocked());
}
AddChildView(message_center_view);
layout->SetFlexForView(message_center_view, 1);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment