Commit 9f82bd11 authored by Tommy Steimel's avatar Tommy Steimel Committed by Commit Bot

GMC: Make MediaNotificationView independent of MessageView

This CL adds a new MediaNotificationContainer abstraction to the media
notification so that the MediaNotificationView no longer depends on Ash
or MessageView. This will allow media notifications to be created that
don't need to be sent to the MessageCenter. Instead of
MediaNotificationView being a MessageView, it will be contained in a
MessageView in Ash when it needs to be sent to the MessageCenter.

This is part of a refactoring of the media notification to make it
reusable outside of ash/media.

Bug: 959478
Change-Id: Iac96211b3feb6e640b5e79a61832927c72a4fa88
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1616953Reviewed-by: default avatarBecca Hughes <beccahughes@chromium.org>
Commit-Queue: Tommy Steimel <steimel@chromium.org>
Cr-Commit-Position: refs/heads/master@{#662617}
parent 66cdecd1
...@@ -453,6 +453,9 @@ component("ash") { ...@@ -453,6 +453,9 @@ component("ash") {
"media/media_notification_background.h", "media/media_notification_background.h",
"media/media_notification_constants.cc", "media/media_notification_constants.cc",
"media/media_notification_constants.h", "media/media_notification_constants.h",
"media/media_notification_container.h",
"media/media_notification_container_impl.cc",
"media/media_notification_container_impl.h",
"media/media_notification_controller.h", "media/media_notification_controller.h",
"media/media_notification_controller_impl.cc", "media/media_notification_controller_impl.cc",
"media/media_notification_controller_impl.h", "media/media_notification_controller_impl.h",
......
// Copyright 2019 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.
#ifndef ASH_MEDIA_MEDIA_NOTIFICATION_CONTAINER_H_
#define ASH_MEDIA_MEDIA_NOTIFICATION_CONTAINER_H_
#include "ash/ash_export.h"
namespace ash {
// MediaNotificationContainer is an interface for containers of
// MediaNotificationView components to receive events from the
// MediaNotificationView.
class ASH_EXPORT MediaNotificationContainer {
public:
// Called when MediaNotificationView's expanded state changes.
virtual void OnExpanded(bool expanded) = 0;
};
} // namespace ash
#endif // ASH_MEDIA_MEDIA_NOTIFICATION_CONTAINER_H_
// Copyright 2019 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/media/media_notification_container_impl.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/views/notification_control_buttons_view.h"
#include "ui/native_theme/native_theme_dark_aura.h"
#include "ui/views/background.h"
#include "ui/views/layout/fill_layout.h"
namespace ash {
MediaNotificationContainerImpl::MediaNotificationContainerImpl(
const message_center::Notification& notification,
base::WeakPtr<MediaNotificationItem> item)
: message_center::MessageView(notification),
control_buttons_view_(
std::make_unique<message_center::NotificationControlButtonsView>(
this)),
view_(this,
std::move(item),
control_buttons_view_.get(),
message_center::MessageCenter::Get()
->GetSystemNotificationAppName()) {
SetLayoutManager(std::make_unique<views::FillLayout>());
SetNativeTheme(ui::NativeThemeDarkAura::instance());
// Since we own these, we don't want Views to destroy them when their parent
// is destroyed.
control_buttons_view_->set_owned_by_client();
view_.set_owned_by_client();
AddChildView(&view_);
SetBackground(views::CreateSolidBackground(SK_ColorTRANSPARENT));
}
MediaNotificationContainerImpl::~MediaNotificationContainerImpl() = default;
void MediaNotificationContainerImpl::UpdateWithNotification(
const message_center::Notification& notification) {
MessageView::UpdateWithNotification(notification);
UpdateControlButtonsVisibilityWithNotification(notification);
PreferredSizeChanged();
Layout();
SchedulePaint();
}
message_center::NotificationControlButtonsView*
MediaNotificationContainerImpl::GetControlButtonsView() const {
return control_buttons_view_.get();
}
void MediaNotificationContainerImpl::SetExpanded(bool expanded) {
view_.SetExpanded(expanded);
}
void MediaNotificationContainerImpl::UpdateCornerRadius(int top_radius,
int bottom_radius) {
view_.UpdateCornerRadius(top_radius, bottom_radius);
}
void MediaNotificationContainerImpl::OnExpanded(bool expanded) {
PreferredSizeChanged();
}
void MediaNotificationContainerImpl::OnMouseEvent(ui::MouseEvent* event) {
switch (event->type()) {
case ui::ET_MOUSE_ENTERED:
case ui::ET_MOUSE_EXITED:
UpdateControlButtonsVisibility();
break;
default:
break;
}
View::OnMouseEvent(event);
}
void MediaNotificationContainerImpl::
UpdateControlButtonsVisibilityWithNotification(
const message_center::Notification& notification) {
// Media notifications do not use the settings and snooze buttons.
DCHECK(!notification.should_show_settings_button());
DCHECK(!notification.should_show_snooze_button());
control_buttons_view_->ShowCloseButton(!notification.pinned());
UpdateControlButtonsVisibility();
}
} // namespace ash
// Copyright 2019 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.
#ifndef ASH_MEDIA_MEDIA_NOTIFICATION_CONTAINER_IMPL_H_
#define ASH_MEDIA_MEDIA_NOTIFICATION_CONTAINER_IMPL_H_
#include "ash/ash_export.h"
#include "ash/media/media_notification_container.h"
#include "ash/media/media_notification_view.h"
#include "ui/message_center/views/message_view.h"
namespace ash {
class MediaNotificationItem;
// MediaNotificationContainerImpl will show up as a custom notification. It will
// show the currently playing media and provide playback controls. There will
// also be control buttons (e.g. close) in the top right corner that will hide
// and show if the notification is hovered.
class ASH_EXPORT MediaNotificationContainerImpl
: public message_center::MessageView,
public MediaNotificationContainer {
public:
explicit MediaNotificationContainerImpl(
const message_center::Notification& notification,
base::WeakPtr<MediaNotificationItem> item);
~MediaNotificationContainerImpl() override;
// message_center::MessageView:
void UpdateWithNotification(
const message_center::Notification& notification) override;
message_center::NotificationControlButtonsView* GetControlButtonsView()
const override;
void SetExpanded(bool expanded) override;
void UpdateCornerRadius(int top_radius, int bottom_radius) override;
// MediaNotificationContainer:
void OnExpanded(bool expanded) override;
// views::View:
void OnMouseEvent(ui::MouseEvent* event) override;
private:
void UpdateControlButtonsVisibilityWithNotification(
const message_center::Notification& notification);
// View containing close and settings buttons.
std::unique_ptr<message_center::NotificationControlButtonsView>
control_buttons_view_;
MediaNotificationView view_;
DISALLOW_COPY_AND_ASSIGN(MediaNotificationContainerImpl);
};
} // namespace ash
#endif // ASH_MEDIA_MEDIA_NOTIFICATION_CONTAINER_IMPL_H_
...@@ -5,8 +5,8 @@ ...@@ -5,8 +5,8 @@
#include "ash/media/media_notification_controller_impl.h" #include "ash/media/media_notification_controller_impl.h"
#include "ash/media/media_notification_constants.h" #include "ash/media/media_notification_constants.h"
#include "ash/media/media_notification_container_impl.h"
#include "ash/media/media_notification_item.h" #include "ash/media/media_notification_item.h"
#include "ash/media/media_notification_view.h"
#include "ash/public/cpp/notification_utils.h" #include "ash/public/cpp/notification_utils.h"
#include "ash/session/session_controller_impl.h" #include "ash/session/session_controller_impl.h"
#include "ash/session/session_observer.h" #include "ash/session/session_observer.h"
...@@ -193,7 +193,7 @@ void MediaNotificationControllerImpl::HideNotification(const std::string& id) { ...@@ -193,7 +193,7 @@ void MediaNotificationControllerImpl::HideNotification(const std::string& id) {
message_center::MessageCenter::Get()->RemoveNotification(id, false); message_center::MessageCenter::Get()->RemoveNotification(id, false);
} }
std::unique_ptr<MediaNotificationView> std::unique_ptr<MediaNotificationContainerImpl>
MediaNotificationControllerImpl::CreateMediaNotification( MediaNotificationControllerImpl::CreateMediaNotification(
const message_center::Notification& notification) { const message_center::Notification& notification) {
base::WeakPtr<MediaNotificationItem> item; base::WeakPtr<MediaNotificationItem> item;
...@@ -202,7 +202,8 @@ MediaNotificationControllerImpl::CreateMediaNotification( ...@@ -202,7 +202,8 @@ MediaNotificationControllerImpl::CreateMediaNotification(
if (it != notifications_.end()) if (it != notifications_.end())
item = it->second.GetWeakPtr(); item = it->second.GetWeakPtr();
return std::make_unique<MediaNotificationView>(notification, std::move(item)); return std::make_unique<MediaNotificationContainerImpl>(notification,
std::move(item));
} }
void MediaNotificationControllerImpl::RecordConcurrentNotificationCount() { void MediaNotificationControllerImpl::RecordConcurrentNotificationCount() {
......
...@@ -31,7 +31,7 @@ namespace { ...@@ -31,7 +31,7 @@ namespace {
class MediaNotificationBlocker; class MediaNotificationBlocker;
} // namespace } // namespace
class MediaNotificationView; class MediaNotificationContainerImpl;
// MediaNotificationControllerImpl will show/hide media notifications when media // MediaNotificationControllerImpl will show/hide media notifications when media
// sessions are active. These notifications will show metadata and playback // sessions are active. These notifications will show metadata and playback
...@@ -58,7 +58,7 @@ class ASH_EXPORT MediaNotificationControllerImpl ...@@ -58,7 +58,7 @@ class ASH_EXPORT MediaNotificationControllerImpl
void ShowNotification(const std::string& id) override; void ShowNotification(const std::string& id) override;
void HideNotification(const std::string& id) override; void HideNotification(const std::string& id) override;
std::unique_ptr<MediaNotificationView> CreateMediaNotification( std::unique_ptr<MediaNotificationContainerImpl> CreateMediaNotification(
const message_center::Notification& notification); const message_center::Notification& notification);
ash::MediaNotificationItem* GetItem(const std::string& id) { ash::MediaNotificationItem* GetItem(const std::string& id) {
......
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
#include "ash/media/media_notification_background.h" #include "ash/media/media_notification_background.h"
#include "ash/media/media_notification_constants.h" #include "ash/media/media_notification_constants.h"
#include "ash/media/media_notification_container.h"
#include "ash/media/media_notification_item.h" #include "ash/media/media_notification_item.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h" #include "ash/strings/grit/ash_strings.h"
#include "base/metrics/histogram_macros.h" #include "base/metrics/histogram_macros.h"
#include "base/stl_util.h" #include "base/stl_util.h"
...@@ -18,11 +18,8 @@ ...@@ -18,11 +18,8 @@
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/font.h" #include "ui/gfx/font.h"
#include "ui/gfx/font_list.h" #include "ui/gfx/font_list.h"
#include "ui/message_center/message_center.h"
#include "ui/message_center/public/cpp/message_center_constants.h" #include "ui/message_center/public/cpp/message_center_constants.h"
#include "ui/message_center/views/notification_control_buttons_view.h"
#include "ui/message_center/views/notification_header_view.h" #include "ui/message_center/views/notification_header_view.h"
#include "ui/native_theme/native_theme_dark_aura.h"
#include "ui/views/controls/button/image_button_factory.h" #include "ui/views/controls/button/image_button_factory.h"
#include "ui/views/layout/box_layout.h" #include "ui/views/layout/box_layout.h"
#include "ui/views/style/typography.h" #include "ui/views/style/typography.h"
...@@ -108,24 +105,26 @@ const char MediaNotificationView::kMetadataHistogramName[] = ...@@ -108,24 +105,26 @@ const char MediaNotificationView::kMetadataHistogramName[] =
"Media.Notification.MetadataPresent"; "Media.Notification.MetadataPresent";
MediaNotificationView::MediaNotificationView( MediaNotificationView::MediaNotificationView(
const message_center::Notification& notification, MediaNotificationContainer* container,
base::WeakPtr<MediaNotificationItem> item) base::WeakPtr<MediaNotificationItem> item,
: message_center::MessageView(notification), item_(std::move(item)) { views::View* header_row_controls_view,
const base::string16& default_app_name)
: container_(container),
item_(std::move(item)),
header_row_controls_view_(header_row_controls_view),
default_app_name_(default_app_name) {
DCHECK(container_);
SetLayoutManager(std::make_unique<views::BoxLayout>( SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::kVertical, gfx::Insets(), 0)); views::BoxLayout::kVertical, gfx::Insets(), 0));
SetNativeTheme(ui::NativeThemeDarkAura::instance());
// |controls_button_view_| has the common notification control buttons.
control_buttons_view_ =
std::make_unique<message_center::NotificationControlButtonsView>(this);
control_buttons_view_->set_owned_by_client();
// |header_row_| contains app_icon, app_name, control buttons, etc.
auto header_row = auto header_row =
std::make_unique<message_center::NotificationHeaderView>(this); std::make_unique<message_center::NotificationHeaderView>(this);
header_row->AddChildView(control_buttons_view_.get());
header_row->SetAppName( if (header_row_controls_view_)
message_center::MessageCenter::Get()->GetSystemNotificationAppName()); header_row->AddChildView(header_row_controls_view_);
header_row->SetAppName(default_app_name_);
header_row->ClearAppIcon(); header_row->ClearAppIcon();
header_row->SetProperty(views::kMarginsKey, header_row->SetProperty(views::kMarginsKey,
new gfx::Insets(kMediaNotificationHeaderTopInset, new gfx::Insets(kMediaNotificationHeaderTopInset,
...@@ -205,7 +204,6 @@ MediaNotificationView::MediaNotificationView( ...@@ -205,7 +204,6 @@ MediaNotificationView::MediaNotificationView(
message_center::kNotificationCornerRadius, kMediaImageMaxWidthPct)); message_center::kNotificationCornerRadius, kMediaImageMaxWidthPct));
UpdateForegroundColor(); UpdateForegroundColor();
UpdateControlButtonsVisibilityWithNotification(notification);
UpdateCornerRadius(message_center::kNotificationCornerRadius, UpdateCornerRadius(message_center::kNotificationCornerRadius,
message_center::kNotificationCornerRadius); message_center::kNotificationCornerRadius);
UpdateViewForExpandedState(); UpdateViewForExpandedState();
...@@ -219,22 +217,6 @@ MediaNotificationView::~MediaNotificationView() { ...@@ -219,22 +217,6 @@ MediaNotificationView::~MediaNotificationView() {
item_->SetView(nullptr); item_->SetView(nullptr);
} }
void MediaNotificationView::UpdateWithNotification(
const message_center::Notification& notification) {
MessageView::UpdateWithNotification(notification);
UpdateControlButtonsVisibilityWithNotification(notification);
PreferredSizeChanged();
Layout();
SchedulePaint();
}
message_center::NotificationControlButtonsView*
MediaNotificationView::GetControlButtonsView() const {
return control_buttons_view_.get();
}
void MediaNotificationView::SetExpanded(bool expanded) { void MediaNotificationView::SetExpanded(bool expanded) {
if (expanded_ == expanded) if (expanded_ == expanded)
return; return;
...@@ -264,19 +246,6 @@ void MediaNotificationView::GetAccessibleNodeData(ui::AXNodeData* node_data) { ...@@ -264,19 +246,6 @@ void MediaNotificationView::GetAccessibleNodeData(ui::AXNodeData* node_data) {
node_data->SetName(accessible_name_); node_data->SetName(accessible_name_);
} }
void MediaNotificationView::OnMouseEvent(ui::MouseEvent* event) {
switch (event->type()) {
case ui::ET_MOUSE_ENTERED:
case ui::ET_MOUSE_EXITED:
UpdateControlButtonsVisibility();
break;
default:
break;
}
View::OnMouseEvent(event);
}
void MediaNotificationView::ButtonPressed(views::Button* sender, void MediaNotificationView::ButtonPressed(views::Button* sender,
const ui::Event& event) { const ui::Event& event) {
if (sender == header_row_) { if (sender == header_row_) {
...@@ -315,11 +284,9 @@ void MediaNotificationView::UpdateWithMediaSessionInfo( ...@@ -315,11 +284,9 @@ void MediaNotificationView::UpdateWithMediaSessionInfo(
void MediaNotificationView::UpdateWithMediaMetadata( void MediaNotificationView::UpdateWithMediaMetadata(
const media_session::MediaMetadata& metadata) { const media_session::MediaMetadata& metadata) {
header_row_->SetAppName( header_row_->SetAppName(metadata.source_title.empty()
metadata.source_title.empty() ? default_app_name_
? message_center::MessageCenter::Get()->GetSystemNotificationAppName() : metadata.source_title);
: metadata.source_title);
title_label_->SetText(metadata.title); title_label_->SetText(metadata.title);
artist_label_->SetText(metadata.artist); artist_label_->SetText(metadata.artist);
header_row_->SetSummaryText(metadata.album); header_row_->SetSummaryText(metadata.album);
...@@ -386,16 +353,6 @@ void MediaNotificationView::UpdateWithMediaIcon(const gfx::ImageSkia& image) { ...@@ -386,16 +353,6 @@ void MediaNotificationView::UpdateWithMediaIcon(const gfx::ImageSkia& image) {
} }
} }
void MediaNotificationView::UpdateControlButtonsVisibilityWithNotification(
const message_center::Notification& notification) {
// Media notifications do not use the settings and snooze buttons.
DCHECK(!notification.should_show_settings_button());
DCHECK(!notification.should_show_snooze_button());
control_buttons_view_->ShowCloseButton(!notification.pinned());
UpdateControlButtonsVisibility();
}
void MediaNotificationView::UpdateActionButtonsVisibility() { void MediaNotificationView::UpdateActionButtonsVisibility() {
std::set<MediaSessionAction> visible_actions = std::set<MediaSessionAction> visible_actions =
CalculateVisibleActions(IsActuallyExpanded()); CalculateVisibleActions(IsActuallyExpanded());
...@@ -447,6 +404,8 @@ void MediaNotificationView::UpdateViewForExpandedState() { ...@@ -447,6 +404,8 @@ void MediaNotificationView::UpdateViewForExpandedState() {
header_row_->SetExpanded(expanded); header_row_->SetExpanded(expanded);
UpdateActionButtonsVisibility(); UpdateActionButtonsVisibility();
container_->OnExpanded(expanded);
} }
void MediaNotificationView::CreateMediaButton( void MediaNotificationView::CreateMediaButton(
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#include "ash/ash_export.h" #include "ash/ash_export.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "services/media_session/public/mojom/media_session.mojom.h" #include "services/media_session/public/mojom/media_session.mojom.h"
#include "ui/message_center/views/message_view.h"
#include "ui/views/controls/button/button.h" #include "ui/views/controls/button/button.h"
#include "ui/views/controls/button/image_button.h" #include "ui/views/controls/button/image_button.h"
#include "ui/views/controls/label.h" #include "ui/views/controls/label.h"
...@@ -35,13 +34,12 @@ class View; ...@@ -35,13 +34,12 @@ class View;
namespace ash { namespace ash {
class MediaNotificationBackground; class MediaNotificationBackground;
class MediaNotificationContainer;
class MediaNotificationItem; class MediaNotificationItem;
// MediaNotificationView will show up as a custom notification. It will show the // MediaNotificationView will show up as a custom view. It will show the
// currently playing media and provide playback controls. There will also be // currently playing media and provide playback controls.
// control buttons (e.g. close) in the top right corner that will hide and show class ASH_EXPORT MediaNotificationView : public views::View,
// if the notification is hovered.
class ASH_EXPORT MediaNotificationView : public message_center::MessageView,
public views::ButtonListener { public views::ButtonListener {
public: public:
// The name of the histogram used when recorded whether the artwork was // The name of the histogram used when recorded whether the artwork was
...@@ -62,21 +60,17 @@ class ASH_EXPORT MediaNotificationView : public message_center::MessageView, ...@@ -62,21 +60,17 @@ class ASH_EXPORT MediaNotificationView : public message_center::MessageView,
kMaxValue = kCount, kMaxValue = kCount,
}; };
MediaNotificationView(const message_center::Notification& notification, MediaNotificationView(MediaNotificationContainer* container,
base::WeakPtr<MediaNotificationItem> item); base::WeakPtr<MediaNotificationItem> item,
views::View* header_row_controls_view,
const base::string16& default_app_name);
~MediaNotificationView() override; ~MediaNotificationView() override;
// message_center::MessageView: void SetExpanded(bool expanded);
void UpdateWithNotification( void UpdateCornerRadius(int top_radius, int bottom_radius);
const message_center::Notification& notification) override;
message_center::NotificationControlButtonsView* GetControlButtonsView()
const override;
void SetExpanded(bool expanded) override;
void UpdateCornerRadius(int top_radius, int bottom_radius) override;
void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
// views::View: // views::View:
void OnMouseEvent(ui::MouseEvent* event) override; void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
// views::ButtonListener: // views::ButtonListener:
void ButtonPressed(views::Button* sender, const ui::Event& event) override; void ButtonPressed(views::Button* sender, const ui::Event& event) override;
...@@ -92,9 +86,6 @@ class ASH_EXPORT MediaNotificationView : public message_center::MessageView, ...@@ -92,9 +86,6 @@ class ASH_EXPORT MediaNotificationView : public message_center::MessageView,
private: private:
friend class MediaNotificationViewTest; friend class MediaNotificationViewTest;
void UpdateControlButtonsVisibilityWithNotification(
const message_center::Notification& notification);
// Creates an image button with an icon that matches |action| and adds it // Creates an image button with an icon that matches |action| and adds it
// to |button_row_|. When clicked it will trigger |action| on the session. // to |button_row_|. When clicked it will trigger |action| on the session.
// |accessible_name| is the text used for screen readers. // |accessible_name| is the text used for screen readers.
...@@ -114,11 +105,19 @@ class ASH_EXPORT MediaNotificationView : public message_center::MessageView, ...@@ -114,11 +105,19 @@ class ASH_EXPORT MediaNotificationView : public message_center::MessageView,
void UpdateForegroundColor(); void UpdateForegroundColor();
// Container that receives OnExpanded events.
MediaNotificationContainer* const container_;
// Keeps track of media metadata and controls the session when buttons are
// clicked.
base::WeakPtr<MediaNotificationItem> item_; base::WeakPtr<MediaNotificationItem> item_;
// View containing close and settings buttons. // Optional View that is put into the header row. E.g. in Ash we show
std::unique_ptr<message_center::NotificationControlButtonsView> // notification control buttons.
control_buttons_view_; views::View* header_row_controls_view_;
// String to set as the app name of the header when there is no source title.
base::string16 default_app_name_;
bool has_artwork_ = false; bool has_artwork_ = false;
......
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