Skip to content

Commit

Permalink
Added drag'n'drop support
Browse files Browse the repository at this point in the history
  • Loading branch information
kettch committed Jun 2, 2016
1 parent b2a7fb1 commit dd1e21f
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 38 deletions.
1 change: 1 addition & 0 deletions JNWCollectionView/JNWCollectionView+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- (void)mouseMovedInCollectionViewCell:(JNWCollectionViewCell *)cell withEvent:(NSEvent *)event;
- (void)mouseExitedInCollectionViewCell:(JNWCollectionViewCell *)cell withEvent:(NSEvent *)event;
- (void)mouseEnteredInCollectionViewCell:(JNWCollectionViewCell *)cell withEvent:(NSEvent *)event;
- (void)mouseDraggedInCollectionViewCell:(JNWCollectionViewCell *)cell withEvent:(NSEvent *)event;
- (void)doubleClickInCollectionViewCell:(JNWCollectionViewCell *)cell withEvent:(NSEvent *)event;
- (void)rightClickInCollectionViewCell:(JNWCollectionViewCell *)cell withEvent:(NSEvent *)event;

Expand Down
6 changes: 6 additions & 0 deletions JNWCollectionView/JNWCollectionViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ - (void)mouseExited:(NSEvent *)theEvent {
[self.collectionView mouseExitedInCollectionViewCell:self withEvent:theEvent];
}

- (void)mouseDragged:(NSEvent *)theEvent {
[super mouseMoved:theEvent];

[self.collectionView mouseDraggedInCollectionViewCell:self withEvent:theEvent];
}

