Commit 023c8cd8 authored by estade's avatar estade Committed by Commit bot

Clean up Throbber.

- TimeTicks in place of Time
- remove unnecessary member var running_
- etc.

BUG=none

Review URL: https://codereview.chromium.org/1103903003

Cr-Commit-Position: refs/heads/master@{#327073}
parent 2d07d57a
...@@ -4,24 +4,18 @@ ...@@ -4,24 +4,18 @@
#include "ui/views/controls/throbber.h" #include "ui/views/controls/throbber.h"
#include "base/time/time.h"
#include "ui/base/resource/resource_bundle.h" #include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h" #include "ui/gfx/canvas.h"
#include "ui/gfx/image/image.h" #include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h" #include "ui/gfx/image/image_skia.h"
#include "ui/resources/grit/ui_resources.h" #include "ui/resources/grit/ui_resources.h"
using base::Time;
using base::TimeDelta;
namespace views { namespace views {
Throbber::Throbber(int frame_time_ms, Throbber::Throbber(int frame_time_ms, bool paint_while_stopped)
bool paint_while_stopped) : paint_while_stopped_(paint_while_stopped),
: running_(false),
paint_while_stopped_(paint_while_stopped),
frames_(NULL), frames_(NULL),
frame_time_(TimeDelta::FromMilliseconds(frame_time_ms)) { frame_time_(base::TimeDelta::FromMilliseconds(frame_time_ms)) {
SetFrames(ui::ResourceBundle::GetSharedInstance().GetImageNamed( SetFrames(ui::ResourceBundle::GetSharedInstance().GetImageNamed(
IDR_THROBBER).ToImageSkia()); IDR_THROBBER).ToImageSkia());
} }
...@@ -31,26 +25,20 @@ Throbber::~Throbber() { ...@@ -31,26 +25,20 @@ Throbber::~Throbber() {
} }
void Throbber::Start() { void Throbber::Start() {
if (running_) if (IsRunning())
return; return;
start_time_ = Time::Now(); start_time_ = base::TimeTicks::Now();
timer_.Start(FROM_HERE, frame_time_ - base::TimeDelta::FromMilliseconds(10),
timer_.Start(FROM_HERE, frame_time_ - TimeDelta::FromMilliseconds(10), this, &Throbber::SchedulePaint);
this, &Throbber::Run);
running_ = true;
SchedulePaint(); // paint right away SchedulePaint(); // paint right away
} }
void Throbber::Stop() { void Throbber::Stop() {
if (!running_) if (!IsRunning())
return; return;
timer_.Stop(); timer_.Stop();
running_ = false;
SchedulePaint(); // Important if we're not painting while stopped SchedulePaint(); // Important if we're not painting while stopped
} }
...@@ -62,21 +50,15 @@ void Throbber::SetFrames(const gfx::ImageSkia* frames) { ...@@ -62,21 +50,15 @@ void Throbber::SetFrames(const gfx::ImageSkia* frames) {
PreferredSizeChanged(); PreferredSizeChanged();
} }
void Throbber::Run() {
DCHECK(running_);
SchedulePaint();
}
gfx::Size Throbber::GetPreferredSize() const { gfx::Size Throbber::GetPreferredSize() const {
return gfx::Size(frames_->height(), frames_->height()); return gfx::Size(frames_->height(), frames_->height());
} }
void Throbber::OnPaint(gfx::Canvas* canvas) { void Throbber::OnPaint(gfx::Canvas* canvas) {
if (!running_ && !paint_while_stopped_) if (!IsRunning() && !paint_while_stopped_)
return; return;
const TimeDelta elapsed_time = Time::Now() - start_time_; const base::TimeDelta elapsed_time = base::TimeTicks::Now() - start_time_;
const int current_frame = const int current_frame =
static_cast<int>(elapsed_time / frame_time_) % frame_count_; static_cast<int>(elapsed_time / frame_time_) % frame_count_;
...@@ -88,18 +70,18 @@ void Throbber::OnPaint(gfx::Canvas* canvas) { ...@@ -88,18 +70,18 @@ void Throbber::OnPaint(gfx::Canvas* canvas) {
false); false);
} }
bool Throbber::IsRunning() const {
return timer_.IsRunning();
}
// Smoothed throbber --------------------------------------------------------- // Smoothed throbber ---------------------------------------------------------
// Delay after work starts before starting throbber, in milliseconds. // Delay after work starts before starting throbber, in milliseconds.
static const int kStartDelay = 200; static const int kStartDelay = 200;
// Delay after work stops before stopping, in milliseconds. // Delay after work stops before stopping, in milliseconds.
static const int kStopDelay = 50; static const int kStopDelay = 50;
SmoothedThrobber::SmoothedThrobber(int frame_time_ms) SmoothedThrobber::SmoothedThrobber(int frame_time_ms)
: Throbber(frame_time_ms, /* paint_while_stopped= */ false), : Throbber(frame_time_ms, /* paint_while_stopped= */ false),
start_delay_ms_(kStartDelay), start_delay_ms_(kStartDelay),
...@@ -111,9 +93,10 @@ SmoothedThrobber::~SmoothedThrobber() {} ...@@ -111,9 +93,10 @@ SmoothedThrobber::~SmoothedThrobber() {}
void SmoothedThrobber::Start() { void SmoothedThrobber::Start() {
stop_timer_.Stop(); stop_timer_.Stop();
if (!running() && !start_timer_.IsRunning()) { if (!IsRunning() && !start_timer_.IsRunning()) {
start_timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(start_delay_ms_), start_timer_.Start(FROM_HERE,
this, &SmoothedThrobber::StartDelayOver); base::TimeDelta::FromMilliseconds(start_delay_ms_), this,
&SmoothedThrobber::StartDelayOver);
} }
} }
...@@ -122,12 +105,13 @@ void SmoothedThrobber::StartDelayOver() { ...@@ -122,12 +105,13 @@ void SmoothedThrobber::StartDelayOver() {
} }
void SmoothedThrobber::Stop() { void SmoothedThrobber::Stop() {
if (!running()) if (!IsRunning())
start_timer_.Stop(); start_timer_.Stop();
stop_timer_.Stop(); stop_timer_.Stop();
stop_timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(stop_delay_ms_), stop_timer_.Start(FROM_HERE,
this, &SmoothedThrobber::StopDelayOver); base::TimeDelta::FromMilliseconds(stop_delay_ms_), this,
&SmoothedThrobber::StopDelayOver);
} }
void SmoothedThrobber::StopDelayOver() { void SmoothedThrobber::StopDelayOver() {
...@@ -170,7 +154,7 @@ int MaterialThrobber::GetHeightForWidth(int w) const { ...@@ -170,7 +154,7 @@ int MaterialThrobber::GetHeightForWidth(int w) const {
} }
void MaterialThrobber::OnPaint(gfx::Canvas* canvas) { void MaterialThrobber::OnPaint(gfx::Canvas* canvas) {
if (!running()) { if (!IsRunning()) {
if (checked_) { if (checked_) {
if (!checkmark_) { if (!checkmark_) {
checkmark_ = ui::ResourceBundle::GetSharedInstance().GetImageNamed( checkmark_ = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
...@@ -197,7 +181,7 @@ void MaterialThrobber::OnPaint(gfx::Canvas* canvas) { ...@@ -197,7 +181,7 @@ void MaterialThrobber::OnPaint(gfx::Canvas* canvas) {
// (90 degrees) and rotates steadily. The start angle trails 180 degrees // (90 degrees) and rotates steadily. The start angle trails 180 degrees
// behind, except for the first half revolution, when it stays at 12 o'clock. // behind, except for the first half revolution, when it stays at 12 o'clock.
base::TimeDelta revolution_time = base::TimeDelta::FromMilliseconds(1320); base::TimeDelta revolution_time = base::TimeDelta::FromMilliseconds(1320);
base::TimeDelta elapsed_time = base::Time::Now() - start_time(); base::TimeDelta elapsed_time = base::TimeTicks::Now() - start_time();
int64_t twelve_oclock = 90; int64_t twelve_oclock = 90;
int64_t finish_angle = twelve_oclock + 360 * elapsed_time / revolution_time; int64_t finish_angle = twelve_oclock + 360 * elapsed_time / revolution_time;
int64_t start_angle = std::max(finish_angle - 180, twelve_oclock); int64_t start_angle = std::max(finish_angle - 180, twelve_oclock);
......
...@@ -41,18 +41,14 @@ class VIEWS_EXPORT Throbber : public View { ...@@ -41,18 +41,14 @@ class VIEWS_EXPORT Throbber : public View {
void OnPaint(gfx::Canvas* canvas) override; void OnPaint(gfx::Canvas* canvas) override;
protected: protected:
bool running() const { return running_; }
base::Time start_time() const { return start_time_; }
// Specifies whether the throbber is currently animating or not // Specifies whether the throbber is currently animating or not
bool IsRunning() const;
base::TimeTicks start_time() const { return start_time_; }
private: private:
void Run();
bool running_;
bool paint_while_stopped_; bool paint_while_stopped_;
int frame_count_; // How many frames we have. int frame_count_; // How many frames we have.
base::Time start_time_; // Time when Start was called. base::TimeTicks start_time_; // Time when Start was called.
const gfx::ImageSkia* frames_; // Frames images. const gfx::ImageSkia* frames_; // Frames images.
base::TimeDelta frame_time_; // How long one frame is displayed. base::TimeDelta frame_time_; // How long one frame is displayed.
base::RepeatingTimer<Throbber> timer_; // Used to schedule Run calls. base::RepeatingTimer<Throbber> timer_; // Used to schedule Run calls.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment