File: throbber.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 (122 lines) | stat: -rw-r--r-- 3,552 bytes parent folder | download | duplicates (3)
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
// 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.

#ifndef UI_VIEWS_CONTROLS_THROBBER_H_
#define UI_VIEWS_CONTROLS_THROBBER_H_

#include "base/time/time.h"
#include "base/timer/timer.h"
#include "ui/color/color_provider.h"
#include "ui/views/view.h"

namespace views {

// Throbbers display an animation, usually used as a status indicator.

class VIEWS_EXPORT Throbber : public View {
  METADATA_HEADER(Throbber, View)

 public:
  explicit Throbber(int diameter = kDefaultDiameter);

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

  ~Throbber() override;

  // Start and stop the throbber animation.
  virtual void Start();
  virtual void Stop();

  // Gets/Sets checked. For SetChecked, stop spinning and, if
  // checked is true, display a checkmark.
  bool GetChecked() const;
  void SetChecked(bool checked);

  // Overridden from View:
  gfx::Size CalculatePreferredSize(
      const SizeBounds& /*available_size*/) const override;
  void OnPaint(gfx::Canvas* canvas) override;

  int GetDiameter() const { return diameter_; }
  void SetColorId(ui::ColorId color) { color_id_ = color; }

 protected:
  // Specifies whether the throbber is currently animating or not
  bool IsRunning() const;

  // The default diameter of a Throbber.
  static constexpr int kDefaultDiameter = 16;

 private:
  base::TimeTicks start_time_;  // Time when Start was called.
  base::RepeatingTimer timer_;  // Used to schedule Run calls.

  // Whether or not we should display a checkmark.
  bool checked_ = false;

  const int diameter_;

  // Overrides the default color, ui::kColorThrobber, if set.
  std::optional<ui::ColorId> color_id_;
};

BEGIN_VIEW_BUILDER(VIEWS_EXPORT, Throbber, View)
VIEW_BUILDER_PROPERTY(bool, Checked)
END_VIEW_BUILDER

// A SmoothedThrobber is a throbber that is representing potentially short
// and nonoverlapping bursts of work.  SmoothedThrobber ignores small
// pauses in the work stops and starts, and only starts its throbber after
// a small amount of work time has passed.
class VIEWS_EXPORT SmoothedThrobber : public Throbber {
  METADATA_HEADER(SmoothedThrobber, Throbber)

 public:
  explicit SmoothedThrobber(int diameter = kDefaultDiameter);

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

  ~SmoothedThrobber() override;

  void Start() override;
  void Stop() override;

  base::TimeDelta GetStartDelay() const;
  void SetStartDelay(const base::TimeDelta& start_delay);

  base::TimeDelta GetStopDelay() const;
  void SetStopDelay(const base::TimeDelta& stop_delay);

 private:
  // Called when the startup-delay timer fires
  // This function starts the actual throbbing.
  void StartDelayOver();

  // Called when the shutdown-delay timer fires.
  // This function stops the actual throbbing.
  void StopDelayOver();

  // Delay after work starts before starting throbber.
  base::TimeDelta start_delay_;

  // Delay after work stops before stopping.
  base::TimeDelta stop_delay_;

  base::OneShotTimer start_timer_;
  base::OneShotTimer stop_timer_;
};

BEGIN_VIEW_BUILDER(VIEWS_EXPORT, SmoothedThrobber, Throbber)
VIEW_BUILDER_PROPERTY(const base::TimeDelta&, StartDelay)
VIEW_BUILDER_PROPERTY(const base::TimeDelta&, StopDelay)
END_VIEW_BUILDER

}  // namespace views

DEFINE_VIEW_BUILDER(VIEWS_EXPORT, Throbber)
DEFINE_VIEW_BUILDER(VIEWS_EXPORT, SmoothedThrobber)

#endif  // UI_VIEWS_CONTROLS_THROBBER_H_