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 #import "chrome/browser/ui/cocoa/spinner_progress_indicator.h" |
| 6 |
| 7 #include "base/timer.h" |
| 8 #include "chrome/browser/download/download_util.h" |
| 9 #include "grit/theme_resources.h" |
| 10 #include "ui/base/resource/resource_bundle.h" |
| 11 #include "ui/gfx/canvas_skia_paint.h" |
| 12 #include "ui/gfx/rect.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 // Interval between spinner updates in miliseconds. |
| 17 const int kTimerIntervalMs = 1000 / 30; |
| 18 |
| 19 // Degrees to rotate the indeterminte spinner per second. |
| 20 const CGFloat kSpinRateDegreesPerSecond = 270; |
| 21 |
| 22 // Callback for indeterminte spinner animation timer. |
| 23 void OnTimer(SpinnerProgressIndicator* indicator) { |
| 24 [indicator setNeedsDisplay:YES]; |
| 25 } |
| 26 |
| 27 } // namespace |
| 28 |
| 29 @interface SpinnerProgressIndicator () |
| 30 - (int)progressAngle; |
| 31 - (void)updateTimer; |
| 32 @end |
| 33 |
| 34 @implementation SpinnerProgressIndicator |
| 35 |
| 36 @synthesize percentDone = percentDone_; |
| 37 @synthesize isIndeterminate = isIndeterminate_; |
| 38 |
| 39 - (void)setPercentDone:(int)percent { |
| 40 percentDone_ = percent; |
| 41 [self setNeedsDisplay:YES]; |
| 42 [self updateTimer]; |
| 43 } |
| 44 |
| 45 - (void)setIsIndeterminate:(BOOL)isIndeterminate { |
| 46 isIndeterminate_ = isIndeterminate; |
| 47 [self setNeedsDisplay:YES]; |
| 48 [self updateTimer]; |
| 49 } |
| 50 |
| 51 - (void)sizeToFit { |
| 52 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 53 gfx::ImageSkia* foreground = rb.GetImageSkiaNamed( |
| 54 IDR_WEB_INTENT_PROGRESS_FOREGROUND); |
| 55 NSRect frame = [self frame]; |
| 56 frame.size.width = foreground->width(); |
| 57 frame.size.height = foreground->height(); |
| 58 [self setFrame:frame]; |
| 59 } |
| 60 |
| 61 - (void)drawRect:(NSRect)rect { |
| 62 NSRect bounds = [self bounds]; |
| 63 gfx::CanvasSkiaPaint canvas(bounds, false); |
| 64 canvas.set_composite_alpha(true); |
| 65 |
| 66 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
| 67 gfx::ImageSkia* background = rb.GetImageSkiaNamed( |
| 68 IDR_WEB_INTENT_PROGRESS_BACKGROUND); |
| 69 gfx::ImageSkia* foreground = rb.GetImageSkiaNamed( |
| 70 IDR_WEB_INTENT_PROGRESS_FOREGROUND); |
| 71 |
| 72 download_util::PaintCustomDownloadProgress( |
| 73 &canvas, |
| 74 *background, |
| 75 *foreground, |
| 76 foreground->width(), |
| 77 gfx::Rect(NSRectToCGRect(bounds)), |
| 78 [self progressAngle], |
| 79 isIndeterminate_ ? -1 : percentDone_); |
| 80 } |
| 81 |
| 82 - (void)viewDidMoveToWindow { |
| 83 [self updateTimer]; |
| 84 } |
| 85 |
| 86 - (int)progressAngle { |
| 87 if (!isIndeterminate_) |
| 88 return download_util::kStartAngleDegrees; |
| 89 base::TimeDelta delta = base::TimeTicks::Now() - startTime_; |
| 90 int angle = delta.InSecondsF() * kSpinRateDegreesPerSecond; |
| 91 return angle % 360; |
| 92 } |
| 93 |
| 94 - (void)updateTimer { |
| 95 // Only run the timer if the control is in a window and showing an |
| 96 // indeterminte progress. |
| 97 if (![self window] || !isIndeterminate_) { |
| 98 timer_.reset(); |
| 99 return; |
| 100 } |
| 101 |
| 102 if (!timer_.get()) { |
| 103 startTime_ = base::TimeTicks::Now(); |
| 104 timer_.reset(new base::Timer( |
| 105 FROM_HERE, |
| 106 base::TimeDelta::FromMilliseconds(kTimerIntervalMs), |
| 107 base::Bind(OnTimer, self), |
| 108 true)); |
| 109 timer_->Reset(); |
| 110 } |
| 111 } |
| 112 |
| 113 @end |
OLD | NEW |