| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/views/throbber.h" | 5 #include "chrome/views/throbber.h" |
| 6 | 6 |
| 7 #include "base/time.h" |
| 7 #include "chrome/app/theme/theme_resources.h" | 8 #include "chrome/app/theme/theme_resources.h" |
| 8 #include "chrome/common/gfx/chrome_canvas.h" | 9 #include "chrome/common/gfx/chrome_canvas.h" |
| 9 #include "chrome/common/logging_chrome.h" | 10 #include "chrome/common/logging_chrome.h" |
| 10 #include "chrome/common/resource_bundle.h" | 11 #include "chrome/common/resource_bundle.h" |
| 11 #include "skia/include/SkBitmap.h" | 12 #include "skia/include/SkBitmap.h" |
| 12 | 13 |
| 14 using base::Time; |
| 13 using base::TimeDelta; | 15 using base::TimeDelta; |
| 14 | 16 |
| 15 namespace views { | 17 namespace views { |
| 16 | 18 |
| 17 Throbber::Throbber(int frame_time_ms, | 19 Throbber::Throbber(int frame_time_ms, |
| 18 bool paint_while_stopped) | 20 bool paint_while_stopped) |
| 19 : paint_while_stopped_(paint_while_stopped), | 21 : running_(false), |
| 20 running_(false), | 22 paint_while_stopped_(paint_while_stopped), |
| 21 last_frame_drawn_(-1), | |
| 22 frame_time_ms_(frame_time_ms), | |
| 23 frames_(NULL), | 23 frames_(NULL), |
| 24 last_time_recorded_(0) { | 24 frame_time_(TimeDelta::FromMilliseconds(frame_time_ms)) { |
| 25 ResourceBundle &rb = ResourceBundle::GetSharedInstance(); | 25 ResourceBundle &rb = ResourceBundle::GetSharedInstance(); |
| 26 frames_ = rb.GetBitmapNamed(IDR_THROBBER); | 26 frames_ = rb.GetBitmapNamed(IDR_THROBBER); |
| 27 DCHECK(frames_->width() > 0 && frames_->height() > 0); | 27 DCHECK(frames_->width() > 0 && frames_->height() > 0); |
| 28 DCHECK(frames_->width() % frames_->height() == 0); | 28 DCHECK(frames_->width() % frames_->height() == 0); |
| 29 frame_count_ = frames_->width() / frames_->height(); | 29 frame_count_ = frames_->width() / frames_->height(); |
| 30 } | 30 } |
| 31 | 31 |
| 32 Throbber::~Throbber() { | 32 Throbber::~Throbber() { |
| 33 Stop(); | 33 Stop(); |
| 34 } | 34 } |
| 35 | 35 |
| 36 void Throbber::Start() { | 36 void Throbber::Start() { |
| 37 if (running_) | 37 if (running_) |
| 38 return; | 38 return; |
| 39 | 39 |
| 40 start_time_ = GetTickCount(); | 40 start_time_ = Time::Now(); |
| 41 last_time_recorded_ = start_time_; | |
| 42 | 41 |
| 43 timer_.Start( | 42 timer_.Start(frame_time_ - TimeDelta::FromMilliseconds(10), |
| 44 TimeDelta::FromMilliseconds(frame_time_ms_ - 10), this, &Throbber::Run); | 43 this, &Throbber::Run); |
| 45 | 44 |
| 46 running_ = true; | 45 running_ = true; |
| 47 | 46 |
| 48 SchedulePaint(); // paint right away | 47 SchedulePaint(); // paint right away |
| 49 } | 48 } |
| 50 | 49 |
| 51 void Throbber::Stop() { | 50 void Throbber::Stop() { |
| 52 if (!running_) | 51 if (!running_) |
| 53 return; | 52 return; |
| 54 | 53 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 65 } | 64 } |
| 66 | 65 |
| 67 gfx::Size Throbber::GetPreferredSize() { | 66 gfx::Size Throbber::GetPreferredSize() { |
| 68 return gfx::Size(frames_->height(), frames_->height()); | 67 return gfx::Size(frames_->height(), frames_->height()); |
| 69 } | 68 } |
| 70 | 69 |
| 71 void Throbber::Paint(ChromeCanvas* canvas) { | 70 void Throbber::Paint(ChromeCanvas* canvas) { |
| 72 if (!running_ && !paint_while_stopped_) | 71 if (!running_ && !paint_while_stopped_) |
| 73 return; | 72 return; |
| 74 | 73 |
| 75 DWORD current_time = GetTickCount(); | 74 const TimeDelta elapsed_time = Time::Now() - start_time_; |
| 76 int current_frame = 0; | 75 const int current_frame = |
| 77 | 76 static_cast<int>(elapsed_time / frame_time_) % frame_count_; |
| 78 // deal with timer wraparound | |
| 79 if (current_time < last_time_recorded_) { | |
| 80 start_time_ = current_time; | |
| 81 current_frame = (last_frame_drawn_ + 1) % frame_count_; | |
| 82 } else { | |
| 83 current_frame = | |
| 84 ((current_time - start_time_) / frame_time_ms_) % frame_count_; | |
| 85 } | |
| 86 | |
| 87 last_time_recorded_ = current_time; | |
| 88 last_frame_drawn_ = current_frame; | |
| 89 | 77 |
| 90 int image_size = frames_->height(); | 78 int image_size = frames_->height(); |
| 91 int image_offset = current_frame * image_size; | 79 int image_offset = current_frame * image_size; |
| 92 canvas->DrawBitmapInt(*frames_, | 80 canvas->DrawBitmapInt(*frames_, |
| 93 image_offset, 0, image_size, image_size, | 81 image_offset, 0, image_size, image_size, |
| 94 0, 0, image_size, image_size, | 82 0, 0, image_size, image_size, |
| 95 false); | 83 false); |
| 96 } | 84 } |
| 97 | 85 |
| 98 | 86 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 &SmoothedThrobber::StopDelayOver); | 121 &SmoothedThrobber::StopDelayOver); |
| 134 } | 122 } |
| 135 | 123 |
| 136 void SmoothedThrobber::StopDelayOver() { | 124 void SmoothedThrobber::StopDelayOver() { |
| 137 Throbber::Stop(); | 125 Throbber::Stop(); |
| 138 } | 126 } |
| 139 | 127 |
| 140 // Checkmark throbber --------------------------------------------------------- | 128 // Checkmark throbber --------------------------------------------------------- |
| 141 | 129 |
| 142 CheckmarkThrobber::CheckmarkThrobber() | 130 CheckmarkThrobber::CheckmarkThrobber() |
| 143 : checked_(false), | 131 : Throbber(kFrameTimeMs, false), |
| 144 Throbber(kFrameTimeMs, false) { | 132 checked_(false) { |
| 145 InitClass(); | 133 InitClass(); |
| 146 } | 134 } |
| 147 | 135 |
| 148 void CheckmarkThrobber::SetChecked(bool checked) { | 136 void CheckmarkThrobber::SetChecked(bool checked) { |
| 149 bool changed = checked != checked_; | 137 bool changed = checked != checked_; |
| 150 if (changed) { | 138 if (changed) { |
| 151 checked_ = checked; | 139 checked_ = checked; |
| 152 SchedulePaint(); | 140 SchedulePaint(); |
| 153 } | 141 } |
| 154 } | 142 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 175 checkmark_ = rb.GetBitmapNamed(IDR_INPUT_GOOD); | 163 checkmark_ = rb.GetBitmapNamed(IDR_INPUT_GOOD); |
| 176 initialized = true; | 164 initialized = true; |
| 177 } | 165 } |
| 178 } | 166 } |
| 179 | 167 |
| 180 // static | 168 // static |
| 181 SkBitmap* CheckmarkThrobber::checkmark_ = NULL; | 169 SkBitmap* CheckmarkThrobber::checkmark_ = NULL; |
| 182 | 170 |
| 183 } // namespace views | 171 } // namespace views |
| 184 | 172 |
| OLD | NEW |