Jazz Xu | 4675273 | 2020-08-25 23:01:05 | [diff] [blame] | 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 | |
| 5 | #include "ash/system/media/media_tray.h" |
| 6 | |
Jazz Xu | 943f66a | 2020-10-28 12:05:53 | [diff] [blame] | 7 | #include "ash/focus_cycler.h" |
Jazz Xu | bfefcba | 2020-09-25 01:59:13 | [diff] [blame] | 8 | #include "ash/public/cpp/ash_pref_names.h" |
Jazz Xu | 4675273 | 2020-08-25 23:01:05 | [diff] [blame] | 9 | #include "ash/public/cpp/media_notification_provider.h" |
| 10 | #include "ash/resources/vector_icons/vector_icons.h" |
Jazz Xu | bfefcba | 2020-09-25 01:59:13 | [diff] [blame] | 11 | #include "ash/root_window_controller.h" |
Jazz Xu | 4675273 | 2020-08-25 23:01:05 | [diff] [blame] | 12 | #include "ash/session/session_controller_impl.h" |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 13 | #include "ash/shelf/shelf.h" |
Jazz Xu | 4675273 | 2020-08-25 23:01:05 | [diff] [blame] | 14 | #include "ash/shell.h" |
| 15 | #include "ash/strings/grit/ash_strings.h" |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 16 | #include "ash/style/ash_color_provider.h" |
| 17 | #include "ash/system/tray/tray_bubble_view.h" |
| 18 | #include "ash/system/tray/tray_bubble_wrapper.h" |
Jazz Xu | 4675273 | 2020-08-25 23:01:05 | [diff] [blame] | 19 | #include "ash/system/tray/tray_constants.h" |
| 20 | #include "ash/system/tray/tray_container.h" |
| 21 | #include "ash/system/tray/tray_utils.h" |
Jazz Xu | 5ba3329 | 2020-10-08 22:00:13 | [diff] [blame] | 22 | #include "base/metrics/histogram_functions.h" |
Jazz Xu | 4675273 | 2020-08-25 23:01:05 | [diff] [blame] | 23 | #include "base/strings/string_util.h" |
Jazz Xu | ac3c3d0 | 2020-10-07 19:55:55 | [diff] [blame] | 24 | #include "components/media_message_center/media_notification_view_impl.h" |
Jazz Xu | bfefcba | 2020-09-25 01:59:13 | [diff] [blame] | 25 | #include "components/prefs/pref_change_registrar.h" |
| 26 | #include "components/prefs/pref_registry_simple.h" |
| 27 | #include "components/prefs/pref_service.h" |
Jazz Xu | 88df8955 | 2020-10-23 22:56:51 | [diff] [blame] | 28 | #include "media/base/media_switches.h" |
Jazz Xu | 4675273 | 2020-08-25 23:01:05 | [diff] [blame] | 29 | #include "ui/base/l10n/l10n_util.h" |
Jazz Xu | bfefcba | 2020-09-25 01:59:13 | [diff] [blame] | 30 | #include "ui/display/manager/display_manager.h" |
| 31 | #include "ui/display/manager/managed_display_info.h" |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 32 | #include "ui/gfx/geometry/insets.h" |
Jazz Xu | 4675273 | 2020-08-25 23:01:05 | [diff] [blame] | 33 | #include "ui/gfx/paint_vector_icon.h" |
Jazz Xu | bfefcba | 2020-09-25 01:59:13 | [diff] [blame] | 34 | #include "ui/views/controls/button/image_button.h" |
Jazz Xu | 4675273 | 2020-08-25 23:01:05 | [diff] [blame] | 35 | #include "ui/views/controls/image_view.h" |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 36 | #include "ui/views/controls/label.h" |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 37 | #include "ui/views/layout/box_layout.h" |
Jazz Xu | 0fdf9911 | 2020-10-02 18:13:13 | [diff] [blame] | 38 | #include "ui/views/layout/fill_layout.h" |
Jazz Xu | 4675273 | 2020-08-25 23:01:05 | [diff] [blame] | 39 | |
| 40 | namespace ash { |
| 41 | |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 42 | namespace { |
| 43 | |
Jazz Xu | 0fdf9911 | 2020-10-02 18:13:13 | [diff] [blame] | 44 | constexpr int kNoMediaTextFontSizeIncrease = 2; |
Jazz Xu | 58a681f | 2020-09-30 01:40:37 | [diff] [blame] | 45 | constexpr int kTitleFontSizeIncrease = 4; |
| 46 | constexpr int kTitleViewHeight = 56; |
| 47 | |
Jazz Xu | bfefcba | 2020-09-25 01:59:13 | [diff] [blame] | 48 | constexpr gfx::Insets kTitleViewInsets = gfx::Insets(0, 16, 0, 16); |
| 49 | |
| 50 | // Minimum screen diagonal (in inches) for pinning global media controls |
| 51 | // on shelf by default. |
| 52 | constexpr float kMinimumScreenSizeDiagonal = 10.0f; |
| 53 | |
| 54 | // Calculate screen size and returns true if screen size is larger than |
| 55 | // kMinimumScreenSizeDiagonal. |
| 56 | bool GetIsPinnedToShelfByDefault() { |
| 57 | // Happens in test. |
| 58 | if (!Shell::HasInstance()) |
| 59 | return false; |
| 60 | |
| 61 | display::ManagedDisplayInfo info = |
| 62 | Shell::Get()->display_manager()->GetDisplayInfo( |
| 63 | display::Screen::GetScreen()->GetPrimaryDisplay().id()); |
| 64 | DCHECK(info.device_dpi()); |
| 65 | float screen_width = info.size_in_pixel().width() / info.device_dpi(); |
| 66 | float screen_height = info.size_in_pixel().height() / info.device_dpi(); |
| 67 | |
| 68 | float diagonal_len = sqrt(pow(screen_width, 2) + pow(screen_height, 2)); |
| 69 | return diagonal_len > kMinimumScreenSizeDiagonal; |
| 70 | } |
| 71 | |
Jazz Xu | 88df8955 | 2020-10-23 22:56:51 | [diff] [blame] | 72 | // Used for getting default pin state for experiment. |
| 73 | bool GetIsPinnedToShelfByFeatureParams() { |
| 74 | switch (media::kCrosGlobalMediaControlsPinParam.Get()) { |
| 75 | case media::kCrosGlobalMediaControlsPinOptions::kPin: |
| 76 | return true; |
| 77 | case media::kCrosGlobalMediaControlsPinOptions::kNotPin: |
| 78 | return false; |
| 79 | case media::kCrosGlobalMediaControlsPinOptions::kHeuristic: |
| 80 | return GetIsPinnedToShelfByDefault(); |
| 81 | } |
| 82 | |
| 83 | NOTREACHED(); |
| 84 | return false; |
| 85 | } |
| 86 | |
Jazz Xu | bfefcba | 2020-09-25 01:59:13 | [diff] [blame] | 87 | // Enum that specifies the pin state of global media controls. |
| 88 | enum PinState { |
| 89 | kDefault = 0, |
| 90 | kUnpinned, |
| 91 | kPinned, |
| 92 | }; |
| 93 | |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 94 | // View that contains global media controls' title. |
| 95 | class GlobalMediaControlsTitleView : public views::View { |
| 96 | public: |
| 97 | GlobalMediaControlsTitleView() { |
| 98 | SetBorder(views::CreatePaddedBorder( |
| 99 | views::CreateSolidSidedBorder( |
| 100 | 0, 0, kMenuSeparatorWidth, 0, |
| 101 | AshColorProvider::Get()->GetContentLayerColor( |
| 102 | AshColorProvider::ContentLayerType::kSeparatorColor)), |
| 103 | gfx::Insets(kMenuSeparatorVerticalPadding, 0, |
| 104 | kMenuSeparatorVerticalPadding - kMenuSeparatorWidth, 0))); |
| 105 | |
| 106 | auto* box_layout = SetLayoutManager(std::make_unique<views::BoxLayout>( |
Jazz Xu | bfefcba | 2020-09-25 01:59:13 | [diff] [blame] | 107 | views::BoxLayout::Orientation::kHorizontal, kTitleViewInsets)); |
Jazz Xu | 58a681f | 2020-09-30 01:40:37 | [diff] [blame] | 108 | box_layout->set_minimum_cross_axis_size(kTitleViewHeight); |
Jazz Xu | bfefcba | 2020-09-25 01:59:13 | [diff] [blame] | 109 | box_layout->set_cross_axis_alignment( |
| 110 | views::BoxLayout::CrossAxisAlignment::kCenter); |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 111 | |
| 112 | auto* title_label = AddChildView(std::make_unique<views::Label>()); |
| 113 | title_label->SetText( |
| 114 | l10n_util::GetStringUTF16(IDS_ASH_GLOBAL_MEDIA_CONTROLS_TITLE)); |
| 115 | title_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
Jazz Xu | 58a681f | 2020-09-30 01:40:37 | [diff] [blame] | 116 | title_label->SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor( |
| 117 | AshColorProvider::ContentLayerType::kTextColorPrimary)); |
| 118 | title_label->SetAutoColorReadabilityEnabled(false); |
| 119 | title_label->SetFontList(views::Label::GetDefaultFontList().Derive( |
| 120 | kTitleFontSizeIncrease, gfx::Font::NORMAL, gfx::Font::Weight::MEDIUM)); |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 121 | |
Jazz Xu | bfefcba | 2020-09-25 01:59:13 | [diff] [blame] | 122 | // Media tray should always be pinned to shelf when we are opening the |
| 123 | // dialog. |
| 124 | DCHECK(MediaTray::IsPinnedToShelf()); |
| 125 | pin_button_ = AddChildView(std::make_unique<MediaTray::PinButton>()); |
| 126 | |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 127 | box_layout->SetFlexForView(title_label, 1); |
| 128 | } |
Jazz Xu | bfefcba | 2020-09-25 01:59:13 | [diff] [blame] | 129 | |
| 130 | views::Button* pin_button() { return pin_button_; } |
| 131 | |
| 132 | private: |
| 133 | MediaTray::PinButton* pin_button_ = nullptr; |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | } // namespace |
| 137 | |
Jazz Xu | bfefcba | 2020-09-25 01:59:13 | [diff] [blame] | 138 | // static |
| 139 | void MediaTray::RegisterProfilePrefs(PrefRegistrySimple* registry) { |
| 140 | registry->RegisterIntegerPref(prefs::kGlobalMediaControlsPinned, |
| 141 | PinState::kDefault); |
| 142 | } |
| 143 | |
| 144 | // static |
| 145 | bool MediaTray::IsPinnedToShelf() { |
| 146 | PrefService* pref_service = |
| 147 | Shell::Get()->session_controller()->GetActivePrefService(); |
| 148 | DCHECK(pref_service); |
| 149 | switch (pref_service->GetInteger(prefs::kGlobalMediaControlsPinned)) { |
| 150 | case PinState::kPinned: |
| 151 | return true; |
| 152 | case PinState::kUnpinned: |
| 153 | return false; |
| 154 | case PinState::kDefault: |
Jazz Xu | 88df8955 | 2020-10-23 22:56:51 | [diff] [blame] | 155 | return GetIsPinnedToShelfByFeatureParams(); |
Jazz Xu | bfefcba | 2020-09-25 01:59:13 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | NOTREACHED(); |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | // static |
| 163 | void MediaTray::SetPinnedToShelf(bool pinned) { |
| 164 | PrefService* pref_service = |
| 165 | Shell::Get()->session_controller()->GetActivePrefService(); |
| 166 | DCHECK(pref_service); |
| 167 | pref_service->SetInteger(prefs::kGlobalMediaControlsPinned, |
| 168 | pinned ? PinState::kPinned : PinState::kUnpinned); |
| 169 | } |
| 170 | |
| 171 | MediaTray::PinButton::PinButton() |
| 172 | : TopShortcutButton( |
Peter Kasting | 256b947 | 2020-10-31 03:50:53 | [diff] [blame] | 173 | base::BindRepeating(&PinButton::ButtonPressed, |
| 174 | base::Unretained(this)), |
Jazz Xu | bfefcba | 2020-09-25 01:59:13 | [diff] [blame] | 175 | MediaTray::IsPinnedToShelf() ? kPinnedIcon : kUnpinnedIcon, |
| 176 | MediaTray::IsPinnedToShelf() |
| 177 | ? IDS_ASH_GLOBAL_MEDIA_CONTROLS_PINNED_BUTTON_TOOLTIP_TEXT |
| 178 | : IDS_ASH_GLOBAL_MEDIA_CONTROLS_UNPINNED_BUTTON_TOOLTIP_TEXT) {} |
| 179 | |
Peter Kasting | 256b947 | 2020-10-31 03:50:53 | [diff] [blame] | 180 | void MediaTray::PinButton::ButtonPressed() { |
Jazz Xu | bfefcba | 2020-09-25 01:59:13 | [diff] [blame] | 181 | MediaTray::SetPinnedToShelf(!MediaTray::IsPinnedToShelf()); |
Jazz Xu | bc6f656 | 2020-10-09 04:28:43 | [diff] [blame] | 182 | base::UmaHistogramBoolean("Media.CrosGlobalMediaControls.PinAction", |
| 183 | MediaTray::IsPinnedToShelf()); |
| 184 | |
Jazz Xu | bfefcba | 2020-09-25 01:59:13 | [diff] [blame] | 185 | SetImage(views::Button::STATE_NORMAL, |
| 186 | CreateVectorIcon( |
| 187 | MediaTray::IsPinnedToShelf() ? kPinnedIcon : kUnpinnedIcon, |
| 188 | kTrayTopShortcutButtonIconSize, |
| 189 | AshColorProvider::Get()->GetContentLayerColor( |
| 190 | AshColorProvider::ContentLayerType::kIconColorPrimary))); |
| 191 | SetTooltipText(l10n_util::GetStringUTF16( |
| 192 | MediaTray::IsPinnedToShelf() |
| 193 | ? IDS_ASH_GLOBAL_MEDIA_CONTROLS_PINNED_BUTTON_TOOLTIP_TEXT |
| 194 | : IDS_ASH_GLOBAL_MEDIA_CONTROLS_UNPINNED_BUTTON_TOOLTIP_TEXT)); |
| 195 | } |
| 196 | |
Jazz Xu | 4675273 | 2020-08-25 23:01:05 | [diff] [blame] | 197 | MediaTray::MediaTray(Shelf* shelf) : TrayBackgroundView(shelf) { |
Jazz Xu | 0dda0e3d | 2020-09-21 22:44:09 | [diff] [blame] | 198 | if (MediaNotificationProvider::Get()) |
| 199 | MediaNotificationProvider::Get()->AddObserver(this); |
Jazz Xu | 4675273 | 2020-08-25 23:01:05 | [diff] [blame] | 200 | |
Jazz Xu | 14f07c9 | 2020-09-23 04:08:56 | [diff] [blame] | 201 | Shell::Get()->session_controller()->AddObserver(this); |
| 202 | |
Jazz Xu | 4675273 | 2020-08-25 23:01:05 | [diff] [blame] | 203 | auto icon = std::make_unique<views::ImageView>(); |
Allen Bauer | d9daac11 | 2020-09-15 21:54:04 | [diff] [blame] | 204 | icon->SetTooltipText(l10n_util::GetStringUTF16( |
Jazz Xu | 4675273 | 2020-08-25 23:01:05 | [diff] [blame] | 205 | IDS_ASH_GLOBAL_MEDIA_CONTROLS_BUTTON_TOOLTIP_TEXT)); |
| 206 | icon->SetImage(gfx::CreateVectorIcon( |
| 207 | kGlobalMediaControlsIcon, |
| 208 | TrayIconColor(Shell::Get()->session_controller()->GetSessionState()))); |
| 209 | |
| 210 | tray_container()->SetMargin(kMediaTrayPadding, 0); |
| 211 | icon_ = tray_container()->AddChildView(std::move(icon)); |
| 212 | } |
| 213 | |
| 214 | MediaTray::~MediaTray() { |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 215 | if (bubble_) |
| 216 | bubble_->GetBubbleView()->ResetDelegate(); |
| 217 | |
Jazz Xu | 4675273 | 2020-08-25 23:01:05 | [diff] [blame] | 218 | if (MediaNotificationProvider::Get()) |
| 219 | MediaNotificationProvider::Get()->RemoveObserver(this); |
Jazz Xu | 14f07c9 | 2020-09-23 04:08:56 | [diff] [blame] | 220 | |
| 221 | Shell::Get()->session_controller()->RemoveObserver(this); |
Jazz Xu | 4675273 | 2020-08-25 23:01:05 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | void MediaTray::OnNotificationListChanged() { |
| 225 | UpdateDisplayState(); |
| 226 | } |
| 227 | |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 228 | void MediaTray::OnNotificationListViewSizeChanged() { |
| 229 | if (!bubble_) |
| 230 | return; |
| 231 | |
| 232 | bubble_->GetBubbleView()->UpdateBubble(); |
| 233 | } |
Jazz Xu | 4675273 | 2020-08-25 23:01:05 | [diff] [blame] | 234 | |
Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame^] | 235 | std::u16string MediaTray::GetAccessibleNameForTray() { |
Jazz Xu | 4675273 | 2020-08-25 23:01:05 | [diff] [blame] | 236 | return l10n_util::GetStringUTF16( |
| 237 | IDS_ASH_GLOBAL_MEDIA_CONTROLS_BUTTON_TOOLTIP_TEXT); |
| 238 | } |
| 239 | |
| 240 | void MediaTray::UpdateAfterLoginStatusChange() { |
| 241 | UpdateDisplayState(); |
| 242 | PreferredSizeChanged(); |
| 243 | } |
| 244 | |
| 245 | void MediaTray::HandleLocaleChange() { |
Allen Bauer | d9daac11 | 2020-09-15 21:54:04 | [diff] [blame] | 246 | icon_->SetTooltipText(l10n_util::GetStringUTF16( |
Jazz Xu | 4675273 | 2020-08-25 23:01:05 | [diff] [blame] | 247 | IDS_ASH_GLOBAL_MEDIA_CONTROLS_BUTTON_TOOLTIP_TEXT)); |
| 248 | } |
| 249 | |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 250 | bool MediaTray::PerformAction(const ui::Event& event) { |
| 251 | if (bubble_) |
| 252 | CloseBubble(); |
| 253 | else |
Ahmed Mehfooz | 15cebdd | 2021-03-03 01:07:07 | [diff] [blame] | 254 | ShowBubble(); |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 255 | return true; |
| 256 | } |
| 257 | |
Ahmed Mehfooz | 15cebdd | 2021-03-03 01:07:07 | [diff] [blame] | 258 | void MediaTray::ShowBubble() { |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 259 | DCHECK(MediaNotificationProvider::Get()); |
Jazz Xu | ee0ed7a | 2020-11-10 22:43:05 | [diff] [blame] | 260 | SetNotificationColorTheme(); |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 261 | |
| 262 | TrayBubbleView::InitParams init_params; |
| 263 | init_params.delegate = this; |
| 264 | init_params.parent_window = GetBubbleWindowContainer(); |
| 265 | init_params.anchor_view = nullptr; |
| 266 | init_params.anchor_mode = TrayBubbleView::AnchorMode::kRect; |
Jazz Xu | bd417d8 | 2020-10-15 22:16:34 | [diff] [blame] | 267 | init_params.anchor_rect = GetAnchorBoundsInScreen(); |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 268 | init_params.insets = GetTrayBubbleInsets(); |
| 269 | init_params.shelf_alignment = shelf()->alignment(); |
| 270 | init_params.preferred_width = kTrayMenuWidth; |
| 271 | init_params.close_on_deactivate = true; |
| 272 | init_params.has_shadow = false; |
| 273 | init_params.translucent = true; |
| 274 | init_params.corner_radius = kTrayItemCornerRadius; |
Ahmed Mehfooz | 15cebdd | 2021-03-03 01:07:07 | [diff] [blame] | 275 | init_params.reroute_event_handler = true; |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 276 | |
| 277 | TrayBubbleView* bubble_view = new TrayBubbleView(init_params); |
| 278 | |
Jazz Xu | bfefcba | 2020-09-25 01:59:13 | [diff] [blame] | 279 | auto* title_view = bubble_view->AddChildView( |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 280 | std::make_unique<GlobalMediaControlsTitleView>()); |
Jazz Xu | bfefcba | 2020-09-25 01:59:13 | [diff] [blame] | 281 | title_view->SetPaintToLayer(); |
| 282 | title_view->layer()->SetFillsBoundsOpaquely(false); |
| 283 | pin_button_ = title_view->pin_button(); |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 284 | |
Jazz Xu | 0fdf9911 | 2020-10-02 18:13:13 | [diff] [blame] | 285 | content_view_ = bubble_view->AddChildView( |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 286 | MediaNotificationProvider::Get()->GetMediaNotificationListView( |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 287 | kMenuSeparatorWidth)); |
| 288 | |
| 289 | bubble_ = std::make_unique<TrayBubbleWrapper>(this, bubble_view, |
| 290 | false /*is_persistent*/); |
| 291 | SetIsActive(true); |
Jazz Xu | 5ba3329 | 2020-10-08 22:00:13 | [diff] [blame] | 292 | |
| 293 | base::UmaHistogramBoolean("Media.CrosGlobalMediaControls.RepeatUsageOnShelf", |
| 294 | bubble_has_shown_); |
| 295 | bubble_has_shown_ = true; |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | void MediaTray::CloseBubble() { |
| 299 | if (MediaNotificationProvider::Get()) |
| 300 | MediaNotificationProvider::Get()->OnBubbleClosing(); |
| 301 | SetIsActive(false); |
Jazz Xu | 0fdf9911 | 2020-10-02 18:13:13 | [diff] [blame] | 302 | empty_state_view_ = nullptr; |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 303 | bubble_.reset(); |
| 304 | shelf()->UpdateAutoHideState(); |
| 305 | } |
| 306 | |
| 307 | void MediaTray::HideBubbleWithView(const TrayBubbleView* bubble_view) { |
| 308 | if (bubble_ && bubble_->bubble_view() == bubble_view) |
| 309 | CloseBubble(); |
| 310 | } |
| 311 | |
| 312 | void MediaTray::ClickedOutsideBubble() { |
| 313 | CloseBubble(); |
| 314 | } |
| 315 | |
Jazz Xu | 14f07c9 | 2020-09-23 04:08:56 | [diff] [blame] | 316 | void MediaTray::OnLockStateChanged(bool locked) { |
| 317 | UpdateDisplayState(); |
| 318 | } |
| 319 | |
Jazz Xu | bfefcba | 2020-09-25 01:59:13 | [diff] [blame] | 320 | void MediaTray::OnActiveUserPrefServiceChanged(PrefService* pref_service) { |
| 321 | pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>(); |
| 322 | pref_change_registrar_->Init(pref_service); |
| 323 | pref_change_registrar_->Add( |
| 324 | prefs::kGlobalMediaControlsPinned, |
| 325 | base::BindRepeating(&MediaTray::OnGlobalMediaControlsPinPrefChanged, |
| 326 | base::Unretained(this))); |
| 327 | OnGlobalMediaControlsPinPrefChanged(); |
| 328 | } |
| 329 | |
Jazz Xu | 4675273 | 2020-08-25 23:01:05 | [diff] [blame] | 330 | void MediaTray::UpdateDisplayState() { |
| 331 | if (!MediaNotificationProvider::Get()) |
| 332 | return; |
| 333 | |
Jazz Xu | 0fdf9911 | 2020-10-02 18:13:13 | [diff] [blame] | 334 | if (bubble_ && Shell::Get()->session_controller()->IsScreenLocked()) |
Jazz Xu | f0d66f2e | 2020-09-11 20:56:04 | [diff] [blame] | 335 | CloseBubble(); |
Jazz Xu | aa4cbc5 | 2020-09-30 19:00:33 | [diff] [blame] | 336 | |
Jazz Xu | 0fdf9911 | 2020-10-02 18:13:13 | [diff] [blame] | 337 | bool has_session = |
| 338 | MediaNotificationProvider::Get()->HasActiveNotifications() || |
| 339 | MediaNotificationProvider::Get()->HasFrozenNotifications(); |
| 340 | |
| 341 | if (bubble_ && !has_session) |
| 342 | ShowEmptyState(); |
| 343 | |
Jazz Xu | 7cfbf5f | 2020-11-10 21:14:55 | [diff] [blame] | 344 | if (bubble_ && has_session && empty_state_view_) |
| 345 | empty_state_view_->SetVisible(false); |
| 346 | |
Jazz Xu | 0fdf9911 | 2020-10-02 18:13:13 | [diff] [blame] | 347 | bool should_show = has_session && |
| 348 | !Shell::Get()->session_controller()->IsScreenLocked() && |
| 349 | IsPinnedToShelf(); |
| 350 | |
| 351 | SetVisiblePreferred(should_show); |
Jazz Xu | 4675273 | 2020-08-25 23:01:05 | [diff] [blame] | 352 | } |
| 353 | |
Jan Wilken Dörrie | 85285b0 | 2021-03-11 23:38:47 | [diff] [blame^] | 354 | std::u16string MediaTray::GetAccessibleNameForBubble() { |
Jazz Xu | 4935f6b | 2020-11-05 19:26:02 | [diff] [blame] | 355 | return l10n_util::GetStringUTF16(IDS_ASH_GLOBAL_MEDIA_CONTROLS_TITLE); |
| 356 | } |
| 357 | |
Jazz Xu | ac3c3d0 | 2020-10-07 19:55:55 | [diff] [blame] | 358 | void MediaTray::SetNotificationColorTheme() { |
| 359 | if (!MediaNotificationProvider::Get()) |
| 360 | return; |
| 361 | |
| 362 | media_message_center::NotificationTheme theme; |
| 363 | theme.primary_text_color = AshColorProvider::Get()->GetContentLayerColor( |
| 364 | AshColorProvider::ContentLayerType::kTextColorPrimary); |
| 365 | theme.secondary_text_color = AshColorProvider::Get()->GetContentLayerColor( |
| 366 | AshColorProvider::ContentLayerType::kTextColorSecondary); |
| 367 | theme.enabled_icon_color = AshColorProvider::Get()->GetContentLayerColor( |
| 368 | AshColorProvider::ContentLayerType::kIconColorPrimary); |
| 369 | theme.disabled_icon_color = AshColorProvider::Get()->GetContentLayerColor( |
| 370 | AshColorProvider::ContentLayerType::kIconColorSecondary); |
| 371 | theme.separator_color = AshColorProvider::Get()->GetContentLayerColor( |
| 372 | AshColorProvider::ContentLayerType::kSeparatorColor); |
| 373 | MediaNotificationProvider::Get()->SetColorTheme(theme); |
| 374 | } |
| 375 | |
Jazz Xu | bfefcba | 2020-09-25 01:59:13 | [diff] [blame] | 376 | void MediaTray::OnGlobalMediaControlsPinPrefChanged() { |
| 377 | UpdateDisplayState(); |
| 378 | } |
| 379 | |
Jazz Xu | 0fdf9911 | 2020-10-02 18:13:13 | [diff] [blame] | 380 | void MediaTray::ShowEmptyState() { |
| 381 | DCHECK(content_view_); |
Jazz Xu | 7cfbf5f | 2020-11-10 21:14:55 | [diff] [blame] | 382 | if (empty_state_view_) { |
| 383 | empty_state_view_->SetVisible(true); |
Jazz Xu | 0fdf9911 | 2020-10-02 18:13:13 | [diff] [blame] | 384 | return; |
Jazz Xu | 7cfbf5f | 2020-11-10 21:14:55 | [diff] [blame] | 385 | } |
Jazz Xu | 0fdf9911 | 2020-10-02 18:13:13 | [diff] [blame] | 386 | |
| 387 | // Create and add empty state view containing a label indicating there's no |
| 388 | // active session |
| 389 | auto empty_state_view = std::make_unique<views::View>(); |
| 390 | auto* layout = |
| 391 | empty_state_view->SetLayoutManager(std::make_unique<views::BoxLayout>( |
| 392 | views::BoxLayout::Orientation::kHorizontal)); |
| 393 | layout->set_minimum_cross_axis_size(content_view_->bounds().height()); |
| 394 | layout->set_main_axis_alignment(views::BoxLayout::MainAxisAlignment::kCenter); |
| 395 | |
| 396 | auto no_media_label = std::make_unique<views::Label>(); |
| 397 | no_media_label->SetAutoColorReadabilityEnabled(false); |
| 398 | no_media_label->SetSubpixelRenderingEnabled(false); |
| 399 | no_media_label->SetEnabledColor(AshColorProvider::Get()->GetContentLayerColor( |
| 400 | AshColorProvider::ContentLayerType::kTextColorSecondary)); |
| 401 | no_media_label->SetText( |
| 402 | l10n_util::GetStringUTF16(IDS_ASH_GLOBAL_MEDIA_CONTROLS_NO_MEDIA_TEXT)); |
| 403 | no_media_label->SetFontList( |
| 404 | views::Label::GetDefaultFontList().DeriveWithSizeDelta( |
| 405 | kNoMediaTextFontSizeIncrease)); |
| 406 | empty_state_view->AddChildView(std::move(no_media_label)); |
| 407 | |
| 408 | empty_state_view->SetPaintToLayer(); |
| 409 | empty_state_view->layer()->SetFillsBoundsOpaquely(false); |
| 410 | empty_state_view_ = |
| 411 | bubble_->GetBubbleView()->AddChildView(std::move(empty_state_view)); |
| 412 | } |
| 413 | |
Jazz Xu | bd417d8 | 2020-10-15 22:16:34 | [diff] [blame] | 414 | void MediaTray::AnchorUpdated() { |
| 415 | if (!bubble_) |
| 416 | return; |
| 417 | |
| 418 | bubble_->GetBubbleView()->SetAnchorRect( |
| 419 | shelf()->GetStatusAreaWidget()->GetMediaTrayAnchorRect()); |
| 420 | } |
| 421 | |
Jazz Xu | 4675273 | 2020-08-25 23:01:05 | [diff] [blame] | 422 | } // namespace ash |