1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ui/app_list/cocoa/apps_collection_view_drag_manager.h"
#include "base/logging.h"
#include "base/mac/foundation_util.h"
#import "ui/app_list/cocoa/apps_grid_controller.h"
#import "ui/app_list/cocoa/apps_grid_view_item.h"
#import "ui/app_list/cocoa/item_drag_controller.h"
namespace {
// Distance cursor must travel in either x or y direction to start a drag.
const CGFloat kDragThreshold = 5;
} // namespace
@interface AppsCollectionViewDragManager ()
// Returns the item index that |theEvent| would hit in the page at |pageIndex|
// or NSNotFound if no item is hit.
- (size_t)itemIndexForPage:(size_t)pageIndex
hitWithEvent:(NSEvent*)theEvent;
- (void)initiateDrag:(NSEvent*)theEvent;
- (void)updateDrag:(NSEvent*)theEvent;
- (void)completeDrag;
- (NSMenu*)menuForEvent:(NSEvent*)theEvent
inPage:(NSCollectionView*)page;
@end
// An NSCollectionView that forwards mouse events to the factory they share.
@interface GridCollectionView : NSCollectionView {
@private
AppsCollectionViewDragManager* factory_;
}
@property(assign, nonatomic) AppsCollectionViewDragManager* factory;
@end
@implementation AppsCollectionViewDragManager
- (id)initWithCellSize:(NSSize)cellSize
rows:(size_t)rows
columns:(size_t)columns
gridController:(AppsGridController*)gridController {
if ((self = [super init])) {
cellSize_ = cellSize;
rows_ = rows;
columns_ = columns;
gridController_ = gridController;
}
return self;
}
- (NSCollectionView*)makePageWithFrame:(NSRect)pageFrame {
base::scoped_nsobject<GridCollectionView> itemCollectionView(
[[GridCollectionView alloc] initWithFrame:pageFrame]);
[itemCollectionView setFactory:self];
[itemCollectionView setMaxNumberOfRows:rows_];
[itemCollectionView setMinItemSize:cellSize_];
[itemCollectionView setMaxItemSize:cellSize_];
[itemCollectionView setSelectable:YES];
[itemCollectionView setFocusRingType:NSFocusRingTypeNone];
[itemCollectionView setBackgroundColors:
[NSArray arrayWithObject:[NSColor clearColor]]];
[itemCollectionView setDelegate:gridController_];
base::scoped_nsobject<AppsGridViewItem> itemPrototype(
[[AppsGridViewItem alloc] initWithSize:cellSize_]);
[[itemPrototype button] setTarget:gridController_];
[[itemPrototype button] setAction:@selector(onItemClicked:)];
[itemCollectionView setItemPrototype:itemPrototype];
return itemCollectionView.autorelease();
}
- (void)onMouseDownInPage:(NSCollectionView*)page
withEvent:(NSEvent*)theEvent {
size_t pageIndex = [gridController_ pageIndexForCollectionView:page];
itemHitIndex_ = [self itemIndexForPage:pageIndex
hitWithEvent:theEvent];
if (itemHitIndex_ == NSNotFound)
return;
mouseDownLocation_ = [theEvent locationInWindow];
[[[gridController_ itemAtIndex:itemHitIndex_] view] mouseDown:theEvent];
}
- (void)onMouseDragged:(NSEvent*)theEvent {
if (itemHitIndex_ == NSNotFound)
return;
if (dragging_) {
[self updateDrag:theEvent];
return;
}
NSPoint mouseLocation = [theEvent locationInWindow];
CGFloat deltaX = mouseLocation.x - mouseDownLocation_.x;
CGFloat deltaY = mouseLocation.y - mouseDownLocation_.y;
if (deltaX * deltaX + deltaY * deltaY > kDragThreshold * kDragThreshold) {
[self initiateDrag:theEvent];
return;
}
[[[gridController_ itemAtIndex:itemHitIndex_] view] mouseDragged:theEvent];
}
- (void)onMouseUp:(NSEvent*)theEvent {
if (itemHitIndex_ == NSNotFound)
return;
if (dragging_) {
[self completeDrag];
return;
}
[[[gridController_ itemAtIndex:itemHitIndex_] view] mouseUp:theEvent];
}
- (size_t)itemIndexForPage:(size_t)pageIndex
hitWithEvent:(NSEvent*)theEvent {
NSCollectionView* page =
[gridController_ collectionViewAtPageIndex:pageIndex];
NSPoint pointInView = [page convertPoint:[theEvent locationInWindow]
fromView:nil];
const size_t itemsInPage = [[page content] count];
for (size_t indexInPage = 0; indexInPage < itemsInPage; ++indexInPage) {
if ([page mouse:pointInView
inRect:[page frameForItemAtIndex:indexInPage]]) {
return indexInPage + pageIndex * rows_ * columns_;
}
}
return NSNotFound;
}
- (void)initiateDrag:(NSEvent*)theEvent {
DCHECK_LT(itemHitIndex_, [gridController_ itemCount]);
dragging_ = YES;
if (!itemDragController_) {
itemDragController_.reset(
[[ItemDragController alloc] initWithGridCellSize:cellSize_]);
[[[gridController_ view] superview] addSubview:[itemDragController_ view]];
}
[itemDragController_ initiate:[gridController_ itemAtIndex:itemHitIndex_]
mouseDownLocation:mouseDownLocation_
currentLocation:[theEvent locationInWindow]
timestamp:[theEvent timestamp]];
itemDragIndex_ = itemHitIndex_;
[self updateDrag:theEvent];
}
- (void)updateDrag:(NSEvent*)theEvent {
[itemDragController_ update:[theEvent locationInWindow]
timestamp:[theEvent timestamp]];
[gridController_ maybeChangePageForPoint:[theEvent locationInWindow]];
size_t visiblePage = [gridController_ visiblePage];
size_t itemIndexOver = [self itemIndexForPage:visiblePage
hitWithEvent:theEvent];
DCHECK_NE(0u, [gridController_ itemCount]);
if (itemIndexOver == NSNotFound) {
if (visiblePage != [gridController_ pageCount] - 1)
return;
// If nothing was hit, but the last page is shown, move to the end.
itemIndexOver = [gridController_ itemCount] - 1;
}
if (itemDragIndex_ == itemIndexOver)
return;
[gridController_ moveItemInView:itemDragIndex_
toItemIndex:itemIndexOver];
// A new item may be created when moving between pages. Ensure it is hidden.
[[[gridController_ itemAtIndex:itemIndexOver] button] setHidden:YES];
itemDragIndex_ = itemIndexOver;
}
- (void)cancelDrag {
if (!dragging_)
return;
[gridController_ moveItemInView:itemDragIndex_
toItemIndex:itemHitIndex_];
itemDragIndex_ = itemHitIndex_;
[self completeDrag];
itemHitIndex_ = NSNotFound; // Ignore future mouse events for this drag.
}
- (void)completeDrag {
DCHECK_GE(itemDragIndex_, 0u);
[gridController_ cancelScrollTimer];
AppsGridViewItem* item = [gridController_ itemAtIndex:itemDragIndex_];
// The item could still be animating in the NSCollectionView, so ask it where
// it would place the item.
NSCollectionView* pageView = base::mac::ObjCCastStrict<NSCollectionView>(
[[item view] superview]);
size_t indexInPage = itemDragIndex_ % (rows_ * columns_);
NSPoint targetOrigin = [[[itemDragController_ view] superview]
convertPoint:[pageView frameForItemAtIndex:indexInPage].origin
fromView:pageView];
[itemDragController_ complete:item
targetOrigin:targetOrigin];
[gridController_ moveItemWithIndex:itemHitIndex_
toModelIndex:itemDragIndex_];
dragging_ = NO;
}
- (NSMenu*)menuForEvent:(NSEvent*)theEvent
inPage:(NSCollectionView*)page {
size_t pageIndex = [gridController_ pageIndexForCollectionView:page];
size_t itemIndex = [self itemIndexForPage:pageIndex
hitWithEvent:theEvent];
if (itemIndex == NSNotFound)
return nil;
return [[gridController_ itemAtIndex:itemIndex] contextMenu];
}
@end
@implementation GridCollectionView
@synthesize factory = factory_;
- (NSMenu*)menuForEvent:(NSEvent*)theEvent {
return [factory_ menuForEvent:theEvent
inPage:self];
}
- (void)mouseDown:(NSEvent*)theEvent {
[factory_ onMouseDownInPage:self
withEvent:theEvent];
}
- (void)mouseDragged:(NSEvent*)theEvent {
[factory_ onMouseDragged:theEvent];
}
- (void)mouseUp:(NSEvent*)theEvent {
[factory_ onMouseUp:theEvent];
}
- (BOOL)acceptsFirstResponder {
return NO;
}
@end
|