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