File: progress_bar.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; 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 (361 lines) | stat: -rw-r--r-- 11,454 bytes parent folder | download | duplicates (8)
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/views/controls/progress_bar.h"

#include <algorithm>
#include <cmath>
#include <memory>
#include <string>

#include "base/check_op.h"
#include "base/i18n/number_formatting.h"
#include "cc/paint/paint_flags.h"
#include "third_party/skia/include/core/SkRRect.h"
#include "third_party/skia/include/effects/SkGradientShader.h"
#include "ui/accessibility/ax_enums.mojom.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/gfx/animation/linear_animation.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/color_utils.h"
#include "ui/gfx/geometry/skia_conversions.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/widget/widget.h"

namespace views {

namespace {

// Adds a rectangle to the path.
void AddPossiblyRoundRectToPath(
    const gfx::Rect& rectangle,
    const gfx::RoundedCornersF& preferred_corner_radii,
    SkPath* path) {
  if (preferred_corner_radii.IsEmpty() || rectangle.height() == 0) {
    path->addRect(gfx::RectToSkRect(rectangle));
    return;
  }
  SkVector radii[4] = {{preferred_corner_radii.upper_left(),
                        preferred_corner_radii.upper_left()},
                       {preferred_corner_radii.upper_right(),
                        preferred_corner_radii.upper_right()},
                       {preferred_corner_radii.lower_right(),
                        preferred_corner_radii.lower_right()},
                       {preferred_corner_radii.lower_left(),
                        preferred_corner_radii.lower_left()}};

  SkRRect rr;
  rr.setRectRadii(gfx::RectToSkRect(rectangle), radii);
  path->addRRect(rr);
}

int RoundToPercent(double fractional_value) {
  return static_cast<int>(fractional_value * 100);
}

}  // namespace

ProgressBar::ProgressBar() {
  SetFlipCanvasOnPaintForRTLUI(true);
  GetViewAccessibility().SetRole(ax::mojom::Role::kProgressIndicator);
}

ProgressBar::~ProgressBar() = default;

gfx::Size ProgressBar::CalculatePreferredSize(
    const SizeBounds& /*available_size*/) const {
  // The width will typically be ignored.
  gfx::Size pref_size(1, preferred_height_);
  gfx::Insets insets = GetInsets();
  pref_size.Enlarge(insets.width(), insets.height());
  return pref_size;
}

void ProgressBar::VisibilityChanged(View* starting_from, bool is_visible) {
  MaybeNotifyAccessibilityValueChanged();
}

void ProgressBar::AddedToWidget() {
  MaybeNotifyAccessibilityValueChanged();
}

void ProgressBar::OnPaint(gfx::Canvas* canvas) {
  if (IsIndeterminate()) {
    return OnPaintIndeterminate(canvas);
  }

  gfx::Rect content_bounds = GetContentsBounds();

  // Draw background.
  SkPath background_path;
  gfx::RoundedCornersF rounded_corners = GetPreferredCornerRadii();
  AddPossiblyRoundRectToPath(content_bounds, rounded_corners, &background_path);
  cc::PaintFlags background_flags;
  background_flags.setStyle(cc::PaintFlags::kFill_Style);
  background_flags.setAntiAlias(true);
  background_flags.setColor(GetBackgroundColor());
  canvas->DrawPath(background_path, background_flags);

  // Draw slice.
  SkPath slice_path;
  const int slice_width = static_cast<int>(
      content_bounds.width() * std::min(current_value_, 1.0) + 0.5);
  if (slice_width < 1) {
    return;
  }

  gfx::Rect slice_bounds = content_bounds;
  slice_bounds.set_width(slice_width);
  AddPossiblyRoundRectToPath(slice_bounds, rounded_corners, &slice_path);

  cc::PaintFlags slice_flags;
  slice_flags.setStyle(cc::PaintFlags::kFill_Style);
  slice_flags.setAntiAlias(true);
  slice_flags.setColor(GetForegroundColor());
  canvas->DrawPath(slice_path, slice_flags);
}

double ProgressBar::GetValue() const {
  return current_value_;
}

void ProgressBar::SetValue(double value) {
  double adjusted_value = (value < 0.0 || value > 1.0) ? -1.0 : value;

  if (adjusted_value == current_value_) {
    return;
  }

  current_value_ = adjusted_value;
  if (IsIndeterminate()) {
    indeterminate_bar_animation_ = std::make_unique<gfx::LinearAnimation>(this);
    indeterminate_bar_animation_->SetDuration(base::Seconds(2));
    indeterminate_bar_animation_->Start();
  } else {
    indeterminate_bar_animation_.reset();
    OnPropertyChanged(&current_value_, kPropertyEffectsPaint);
  }

  MaybeNotifyAccessibilityValueChanged();
}

void ProgressBar::SetPaused(bool is_paused) {
  if (is_paused_ == is_paused) {
    return;
  }

  is_paused_ = is_paused;
  OnPropertyChanged(&is_paused_, kPropertyEffectsPaint);
}

SkColor ProgressBar::GetForegroundColor() const {
  if (foreground_color_) {
    return foreground_color_.value();
  }

  return GetColorProvider()->GetColor(foreground_color_id_.value_or(
      GetPaused() ? ui::kColorProgressBarPaused : ui::kColorProgressBar));
}

void ProgressBar::SetForegroundColor(SkColor color) {
  if (foreground_color_ == color) {
    return;
  }

  foreground_color_ = color;
  foreground_color_id_ = std::nullopt;
  OnPropertyChanged(&foreground_color_, kPropertyEffectsPaint);
}

std::optional<ui::ColorId> ProgressBar::GetForegroundColorId() const {
  return foreground_color_id_;
}

void ProgressBar::SetForegroundColorId(std::optional<ui::ColorId> color_id) {
  if (foreground_color_id_ == color_id) {
    return;
  }

  foreground_color_id_ = color_id;
  foreground_color_ = std::nullopt;
  OnPropertyChanged(&foreground_color_id_, kPropertyEffectsPaint);
}

SkColor ProgressBar::GetBackgroundColor() const {
  if (background_color_id_) {
    return GetColorProvider()->GetColor(background_color_id_.value());
  }

  return background_color_.value_or(
      GetColorProvider()->GetColor(ui::kColorProgressBarBackground));
}

void ProgressBar::SetBackgroundColor(SkColor color) {
  if (background_color_ == color) {
    return;
  }

  background_color_ = color;
  background_color_id_ = std::nullopt;
  OnPropertyChanged(&background_color_, kPropertyEffectsPaint);
}

std::optional<ui::ColorId> ProgressBar::GetBackgroundColorId() const {
  return background_color_id_;
}

void ProgressBar::SetBackgroundColorId(std::optional<ui::ColorId> color_id) {
  if (background_color_id_ == color_id) {
    return;
  }

  background_color_id_ = color_id;
  background_color_ = std::nullopt;
  OnPropertyChanged(&background_color_id_, kPropertyEffectsPaint);
}

int ProgressBar::GetPreferredHeight() const {
  return preferred_height_;
}

void ProgressBar::SetPreferredHeight(int preferred_height) {
  if (preferred_height_ == preferred_height) {
    return;
  }
  preferred_height_ = preferred_height;
  OnPropertyChanged(&preferred_height_, kPropertyEffectsPreferredSizeChanged);
}

gfx::RoundedCornersF ProgressBar::GetPreferredCornerRadii() const {
  if (!preferred_corner_radii_) {
    return gfx::RoundedCornersF(0);
  }
  const float max_radius = GetContentsBounds().height();

  // No corner should have a radius greater than the height of the bar.
  return gfx::RoundedCornersF(
      std::min(max_radius, preferred_corner_radii_->upper_left()),
      std::min(max_radius, preferred_corner_radii_->upper_right()),
      std::min(max_radius, preferred_corner_radii_->lower_right()),
      std::min(max_radius, preferred_corner_radii_->lower_left()));
}

void ProgressBar::SetPreferredCornerRadii(
    std::optional<gfx::RoundedCornersF> preferred_corner_radii) {
  if (preferred_corner_radii_ == preferred_corner_radii) {
    return;
  }
  preferred_corner_radii_ = preferred_corner_radii;
  OnPropertyChanged(&preferred_corner_radii_, kPropertyEffectsPaint);
}

void ProgressBar::AnimationProgressed(const gfx::Animation* animation) {
  DCHECK_EQ(animation, indeterminate_bar_animation_.get());
  DCHECK(IsIndeterminate());
  SchedulePaint();
}

void ProgressBar::AnimationEnded(const gfx::Animation* animation) {
  DCHECK_EQ(animation, indeterminate_bar_animation_.get());
  // Restarts animation.
  if (IsIndeterminate()) {
    indeterminate_bar_animation_->Start();
  }
}

bool ProgressBar::IsIndeterminate() {
  return current_value_ < 0.0;
}

void ProgressBar::OnPaintIndeterminate(gfx::Canvas* canvas) {
  gfx::Rect content_bounds = GetContentsBounds();

  // Draw background.
  SkPath background_path;
  gfx::RoundedCornersF rounded_corners = GetPreferredCornerRadii();
  AddPossiblyRoundRectToPath(content_bounds, rounded_corners, &background_path);
  cc::PaintFlags background_flags;
  background_flags.setStyle(cc::PaintFlags::kFill_Style);
  background_flags.setAntiAlias(true);
  background_flags.setColor(GetBackgroundColor());
  canvas->DrawPath(background_path, background_flags);

  // Draw slice.
  SkPath slice_path;
  double time = indeterminate_bar_animation_->GetCurrentValue();

  // The animation spec corresponds to the material design lite's parameter.
  // (cf. https://github.com/google/material-design-lite/)
  double bar1_left;
  double bar1_width;
  double bar2_left;
  double bar2_width;
  if (time < 0.50) {
    bar1_left = time / 2;
    bar1_width = time * 1.5;
    bar2_left = 0;
    bar2_width = 0;
  } else if (time < 0.75) {
    bar1_left = time * 3 - 1.25;
    bar1_width = 0.75 - (time - 0.5) * 3;
    bar2_left = 0;
    bar2_width = time - 0.5;
  } else {
    bar1_left = 1;
    bar1_width = 0;
    bar2_left = (time - 0.75) * 4;
    bar2_width = 0.25 - (time - 0.75);
  }

  int bar1_start_x = std::round(content_bounds.width() * bar1_left);
  int bar1_end_x = std::round(content_bounds.width() *
                              std::min(1.0, bar1_left + bar1_width));
  int bar2_start_x = std::round(content_bounds.width() * bar2_left);
  int bar2_end_x = std::round(content_bounds.width() *
                              std::min(1.0, bar2_left + bar2_width));

  gfx::Rect slice_bounds = content_bounds;
  slice_bounds.set_x(content_bounds.x() + bar1_start_x);
  slice_bounds.set_width(bar1_end_x - bar1_start_x);
  AddPossiblyRoundRectToPath(slice_bounds, rounded_corners, &slice_path);
  slice_bounds.set_x(content_bounds.x() + bar2_start_x);
  slice_bounds.set_width(bar2_end_x - bar2_start_x);
  AddPossiblyRoundRectToPath(slice_bounds, rounded_corners, &slice_path);

  cc::PaintFlags slice_flags;
  slice_flags.setStyle(cc::PaintFlags::kFill_Style);
  slice_flags.setAntiAlias(true);
  slice_flags.setColor(GetForegroundColor());
  canvas->DrawPath(slice_path, slice_flags);
}

void ProgressBar::MaybeNotifyAccessibilityValueChanged() {
  // Exit early if ProgressBar is Indeterminate or not visible.
  if (IsIndeterminate()) {
    GetViewAccessibility().RemoveValue();
    return;
  }
  if (!GetWidget() || !GetWidget()->IsVisible() ||
      RoundToPercent(current_value_) == last_announced_percentage_) {
    return;
  }
  last_announced_percentage_ = RoundToPercent(current_value_);
  GetViewAccessibility().SetValue(
      base::FormatPercent(last_announced_percentage_));
}

BEGIN_METADATA(ProgressBar)
ADD_PROPERTY_METADATA(int, PreferredHeight)
ADD_PROPERTY_METADATA(std::optional<gfx::RoundedCornersF>, PreferredCornerRadii)
ADD_PROPERTY_METADATA(SkColor, ForegroundColor, ui::metadata::SkColorConverter)
ADD_PROPERTY_METADATA(SkColor, BackgroundColor, ui::metadata::SkColorConverter)
ADD_PROPERTY_METADATA(std::optional<ui::ColorId>, ForegroundColorId);
ADD_PROPERTY_METADATA(std::optional<ui::ColorId>, BackgroundColorId);
ADD_PROPERTY_METADATA(bool, Paused)
END_METADATA

}  // namespace views