Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h" | 5 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button.h" |
| 6 | 6 |
| 7 #include <complex> | |
|
Mark Mentovai
2011/08/11 00:51:17
<cmath> (see elsewhere for rationale)
| |
| 8 | |
| 7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 8 #import "base/memory/scoped_nsobject.h" | 10 #import "base/memory/scoped_nsobject.h" |
| 9 #include "chrome/browser/bookmarks/bookmark_model.h" | 11 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 10 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button_cell.h" | 12 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_button_cell.h" |
| 11 #import "chrome/browser/ui/cocoa/browser_window_controller.h" | 13 #import "chrome/browser/ui/cocoa/browser_window_controller.h" |
| 12 #import "chrome/browser/ui/cocoa/view_id_util.h" | 14 #import "chrome/browser/ui/cocoa/view_id_util.h" |
| 13 #include "content/browser/user_metrics.h" | 15 #include "content/browser/user_metrics.h" |
| 14 | 16 |
| 15 // The opacity of the bookmark button drag image. | 17 // The opacity of the bookmark button drag image. |
| 16 static const CGFloat kDragImageOpacity = 0.7; | 18 static const CGFloat kDragImageOpacity = 0.7; |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 115 | 117 |
| 116 return point; | 118 return point; |
| 117 } | 119 } |
| 118 | 120 |
| 119 | 121 |
| 120 - (void)updateTrackingAreas { | 122 - (void)updateTrackingAreas { |
| 121 [self installCustomTrackingArea]; | 123 [self installCustomTrackingArea]; |
| 122 [super updateTrackingAreas]; | 124 [super updateTrackingAreas]; |
| 123 } | 125 } |
| 124 | 126 |
| 125 - (BOOL)deltaIndicatesDragStartWithXDelta:(float)xDelta | 127 - (DraggableButtonResult)deltaIndicatesDragStartWithXDelta:(float)xDelta |
| 126 yDelta:(float)yDelta | 128 yDelta:(float)yDelta |
| 127 xHysteresis:(float)xHysteresis | 129 xHysteresis:(float)xHysteresis |
| 128 yHysteresis:(float)yHysteresis { | 130 yHysteresis:(float)yHysteresis |
| 131 indicates:(BOOL*)result { | |
| 129 const float kDownProportion = 1.4142135f; // Square root of 2. | 132 const float kDownProportion = 1.4142135f; // Square root of 2. |
| 130 | 133 |
| 131 // We want to show a folder menu when you drag down on folder buttons, | 134 // We want to show a folder menu when you drag down on folder buttons, |
| 132 // so don't classify this as a drag for that case. | 135 // so don't classify this as a drag for that case. |
| 133 if ([self isFolder] && | 136 if ([self isFolder] && |
| 134 (yDelta <= -yHysteresis) && // Bottom of hysteresis box was hit. | 137 (yDelta <= -yHysteresis) && // Bottom of hysteresis box was hit. |
| 135 (ABS(yDelta)/ABS(xDelta)) >= kDownProportion) | 138 (std::abs(yDelta) / std::abs(xDelta)) >= kDownProportion) { |
| 136 return NO; | 139 *result = NO; |
| 140 return kDraggableButtonMixinDidWork; | |
| 141 } | |
| 137 | 142 |
| 138 return [super deltaIndicatesDragStartWithXDelta:xDelta | 143 return kDraggableButtonImplUseBase; |
| 139 yDelta:yDelta | |
| 140 xHysteresis:xHysteresis | |
| 141 yHysteresis:yHysteresis]; | |
| 142 } | 144 } |
| 143 | 145 |
| 144 | 146 |
| 145 // By default, NSButton ignores middle-clicks. | 147 // By default, NSButton ignores middle-clicks. |
| 146 // But we want them. | 148 // But we want them. |
| 147 - (void)otherMouseUp:(NSEvent*)event { | 149 - (void)otherMouseUp:(NSEvent*)event { |
| 148 [self performClick:self]; | 150 [self performClick:self]; |
| 149 } | 151 } |
| 150 | 152 |
| 151 - (BOOL)acceptsTrackInFrom:(id)sender { | 153 - (BOOL)acceptsTrackInFrom:(id)sender { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 211 [self setHidden:NO]; | 213 [self setHidden:NO]; |
| 212 | 214 |
| 213 // And we're done. | 215 // And we're done. |
| 214 dragPending_ = NO; | 216 dragPending_ = NO; |
| 215 gDraggedButton = nil; | 217 gDraggedButton = nil; |
| 216 | 218 |
| 217 [self autorelease]; | 219 [self autorelease]; |
| 218 } | 220 } |
| 219 | 221 |
| 220 // Overridden to release bar visibility. | 222 // Overridden to release bar visibility. |
| 221 - (void)endDrag { | 223 - (DraggableButtonResult)endDrag { |
| 222 gDraggedButton = nil; | 224 gDraggedButton = nil; |
| 223 | 225 |
| 224 // visibilityDelegate_ can be nil if we're detached, and that's fine. | 226 // visibilityDelegate_ can be nil if we're detached, and that's fine. |
| 225 [visibilityDelegate_ releaseBarVisibilityForOwner:self | 227 [visibilityDelegate_ releaseBarVisibilityForOwner:self |
| 226 withAnimation:YES | 228 withAnimation:YES |
| 227 delay:YES]; | 229 delay:YES]; |
| 228 visibilityDelegate_ = nil; | 230 visibilityDelegate_ = nil; |
| 229 [super endDrag]; | 231 |
| 232 return kDraggableButtonImplUseBase; | |
|
Mark Mentovai
2011/08/11 00:51:17
In a sense, this is actually cleaner than what it
| |
| 230 } | 233 } |
| 231 | 234 |
| 232 - (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal { | 235 - (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal { |
| 233 NSDragOperation operation = NSDragOperationCopy; | 236 NSDragOperation operation = NSDragOperationCopy; |
| 234 if (isLocal) { | 237 if (isLocal) { |
| 235 operation |= NSDragOperationMove; | 238 operation |= NSDragOperationMove; |
| 236 } | 239 } |
| 237 if ([delegate_ canDragBookmarkButtonToTrash:self]) { | 240 if ([delegate_ canDragBookmarkButtonToTrash:self]) { |
| 238 operation |= NSDragOperationDelete; | 241 operation |= NSDragOperationDelete; |
| 239 } | 242 } |
| 240 return operation; | 243 return operation; |
| 241 } | 244 } |
| 242 | 245 |
| 243 - (void)draggedImage:(NSImage *)anImage | 246 - (void)draggedImage:(NSImage *)anImage |
| 244 endedAt:(NSPoint)aPoint | 247 endedAt:(NSPoint)aPoint |
| 245 operation:(NSDragOperation)operation { | 248 operation:(NSDragOperation)operation { |
| 246 gDraggedButton = nil; | 249 gDraggedButton = nil; |
| 247 // Inform delegate of drag source that we're finished dragging, | 250 // Inform delegate of drag source that we're finished dragging, |
| 248 // so it can close auto-opened bookmark folders etc. | 251 // so it can close auto-opened bookmark folders etc. |
| 249 [delegate_ bookmarkDragDidEnd:self | 252 [delegate_ bookmarkDragDidEnd:self |
| 250 operation:operation]; | 253 operation:operation]; |
| 251 // Tell delegate if it should delete us. | 254 // Tell delegate if it should delete us. |
| 252 if (operation & NSDragOperationDelete) { | 255 if (operation & NSDragOperationDelete) { |
| 253 dragEndScreenLocation_ = aPoint; | 256 dragEndScreenLocation_ = aPoint; |
| 254 [delegate_ didDragBookmarkToTrash:self]; | 257 [delegate_ didDragBookmarkToTrash:self]; |
| 255 } | 258 } |
| 256 } | 259 } |
| 257 | 260 |
| 258 - (void)performMouseDownAction:(NSEvent*)theEvent { | 261 - (DraggableButtonResult)performMouseDownAction:(NSEvent*)theEvent { |
| 259 [[self target] performSelector:[self action] withObject:self]; | 262 [[self target] performSelector:[self action] withObject:self]; |
| 260 self.actionHasFired = YES; | 263 self.draggableButton.actionHasFired = YES; |
| 264 return kDraggableButtonMixinDidWork; | |
| 261 } | 265 } |
| 262 | 266 |
| 263 // BookmarkButtonCell. We redirect this information to our delegate. | 267 // BookmarkButtonCell. We redirect this information to our delegate. |
| 264 // The controller can then perform menu-like actions (e.g. "hover over | 268 // The controller can then perform menu-like actions (e.g. "hover over |
| 265 // to open menu"). | 269 // to open menu"). |
| 266 - (void)mouseEntered:(NSEvent*)event { | 270 - (void)mouseEntered:(NSEvent*)event { |
| 267 [delegate_ mouseEnteredButton:self event:event]; | 271 [delegate_ mouseEnteredButton:self event:event]; |
| 268 } | 272 } |
| 269 | 273 |
| 270 // See comments above mouseEntered:. | 274 // See comments above mouseEntered:. |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 284 | 288 |
| 285 + (BookmarkButton*)draggedButton { | 289 + (BookmarkButton*)draggedButton { |
| 286 return gDraggedButton; | 290 return gDraggedButton; |
| 287 } | 291 } |
| 288 | 292 |
| 289 - (BOOL)canBecomeKeyView { | 293 - (BOOL)canBecomeKeyView { |
| 290 return NO; | 294 return NO; |
| 291 } | 295 } |
| 292 | 296 |
| 293 // This only gets called after a click that wasn't a drag, and only on folders. | 297 // This only gets called after a click that wasn't a drag, and only on folders. |
| 294 - (void)secondaryMouseUpAction:(BOOL)wasInside { | 298 - (DraggableButtonResult)secondaryMouseUpAction:(BOOL)wasInside { |
| 295 const NSTimeInterval kShortClickLength = 0.5; | 299 const NSTimeInterval kShortClickLength = 0.5; |
| 296 // Long clicks that end over the folder button result in the menu hiding. | 300 // Long clicks that end over the folder button result in the menu hiding. |
| 297 if (wasInside && ([self durationMouseWasDown] > kShortClickLength)) { | 301 if (wasInside && |
| 302 self.draggableButton.durationMouseWasDown > kShortClickLength) { | |
| 298 [[self target] performSelector:[self action] withObject:self]; | 303 [[self target] performSelector:[self action] withObject:self]; |
| 299 } else { | 304 } else { |
| 300 // Mouse tracked out of button during menu track. Hide menus. | 305 // Mouse tracked out of button during menu track. Hide menus. |
| 301 if (!wasInside) | 306 if (!wasInside) |
| 302 [delegate_ bookmarkDragDidEnd:self | 307 [delegate_ bookmarkDragDidEnd:self |
| 303 operation:NSDragOperationNone]; | 308 operation:NSDragOperationNone]; |
| 304 } | 309 } |
| 310 return kDraggableButtonMixinDidWork; | |
| 305 } | 311 } |
| 306 | 312 |
| 307 @end | 313 @end |
| 308 | 314 |
| 309 @implementation BookmarkButton(Private) | 315 @implementation BookmarkButton(Private) |
| 310 | 316 |
| 311 | 317 |
| 312 - (void)installCustomTrackingArea { | 318 - (void)installCustomTrackingArea { |
| 313 const NSTrackingAreaOptions options = | 319 const NSTrackingAreaOptions options = |
| 314 NSTrackingActiveAlways | | 320 NSTrackingActiveAlways | |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 355 [image drawAtPoint:NSMakePoint(0, 0) | 361 [image drawAtPoint:NSMakePoint(0, 0) |
| 356 fromRect:NSMakeRect(0, 0, NSWidth(bounds), NSHeight(bounds)) | 362 fromRect:NSMakeRect(0, 0, NSWidth(bounds), NSHeight(bounds)) |
| 357 operation:NSCompositeSourceOver | 363 operation:NSCompositeSourceOver |
| 358 fraction:kDragImageOpacity]; | 364 fraction:kDragImageOpacity]; |
| 359 | 365 |
| 360 [dragImage unlockFocus]; | 366 [dragImage unlockFocus]; |
| 361 return dragImage; | 367 return dragImage; |
| 362 } | 368 } |
| 363 | 369 |
| 364 @end // @implementation BookmarkButton(Private) | 370 @end // @implementation BookmarkButton(Private) |
| OLD | NEW |