Skip to content

Commit

Permalink
Migrated changes in from another project.
Browse files Browse the repository at this point in the history
  • Loading branch information
joshaber committed Jan 5, 2011
1 parent 99faf0f commit 52322a7
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 90 deletions.
12 changes: 2 additions & 10 deletions JAListView.h
Expand Up @@ -85,6 +85,8 @@ extern NSString * const JAListViewDraggingPasteboardType;
- (NSInteger)indexForView:(JAListViewItem *)view;
- (BOOL)isViewVisible:(JAListViewItem *)view;

- (BOOL)containsViewItem:(JAListViewItem *)viewItem;

- (JAListViewItem *)viewAtPoint:(NSPoint)point;

/*
Expand All @@ -97,16 +99,6 @@ extern NSString * const JAListViewDraggingPasteboardType;
*/
- (CGFloat)heightForView:(JAListViewItem *)view proposedHeight:(CGFloat)proposedHeight;

- (void)setView:(JAListViewItem *)view forKey:(id)key;
- (id)viewForKey:(id)key;
- (void)removeViewForKey:(id)key;
- (id)keyForView:(JAListViewItem *)view;

- (void)setView:(JAListViewItem *)view forObject:(id)object;
- (id)viewForObject:(id)object;
- (void)removeViewForObject:(id)object;
- (void)removeAllStoredViews;

- (CGFloat)cachedYLocationForView:(JAListViewItem *)view;

