| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "chrome/browser/cocoa/draggable_button.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #import "base/scoped_nsobject.h" |
| 9 |
| 10 namespace { |
| 11 |
| 12 // Code taken from <http://codereview.chromium.org/180036/diff/3001/3004>. |
| 13 // TODO(viettrungluu): Do we want common, standard code for drag hysteresis? |
| 14 const CGFloat kWebDragStartHysteresisX = 5.0; |
| 15 const CGFloat kWebDragStartHysteresisY = 5.0; |
| 16 |
| 17 } |
| 18 |
| 19 @implementation DraggableButton |
| 20 |
| 21 @synthesize draggable = draggable_; |
| 22 |
| 23 - (id)initWithFrame:(NSRect)frame { |
| 24 if ((self = [super initWithFrame:frame])) { |
| 25 draggable_ = YES; |
| 26 } |
| 27 return self; |
| 28 } |
| 29 |
| 30 - (id)initWithCoder:(NSCoder*)coder { |
| 31 if ((self = [super initWithCoder:coder])) { |
| 32 draggable_ = YES; |
| 33 } |
| 34 return self; |
| 35 } |
| 36 |
| 37 - (void)mouseUp:(NSEvent*)theEvent { |
| 38 // Make sure that we can't start a drag until we see a mouse down again. |
| 39 mayDragStart_ = NO; |
| 40 |
| 41 // This conditional is never true (DnD loops in Cocoa eat the mouse |
| 42 // up) but I added it in case future versions of Cocoa do unexpected |
| 43 // things. |
| 44 if (beingDragged_) { |
| 45 NOTREACHED(); |
| 46 return [super mouseUp:theEvent]; |
| 47 } |
| 48 |
| 49 // There are non-drag cases where a mouseUp: may happen |
| 50 // (e.g. mouse-down, cmd-tab to another application, move mouse, |
| 51 // mouse-up). So we check. |
| 52 NSPoint viewLocal = [self convertPoint:[theEvent locationInWindow] |
| 53 fromView:[[self window] contentView]]; |
| 54 if (NSPointInRect(viewLocal, [self bounds])) { |
| 55 [self performClick:self]; |
| 56 } else { |
| 57 [self endDrag]; |
| 58 } |
| 59 } |
| 60 |
| 61 // Mimic "begin a click" operation visually. Do NOT follow through |
| 62 // with normal button event handling. |
| 63 - (void)mouseDown:(NSEvent*)theEvent { |
| 64 mayDragStart_ = YES; |
| 65 [[self cell] setHighlighted:YES]; |
| 66 initialMouseDownLocation_ = [theEvent locationInWindow]; |
| 67 } |
| 68 |
| 69 // Return YES if we have crossed a threshold of movement after |
| 70 // mouse-down when we should begin a drag. Else NO. |
| 71 - (BOOL)hasCrossedDragThreshold:(NSEvent*)theEvent { |
| 72 NSPoint currentLocation = [theEvent locationInWindow]; |
| 73 |
| 74 if ((abs(currentLocation.x - initialMouseDownLocation_.x) > |
| 75 kWebDragStartHysteresisX) || |
| 76 (abs(currentLocation.y - initialMouseDownLocation_.y) > |
| 77 kWebDragStartHysteresisY)) { |
| 78 return YES; |
| 79 } else { |
| 80 return NO; |
| 81 } |
| 82 } |
| 83 |
| 84 - (void)mouseDragged:(NSEvent*)theEvent { |
| 85 if (beingDragged_) { |
| 86 [super mouseDragged:theEvent]; |
| 87 } else if (draggable_ && mayDragStart_ && |
| 88 [self hasCrossedDragThreshold:theEvent]) { |
| 89 // Starting drag. Never start another drag until another mouse down. |
| 90 mayDragStart_ = NO; |
| 91 [self beginDrag:theEvent]; |
| 92 } |
| 93 } |
| 94 |
| 95 - (void)beginDrag:(NSEvent*)dragEvent { |
| 96 // Must be overridden by subclasses. |
| 97 NOTREACHED(); |
| 98 } |
| 99 |
| 100 - (void)endDrag { |
| 101 beingDragged_ = NO; |
| 102 [[self cell] setHighlighted:NO]; |
| 103 } |
| 104 |
| 105 @end |
| OLD | NEW |