| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #import "chrome/browser/cocoa/throbber_view.h" | 5 #import "chrome/browser/cocoa/throbber_view.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 | 8 |
| 9 const float kAnimationIntervalSeconds = 0.03; // 30ms, same as windows | 9 const float kAnimationIntervalSeconds = 0.03; // 30ms, same as windows |
| 10 | 10 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 if ((self = [super initWithFrame:frame])) { | 41 if ((self = [super initWithFrame:frame])) { |
| 42 // Ensure that the height divides evenly into the width. Cache the | 42 // Ensure that the height divides evenly into the width. Cache the |
| 43 // number of frames in the animation for later. | 43 // number of frames in the animation for later. |
| 44 NSSize imageSize = [image size]; | 44 NSSize imageSize = [image size]; |
| 45 DCHECK(imageSize.height && imageSize.width); | 45 DCHECK(imageSize.height && imageSize.width); |
| 46 if (!imageSize.height) | 46 if (!imageSize.height) |
| 47 return nil; | 47 return nil; |
| 48 DCHECK((int)imageSize.width % (int)imageSize.height == 0); | 48 DCHECK((int)imageSize.width % (int)imageSize.height == 0); |
| 49 numFrames_ = (int)imageSize.width / (int)imageSize.height; | 49 numFrames_ = (int)imageSize.width / (int)imageSize.height; |
| 50 DCHECK(numFrames_); | 50 DCHECK(numFrames_); |
| 51 image_.reset([image retain]); | 51 |
| 52 // First check if we have a bitmap image rep and use it, otherwise fall |
| 53 // back to creating one. |
| 54 NSBitmapImageRep* rep = [[image representations] objectAtIndex:0]; |
| 55 if (![rep isKindOfClass:[NSBitmapImageRep class]]) { |
| 56 [image lockFocus]; |
| 57 NSRect imageRect = NSMakeRect(0, 0, imageSize.width, imageSize.height); |
| 58 rep = [[[NSBitmapImageRep alloc] initWithFocusedViewRect:imageRect] |
| 59 autorelease]; |
| 60 [image unlockFocus]; |
| 61 } |
| 62 image_.reset([[CIImage alloc] initWithBitmapImageRep:rep]); |
| 52 | 63 |
| 53 // Start a timer for the animation frames. | 64 // Start a timer for the animation frames. |
| 54 target_.reset([[TimerTarget alloc] initWithThrobber:self]); | 65 target_.reset([[TimerTarget alloc] initWithThrobber:self]); |
| 55 timer_ = | 66 timer_ = |
| 56 [NSTimer scheduledTimerWithTimeInterval:kAnimationIntervalSeconds | 67 [NSTimer scheduledTimerWithTimeInterval:kAnimationIntervalSeconds |
| 57 target:target_.get() | 68 target:target_.get() |
| 58 selector:@selector(animate:) | 69 selector:@selector(animate:) |
| 59 userInfo:nil | 70 userInfo:nil |
| 60 repeats:YES]; | 71 repeats:YES]; |
| 61 } | 72 } |
| 62 return self; | 73 return self; |
| 63 } | 74 } |
| 64 | 75 |
| 65 - (void)dealloc { | 76 - (void)dealloc { |
| 66 [timer_ invalidate]; | 77 [timer_ invalidate]; |
| 67 [super dealloc]; | 78 [super dealloc]; |
| 68 } | 79 } |
| 69 | 80 |
| 70 - (void)removeFromSuperview { | 81 - (void)removeFromSuperview { |
| 71 [timer_ invalidate]; | 82 [timer_ invalidate]; |
| 72 timer_ = nil; | 83 timer_ = nil; |
| 73 | 84 |
| 74 [super removeFromSuperview]; | 85 [super removeFromSuperview]; |
| 75 } | 86 } |
| 76 | 87 |
| 77 // Called when the TimerTarget gets tickled by our timer. Increment the frame | 88 // Called when the TimerTarget gets tickled by our timer. Increment the frame |
| 78 // counter and mark as needing display. | 89 // counter and mark as needing display. |
| 79 - (void)animate { | 90 - (void)animate { |
| 80 animationFrame_ = ++animationFrame_ % numFrames_; | 91 animationFrame_ = ++animationFrame_ % numFrames_; |
| 81 //[self setNeedsDisplay:YES]; | 92 [self setNeedsDisplay:YES]; |
| 82 } | 93 } |
| 83 | 94 |
| 84 // Overridden to draw the appropriate frame in the image strip. | 95 // Overridden to draw the appropriate frame in the image strip. |
| 85 - (void)drawRect:(NSRect)rect { | 96 - (void)drawRect:(NSRect)rect { |
| 86 float imageDimension = [image_ size].height; | 97 float imageDimension = [image_ extent].size.height; |
| 87 float xOffset = animationFrame_ * imageDimension; | 98 float xOffset = animationFrame_ * imageDimension; |
| 88 NSRect sourceImageRect = | 99 NSRect sourceImageRect = |
| 89 NSMakeRect(xOffset, 0, imageDimension, imageDimension); | 100 NSMakeRect(xOffset, 0, imageDimension, imageDimension); |
| 90 [image_ compositeToPoint:NSMakePoint(0, 0) | 101 [image_ drawInRect:[self bounds] |
| 91 fromRect:sourceImageRect | 102 fromRect:sourceImageRect |
| 92 operation:NSCompositeSourceOver]; | 103 operation:NSCompositeSourceOver |
| 104 fraction:1.0]; |
| 93 } | 105 } |
| 94 | 106 |
| 95 @end | 107 @end |
| OLD | NEW |