/**
Expand Down
78 changes: 14 additions & 64 deletions JAListView.m
Expand Up @@ -26,7 +26,6 @@ - (void)showVisibleViewsAnimated:(BOOL)animated;
- (void)updateCachedVisibleViews;
- (void)clearCachedLocations;
- (void)seriouslyShowVisibleViewsWithAnimation;
- (id)keyForObject:(id)object;
- (void)getSelectionMinimumIndex:(NSUInteger *)minIndex maximumIndex:(NSUInteger *)maxIndex;
- (JAListViewItem *)nextSelectableView;
- (JAListViewItem *)nextSelectableViewFromIndex:(NSUInteger)startingIndex;
Expand All @@ -40,7 +39,6 @@ - (void)standardLayoutAnimated:(BOOL)animated removeViews:(NSArray *)viewsToRemo
@property (nonatomic, retain) NSArray *cachedVisibleViews;
@property (nonatomic, assign) __weak JAListViewItem *viewBeingSelected;
@property (nonatomic, retain) NSArray *viewsCurrentlyBeingDragged;
@property (nonatomic, retain) NSMutableDictionary *viewStorage;
@property (nonatomic, retain) NSMutableArray *currentlySelectedViews;
@property (nonatomic, retain) NSTrackingArea *currentTrackingArea;
@property (nonatomic, copy) void (^currentAnimationBlock)(NSView *newSuperview, NSArray *viewsToAdd, NSArray *viewsToRemove, NSArray *viewsToMove);
Expand All @@ -67,7 +65,6 @@ - (void)dealloc {
self.viewBeingUsedForInertialScroll = nil;
self.backgroundColor = nil;
self.cachedVisibleViews = nil;
self.viewStorage = nil;
self.currentlySelectedViews = nil;
self.currentTrackingArea = nil;
self.currentAnimationBlock = nil;
Expand Down Expand Up @@ -194,13 +191,13 @@ - (void)mouseUp:(NSEvent *)event {
self.viewBeingSelected.highlighted = NO;
self.viewBeingSelected = nil;
} else {
for(JAListViewItem *selectedView in [[self.currentlySelectedViews copy] autorelease]) {
[[selectedView retain] autorelease];
selectedView.selected = NO;
[self.currentlySelectedViews removeObject:selectedView];
for(JAListViewItem *itemView in [[self.currentlySelectedViews copy] autorelease]) {
[[itemView retain] autorelease];
itemView.selected = NO;
[self.currentlySelectedViews removeObject:itemView];

if(respondsToUnSelect) {
[self.delegate listView:self didDeselectView:selectedView];
[self.delegate listView:self didDeselectView:itemView];
}
}
}
Expand Down Expand Up @@ -278,13 +275,13 @@ - (void)mouseUp:(NSEvent *)event {
[self.delegate listView:self didSelectView:view];
}
} else {
for(JAListViewItem *selectedView in [[self.currentlySelectedViews copy] autorelease]) {
[[selectedView retain] autorelease];
selectedView.selected = NO;
[self.currentlySelectedViews removeObject:selectedView];
for(JAListViewItem *itemView in [[self.currentlySelectedViews copy] autorelease]) {
[[itemView retain] autorelease];
itemView.selected = NO;
[self.currentlySelectedViews removeObject:itemView];

if(respondsToUnSelect) {
[self.delegate listView:self didDeselectView:selectedView];
[self.delegate listView:self didDeselectView:itemView];
}
}

Expand Down Expand Up @@ -513,7 +510,6 @@ - (void)setup {
margin = NSZeroPoint;
self.backgroundColor = [NSColor darkGrayColor];
[self registerForDraggedTypes:[NSArray arrayWithObject:JAListViewDraggingPasteboardType]];
self.viewStorage = [NSMutableDictionary dictionary];
self.currentlySelectedViews = [NSMutableArray array];
}

Expand Down Expand Up @@ -801,6 +797,10 @@ - (BOOL)isViewVisible:(JAListViewItem *)view {
return [self.visibleViews containsObject:view];
}

- (BOOL)containsViewItem:(JAListViewItem *)viewItem {
return [self.cachedViews containsObject:viewItem];
}

- (NSArray *)visibleViews {
return self.cachedVisibleViews;
}
Expand All @@ -820,55 +820,6 @@ - (void)updateCachedVisibleViews {
self.cachedVisibleViews = newVisibleViews;
}

- (void)setView:(JAListViewItem *)view forKey:(id)key {
[self.viewStorage setObject:view forKey:key];
}

- (id)viewForKey:(id)key {
return [self.viewStorage objectForKey:key];
}

- (void)removeViewForKey:(id)key {
[self.viewStorage removeObjectForKey:key];
}

- (id)keyForView:(JAListViewItem *)view {
for(id key in self.viewStorage) {
if([[self.viewStorage objectForKey:key] isEqualTo:view]) {
return key;
}
}

return nil;
}

- (void)setView:(JAListViewItem *)view forObject:(id)object {
[self setView:view forKey:[self keyForObject:object]];
}

- (id)viewForObject:(id)object {
return [self viewForKey:[self keyForObject:object]];
}

- (void)removeViewForObject:(id)object {
[self removeViewForKey:[self keyForObject:object]];
}

- (void)removeAllStoredViews {
[self.viewStorage removeAllObjects];
}

- (id)keyForObject:(id)object {
// keys are copied when put into a dictionary, so if they object isn't copyable then we need to just use its pointer address as the key
if([object conformsToProtocol:@protocol(NSCopying)]) {
return object;
} else if([object respondsToSelector:@selector(hash)]) {
return [NSNumber numberWithUnsignedInteger:[object hash]];
} else {
return [NSString stringWithFormat:@"%p", object];
}
}

- (NSScrollView *)scrollView {
return [self enclosingScrollView];
}
Expand Down Expand Up @@ -1037,7 +988,6 @@ - (void)deselectAllViews {
@synthesize backgroundColor;
@synthesize heightForAllContent;
@synthesize conditionallyUseLayerBacking;
@synthesize viewStorage;
@synthesize currentlySelectedViews;
@synthesize currentTrackingArea;
@synthesize currentAnimationBlock;
Expand Down
13 changes: 8 additions & 5 deletions JAObjectListView.h
Expand Up @@ -9,21 +9,24 @@
#import <Cocoa/Cocoa.h>
#import "JASectionedListView.h"

@class JAListViewItem;
@class JAObjectListViewItem;


@interface JAObjectListView : JASectionedListView <JASectionedListViewDataSource> {}

- (void)addListViewItem:(JAListViewItem *)view inSection:(NSUInteger)section atIndex:(NSUInteger)index;
- (void)addListViewItem:(JAListViewItem *)view inSection:(NSUInteger)section;
- (void)addListViewItem:(JAObjectListViewItem *)view inSection:(NSUInteger)section atIndex:(NSUInteger)index;
- (void)addListViewItem:(JAObjectListViewItem *)view inSection:(NSUInteger)section;
- (void)removeListViewItemInSection:(NSUInteger)section atIndex:(NSUInteger)index;

- (void)addListViewItem:(JAListViewItem *)view forHeaderForSection:(NSUInteger)section;
- (void)addListViewItem:(JAObjectListViewItem *)view forHeaderForSection:(NSUInteger)section;
- (void)removeListViewItemForHeaderForSection:(NSUInteger)section;

- (void)removeListViewItem:(JAListViewItem *)view;
- (void)removeListViewItem:(JAObjectListViewItem *)view;
- (void)removeAllListViewItems;
- (NSArray *)viewsInSection:(NSUInteger)section;
- (NSUInteger)numberOfSections;

- (JAObjectListViewItem *)viewItemForObject:(id)object;
- (NSIndexPath *)indexPathForObject:(id)object;

@end
91 changes: 80 additions & 11 deletions JAObjectListView.m
Expand Up @@ -7,9 +7,14 @@
//

#import "JAObjectListView.h"
#import "JAObjectListViewItem.h"

@interface JAListView (Private)
- (NSArray *)cachedViews;
@end

@interface JAObjectListView ()
- (void)reallyAddListViewItem:(JAListViewItem *)view inSection:(NSUInteger)section atIndex:(NSUInteger)index;
- (void)reallyAddListViewItem:(JAObjectListViewItem *)view inSection:(NSUInteger)section atIndex:(NSUInteger)index;

@property (nonatomic, retain) NSMutableArray *sectionRowViews;
@end
Expand Down Expand Up @@ -66,60 +71,89 @@ - (JAListViewItem *)listView:(JAListView *)listView viewForSection:(NSUInteger)s

@synthesize sectionRowViews;

- (void)addListViewItem:(JAListViewItem *)view inSection:(NSUInteger)section atIndex:(NSUInteger)index {
- (void)addListViewItem:(JAObjectListViewItem *)view inSection:(NSUInteger)section atIndex:(NSUInteger)index {
[self reallyAddListViewItem:view inSection:section atIndex:index + 1];
}

- (void)reallyAddListViewItem:(JAListViewItem *)view inSection:(NSUInteger)section atIndex:(NSUInteger)index {
- (void)reallyAddListViewItem:(JAObjectListViewItem *)view inSection:(NSUInteger)section atIndex:(NSUInteger)index {
NSMutableArray *sectionViews = nil;
if(section < self.sectionRowViews.count) {
sectionViews = [self.sectionRowViews objectAtIndex:section];
} else if(section != NSNotFound) { // added NSNotFound check
sectionViews = [NSMutableArray array];
[self.sectionRowViews insertObject:sectionViews atIndex:section]; //!!! boom - bounds
} else {
NSAssert1(NO, @"Tried to insert view into a non-existent section: %@", view);
}

if(index < sectionViews.count) {
[sectionViews replaceObjectAtIndex:index withObject:view];
} else if(index != NSNotFound) { // added NSNotFound check
[sectionViews insertObject:view atIndex:index]; //!!! boom - bounds
} else {
NSAssert1(NO, @"Tried to insert view into a non-existent row: %@", view);
}
}

- (void)addListViewItem:(JAListViewItem *)view inSection:(NSUInteger)section {
- (void)addListViewItem:(JAObjectListViewItem *)view inSection:(NSUInteger)section {
[self addListViewItem:view inSection:section atIndex:[self viewsInSection:section].count];
}

- (void)removeListViewItemInSection:(NSUInteger)section atIndex:(NSUInteger)index {
NSMutableArray *sectionViews = [self.sectionRowViews objectAtIndex:section];
[sectionViews removeObjectAtIndex:index + 1];

NSUInteger trueIndex = index + 1;
JAObjectListViewItem *view = [sectionViews objectAtIndex:trueIndex];
if(self.viewBeingUsedForInertialScroll == view) {
self.viewBeingUsedForInertialScroll = nil;
}

[self deselectView:view];

[sectionViews removeObjectAtIndex:trueIndex];
}

- (void)addListViewItem:(JAListViewItem *)view forHeaderForSection:(NSUInteger)section {
- (void)addListViewItem:(JAObjectListViewItem *)view forHeaderForSection:(NSUInteger)section {
NSMutableArray *sectionViews = [NSMutableArray array];
[self.sectionRowViews insertObject:sectionViews atIndex:section];
[sectionViews insertObject:view atIndex:0];
}

- (void)removeListViewItemForHeaderForSection:(NSUInteger)section {
NSArray *views = [self.sectionRowViews objectAtIndex:section];
for(JAObjectListViewItem *item in views) {
if(self.viewBeingUsedForInertialScroll == item) {
self.viewBeingUsedForInertialScroll = nil;
}

[self deselectView:item];
}

[self.sectionRowViews removeObjectAtIndex:section];
}

- (void)removeListViewItem:(JAListViewItem *)view {
- (void)removeListViewItem:(JAObjectListViewItem *)view {
if(self.viewBeingUsedForInertialScroll == view) {
self.viewBeingUsedForInertialScroll = nil;
}

[self deselectView:view];

for(NSUInteger sectionIndex = 0; sectionIndex < self.sectionRowViews.count; sectionIndex++) {
NSMutableArray *sectionViews = [self.sectionRowViews objectAtIndex:sectionIndex];
NSUInteger index = [sectionViews indexOfObject:view];
if(index == 0) {
[self.sectionRowViews removeObjectAtIndex:sectionIndex];
} else if(index != NSNotFound) {
[sectionViews removeObjectAtIndex:index];
if(index != NSNotFound) {
if(index == 0) {
[self.sectionRowViews removeObjectAtIndex:sectionIndex];
} else {
[sectionViews removeObjectAtIndex:index];
}
}
}
}

- (void)removeAllListViewItems {
self.viewBeingUsedForInertialScroll = nil;
[self deselectAllViews];
[self.sectionRowViews removeAllObjects];
}
Expand All @@ -138,4 +172,39 @@ - (NSUInteger)numberOfSections {
return self.sectionRowViews.count;
}

- (JAObjectListViewItem *)viewItemForObject:(id)object {
for(NSArray *views in self.sectionRowViews) {
for(JAObjectListViewItem *item in views) {
if([item.object isEqual:object]) {
return item;
}
}
}

return nil;
}

- (NSIndexPath *)indexPathForObject:(id)object {
NSUInteger section = 0;
for(NSArray *views in self.sectionRowViews) {

NSUInteger index = 0;
for(JAObjectListViewItem *item in views) {
if([item.object isEqual:object]) {
if(index == 0) {
return [NSIndexPath indexPathForIndex:JASectionedListViewHeaderIndex inSection:section];
} else {
return [NSIndexPath indexPathForIndex:index - 1 inSection:section];
}
}

index++;
}

section++;
}

return nil;
}

@end
17 changes: 17 additions & 0 deletions JAObjectListViewItem.h
@@ -0,0 +1,17 @@
//
// JAObjectListViewItem.h
// GitHub
//
// Created by Josh Abernathy on 12/29/10.
// Copyright 2010 GitHub. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import "JAListViewItem.h"


@interface JAObjectListViewItem : JAListViewItem {}

@property (nonatomic, retain) id object;

@end
19 changes: 19 additions & 0 deletions JAObjectListViewItem.m
@@ -0,0 +1,19 @@
//
// JAObjectListViewItem.m
// GitHub
//
// Created by Josh Abernathy on 12/29/10.
// Copyright 2010 GitHub. All rights reserved.
//

#import "JAObjectListViewItem.h"


@implementation JAObjectListViewItem


#pragma mark API

@synthesize object;

@end

0 comments on commit 52322a7

Please sign in to comment.