blob: 654ae25e8fe702f4140a9b4859e914ad36780b0e (
plain)
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
|
// Copyright (c) 2010 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/draggable_button.h"
#include "base/logging.h"
#import "base/scoped_nsobject.h"
namespace {
// Code taken from <http://codereview.chromium.org/180036/diff/3001/3004>.
// TODO(viettrungluu): Do we want common, standard code for drag hysteresis?
const CGFloat kWebDragStartHysteresisX = 5.0;
const CGFloat kWebDragStartHysteresisY = 5.0;
const CGFloat kDragExpirationTimeout = 1.0;
}
@implementation DraggableButton
@synthesize draggable = draggable_;
@synthesize actsOnMouseDown = actsOnMouseDown_;
@synthesize durationMouseWasDown = durationMouseWasDown_;
- (id)initWithFrame:(NSRect)frame {
if ((self = [super initWithFrame:frame])) {
draggable_ = YES;
actsOnMouseDown_ = NO;
actionHasFired_ = NO;
}
return self;
}
- (id)initWithCoder:(NSCoder*)coder {
if ((self = [super initWithCoder:coder])) {
draggable_ = YES;
actsOnMouseDown_ = NO;
actionHasFired_ = NO;
}
return self;
}
- (BOOL)deltaIndicatesDragStartWithXDelta:(float)xDelta
yDelta:(float)yDelta
xHysteresis:(float)xHysteresis
yHysteresis:(float)yHysteresis {
return (ABS(xDelta) >= xHysteresis) || (ABS(yDelta) >= yHysteresis);
}
- (BOOL)deltaIndicatesConclusionReachedWithXDelta:(float)xDelta
yDelta:(float)yDelta
xHysteresis:(float)xHysteresis
yHysteresis:(float)yHysteresis {
return (ABS(xDelta) >= xHysteresis) || (ABS(yDelta) >= yHysteresis);
}
// Determine whether a mouse down should turn into a drag; started as copy of
// NSTableView code.
- (BOOL)dragShouldBeginFromMouseDown:(NSEvent*)mouseDownEvent
withExpiration:(NSDate*)expiration
xHysteresis:(float)xHysteresis
yHysteresis:(float)yHysteresis {
if ([mouseDownEvent type] != NSLeftMouseDown) {
return NO;
}
NSEvent* nextEvent = nil;
NSEvent* firstEvent = nil;
NSEvent* dragEvent = nil;
NSEvent* mouseUp = nil;
BOOL dragIt = NO;
while ((nextEvent = [[self window]
nextEventMatchingMask:(NSLeftMouseUpMask | NSLeftMouseDraggedMask)
untilDate:expiration
inMode:NSEventTrackingRunLoopMode
dequeue:YES]) != nil) {
if (firstEvent == nil) {
firstEvent = nextEvent;
}
if ([nextEvent type] == NSLeftMouseDragged) {
float deltax = [nextEvent locationInWindow].x -
[mouseDownEvent locationInWindow].x;
float deltay = [nextEvent locationInWindow].y -
[mouseDownEvent locationInWindow].y;
dragEvent = nextEvent;
if ([self deltaIndicatesConclusionReachedWithXDelta:deltax
yDelta:deltay
xHysteresis:xHysteresis
yHysteresis:yHysteresis]) {
dragIt = [self deltaIndicatesDragStartWithXDelta:deltax
yDelta:deltay
xHysteresis:xHysteresis
yHysteresis:yHysteresis];
break;
}
} else if ([nextEvent type] == NSLeftMouseUp) {
mouseUp = nextEvent;
break;
}
}
// Since we've been dequeuing the events (If we don't, we'll never see
// the mouse up...), we need to push some of the events back on.
// It makes sense to put the first and last drag events and the mouse
// up if there was one.
if (mouseUp != nil) {
[NSApp postEvent:mouseUp atStart:YES];
}
if (dragEvent != nil) {
[NSApp postEvent:dragEvent atStart:YES];
}
if (firstEvent != mouseUp && firstEvent != dragEvent) {
[NSApp postEvent:firstEvent atStart:YES];
}
return dragIt;
}
- (BOOL)dragShouldBeginFromMouseDown:(NSEvent*)mouseDownEvent
withExpiration:(NSDate*)expiration {
return [self dragShouldBeginFromMouseDown:mouseDownEvent
withExpiration:expiration
xHysteresis:kWebDragStartHysteresisX
yHysteresis:kWebDragStartHysteresisY];
}
- (void)mouseUp:(NSEvent*)theEvent {
durationMouseWasDown_ = [theEvent timestamp] - whenMouseDown_;
if (actionHasFired_)
return;
if (!draggable_) {
[super mouseUp:theEvent];
return;
}
// There are non-drag cases where a mouseUp: may happen
// (e.g. mouse-down, cmd-tab to another application, move mouse,
// mouse-up). So we check.
NSPoint viewLocal = [self convertPoint:[theEvent locationInWindow]
fromView:[[self window] contentView]];
if (NSPointInRect(viewLocal, [self bounds])) {
[self performClick:self];
}
}
- (void)secondaryMouseUpAction:(BOOL)wasInside {
// Override if you want to do any extra work on mouseUp, after a mouseDown
// action has already fired.
}
- (BOOL)acceptsTrackInFrom:(id)sender {
return (sender == self);
}
- (void)performMouseDownAction:(NSEvent*)theEvent {
int eventMask = NSLeftMouseUpMask | NSMouseEnteredMask | NSMouseExitedMask;
[[self target] performSelector:[self action] withObject:self];
actionHasFired_ = YES;
DraggableButton* insideBtn = nil;
while (1) {
theEvent = [[self window] nextEventMatchingMask:eventMask];
NSPoint mouseLoc = [self convertPoint:[theEvent locationInWindow]
fromView:nil];
BOOL isInside = [self mouse:mouseLoc inRect:[self bounds]];
switch ([theEvent type]) {
case NSMouseEntered:
case NSMouseExited: {
NSView* trackedView = (NSView*)[[theEvent trackingArea] owner];
if (trackedView && [trackedView isKindOfClass:[self class]]) {
DraggableButton *btn = static_cast<DraggableButton*>(trackedView);
if (![btn acceptsTrackInFrom:self])
break;
if ([theEvent type] == NSMouseEntered) {
[[NSCursor arrowCursor] set];
[[btn cell] mouseEntered:theEvent];
insideBtn = btn;
} else {
[[btn cell] mouseExited:theEvent];
if (insideBtn == btn)
insideBtn = nil;
}
}
break;
}
case NSLeftMouseUp: {
if (!isInside && insideBtn && insideBtn != self) {
// Has tracked onto another DraggableButton menu item, and released,
// so click it.
[insideBtn performClick:self];
}
durationMouseWasDown_ = [theEvent timestamp] - whenMouseDown_;
[self secondaryMouseUpAction:isInside];
[[self cell] mouseExited:theEvent];
[[insideBtn cell] mouseExited:theEvent];
return;
break;
}
default:
/* Ignore any other kind of event. */
break;
}
}
}
// Mimic "begin a click" operation visually. Do NOT follow through
// with normal button event handling.
- (void)mouseDown:(NSEvent*)theEvent {
[[NSCursor arrowCursor] set];
whenMouseDown_ = [theEvent timestamp];
actionHasFired_ = NO;
if (draggable_) {
NSDate* date = [NSDate dateWithTimeIntervalSinceNow:kDragExpirationTimeout];
if ([self dragShouldBeginFromMouseDown:theEvent
withExpiration:date]) {
[self beginDrag:theEvent];
[self endDrag];
} else {
if (actsOnMouseDown_) {
[self performMouseDownAction:theEvent];
} else {
[super mouseDown:theEvent];
}
}
} else {
if (actsOnMouseDown_) {
[self performMouseDownAction:theEvent];
} else {
[super mouseDown:theEvent];
}
}
}
- (void)beginDrag:(NSEvent*)dragEvent {
// Must be overridden by subclasses.
NOTREACHED();
}
- (void)endDrag {
[self highlight:NO];
}
@end // @interface DraggableButton
|