File: progress_bar.h

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 (130 lines) | stat: -rw-r--r-- 4,521 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
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_VIEWS_CONTROLS_PROGRESS_BAR_H_
#define UI_VIEWS_CONTROLS_PROGRESS_BAR_H_

#include <memory>
#include <optional>

#include "ui/color/color_id.h"
#include "ui/gfx/animation/animation_delegate.h"
#include "ui/gfx/geometry/rounded_corners_f.h"
#include "ui/views/metadata/view_factory.h"
#include "ui/views/view.h"

namespace gfx {
class LinearAnimation;
}

namespace views {

// Progress bar is a control that indicates progress visually.
class VIEWS_EXPORT ProgressBar : public View, public gfx::AnimationDelegate {
  METADATA_HEADER(ProgressBar, View)

 public:
  ProgressBar();

  ProgressBar(const ProgressBar&) = delete;
  ProgressBar& operator=(const ProgressBar&) = delete;

  ~ProgressBar() override;

  gfx::Size CalculatePreferredSize(
      const SizeBounds& /*available_size*/) const override;
  void VisibilityChanged(View* starting_from, bool is_visible) override;
  void AddedToWidget() override;
  void OnPaint(gfx::Canvas* canvas) override;

  double GetValue() const;
  // Sets the current value. Values outside of the display range of 0.0-1.0 will
  // be displayed with an infinite loading animation.
  void SetValue(double value);

  // Sets whether the progress bar is paused.
  void SetPaused(bool is_paused);

  // The color of the progress portion.
  SkColor GetForegroundColor() const;
  void SetForegroundColor(SkColor color);
  std::optional<ui::ColorId> GetForegroundColorId() const;
  void SetForegroundColorId(std::optional<ui::ColorId> color_id);

  // The color of the portion that displays potential progress.
  SkColor GetBackgroundColor() const;
  void SetBackgroundColor(SkColor color);
  std::optional<ui::ColorId> GetBackgroundColorId() const;
  void SetBackgroundColorId(std::optional<ui::ColorId> color_id);

  int GetPreferredHeight() const;
  void SetPreferredHeight(int preferred_height);

  // Calculates the rounded corners of the view based on
  // `preferred_corner_radii_`. If `preferred_corner_radii_` was not provided,
  // empty corners will be returned . If any corner radius in
  // `preferred_corner_radii_` is greater than the height of the bar, its value
  // will be capped to the height of the bar.
  gfx::RoundedCornersF GetPreferredCornerRadii() const;

  void SetPreferredCornerRadii(
      std::optional<gfx::RoundedCornersF> preferred_corner_radii);

 protected:
  int preferred_height() const { return preferred_height_; }

 private:
  // gfx::AnimationDelegate:
  void AnimationProgressed(const gfx::Animation* animation) override;
  void AnimationEnded(const gfx::Animation* animation) override;

  bool IsIndeterminate();
  bool GetPaused() const { return is_paused_; }
  void OnPaintIndeterminate(gfx::Canvas* canvas);

  // Fire an accessibility event if visible and the progress has changed.
  void MaybeNotifyAccessibilityValueChanged();

  // Current progress to display, should be in the range 0.0 to 1.0.
  double current_value_ = 0.0;

  // Is the progress bar paused.
  bool is_paused_ = false;

  // In DP, the preferred height of this progress bar. This makes it easier to
  // use a ProgressBar with layout managers that size to preferred size.
  int preferred_height_ = 5;

  // The radii to round the progress bar corners with. A value of
  // `std::nullopt` will produce a bar with no rounded corners, otherwise a
  // default value of 3 on all corners will be used.
  std::optional<gfx::RoundedCornersF> preferred_corner_radii_ =
      gfx::RoundedCornersF(3);

  std::optional<SkColor> foreground_color_;
  std::optional<ui::ColorId> foreground_color_id_;
  std::optional<SkColor> background_color_;
  std::optional<ui::ColorId> background_color_id_;

  std::unique_ptr<gfx::LinearAnimation> indeterminate_bar_animation_;

  int last_announced_percentage_ = -1;
};

BEGIN_VIEW_BUILDER(VIEWS_EXPORT, ProgressBar, View)
VIEW_BUILDER_PROPERTY(double, Value)
VIEW_BUILDER_PROPERTY(bool, Paused)
VIEW_BUILDER_PROPERTY(SkColor, ForegroundColor)
VIEW_BUILDER_PROPERTY(std::optional<ui::ColorId>, ForegroundColorId)
VIEW_BUILDER_PROPERTY(SkColor, BackgroundColor)
VIEW_BUILDER_PROPERTY(std::optional<ui::ColorId>, BackgroundColorId)
VIEW_BUILDER_PROPERTY(int, PreferredHeight)
VIEW_BUILDER_PROPERTY(std::optional<gfx::RoundedCornersF>, PreferredCornerRadii)
END_VIEW_BUILDER

}  // namespace views

DEFINE_VIEW_BUILDER(VIEWS_EXPORT, ProgressBar)

#endif  // UI_VIEWS_CONTROLS_PROGRESS_BAR_H_