Skip to content

Commit

Permalink
[CHANGE] Added imageKeyPath property to allow non-NSImage content
Browse files Browse the repository at this point in the history
  • Loading branch information
mattball committed Mar 29, 2009
1 parent 29a770b commit 4292f24
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
8 changes: 8 additions & 0 deletions MBCoverFlowView.h
Expand Up @@ -51,6 +51,8 @@


BOOL _showsScrollbar; BOOL _showsScrollbar;
BOOL _autoresizesItems; BOOL _autoresizesItems;

NSString *_imageKeyPath;
} }


/** /**
Expand All @@ -62,6 +64,12 @@
*/ */
@property (nonatomic, copy) NSArray *content; @property (nonatomic, copy) NSArray *content;


/**
* @brief The key path which returns the image for an item
* in the receiver's \c content array.
*/
@property (nonatomic, copy) NSString *imageKeyPath;

/** /**
* @name Setting Display Attributes * @name Setting Display Attributes
*/ */
Expand Down
55 changes: 40 additions & 15 deletions MBCoverFlowView.m
Expand Up @@ -62,14 +62,15 @@ @interface MBCoverFlowView ()
- (float)_positionOfSelectedItem; - (float)_positionOfSelectedItem;
- (CALayer *)_newLayer; - (CALayer *)_newLayer;
- (void)_scrollerChange:(MBCoverFlowScroller *)scroller; - (void)_scrollerChange:(MBCoverFlowScroller *)scroller;
- (void)_refreshImagesWithOldContent:(NSArray *)oldContent;
@end @end




@implementation MBCoverFlowView @implementation MBCoverFlowView


@synthesize accessoryController=_accessoryController, selectionIndex=_selectionIndex, @synthesize accessoryController=_accessoryController, selectionIndex=_selectionIndex,
itemSize=_itemSize, content=_content, showsScrollbar=_showsScrollbar, itemSize=_itemSize, content=_content, showsScrollbar=_showsScrollbar,
autoresizesItems=_autoresizesItems; autoresizesItems=_autoresizesItems, imageKeyPath=_imageKeyPath;


#pragma mark - #pragma mark -
#pragma mark Life Cycle #pragma mark Life Cycle
Expand Down Expand Up @@ -235,6 +236,8 @@ - (void)dealloc
[_scrollLayer release]; [_scrollLayer release];
[_containerLayer release]; [_containerLayer release];
self.accessoryController = nil; self.accessoryController = nil;
self.content = nil;
self.imageKeyPath = nil;
CGImageRelease(_shadowImage); CGImageRelease(_shadowImage);
[super dealloc]; [super dealloc];
} }
Expand Down Expand Up @@ -339,29 +342,19 @@ - (void)resizeSubviewsWithOldSize:(NSSize)oldSize


- (void)setContent:(NSArray *)newContents - (void)setContent:(NSArray *)newContents
{ {
NSArray *oldContent = [self.content retain];

if (_content) { if (_content) {
[_content release]; [_content release];
_content = nil; _content = nil;
} }


if (newContents != nil) { if (newContents != nil) {
_content = [newContents copy]; _content = [newContents copy];
for (NSImage *image in self.content) {
CALayer *layer = [self _newLayer];
CALayer *imageLayer = [[layer sublayers] objectAtIndex:0];
CALayer *reflectionLayer = [[imageLayer sublayers] objectAtIndex:0];

CGImageRef imageRef = [image imageRef];

imageLayer.contents = (id)imageRef;
reflectionLayer.contents = (id)imageRef;
imageLayer.backgroundColor = NULL;
reflectionLayer.backgroundColor = NULL;
}
} }


[_scroller setNumberOfIncrements:([self.content count]-1)]; [self _refreshImagesWithOldContent:oldContent];
self.selectionIndex = self.selectionIndex; [oldContent release];
} }


#pragma mark Setting Display Attributes #pragma mark Setting Display Attributes
Expand Down Expand Up @@ -608,6 +601,38 @@ - (void)_scrollerChange:(MBCoverFlowScroller *)sender
} }
} }


- (void)_refreshImagesWithOldContent:(NSArray *)oldContent
{
for (NSObject *object in self.content) {
CALayer *layer = [self _newLayer];
CALayer *imageLayer = [[layer sublayers] objectAtIndex:0];
CALayer *reflectionLayer = [[imageLayer sublayers] objectAtIndex:0];

@try {
NSImage *image;

if (self.imageKeyPath != nil) {
image = [object valueForKeyPath:self.imageKeyPath];
} else if ([object isKindOfClass:[NSImage class]]) {
image = (NSImage *)object;
}

CGImageRef imageRef = [image imageRef];

imageLayer.contents = (id)imageRef;
reflectionLayer.contents = (id)imageRef;
imageLayer.backgroundColor = NULL;
reflectionLayer.backgroundColor = NULL;
} @catch (NSException *e) {
// If the key path isn't valid, move to the next item
continue;
}
}

[_scroller setNumberOfIncrements:([self.content count]-1)];
self.selectionIndex = self.selectionIndex;
}

#pragma mark - #pragma mark -
#pragma mark Protocol Methods #pragma mark Protocol Methods


Expand Down

0 comments on commit 4292f24

Please sign in to comment.