| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009 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/clickhold_button_cell.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 |
| 9 // Minimum and maximum click-hold timeout. |
| 10 static const NSTimeInterval kMinTimeout = 0.01; |
| 11 static const NSTimeInterval kMaxTimeout = 3600.0; |
| 12 |
| 13 @implementation ClickHoldButtonCell |
| 14 |
| 15 // Overrides: |
| 16 |
| 17 + (BOOL)prefersTrackingUntilMouseUp { |
| 18 return NO; |
| 19 } |
| 20 |
| 21 - (BOOL)startTrackingAt:(NSPoint)startPoint |
| 22 inView:(NSView*)controlView { |
| 23 return enableClickHold_ ? |
| 24 YES : |
| 25 [super startTrackingAt:startPoint |
| 26 inView:controlView]; |
| 27 } |
| 28 |
| 29 - (BOOL)continueTracking:(NSPoint)lastPoint |
| 30 at:(NSPoint)currentPoint |
| 31 inView:(NSView*)controlView { |
| 32 return enableClickHold_ ? |
| 33 YES : |
| 34 [super continueTracking:lastPoint |
| 35 at:currentPoint |
| 36 inView:controlView]; |
| 37 } |
| 38 |
| 39 - (BOOL)trackMouse:(NSEvent*)originalEvent |
| 40 inRect:(NSRect)cellFrame |
| 41 ofView:(NSView*)controlView |
| 42 untilMouseUp:(BOOL)untilMouseUp { |
| 43 if (!enableClickHold_) { |
| 44 return [super trackMouse:originalEvent |
| 45 inRect:cellFrame |
| 46 ofView:controlView |
| 47 untilMouseUp:untilMouseUp]; |
| 48 } |
| 49 |
| 50 // If doing click-hold, track the mouse ourselves. |
| 51 NSPoint currPoint = [controlView convertPoint:[originalEvent locationInWindow] |
| 52 fromView:nil]; |
| 53 NSPoint lastPoint = currPoint; |
| 54 NSTimeInterval timeout = |
| 55 MAX(MIN(clickHoldTimeout_, kMaxTimeout), kMinTimeout); |
| 56 NSDate* clickHoldBailTime = [NSDate dateWithTimeIntervalSinceNow:timeout]; |
| 57 |
| 58 if (![self startTrackingAt:currPoint inView:controlView]) |
| 59 return NO; |
| 60 |
| 61 enum { |
| 62 kContinueTrack, kStopClickHold, kStopMouseUp, kStopLeftRect, kStopNoContinue |
| 63 } state = kContinueTrack; |
| 64 do { |
| 65 NSEvent* event = [NSApp nextEventMatchingMask:(NSLeftMouseDraggedMask | |
| 66 NSLeftMouseUpMask) |
| 67 untilDate:clickHoldBailTime |
| 68 inMode:NSEventTrackingRunLoopMode |
| 69 dequeue:YES]; |
| 70 currPoint = [controlView convertPoint:[event locationInWindow] |
| 71 fromView:nil]; |
| 72 |
| 73 // Time-out or drag. |
| 74 if (!event || (activateOnDrag_ && ([event type] == NSLeftMouseDragged))) { |
| 75 state = kStopClickHold; |
| 76 |
| 77 // Mouse up. |
| 78 } else if ([event type] == NSLeftMouseUp) { |
| 79 state = kStopMouseUp; |
| 80 |
| 81 // Stop tracking if mouse left frame rectangle (if requested to do so). |
| 82 } else if (trackOnlyInRect_ && ![controlView mouse:currPoint |
| 83 inRect:cellFrame]) { |
| 84 state = kStopLeftRect; |
| 85 |
| 86 // Stop tracking if instructed to. |
| 87 } else if (![self continueTracking:lastPoint |
| 88 at:currPoint |
| 89 inView:controlView]) { |
| 90 state = kStopNoContinue; |
| 91 } |
| 92 |
| 93 lastPoint = currPoint; |
| 94 } while (state == kContinueTrack); |
| 95 |
| 96 [self stopTracking:lastPoint |
| 97 at:lastPoint |
| 98 inView:controlView |
| 99 mouseIsUp:NO]; |
| 100 |
| 101 switch (state) { |
| 102 case kStopClickHold: |
| 103 if (clickHoldAction_) { |
| 104 [static_cast<NSControl*>(controlView) sendAction:clickHoldAction_ |
| 105 to:clickHoldTarget_]; |
| 106 } |
| 107 return YES; |
| 108 |
| 109 case kStopMouseUp: |
| 110 if ([self action]) { |
| 111 [static_cast<NSControl*>(controlView) sendAction:[self action] |
| 112 to:[self target]]; |
| 113 } |
| 114 return YES; |
| 115 |
| 116 case kStopLeftRect: |
| 117 case kStopNoContinue: |
| 118 return NO; |
| 119 |
| 120 default: |
| 121 NOTREACHED() << "Unknown terminating state!"; |
| 122 } |
| 123 |
| 124 return NO; |
| 125 } |
| 126 |
| 127 // Accessors and mutators: |
| 128 |
| 129 @synthesize enableClickHold = enableClickHold_; |
| 130 @synthesize clickHoldTimeout = clickHoldTimeout_; |
| 131 @synthesize trackOnlyInRect = trackOnlyInRect_; |
| 132 @synthesize activateOnDrag = activateOnDrag_; |
| 133 @synthesize clickHoldTarget = clickHoldTarget_; |
| 134 @synthesize clickHoldAction = clickHoldAction_; |
| 135 |
| 136 @end // @implementation ClickHoldButtonCell |
| OLD | NEW |