File: media_controls_progress_view.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (252 lines) | stat: -rw-r--r-- 8,722 bytes parent folder | download | duplicates (4)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/media_message_center/media_controls_progress_view.h"

#include <string_view>

#include "base/i18n/time_formatting.h"
#include "services/media_session/public/mojom/media_session.mojom.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/font_list.h"
#include "ui/views/border.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/progress_bar.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/flex_layout.h"
#include "ui/views/layout/flex_layout_types.h"
#include "ui/views/view_class_properties.h"

namespace media_message_center {

namespace {

constexpr int kProgressBarAndTimeSpacing = 8;
constexpr int kProgressTimeFontSize = 11;
constexpr int kProgressBarHeight = 4;
constexpr int kMinClickHeight = 14;
constexpr int kMaxClickHeight = 24;
constexpr gfx::Size kTimeSpacingSize = gfx::Size(150, 10);
constexpr auto kProgressViewInsets = gfx::Insets::TLBR(15, 0, 0, 0);

constexpr int kModernProgressBarHeight = 2;
constexpr auto kModernProgressViewInsets = gfx::Insets::TLBR(8, 0, 8, 0);

}  // namespace

MediaControlsProgressView::MediaControlsProgressView(
    base::RepeatingCallback<void(double)> seek_callback,
    bool is_modern_notification)
    : is_modern_notification_(is_modern_notification),
      seek_callback_(std::move(seek_callback)) {
  SetLayoutManager(std::make_unique<views::BoxLayout>(
      views::BoxLayout::Orientation::kVertical,
      is_modern_notification_ ? kModernProgressViewInsets : kProgressViewInsets,
      kProgressBarAndTimeSpacing));

  progress_bar_ = AddChildView(std::make_unique<views::ProgressBar>());
  progress_bar_->SetPreferredHeight(
      is_modern_notification_ ? kModernProgressBarHeight : kProgressBarHeight);
  progress_bar_->SetPreferredCornerRadii(std::nullopt);

  // Font list for text views.
  gfx::Font default_font;
  int font_size_delta = kProgressTimeFontSize - default_font.GetFontSize();
  gfx::Font font = default_font.Derive(font_size_delta, gfx::Font::NORMAL,
                                       gfx::Font::Weight::NORMAL);
  gfx::FontList font_list(font);

  auto time_view = std::make_unique<views::View>();
  auto* time_view_layout =
      time_view->SetLayoutManager(std::make_unique<views::FlexLayout>());
  time_view_layout->SetOrientation(views::LayoutOrientation::kHorizontal)
      .SetMainAxisAlignment(views::LayoutAlignment::kCenter)
      .SetCrossAxisAlignment(views::LayoutAlignment::kCenter)
      .SetCollapseMargins(true);

  auto progress_time = std::make_unique<views::Label>();
  progress_time->SetFontList(font_list);
  progress_time->SetAutoColorReadabilityEnabled(false);
  progress_time_ = time_view->AddChildView(std::move(progress_time));

  auto time_spacing = std::make_unique<views::View>();
  time_spacing->SetPreferredSize(kTimeSpacingSize);
  time_spacing->SetProperty(
      views::kFlexBehaviorKey,
      views::FlexSpecification(views::MinimumFlexSizeRule::kPreferred,
                               views::MaximumFlexSizeRule::kUnbounded));
  time_view->AddChildView(std::move(time_spacing));

  auto duration = std::make_unique<views::Label>();
  duration->SetFontList(font_list);
  duration->SetAutoColorReadabilityEnabled(false);
  duration_ = time_view->AddChildView(std::move(duration));

  if (is_modern_notification_)
    time_view->SetVisible(false);
  AddChildView(std::move(time_view));
}

MediaControlsProgressView::~MediaControlsProgressView() = default;

void MediaControlsProgressView::UpdateProgress(
    const media_session::MediaPosition& media_position) {
  is_live_ = media_position.duration().is_max();
  if (is_live_ == duration_->GetVisible()) {
    duration_->SetVisible(!is_live_);
    InvalidateLayout();
  }

  // If the media is paused and |update_progress_timer_| is still running, stop
  // the timer.
  if (media_position.playback_rate() == 0 && update_progress_timer_.IsRunning())
    update_progress_timer_.Stop();

  const base::TimeDelta current_position = media_position.GetPosition();
  const base::TimeDelta duration = media_position.duration();
  // Use 1.0 for live playback, correctly, or as a fallback for those cases in
  // which the result is unfriendly.
  SetBarProgress((is_live_ || duration.is_zero() || current_position.is_inf())
                     ? 1.0
                     : current_position / duration);

  // For durations greater than 24 hours, prefer base::DURATION_WIDTH_NARROW for
  // better readability (e.g., 27h 23m 10s rather than 27:23:10).
  base::DurationFormatWidth time_format = duration >= base::Days(1)
                                              ? base::DURATION_WIDTH_NARROW
                                              : base::DURATION_WIDTH_NUMERIC;

  std::u16string elapsed_time;
  bool elapsed_time_received = base::TimeDurationFormatWithSeconds(
      current_position, time_format, &elapsed_time);

  std::u16string total_time;
  bool total_time_received =
      base::TimeDurationFormatWithSeconds(duration, time_format, &total_time);

  if (elapsed_time_received && total_time_received) {
    // If |duration| is less than an hour, we don't want to show  "0:" hours on
    // the progress times.
    if (duration < base::Hours(1)) {
      base::ReplaceFirstSubstringAfterOffset(&elapsed_time, 0, u"0:", u"");
      base::ReplaceFirstSubstringAfterOffset(&total_time, 0, u"0:", u"");
    }

    SetProgressTime(elapsed_time);
    SetDuration(is_live_ ? std::u16string() : total_time);
  }

  if (media_position.playback_rate() != 0) {
    base::TimeDelta update_frequency =
        base::Seconds(std::abs(1 / media_position.playback_rate()));
    update_progress_timer_.Start(
        FROM_HERE, update_frequency,
        base::BindRepeating(&MediaControlsProgressView::UpdateProgress,
                            base::Unretained(this), media_position));
  }
}

void MediaControlsProgressView::SetForegroundColor(SkColor color) {
  progress_bar_->SetForegroundColor(color);
}

void MediaControlsProgressView::SetForegroundColorId(ui::ColorId color_id) {
  progress_bar_->SetForegroundColorId(color_id);
}

void MediaControlsProgressView::SetBackgroundColor(SkColor color) {
  progress_bar_->SetBackgroundColor(color);
}

void MediaControlsProgressView::SetBackgroundColorId(ui::ColorId color_id) {
  progress_bar_->SetBackgroundColorId(color_id);
}

void MediaControlsProgressView::SetTextColor(SkColor color) {
  progress_time_->SetEnabledColor(color);
  duration_->SetEnabledColor(color);
}

void MediaControlsProgressView::SetTextColorId(ui::ColorId color_id) {
  progress_time_->SetEnabledColor(color_id);
  duration_->SetEnabledColor(color_id);
}

bool MediaControlsProgressView::OnMousePressed(const ui::MouseEvent& event) {
  if (is_live_)
    return false;

  if (!event.IsOnlyLeftMouseButton())
    return false;

  if (!is_modern_notification_ &&
      (event.y() < kMinClickHeight || event.y() > kMaxClickHeight)) {
    return false;
  }

  HandleSeeking(event.location());
  return true;
}

void MediaControlsProgressView::OnGestureEvent(ui::GestureEvent* event) {
  if (is_live_)
    return;

  if (event->type() != ui::EventType::kGestureTap) {
    return;
  }

  if (!is_modern_notification_ &&
      (event->y() < kMinClickHeight || event->y() > kMaxClickHeight)) {
    return;
  }

  HandleSeeking(event->location());
  event->SetHandled();
}

const views::ProgressBar* MediaControlsProgressView::progress_bar_for_testing()
    const {
  return progress_bar_;
}

std::u16string_view MediaControlsProgressView::progress_time_for_testing()
    const {
  return progress_time_->GetText();
}

std::u16string_view MediaControlsProgressView::duration_for_testing() const {
  return duration_->GetText();
}

bool MediaControlsProgressView::is_duration_visible_for_testing() const {
  return duration_->GetVisible();
}

void MediaControlsProgressView::SetBarProgress(double progress) {
  progress_bar_->SetValue(progress);
}

void MediaControlsProgressView::SetProgressTime(std::u16string_view time) {
  progress_time_->SetText(time);
}

void MediaControlsProgressView::SetDuration(std::u16string_view duration) {
  duration_->SetText(duration);
}

void MediaControlsProgressView::HandleSeeking(const gfx::Point& location) {
  gfx::Point location_in_bar(location);
  ConvertPointToTarget(this, progress_bar_, &location_in_bar);

  double seek_to_progress =
      static_cast<double>(location_in_bar.x()) / progress_bar_->width();
  seek_callback_.Run(seek_to_progress);
}

BEGIN_METADATA(MediaControlsProgressView)
END_METADATA

}  // namespace media_message_center