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
|
// Copyright (c) 2009 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 "chrome/browser/ui/cocoa/clickhold_button_cell.h"
#include "base/logging.h"
// Minimum and maximum click-hold timeout.
static const NSTimeInterval kMinTimeout = 0.0;
static const NSTimeInterval kMaxTimeout = 5.0;
// Drag distance threshold to activate click-hold; should be >= 0.
static const CGFloat kDragDistThreshold = 2.5;
// See |-resetToDefaults| (and header file) for other default values.
@interface ClickHoldButtonCell (Private)
- (void)resetToDefaults;
- (BOOL)shouldExposeAccessibilityShowMenu;
@end // @interface ClickHoldButtonCell (Private)
@implementation ClickHoldButtonCell
@synthesize enableClickHold = enableClickHold_;
@synthesize enableRightClick = enableRightClick_;
@synthesize clickHoldTimeout = clickHoldTimeout_;
@synthesize trackOnlyInRect = trackOnlyInRect_;
@synthesize activateOnDrag = activateOnDrag_;
@synthesize clickHoldTarget = clickHoldTarget_;
@synthesize clickHoldAction = clickHoldAction_;
@synthesize accessibilityShowMenuTarget = accessibilityShowMenuTarget_;
@synthesize accessibilityShowMenuAction = accessibilityShowMenuAction_;
// Overrides:
+ (BOOL)prefersTrackingUntilMouseUp {
return NO;
}
- (id)init {
if ((self = [super init]))
[self resetToDefaults];
return self;
}
- (id)initWithCoder:(NSCoder*)decoder {
if ((self = [super initWithCoder:decoder]))
[self resetToDefaults];
return self;
}
- (id)initImageCell:(NSImage*)image {
if ((self = [super initImageCell:image]))
[self resetToDefaults];
return self;
}
- (id)initTextCell:(NSString*)string {
if ((self = [super initTextCell:string]))
[self resetToDefaults];
return self;
}
- (void)accessibilityPerformAction:(NSString*)action {
if ([action isEqualToString:NSAccessibilityShowMenuAction] &&
[self shouldExposeAccessibilityShowMenu]) {
NSControl* controlView = static_cast<NSControl*>([self controlView]);
if (controlView)
[controlView sendAction:accessibilityShowMenuAction_
to:accessibilityShowMenuTarget_];
return;
}
[super accessibilityPerformAction:action];
}
- (BOOL)startTrackingAt:(NSPoint)startPoint
inView:(NSView*)controlView {
return enableClickHold_ ? YES :
[super startTrackingAt:startPoint
inView:controlView];
}
- (BOOL)continueTracking:(NSPoint)lastPoint
at:(NSPoint)currentPoint
inView:(NSView*)controlView {
return enableClickHold_ ? YES :
[super continueTracking:lastPoint
at:currentPoint
inView:controlView];
}
- (BOOL)trackMouse:(NSEvent*)originalEvent
inRect:(NSRect)cellFrame
ofView:(NSView*)controlView
untilMouseUp:(BOOL)untilMouseUp {
if (!enableClickHold_) {
return [super trackMouse:originalEvent
inRect:cellFrame
ofView:controlView
untilMouseUp:untilMouseUp];
}
// If doing click-hold, track the mouse ourselves.
NSPoint currPoint = [controlView convertPoint:[originalEvent locationInWindow]
fromView:nil];
NSPoint lastPoint = currPoint;
NSPoint firstPoint = currPoint;
NSTimeInterval timeout =
MAX(MIN(clickHoldTimeout_, kMaxTimeout), kMinTimeout);
NSDate* clickHoldBailTime = [NSDate dateWithTimeIntervalSinceNow:timeout];
if (![self startTrackingAt:currPoint inView:controlView])
return NO;
enum {
kContinueTrack, kStopClickHold, kStopMouseUp, kStopLeftRect, kStopNoContinue
} state = kContinueTrack;
do {
NSEvent* event = [NSApp nextEventMatchingMask:(NSLeftMouseDraggedMask |
NSLeftMouseUpMask)
untilDate:clickHoldBailTime
inMode:NSEventTrackingRunLoopMode
dequeue:YES];
currPoint = [controlView convertPoint:[event locationInWindow]
fromView:nil];
// Time-out.
if (!event) {
state = kStopClickHold;
// Drag? (If distance meets threshold.)
} else if (activateOnDrag_ && ([event type] == NSLeftMouseDragged)) {
CGFloat dx = currPoint.x - firstPoint.x;
CGFloat dy = currPoint.y - firstPoint.y;
if ((dx*dx + dy*dy) >= (kDragDistThreshold*kDragDistThreshold))
state = kStopClickHold;
// Mouse up.
} else if ([event type] == NSLeftMouseUp) {
state = kStopMouseUp;
// Stop tracking if mouse left frame rectangle (if requested to do so).
} else if (trackOnlyInRect_ && ![controlView mouse:currPoint
inRect:cellFrame]) {
state = kStopLeftRect;
// Stop tracking if instructed to.
} else if (![self continueTracking:lastPoint
at:currPoint
inView:controlView]) {
state = kStopNoContinue;
}
lastPoint = currPoint;
} while (state == kContinueTrack);
[self stopTracking:lastPoint
at:lastPoint
inView:controlView
mouseIsUp:NO];
switch (state) {
case kStopClickHold:
if (clickHoldAction_) {
[static_cast<NSControl*>(controlView) sendAction:clickHoldAction_
to:clickHoldTarget_];
}
return YES;
case kStopMouseUp:
if ([self action]) {
[static_cast<NSControl*>(controlView) sendAction:[self action]
to:[self target]];
}
return YES;
case kStopLeftRect:
case kStopNoContinue:
return NO;
default:
NOTREACHED() << "Unknown terminating state!";
}
return NO;
}
// Accessors and mutators:
- (NSArray*)accessibilityActionNames {
NSArray* actionNames = [super accessibilityActionNames];
if ([self shouldExposeAccessibilityShowMenu] &&
![actionNames containsObject:NSAccessibilityShowMenuAction]) {
return [actionNames arrayByAddingObject:NSAccessibilityShowMenuAction];
}
return actionNames;
}
@end // @implementation ClickHoldButtonCell
@implementation ClickHoldButtonCell (Private)
// Resets various members to defaults indicated in the header file. (Those
// without indicated defaults are *not* touched.) Please keep the values below
// in sync with the header file, and please be aware of side-effects on code
// which relies on the "published" defaults.
- (void)resetToDefaults {
[self setEnableClickHold:NO];
[self setClickHoldTimeout:0.25];
[self setTrackOnlyInRect:NO];
[self setActivateOnDrag:YES];
}
- (BOOL)shouldExposeAccessibilityShowMenu {
return (enableRightClick_ ||
(enableClickHold_ && clickHoldTimeout_ > kMinTimeout)) &&
accessibilityShowMenuAction_ && accessibilityShowMenuTarget_;
}
@end // @implementation ClickHoldButtonCell (Private)
|