media_notification_view_modern_impl.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2020 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.
  4. #ifndef COMPONENTS_MEDIA_MESSAGE_CENTER_MEDIA_NOTIFICATION_VIEW_MODERN_IMPL_H_
  5. #define COMPONENTS_MEDIA_MESSAGE_CENTER_MEDIA_NOTIFICATION_VIEW_MODERN_IMPL_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "components/media_message_center/media_notification_view.h"
  8. #include "base/component_export.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "components/media_message_center/media_notification_view.h"
  11. #include "components/media_message_center/notification_theme.h"
  12. #include "services/media_session/public/mojom/media_session.mojom.h"
  13. #include "third_party/abseil-cpp/absl/types/optional.h"
  14. #include "ui/base/metadata/metadata_header_macros.h"
  15. #include "ui/views/controls/button/image_button.h"
  16. #include "ui/views/controls/image_view.h"
  17. #include "ui/views/controls/label.h"
  18. namespace views {
  19. class Button;
  20. class ToggleImageButton;
  21. } // namespace views
  22. namespace media_message_center {
  23. namespace {
  24. class MediaButton;
  25. } // anonymous namespace
  26. class MediaArtworkView;
  27. class MediaControlsProgressView;
  28. class MediaNotificationBackground;
  29. class MediaNotificationContainer;
  30. class MediaNotificationItem;
  31. class MediaNotificationVolumeSliderView;
  32. class COMPONENT_EXPORT(MEDIA_MESSAGE_CENTER) MediaNotificationViewModernImpl
  33. : public MediaNotificationView {
  34. public:
  35. METADATA_HEADER(MediaNotificationViewModernImpl);
  36. // The name of the histogram used when recording whether the artwork was
  37. // present.
  38. static const char kArtworkHistogramName[];
  39. // The name of the histogram used when recording the type of metadata that was
  40. // displayed.
  41. static const char kMetadataHistogramName[];
  42. // The type of metadata that was displayed. This is used in metrics so new
  43. // values must only be added to the end.
  44. enum class Metadata {
  45. kTitle,
  46. kArtist,
  47. kAlbum,
  48. kCount,
  49. kSource,
  50. kMaxValue = kSource,
  51. };
  52. MediaNotificationViewModernImpl(
  53. MediaNotificationContainer* container,
  54. base::WeakPtr<MediaNotificationItem> item,
  55. std::unique_ptr<views::View> notification_controls_view,
  56. std::unique_ptr<views::View> notification_footer_view,
  57. int notification_width,
  58. absl::optional<NotificationTheme> theme = absl::nullopt);
  59. MediaNotificationViewModernImpl(const MediaNotificationViewModernImpl&) =
  60. delete;
  61. MediaNotificationViewModernImpl& operator=(
  62. const MediaNotificationViewModernImpl&) = delete;
  63. ~MediaNotificationViewModernImpl() override;
  64. // views::View:
  65. void GetAccessibleNodeData(ui::AXNodeData* node_data) override;
  66. void OnThemeChanged() override;
  67. // MediaNotificationView
  68. void SetForcedExpandedState(bool* forced_expanded_state) override {}
  69. void SetExpanded(bool expanded) override {}
  70. void UpdateCornerRadius(int top_radius, int bottom_radius) override;
  71. void UpdateWithMediaSessionInfo(
  72. const media_session::mojom::MediaSessionInfoPtr& session_info) override;
  73. void UpdateWithMediaMetadata(
  74. const media_session::MediaMetadata& metadata) override;
  75. void UpdateWithMediaActions(
  76. const base::flat_set<media_session::mojom::MediaSessionAction>& actions)
  77. override;
  78. void UpdateWithMediaPosition(
  79. const media_session::MediaPosition& position) override;
  80. void UpdateWithMediaArtwork(const gfx::ImageSkia& image) override;
  81. void UpdateWithFavicon(const gfx::ImageSkia& icon) override;
  82. void UpdateWithVectorIcon(const gfx::VectorIcon& vector_icon) override {}
  83. void UpdateDeviceSelectorAvailability(bool availability) override;
  84. void UpdateWithMuteStatus(bool mute) override;
  85. void UpdateWithVolume(float volume) override;
  86. // Testing methods
  87. const views::Label* title_label_for_testing() const { return title_label_; }
  88. const views::Label* subtitle_label_for_testing() const {
  89. return subtitle_label_;
  90. }
  91. views::Button* picture_in_picture_button_for_testing() const;
  92. const views::View* media_controls_container_for_testing() const {
  93. return media_controls_container_;
  94. }
  95. private:
  96. friend class MediaNotificationViewModernImplTest;
  97. // Creates an image button with an icon that matches |action| and adds it
  98. // to |parent_view|. When clicked it will trigger |action| on the session.
  99. // |accessible_name| is the text used for screen readers and the
  100. // button's tooltip.
  101. void CreateMediaButton(views::View* parent_view,
  102. media_session::mojom::MediaSessionAction action);
  103. void UpdateActionButtonsVisibility();
  104. MediaNotificationBackground* GetMediaNotificationBackground();
  105. void UpdateForegroundColor();
  106. void ButtonPressed(views::Button* button);
  107. void SeekTo(double seek_progress);
  108. void OnMuteButtonClicked();
  109. void SetVolume(float volume);
  110. // Container that receives events.
  111. const raw_ptr<MediaNotificationContainer> container_;
  112. // Keeps track of media metadata and controls the session when buttons are
  113. // clicked.
  114. base::WeakPtr<MediaNotificationItem> item_;
  115. bool has_artwork_ = false;
  116. // Set of enabled actions.
  117. base::flat_set<media_session::mojom::MediaSessionAction> enabled_actions_;
  118. // Stores the text to be read by screen readers describing the notification.
  119. // Contains the title, artist and album separated by hyphens.
  120. std::u16string accessible_name_;
  121. raw_ptr<MediaNotificationBackground> background_;
  122. media_session::MediaPosition position_;
  123. // Container views directly attached to this view.
  124. raw_ptr<views::View> artwork_container_ = nullptr;
  125. raw_ptr<MediaArtworkView> artwork_ = nullptr;
  126. raw_ptr<views::Label> title_label_ = nullptr;
  127. raw_ptr<views::Label> subtitle_label_ = nullptr;
  128. raw_ptr<MediaButton> picture_in_picture_button_ = nullptr;
  129. raw_ptr<views::View> notification_controls_spacer_ = nullptr;
  130. raw_ptr<views::View> media_controls_container_ = nullptr;
  131. raw_ptr<MediaButton> play_pause_button_ = nullptr;
  132. raw_ptr<MediaControlsProgressView> progress_ = nullptr;
  133. raw_ptr<views::ToggleImageButton> mute_button_ = nullptr;
  134. raw_ptr<MediaNotificationVolumeSliderView> volume_slider_ = nullptr;
  135. absl::optional<NotificationTheme> theme_;
  136. };
  137. } // namespace media_message_center
  138. #endif // COMPONENTS_MEDIA_MESSAGE_CENTER_MEDIA_NOTIFICATION_VIEW_MODERN_IMPL_H_