Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / views / controls / throbber.h
blob73eedb9f15e85e7c3392cd3ee3a1bceb19a49687
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef UI_VIEWS_CONTROLS_THROBBER_H_
6 #define UI_VIEWS_CONTROLS_THROBBER_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/time/time.h"
11 #include "base/timer/timer.h"
12 #include "ui/views/view.h"
14 namespace gfx {
15 class ImageSkia;
18 namespace views {
20 // Throbbers display an animation, usually used as a status indicator.
22 class VIEWS_EXPORT Throbber : public View {
23 public:
24 // |frame_time_ms| is the amount of time that should elapse between frames
25 // (in milliseconds)
26 // If |paint_while_stopped| is false, this view will be invisible when not
27 // running.
28 Throbber(int frame_time_ms, bool paint_while_stopped);
29 Throbber(int frame_time_ms, bool paint_while_stopped, gfx::ImageSkia* frames);
30 ~Throbber() override;
32 // Start and stop the throbber animation
33 virtual void Start();
34 virtual void Stop();
36 // Set custom throbber frames. Otherwise IDR_THROBBER is loaded.
37 void SetFrames(const gfx::ImageSkia* frames);
39 // Overridden from View:
40 gfx::Size GetPreferredSize() const override;
41 void OnPaint(gfx::Canvas* canvas) override;
43 protected:
44 bool running() const { return running_; }
45 base::Time start_time() const { return start_time_; }
47 // Specifies whether the throbber is currently animating or not
49 private:
50 void Run();
52 bool running_;
53 bool paint_while_stopped_;
54 int frame_count_; // How many frames we have.
55 base::Time start_time_; // Time when Start was called.
56 const gfx::ImageSkia* frames_; // Frames images.
57 base::TimeDelta frame_time_; // How long one frame is displayed.
58 base::RepeatingTimer<Throbber> timer_; // Used to schedule Run calls.
60 DISALLOW_COPY_AND_ASSIGN(Throbber);
63 // A SmoothedThrobber is a throbber that is representing potentially short
64 // and nonoverlapping bursts of work. SmoothedThrobber ignores small
65 // pauses in the work stops and starts, and only starts its throbber after
66 // a small amount of work time has passed.
67 class VIEWS_EXPORT SmoothedThrobber : public Throbber {
68 public:
69 explicit SmoothedThrobber(int frame_delay_ms);
70 SmoothedThrobber(int frame_delay_ms, gfx::ImageSkia* frames);
71 ~SmoothedThrobber() override;
73 void Start() override;
74 void Stop() override;
76 void set_start_delay_ms(int value) { start_delay_ms_ = value; }
77 void set_stop_delay_ms(int value) { stop_delay_ms_ = value; }
79 private:
80 // Called when the startup-delay timer fires
81 // This function starts the actual throbbing.
82 void StartDelayOver();
84 // Called when the shutdown-delay timer fires.
85 // This function stops the actual throbbing.
86 void StopDelayOver();
88 // Delay after work starts before starting throbber, in milliseconds.
89 int start_delay_ms_;
91 // Delay after work stops before stopping, in milliseconds.
92 int stop_delay_ms_;
94 base::OneShotTimer<SmoothedThrobber> start_timer_;
95 base::OneShotTimer<SmoothedThrobber> stop_timer_;
97 DISALLOW_COPY_AND_ASSIGN(SmoothedThrobber);
100 // A throbber that follows material design guidelines. This is a specialization
101 // of Throbber for now, but should probably be folded into/replace Throbber
102 // eventually.
103 class VIEWS_EXPORT MaterialThrobber : public Throbber {
104 public:
105 MaterialThrobber();
106 ~MaterialThrobber() override;
108 // Stop spinning and, if checked is true, display a checkmark.
109 void SetChecked(bool checked);
111 void set_preferred_diameter(int diameter) { preferred_diameter_ = diameter; }
113 // View implementation.
114 gfx::Size GetPreferredSize() const override;
115 int GetHeightForWidth(int w) const override;
116 void OnPaint(gfx::Canvas* canvas) override;
118 private:
119 // The preferred width and height for this view. Zero indicates the size is
120 // set by the parent class (i.e. matches the size of the pre-material
121 // sprites).
122 int preferred_diameter_;
124 // Whether or not we should display a checkmark.
125 bool checked_;
127 // The checkmark image. Will be null until it's used (if ever).
128 const gfx::ImageSkia* checkmark_;
130 DISALLOW_COPY_AND_ASSIGN(MaterialThrobber);
133 } // namespace views
135 #endif // UI_VIEWS_CONTROLS_THROBBER_H_