| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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. | |
| 4 | |
| 5 #include "views/controls/throbber.h" | |
| 6 | |
| 7 #include "base/time.h" | |
| 8 #include "grit/ui_resources.h" | |
| 9 #include "third_party/skia/include/core/SkBitmap.h" | |
| 10 #include "ui/base/resource/resource_bundle.h" | |
| 11 #include "ui/gfx/canvas.h" | |
| 12 | |
| 13 using base::Time; | |
| 14 using base::TimeDelta; | |
| 15 | |
| 16 namespace views { | |
| 17 | |
| 18 Throbber::Throbber(int frame_time_ms, | |
| 19 bool paint_while_stopped) | |
| 20 : running_(false), | |
| 21 paint_while_stopped_(paint_while_stopped), | |
| 22 frames_(NULL), | |
| 23 frame_time_(TimeDelta::FromMilliseconds(frame_time_ms)) { | |
| 24 SetFrames(ResourceBundle::GetSharedInstance().GetBitmapNamed(IDR_THROBBER)); | |
| 25 } | |
| 26 | |
| 27 Throbber::~Throbber() { | |
| 28 Stop(); | |
| 29 } | |
| 30 | |
| 31 void Throbber::Start() { | |
| 32 if (running_) | |
| 33 return; | |
| 34 | |
| 35 start_time_ = Time::Now(); | |
| 36 | |
| 37 timer_.Start(FROM_HERE, frame_time_ - TimeDelta::FromMilliseconds(10), | |
| 38 this, &Throbber::Run); | |
| 39 | |
| 40 running_ = true; | |
| 41 | |
| 42 SchedulePaint(); // paint right away | |
| 43 } | |
| 44 | |
| 45 void Throbber::Stop() { | |
| 46 if (!running_) | |
| 47 return; | |
| 48 | |
| 49 timer_.Stop(); | |
| 50 | |
| 51 running_ = false; | |
| 52 SchedulePaint(); // Important if we're not painting while stopped | |
| 53 } | |
| 54 | |
| 55 void Throbber::SetFrames(SkBitmap* frames) { | |
| 56 frames_ = frames; | |
| 57 DCHECK(frames_->width() > 0 && frames_->height() > 0); | |
| 58 DCHECK(frames_->width() % frames_->height() == 0); | |
| 59 frame_count_ = frames_->width() / frames_->height(); | |
| 60 PreferredSizeChanged(); | |
| 61 } | |
| 62 | |
| 63 void Throbber::Run() { | |
| 64 DCHECK(running_); | |
| 65 | |
| 66 SchedulePaint(); | |
| 67 } | |
| 68 | |
| 69 gfx::Size Throbber::GetPreferredSize() { | |
| 70 return gfx::Size(frames_->height(), frames_->height()); | |
| 71 } | |
| 72 | |
| 73 void Throbber::OnPaint(gfx::Canvas* canvas) { | |
| 74 if (!running_ && !paint_while_stopped_) | |
| 75 return; | |
| 76 | |
| 77 const TimeDelta elapsed_time = Time::Now() - start_time_; | |
| 78 const int current_frame = | |
| 79 static_cast<int>(elapsed_time / frame_time_) % frame_count_; | |
| 80 | |
| 81 int image_size = frames_->height(); | |
| 82 int image_offset = current_frame * image_size; | |
| 83 canvas->DrawBitmapInt(*frames_, | |
| 84 image_offset, 0, image_size, image_size, | |
| 85 0, 0, image_size, image_size, | |
| 86 false); | |
| 87 } | |
| 88 | |
| 89 | |
| 90 | |
| 91 // Smoothed throbber --------------------------------------------------------- | |
| 92 | |
| 93 | |
| 94 // Delay after work starts before starting throbber, in milliseconds. | |
| 95 static const int kStartDelay = 200; | |
| 96 | |
| 97 // Delay after work stops before stopping, in milliseconds. | |
| 98 static const int kStopDelay = 50; | |
| 99 | |
| 100 | |
| 101 SmoothedThrobber::SmoothedThrobber(int frame_time_ms) | |
| 102 : Throbber(frame_time_ms, /* paint_while_stopped= */ false), | |
| 103 start_delay_ms_(kStartDelay), | |
| 104 stop_delay_ms_(kStopDelay) { | |
| 105 } | |
| 106 | |
| 107 SmoothedThrobber::~SmoothedThrobber() {} | |
| 108 | |
| 109 void SmoothedThrobber::Start() { | |
| 110 stop_timer_.Stop(); | |
| 111 | |
| 112 if (!running_ && !start_timer_.IsRunning()) { | |
| 113 start_timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(start_delay_ms_), | |
| 114 this, &SmoothedThrobber::StartDelayOver); | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 void SmoothedThrobber::StartDelayOver() { | |
| 119 Throbber::Start(); | |
| 120 } | |
| 121 | |
| 122 void SmoothedThrobber::Stop() { | |
| 123 if (!running_) | |
| 124 start_timer_.Stop(); | |
| 125 | |
| 126 stop_timer_.Stop(); | |
| 127 stop_timer_.Start(FROM_HERE, TimeDelta::FromMilliseconds(stop_delay_ms_), | |
| 128 this, &SmoothedThrobber::StopDelayOver); | |
| 129 } | |
| 130 | |
| 131 void SmoothedThrobber::StopDelayOver() { | |
| 132 Throbber::Stop(); | |
| 133 } | |
| 134 | |
| 135 // Checkmark throbber --------------------------------------------------------- | |
| 136 | |
| 137 CheckmarkThrobber::CheckmarkThrobber() | |
| 138 : Throbber(kFrameTimeMs, false), | |
| 139 checked_(false) { | |
| 140 InitClass(); | |
| 141 } | |
| 142 | |
| 143 void CheckmarkThrobber::SetChecked(bool checked) { | |
| 144 bool changed = checked != checked_; | |
| 145 if (changed) { | |
| 146 checked_ = checked; | |
| 147 SchedulePaint(); | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 void CheckmarkThrobber::OnPaint(gfx::Canvas* canvas) { | |
| 152 if (running_) { | |
| 153 // Let the throbber throb... | |
| 154 Throbber::OnPaint(canvas); | |
| 155 return; | |
| 156 } | |
| 157 // Otherwise we paint our tick mark or nothing depending on our state. | |
| 158 if (checked_) { | |
| 159 int checkmark_x = (width() - checkmark_->width()) / 2; | |
| 160 int checkmark_y = (height() - checkmark_->height()) / 2; | |
| 161 canvas->DrawBitmapInt(*checkmark_, checkmark_x, checkmark_y); | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 // static | |
| 166 void CheckmarkThrobber::InitClass() { | |
| 167 static bool initialized = false; | |
| 168 if (!initialized) { | |
| 169 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
| 170 checkmark_ = rb.GetBitmapNamed(IDR_CHECKMARK); | |
| 171 initialized = true; | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 // static | |
| 176 SkBitmap* CheckmarkThrobber::checkmark_ = NULL; | |
| 177 | |
| 178 } // namespace views | |
| OLD | NEW |