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
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/ambient/ui/media_string_view.h"
#include <memory>
#include <string>
#include <utility>
#include "ash/ambient/ambient_constants.h"
#include "ash/ambient/ui/ambient_view_ids.h"
#include "ash/ambient/util/ambient_util.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/resources/vector_icons/vector_icons.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/shell_delegate.h"
#include "ash/style/dark_light_mode_controller_impl.h"
#include "base/functional/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/time.h"
#include "components/prefs/pref_service.h"
#include "services/media_session/public/cpp/media_session_service.h"
#include "services/media_session/public/mojom/media_session.mojom.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/paint_recorder.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/gfx/font.h"
#include "ui/gfx/geometry/transform.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/gfx/shadow_value.h"
#include "ui/gfx/skia_paint_util.h"
#include "ui/gfx/text_constants.h"
#include "ui/views/border.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/flex_layout.h"
#include "ui/views/layout/flex_layout_types.h"
namespace ash {
namespace {
// Typography.
constexpr char16_t kMiddleDotSeparator[] = u" • ";
constexpr int kMusicNoteIconSizeDip = 20;
// Returns true if we should show media string for ambient mode on lock-screen
// based on user pref. We should keep the same user policy here as the
// lock-screen media controls to avoid exposing user data on lock-screen without
// consent.
bool ShouldShowOnLockScreen() {
PrefService* pref =
Shell::Get()->session_controller()->GetPrimaryUserPrefService();
DCHECK(pref);
return pref->GetBoolean(prefs::kLockScreenMediaControlsEnabled);
}
} // namespace
MediaStringView::MediaStringView(MediaStringView::Delegate* delegate)
: delegate_(delegate) {
SetID(AmbientViewID::kAmbientMediaStringView);
InitLayout();
}
MediaStringView::~MediaStringView() = default;
void MediaStringView::OnThemeChanged() {
views::View::OnThemeChanged();
media_text_->SetShadows(ambient::util::GetTextShadowValues(
GetColorProvider(), delegate_->GetSettings().text_shadow_elevation));
const bool dark_mode_enabled =
DarkLightModeControllerImpl::Get()->IsDarkModeEnabled();
DCHECK(icon_);
icon_->SetImage(ui::ImageModel::FromVectorIcon(
kMusicNoteIcon,
dark_mode_enabled ? delegate_->GetSettings().icon_dark_mode_color
: delegate_->GetSettings().icon_light_mode_color,
kMusicNoteIconSizeDip));
DCHECK(media_text_);
media_text_->SetEnabledColor(
dark_mode_enabled ? delegate_->GetSettings().text_dark_mode_color
: delegate_->GetSettings().text_light_mode_color);
gfx::Insets shadow_insets =
gfx::ShadowValue::GetMargin(ambient::util::GetTextShadowValues(
nullptr, delegate_->GetSettings().text_shadow_elevation));
// Compensate the shadow insets to put the text middle align with the icon.
media_text_->SetBorder(views::CreateEmptyBorder(
gfx::Insets::TLBR(-shadow_insets.bottom(), 0, -shadow_insets.top(), 0)));
}
void MediaStringView::OnViewBoundsChanged(views::View* observed_view) {
UpdateMaskLayer();
}
void MediaStringView::MediaSessionInfoChanged(
media_session::mojom::MediaSessionInfoPtr session_info) {
if (ambient::util::IsShowing(LockScreen::ScreenType::kLock) &&
!ShouldShowOnLockScreen()) {
return;
}
// Don't show the media string if session info is unavailable.
if (!session_info) {
SetVisible(false);
return;
}
bool is_paused = session_info->playback_state ==
media_session::mojom::MediaPlaybackState::kPaused;
// Don't show the media string if paused.
SetVisible(!is_paused);
}
void MediaStringView::MediaSessionMetadataChanged(
const std::optional<media_session::MediaMetadata>& metadata) {
media_session::MediaMetadata session_metadata =
metadata.value_or(media_session::MediaMetadata());
std::u16string media_string;
std::u16string middle_dot = kMiddleDotSeparator;
if (!session_metadata.title.empty() && !session_metadata.artist.empty()) {
media_string =
session_metadata.title + middle_dot + session_metadata.artist;
} else if (!session_metadata.title.empty()) {
media_string = session_metadata.title;
} else {
media_string = session_metadata.artist;
}
// Reset text and stop any ongoing animation.
media_text_->SetText(std::u16string());
media_text_->layer()->GetAnimator()->StopAnimating();
media_text_->SetText(media_string);
media_text_->layer()->SetTransform(gfx::Transform());
const auto& text_size = media_text_->GetPreferredSize(
views::SizeBounds(media_text_->width(), {}));
const int text_width = text_size.width();
media_text_container_->SetPreferredSize(gfx::Size(
std::min(kMediaStringMaxWidthDip, text_width), text_size.height()));
if (NeedToAnimate()) {
media_text_->SetText(media_string + middle_dot + media_string + middle_dot);
ScheduleScrolling(/*is_initial=*/true);
}
}
void MediaStringView::OnImplicitAnimationsCompleted() {
if (!NeedToAnimate())
return;
ScheduleScrolling(/*is_initial=*/false);
}
void MediaStringView::InitLayout() {
// This view will be drawn on its own layer instead of the layer of
// |PhotoView| which has a solid black background.
SetPaintToLayer();
layer()->SetFillsBoundsOpaquely(false);
constexpr int kChildSpacingDip = 8;
auto* layout = SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kHorizontal));
layout->set_main_axis_alignment(views::BoxLayout::MainAxisAlignment::kStart);
layout->set_cross_axis_alignment(
views::BoxLayout::CrossAxisAlignment::kCenter);
layout->set_between_child_spacing(kChildSpacingDip);
icon_ = AddChildView(std::make_unique<views::ImageView>());
icon_->SetPreferredSize(
gfx::Size(kMusicNoteIconSizeDip, kMusicNoteIconSizeDip));
media_text_container_ = AddChildView(std::make_unique<views::View>());
media_text_container_->SetPaintToLayer();
media_text_container_->layer()->SetFillsBoundsOpaquely(false);
media_text_container_->layer()->SetMasksToBounds(true);
auto* text_layout = media_text_container_->SetLayoutManager(
std::make_unique<views::FlexLayout>());
text_layout->SetOrientation(views::LayoutOrientation::kHorizontal);
text_layout->SetMainAxisAlignment(views::LayoutAlignment::kStart);
text_layout->SetCrossAxisAlignment(views::LayoutAlignment::kCenter);
observed_view_.Observe(media_text_container_.get());
media_text_ =
media_text_container_->AddChildView(std::make_unique<views::Label>());
media_text_->SetPaintToLayer();
media_text_->layer()->SetFillsBoundsOpaquely(false);
// Defines the appearance.
constexpr int kDefaultFontSizeDip = 64;
constexpr int kMediaStringFontSizeDip = 18;
media_text_->SetHorizontalAlignment(gfx::HorizontalAlignment::ALIGN_TO_HEAD);
media_text_->SetVerticalAlignment(gfx::VerticalAlignment::ALIGN_MIDDLE);
media_text_->SetAutoColorReadabilityEnabled(false);
media_text_->SetFontList(
ambient::util::GetDefaultFontlist()
.DeriveWithSizeDelta(kMediaStringFontSizeDip - kDefaultFontSizeDip)
.DeriveWithWeight(gfx::Font::Weight::MEDIUM));
media_text_->SetElideBehavior(gfx::ElideBehavior::NO_ELIDE);
BindMediaControllerObserver();
}
void MediaStringView::BindMediaControllerObserver() {
media_session::MediaSessionService* service =
Shell::Get()->shell_delegate()->GetMediaSessionService();
// Service might be unavailable under some test environments.
if (!service)
return;
// Binds to the MediaControllerManager and create a MediaController for the
// current active media session so that we can observe it.
mojo::Remote<media_session::mojom::MediaControllerManager>
controller_manager_remote;
service->BindMediaControllerManager(
controller_manager_remote.BindNewPipeAndPassReceiver());
controller_manager_remote->CreateActiveMediaController(
media_controller_remote_.BindNewPipeAndPassReceiver());
// Observe the active media controller for changes.
media_controller_remote_->AddObserver(
observer_receiver_.BindNewPipeAndPassRemote());
}
void MediaStringView::UpdateMaskLayer() {
if (!NeedToAnimate()) {
media_text_container_->layer()->SetGradientMask(
gfx::LinearGradient::GetEmpty());
return;
}
// Invalid container width.
if (media_text_container_->layer()->size().width() == 0) {
media_text_container_->layer()->SetGradientMask(
gfx::LinearGradient::GetEmpty());
return;
}
if (!media_text_container_->layer()->HasGradientMask()) {
float fade_position = static_cast<float>(kMediaStringGradientWidthDip) /
media_text_container_->layer()->size().width();
gfx::LinearGradient gradient_mask(/*angle=*/0);
gradient_mask.AddStep(/*fraction=*/0, /*alpha=*/0);
gradient_mask.AddStep(fade_position, 255);
gradient_mask.AddStep(1 - fade_position, 255);
gradient_mask.AddStep(1, 0);
media_text_container_->layer()->SetGradientMask(gradient_mask);
}
}
bool MediaStringView::NeedToAnimate() const {
return media_text_
->GetPreferredSize(views::SizeBounds(media_text_->width(), {}))
.width() > media_text_container_->GetPreferredSize().width();
}
gfx::Transform MediaStringView::GetMediaTextTransform(bool is_initial) {
gfx::Transform transform;
if (is_initial) {
// Start animation half way of |media_text_container_|.
transform.Translate(kMediaStringMaxWidthDip / 2, 0);
}
return transform;
}
void MediaStringView::ScheduleScrolling(bool is_initial) {
if (!GetVisible())
return;
base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, base::BindOnce(&MediaStringView::StartScrolling,
weak_factory_.GetWeakPtr(), is_initial));
}
void MediaStringView::StartScrolling(bool is_initial) {
ui::Layer* text_layer = media_text_->layer();
text_layer->SetTransform(GetMediaTextTransform(is_initial));
{
// Desired speed is 10 seconds for kMediaStringMaxWidthDip.
const int text_width =
media_text_
->GetPreferredSize(views::SizeBounds(media_text_->width(), {}))
.width();
const int shadow_width =
gfx::ShadowValue::GetMargin(
ambient::util::GetTextShadowValues(
nullptr, delegate_->GetSettings().text_shadow_elevation))
.width();
const int start_x = text_layer->GetTargetTransform().To2dTranslation().x();
const int end_x = -(text_width + shadow_width) / 2;
const int transform_distance = start_x - end_x;
const base::TimeDelta kScrollingDuration =
base::Seconds(10) * transform_distance / kMediaStringMaxWidthDip;
ui::ScopedLayerAnimationSettings animation(text_layer->GetAnimator());
animation.SetTransitionDuration(kScrollingDuration);
animation.SetTweenType(gfx::Tween::LINEAR);
animation.SetPreemptionStrategy(
ui::LayerAnimator::IMMEDIATELY_SET_NEW_TARGET);
animation.AddObserver(this);
gfx::Transform transform;
transform.Translate(end_x, 0);
text_layer->SetTransform(transform);
}
}
BEGIN_METADATA(MediaStringView)
END_METADATA
} // namespace ash
|