Permalink
Browse files

[FIX] -setContent: should check which items should be added or remove…

…d from the view, rather than just adding everything again.
  • Loading branch information...
1 parent 4292f24 commit b0863e13016bba874d82b2f101edbacf0d769118 @mattball committed Mar 29, 2009
Showing with 128 additions and 19 deletions.
  1. +65 −6 English.lproj/MainMenu.xib
  2. +43 −9 MBCoverFlowView.m
  3. +3 −1 MBCoverFlowViewController.h
  4. +17 −3 MBCoverFlowViewController.m
View
71 English.lproj/MainMenu.xib
@@ -1114,7 +1114,7 @@
<object class="NSWindowTemplate" id="972006081">
<int key="NSWindowStyleMask">15</int>
<int key="NSWindowBacking">2</int>
- <string key="NSWindowRect">{{335, 521}, {623, 229}}</string>
+ <string key="NSWindowRect">{{335, 501}, {623, 249}}</string>
<int key="NSWTFlags">1946157056</int>
<string key="NSWindowTitle">Window</string>
<string key="NSWindowClass">NSWindow</string>
@@ -1128,12 +1128,40 @@
<object class="NSCustomView" id="260292442">
<reference key="NSNextResponder" ref="439893737"/>
<int key="NSvFlags">286</int>
- <string key="NSFrameSize">{623, 229}</string>
+ <string key="NSFrame">{{0, 20}, {623, 229}}</string>
<reference key="NSSuperview" ref="439893737"/>
<string key="NSClassName">MBCoverFlowView</string>
</object>
+ <object class="NSButton" id="184616755">
+ <reference key="NSNextResponder" ref="439893737"/>
+ <int key="NSvFlags">292</int>
+ <string key="NSFrame">{{6, 0}, {33, 19}}</string>
+ <reference key="NSSuperview" ref="439893737"/>
+ <bool key="NSEnabled">YES</bool>
+ <object class="NSButtonCell" key="NSCell" id="18111438">
+ <int key="NSCellFlags">-2080244224</int>
+ <int key="NSCellFlags2">134217728</int>
+ <string key="NSContents">Round Rect Button</string>
+ <object class="NSFont" key="NSSupport">
+ <string key="NSName">LucidaGrande</string>
+ <double key="NSSize">1.200000e+01</double>
+ <int key="NSfFlags">16</int>
+ </object>
+ <reference key="NSControlView" ref="184616755"/>
+ <int key="NSButtonFlags">-2033958657</int>
+ <int key="NSButtonFlags2">164</int>
+ <object class="NSCustomResource" key="NSNormalImage">
+ <string key="NSClassName">NSImage</string>
+ <string key="NSResourceName">NSAddTemplate</string>
+ </object>
+ <string key="NSAlternateContents"/>
+ <string key="NSKeyEquivalent"/>
+ <int key="NSPeriodicDelay">400</int>
+ <int key="NSPeriodicInterval">75</int>
+ </object>
+ </object>
</object>
- <string key="NSFrameSize">{623, 229}</string>
+ <string key="NSFrameSize">{623, 249}</string>
<reference key="NSSuperview"/>
</object>
<string key="NSScreenRect">{{0, 0}, {1440, 878}}</string>
@@ -1685,6 +1713,14 @@
</object>
<int key="connectionID">452</int>
</object>
+ <object class="IBConnectionRecord">
+ <object class="IBActionConnection" key="connection">
+ <string key="label">addItem:</string>
+ <reference key="source" ref="593156221"/>
+ <reference key="destination" ref="184616755"/>
+ </object>
+ <int key="connectionID">455</int>
+ </object>
</object>
<object class="IBMutableOrderedSet" key="objectRecords">
<object class="NSArray" key="orderedObjects">
@@ -2272,6 +2308,7 @@
<object class="NSMutableArray" key="children">
<bool key="EncodedWithXMLCoder">YES</bool>
<reference ref="260292442"/>
+ <reference ref="184616755"/>
</object>
<reference key="parent" ref="972006081"/>
</object>
@@ -2595,6 +2632,20 @@
<reference key="object" ref="593156221"/>
<reference key="parent" ref="1049"/>
</object>
+ <object class="IBObjectRecord">
+ <int key="objectID">453</int>
+ <reference key="object" ref="184616755"/>
+ <object class="NSMutableArray" key="children">
+ <bool key="EncodedWithXMLCoder">YES</bool>
+ <reference ref="18111438"/>
+ </object>
+ <reference key="parent" ref="439893737"/>
+ </object>
+ <object class="IBObjectRecord">
+ <int key="objectID">454</int>
+ <reference key="object" ref="18111438"/>
+ <reference key="parent" ref="184616755"/>
+ </object>
</object>
</object>
<object class="NSMutableDictionary" key="flattenedProperties">
@@ -2789,6 +2840,8 @@
<string>420.IBPluginDependency</string>
<string>450.IBPluginDependency</string>
<string>451.IBPluginDependency</string>
+ <string>453.IBPluginDependency</string>
+ <string>454.IBPluginDependency</string>
<string>5.IBPluginDependency</string>
<string>5.ImportedFromIB2</string>
<string>56.IBPluginDependency</string>
@@ -2959,8 +3012,8 @@
<reference ref="9"/>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<reference ref="9"/>
- <string>{{139, 478}, {623, 229}}</string>
- <string>{{139, 478}, {623, 229}}</string>
+ <string>{{139, 458}, {623, 249}}</string>
+ <string>{{139, 458}, {623, 249}}</string>
<reference ref="9"/>
<string>{{33, 99}, {480, 360}}</string>
<string>{3.40282e+38, 3.40282e+38}</string>
@@ -3016,6 +3069,8 @@
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
+ <string>com.apple.InterfaceBuilder.CocoaPlugin</string>
+ <string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<reference ref="9"/>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<reference ref="9"/>
@@ -3072,7 +3127,7 @@
</object>
</object>
<nil key="sourceID"/>
- <int key="maxID">452</int>
+ <int key="maxID">455</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<object class="NSMutableArray" key="referencedPartialClassDescriptions">
@@ -3088,6 +3143,10 @@
<object class="IBPartialClassDescription">
<string key="className">MBCoverFlowViewController</string>
<string key="superclassName">NSViewController</string>
+ <object class="NSMutableDictionary" key="actions">
+ <string key="NS.key.0">addItem:</string>
+ <string key="NS.object.0">id</string>
+ </object>
<object class="IBClassDescriptionSource" key="sourceIdentifier">
<string key="majorKey">IBProjectSource</string>
<string key="minorKey">MBCoverFlowViewController.h</string>
View
52 MBCoverFlowView.m
@@ -62,7 +62,8 @@ @interface MBCoverFlowView ()
- (float)_positionOfSelectedItem;
- (CALayer *)_newLayer;
- (void)_scrollerChange:(MBCoverFlowScroller *)scroller;
-- (void)_refreshImagesWithOldContent:(NSArray *)oldContent;
+- (void)_refreshLayers;
+- (CALayer *)_layerForObject:(id)object;
@end
@@ -341,7 +342,7 @@ - (void)resizeSubviewsWithOldSize:(NSSize)oldSize
#pragma mark Loading Data
- (void)setContent:(NSArray *)newContents
-{
+{
NSArray *oldContent = [self.content retain];
if (_content) {
@@ -353,8 +354,30 @@ - (void)setContent:(NSArray *)newContents
_content = [newContents copy];
}
- [self _refreshImagesWithOldContent:oldContent];
+ // Add any new items
+ NSMutableArray *itemsToAdd = [NSMutableArray arrayWithArray:self.content];
+ [itemsToAdd removeObjectsInArray:oldContent];
+
+ for (NSObject *object in itemsToAdd) {
+ CALayer *layer = [self _newLayer];
+ [layer setValue:object forKey:@"representedObject"];
+ }
+
+ // Remove any items which are no longer present
+ NSMutableArray *itemsToRemove = [NSMutableArray arrayWithArray:oldContent];
+ [itemsToRemove removeObjectsInArray:self.content];
+ for (NSObject *object in itemsToRemove) {
+ CALayer *layer = [self _layerForObject:object];
+ [layer removeFromSuperlayer];
+ }
+
[oldContent release];
+
+ // Update the images and indexes
+ [self _refreshLayers];
+
+ [_scroller setNumberOfIncrements:([self.content count]-1)];
+ self.selectionIndex = self.selectionIndex;
}
#pragma mark Setting Display Attributes
@@ -601,13 +624,17 @@ - (void)_scrollerChange:(MBCoverFlowScroller *)sender
}
}
-- (void)_refreshImagesWithOldContent:(NSArray *)oldContent
+- (void)_refreshLayers
{
- for (NSObject *object in self.content) {
- CALayer *layer = [self _newLayer];
+ for (CALayer *layer in [_scrollLayer sublayers]) {
CALayer *imageLayer = [[layer sublayers] objectAtIndex:0];
CALayer *reflectionLayer = [[imageLayer sublayers] objectAtIndex:0];
+ NSObject *object = [layer valueForKey:@"representedObject"];
+ NSInteger index = [self.content indexOfObject:object];
+
+ [layer setValue:[NSNumber numberWithInteger:index] forKey:@"index"];
+
@try {
NSImage *image;
@@ -628,9 +655,16 @@ - (void)_refreshImagesWithOldContent:(NSArray *)oldContent
continue;
}
}
-
- [_scroller setNumberOfIncrements:([self.content count]-1)];
- self.selectionIndex = self.selectionIndex;
+}
+
+- (CALayer *)_layerForObject:(id)object
+{
+ for (CALayer *layer in [_scrollLayer sublayers]) {
+ if ([object isEqual:[layer valueForKey:@"representedObject"]]) {
+ return layer;
+ }
+ }
+ return nil;
}
#pragma mark -
View
4 MBCoverFlowViewController.h
@@ -28,7 +28,9 @@
@interface MBCoverFlowViewController : NSViewController {
-
+ NSArray *items;
}
+- (void)addItem:(id)sender;
+
@end
View
20 MBCoverFlowViewController.m
@@ -49,9 +49,11 @@ - (void)awakeFromNib
count++;
}
- [(MBCoverFlowView *)self.view setContent:images];
+ items = [images copy];
- NSViewController *labelViewController = [[NSViewController alloc] initWithNibName:nil bundle:nil];
+// [(MBCoverFlowView *)self.view setContent:images];
+
+/* NSViewController *labelViewController = [[NSViewController alloc] initWithNibName:nil bundle:nil];
NSTextField *label = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 10, 10)];
[label setBordered:NO];
[label setBezeled:NO];
@@ -65,9 +67,21 @@ - (void)awakeFromNib
[labelViewController setView:label];
[label release];
[(MBCoverFlowView *)self.view setAccessoryController:labelViewController];
- [labelViewController release];
+ [labelViewController release];*/
[(MBCoverFlowView *)self.view setShowsScrollbar:YES];
}
+- (void)dealloc
+{
+ [items release];
+ [super dealloc];
+}
+
+- (void)addItem:(id)sender
+{
+ NSArray *content = [items subarrayWithRange:NSMakeRange(0, [[(MBCoverFlowView *)self.view content] count]+1)];
+ [(MBCoverFlowView *)self.view setContent:content];
+}
+
@end

0 comments on commit b0863e1

Please sign in to comment.