Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1559)

Unified Diff: chrome/browser/cocoa/draggable_button.mm

Issue 180036: Make download items drag sources on OS X. (Closed)
Patch Set: comments Created 10 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/cocoa/draggable_button.h ('k') | chrome/browser/cocoa/draggable_button_unittest.mm » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/cocoa/draggable_button.mm
diff --git a/chrome/browser/cocoa/draggable_button.mm b/chrome/browser/cocoa/draggable_button.mm
new file mode 100644
index 0000000000000000000000000000000000000000..ff8270d7a9d4bde297edcb91838c7860630b6b12
--- /dev/null
+++ b/chrome/browser/cocoa/draggable_button.mm
@@ -0,0 +1,105 @@
+// 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/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;
+
+}
+
+@implementation DraggableButton
+
+@synthesize draggable = draggable_;
+
+- (id)initWithFrame:(NSRect)frame {
+ if ((self = [super initWithFrame:frame])) {
+ draggable_ = YES;
+ }
+ return self;
+}
+
+- (id)initWithCoder:(NSCoder*)coder {
+ if ((self = [super initWithCoder:coder])) {
+ draggable_ = YES;
+ }
+ return self;
+}
+
+- (void)mouseUp:(NSEvent*)theEvent {
+ // Make sure that we can't start a drag until we see a mouse down again.
+ mayDragStart_ = NO;
+
+ // This conditional is never true (DnD loops in Cocoa eat the mouse
+ // up) but I added it in case future versions of Cocoa do unexpected
+ // things.
+ if (beingDragged_) {
+ NOTREACHED();
+ return [super mouseUp:theEvent];
+ }
+
+ // 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];
+ } else {
+ [self endDrag];
+ }
+}
+
+// Mimic "begin a click" operation visually. Do NOT follow through
+// with normal button event handling.
+- (void)mouseDown:(NSEvent*)theEvent {
+ mayDragStart_ = YES;
+ [[self cell] setHighlighted:YES];
+ initialMouseDownLocation_ = [theEvent locationInWindow];
+}
+
+// Return YES if we have crossed a threshold of movement after
+// mouse-down when we should begin a drag. Else NO.
+- (BOOL)hasCrossedDragThreshold:(NSEvent*)theEvent {
+ NSPoint currentLocation = [theEvent locationInWindow];
+
+ if ((abs(currentLocation.x - initialMouseDownLocation_.x) >
+ kWebDragStartHysteresisX) ||
+ (abs(currentLocation.y - initialMouseDownLocation_.y) >
+ kWebDragStartHysteresisY)) {
+ return YES;
+ } else {
+ return NO;
+ }
+}
+
+- (void)mouseDragged:(NSEvent*)theEvent {
+ if (beingDragged_) {
+ [super mouseDragged:theEvent];
+ } else if (draggable_ && mayDragStart_ &&
+ [self hasCrossedDragThreshold:theEvent]) {
+ // Starting drag. Never start another drag until another mouse down.
+ mayDragStart_ = NO;
+ [self beginDrag:theEvent];
+ }
+}
+
+- (void)beginDrag:(NSEvent*)dragEvent {
+ // Must be overridden by subclasses.
+ NOTREACHED();
+}
+
+- (void)endDrag {
+ beingDragged_ = NO;
+ [[self cell] setHighlighted:NO];
+}
+
+@end
« no previous file with comments | « chrome/browser/cocoa/draggable_button.h ('k') | chrome/browser/cocoa/draggable_button_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698