- (void)rightMouseDown:(NSEvent *)theEvent {
[self.collectionView rightClickInCollectionViewCell:self withEvent:theEvent];
}
Expand Down
22 changes: 14 additions & 8 deletions JNWCollectionView/JNWCollectionViewFramework.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,24 +87,27 @@ typedef NS_ENUM(NSInteger, JNWCollectionViewScrollPosition) {

@optional
/// Tells the delegate that the mouse is down inside of the item at the specified index path with specific modifier flags.
- (void)collectionView:(JNWCollectionView *)collectionView mouseDownInItemAtIndexPath:(NSIndexPath *)indexPath withModifierFlags:(NSEventModifierFlags)modFlags;
- (void)collectionView:(JNWCollectionView *)collectionView mouseDownInItemAtIndexPath:(NSIndexPath *)indexPath withEvent:(NSEvent *)event;

/// Tells the delegate that the mouse click originating from the item at the specified index path is now up with specific modifier flags.
///
/// The mouse up event can occur outside of the originating cell.
- (void)collectionView:(JNWCollectionView *)collectionView mouseUpInItemAtIndexPath:(NSIndexPath *)indexPath withModifierFlags:(NSEventModifierFlags)modFlags;
- (void)collectionView:(JNWCollectionView *)collectionView mouseUpInItemAtIndexPath:(NSIndexPath *)indexPath withEvent:(NSEvent *)event;

- (void)collectionView:(JNWCollectionView *)collectionView mouseDownInItemAtIndexPath:(NSIndexPath *)indexPath __deprecated_msg("Use collectionView:mouseDownInItemAtIndexPath:withModifierFlags: instead.");
- (void)collectionView:(JNWCollectionView *)collectionView mouseUpInItemAtIndexPath:(NSIndexPath *)indexPath __deprecated_msg("Use collectionView:mouseUpInItemAtIndexPath:withModifierFlags: instead.");
- (void)collectionView:(JNWCollectionView *)collectionView mouseDownInItemAtIndexPath:(NSIndexPath *)indexPath __deprecated_msg("Use collectionView:mouseDownInItemAtIndexPath:withEvent: instead.");
- (void)collectionView:(JNWCollectionView *)collectionView mouseUpInItemAtIndexPath:(NSIndexPath *)indexPath __deprecated_msg("Use collectionView:mouseUpInItemAtIndexPath:withEvent: instead.");

/// Tells the delegate that the mouse moved inside the specified index path cell.
- (void)collectionView:(JNWCollectionView *)collectionView mouseMovedInItemAtIndexPath:(NSIndexPath *)indexPath withModifierFlags:(NSEventModifierFlags)modFlags;
- (void)collectionView:(JNWCollectionView *)collectionView mouseMovedInItemAtIndexPath:(NSIndexPath *)indexPath withEvent:(NSEvent *)event;

/// Tells the delegate that the mouse started a drag session inside the specified index path cell.
- (void)collectionView:(JNWCollectionView *)collectionView mouseDraggedInItemAtIndexPath:(NSIndexPath *)indexPath withEvent:(NSEvent *)event;

/// Tells the delegate that the mouse entered in the specified index path cell.
- (void)collectionView:(JNWCollectionView *)collectionView mouseEnteredInItemAtIndexPath:(NSIndexPath *)indexPath withModifierFlags:(NSEventModifierFlags)modFlags;
- (void)collectionView:(JNWCollectionView *)collectionView mouseEnteredInItemAtIndexPath:(NSIndexPath *)indexPath withEvent:(NSEvent *)event;

/// Tells the delegate that the mouse exited from the specified index path cell.
- (void)collectionView:(JNWCollectionView *)collectionView mouseExitedInItemAtIndexPath:(NSIndexPath *)indexPath withModifierFlags:(NSEventModifierFlags)modFlags;
- (void)collectionView:(JNWCollectionView *)collectionView mouseExitedInItemAtIndexPath:(NSIndexPath *)indexPath withEvent:(NSEvent *)event;

/// Asks the delegate if the item at the specified index path should be selected.
- (BOOL)collectionView:(JNWCollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath;
Expand Down Expand Up @@ -134,7 +137,7 @@ typedef NS_ENUM(NSInteger, JNWCollectionViewScrollPosition) {
/// back into the reuse queue.
- (void)collectionView:(JNWCollectionView *)collectionView didEndDisplayingCell:(JNWCollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath;

// Asks the delegate if a contextual menu should be used for the given event.
/// Asks the delegate if a contextual menu should be used for the given event.
- (NSMenu *)collectionView:(JNWCollectionView *)collectionView menuForEvent:(NSEvent *)event;

@end
Expand Down Expand Up @@ -286,6 +289,9 @@ typedef NS_ENUM(NSInteger, JNWCollectionViewScrollPosition) {
/// Defaults to YES.
@property (nonatomic, assign) BOOL allowsEmptySelection;

/// Returns the list of indexPaths of the selected items
@property (nonatomic, readonly) NSMutableArray *selectedIndexes;

/// Scrolls the collection view to the item at the specified path, optionally animated. The scroll position determines
/// where the item is positioned on the screen.
- (void)scrollToItemAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(JNWCollectionViewScrollPosition)scrollPosition animated:(BOOL)animated;
Expand Down
68 changes: 38 additions & 30 deletions JNWCollectionView/JNWCollectionViewFramework.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ @interface JNWCollectionView() {
unsigned int dataSourceViewForSupplementaryView:1;

unsigned int delegateMouseDown:1;
unsigned int delegateMouseDownWithModifiers:1;
unsigned int delegateMouseDownWithEvent:1;
unsigned int delegateMouseUp:1;
unsigned int delegateMouseUpWithModifiers:1;
unsigned int delegateMouseUpWithEvent:1;
unsigned int delegateMouseMoved:1;
unsigned int delegateMouseDragged:1;
unsigned int delegateMouseEntered:1;
unsigned int delegateMouseExited:1;
unsigned int delegateShouldSelect:1;
Expand All @@ -67,7 +68,7 @@ @interface JNWCollectionView() {
@property (nonatomic, strong) JNWCollectionViewData *data;

// Selection
@property (nonatomic, strong) NSMutableArray *selectedIndexes;
@property (nonatomic, strong, readwrite) NSMutableArray *selectedIndexes;

// Cells
@property (nonatomic, strong) NSMutableDictionary *reusableCells; // { identifier : (cells) }
Expand Down Expand Up @@ -142,12 +143,13 @@ - (id)initWithCoder:(NSCoder *)aDecoder {
- (void)setDelegate:(id<JNWCollectionViewDelegate>)delegate {
_delegate = delegate;
_collectionViewFlags.delegateMouseUp = [delegate respondsToSelector:@selector(collectionView:mouseUpInItemAtIndexPath:)];
_collectionViewFlags.delegateMouseUpWithModifiers = [delegate respondsToSelector:@selector(collectionView:mouseUpInItemAtIndexPath: withModifierFlags:)];
_collectionViewFlags.delegateMouseUpWithEvent = [delegate respondsToSelector:@selector(collectionView:mouseUpInItemAtIndexPath:withEvent:)];
_collectionViewFlags.delegateMouseDown = [delegate respondsToSelector:@selector(collectionView:mouseDownInItemAtIndexPath:)];
_collectionViewFlags.delegateMouseDownWithModifiers = [delegate respondsToSelector:@selector(collectionView:mouseDownInItemAtIndexPath:withModifierFlags:)];
_collectionViewFlags.delegateMouseMoved = [delegate respondsToSelector:@selector(collectionView:mouseMovedInItemAtIndexPath:withModifierFlags:)];
_collectionViewFlags.delegateMouseEntered = [delegate respondsToSelector:@selector(collectionView:mouseEnteredInItemAtIndexPath:withModifierFlags:)];
_collectionViewFlags.delegateMouseExited = [delegate respondsToSelector:@selector(collectionView:mouseExitedInItemAtIndexPath:withModifierFlags:)];
_collectionViewFlags.delegateMouseDownWithEvent = [delegate respondsToSelector:@selector(collectionView:mouseDownInItemAtIndexPath:withEvent:)];
_collectionViewFlags.delegateMouseMoved = [delegate respondsToSelector:@selector(collectionView:mouseMovedInItemAtIndexPath:withEvent:)];
_collectionViewFlags.delegateMouseDragged = [delegate respondsToSelector:@selector(collectionView:mouseDraggedInItemAtIndexPath:withEvent:)];
_collectionViewFlags.delegateMouseEntered = [delegate respondsToSelector:@selector(collectionView:mouseEnteredInItemAtIndexPath:withEvent:)];
_collectionViewFlags.delegateMouseExited = [delegate respondsToSelector:@selector(collectionView:mouseExitedInItemAtIndexPath:withEvent:)];
_collectionViewFlags.delegateShouldSelect = [delegate respondsToSelector:@selector(collectionView:shouldSelectItemAtIndexPath:)];
_collectionViewFlags.delegateDidSelect = [delegate respondsToSelector:@selector(collectionView:didSelectItemAtIndexPath:)];
_collectionViewFlags.delegateShouldDeselect = [delegate respondsToSelector:@selector(collectionView:shouldDeselectItemAtIndexPath:)];
Expand Down Expand Up @@ -1021,22 +1023,34 @@ - (void)selectItemAtIndexPath:(NSIndexPath *)indexPath
}

- (void)mouseDownInCollectionViewCell:(JNWCollectionViewCell *)cell withEvent:(NSEvent *)event {
NSIndexPath *indexPath = [self indexPathForCell:cell];
if (_collectionViewFlags.delegateMouseDownWithEvent) {
[self.delegate collectionView:self mouseDownInItemAtIndexPath:indexPath withEvent:event];
} else if (_collectionViewFlags.delegateMouseDown) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[self.delegate collectionView:self mouseDownInItemAtIndexPath:indexPath];
#pragma clang diagnostic pop
}
}

- (void)mouseUpInCollectionViewCell:(JNWCollectionViewCell *)cell withEvent:(NSEvent *)event {
[self.window makeFirstResponder:self];

NSIndexPath *indexPath = [self indexPathForCell:cell];
if (indexPath == nil) {
NSLog(@"***index path not found for selection.");
}
if (_collectionViewFlags.delegateMouseDownWithModifiers) {
[self.delegate collectionView:self mouseDownInItemAtIndexPath:indexPath withModifierFlags:event.modifierFlags];
} else if (_collectionViewFlags.delegateMouseDown) {

if (_collectionViewFlags.delegateMouseUpWithEvent) {
[self.delegate collectionView:self mouseUpInItemAtIndexPath:indexPath withEvent:event];
} else if (_collectionViewFlags.delegateMouseUp) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[self.delegate collectionView:self mouseDownInItemAtIndexPath:indexPath];
[self.delegate collectionView:self mouseUpInItemAtIndexPath:indexPath];
#pragma clang diagnostic pop
}

// Detect if modifier flags are held down.
// We prioritize the command key over the shift key.
if (event.modifierFlags & NSCommandKeyMask) {
Expand All @@ -1048,30 +1062,17 @@ - (void)mouseDownInCollectionViewCell:(JNWCollectionViewCell *)cell withEvent:(N
}
}

- (void)mouseUpInCollectionViewCell:(JNWCollectionViewCell *)cell withEvent:(NSEvent *)event {
if (_collectionViewFlags.delegateMouseUpWithModifiers) {
NSIndexPath *indexPath = [self indexPathForCell:cell];
[self.delegate collectionView:self mouseUpInItemAtIndexPath:indexPath withModifierFlags:event.modifierFlags];
} else if (_collectionViewFlags.delegateMouseUp) {
NSIndexPath *indexPath = [self indexPathForCell:cell];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[self.delegate collectionView:self mouseUpInItemAtIndexPath:indexPath];
#pragma clang diagnostic pop
}
}

- (void)mouseMovedInCollectionViewCell:(JNWCollectionViewCell *)cell withEvent:(NSEvent *)event {
if (_collectionViewFlags.delegateMouseMoved) {
NSIndexPath *indexPath = [self indexPathForCell:cell];
[self.delegate collectionView:self mouseMovedInItemAtIndexPath:indexPath withModifierFlags:event.modifierFlags];
[self.delegate collectionView:self mouseMovedInItemAtIndexPath:indexPath withEvent:event];
}
}

- (void)mouseEnteredInCollectionViewCell:(JNWCollectionViewCell *)cell withEvent:(NSEvent *)event {
if (_collectionViewFlags.delegateMouseEntered) {
NSIndexPath *indexPath = [self indexPathForCell:cell];
[self.delegate collectionView:self mouseEnteredInItemAtIndexPath:indexPath withModifierFlags:event.modifierFlags];
[self.delegate collectionView:self mouseEnteredInItemAtIndexPath:indexPath withEvent:event];
}

[[self.visibleCellsMap allValues] enumerateObjectsUsingBlock:^(JNWCollectionViewCell *cell, NSUInteger index, BOOL *stop) {
Expand All @@ -1083,12 +1084,19 @@ - (void)mouseEnteredInCollectionViewCell:(JNWCollectionViewCell *)cell withEvent
- (void)mouseExitedInCollectionViewCell:(JNWCollectionViewCell *)cell withEvent:(NSEvent *)event {
if (_collectionViewFlags.delegateMouseExited) {
NSIndexPath *indexPath = [self indexPathForCell:cell];
[self.delegate collectionView:self mouseExitedInItemAtIndexPath:indexPath withModifierFlags:event.modifierFlags];
[self.delegate collectionView:self mouseExitedInItemAtIndexPath:indexPath withEvent:event];
}

cell.hovered = NO;
}

- (void)mouseDraggedInCollectionViewCell:(JNWCollectionViewCell *)cell withEvent:(NSEvent *)event {
if (_collectionViewFlags.delegateMouseDragged) {
NSIndexPath *indexPath = [self indexPathForCell:cell];
[self.delegate collectionView:self mouseDraggedInItemAtIndexPath:indexPath withEvent:event];
}
}

- (void)doubleClickInCollectionViewCell:(JNWCollectionViewCell *)cell withEvent:(NSEvent *)event {
if (_collectionViewFlags.delegateDidDoubleClick) {
NSIndexPath *indexPath = [self indexPathForCell:cell];
Expand Down

0 comments on commit dd1e21f

Please sign in to comment.