Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "chrome/browser/ui/panels/panel_titlebar_view_cocoa.h" | 5 #import "chrome/browser/ui/panels/panel_titlebar_view_cocoa.h" |
| 6 | 6 |
| 7 #include <Carbon/Carbon.h> // kVK_Escape | 7 #include <Carbon/Carbon.h> // kVK_Escape |
| 8 #import <Cocoa/Cocoa.h> | 8 #import <Cocoa/Cocoa.h> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/mac/scoped_nsautorelease_pool.h" | 11 #include "base/mac/scoped_nsautorelease_pool.h" |
| 12 #include "chrome/browser/themes/theme_service.h" | 12 #include "chrome/browser/themes/theme_service.h" |
| 13 #import "chrome/browser/ui/cocoa/hover_image_button.h" | 13 #import "chrome/browser/ui/cocoa/hover_image_button.h" |
| 14 #import "chrome/browser/ui/cocoa/nsview_additions.h" | 14 #import "chrome/browser/ui/cocoa/nsview_additions.h" |
| 15 #import "chrome/browser/ui/cocoa/themed_window.h" | 15 #import "chrome/browser/ui/cocoa/themed_window.h" |
| 16 #import "chrome/browser/ui/cocoa/tracking_area.h" | 16 #import "chrome/browser/ui/cocoa/tracking_area.h" |
| 17 #import "chrome/browser/ui/panels/panel_window_controller_cocoa.h" | 17 #import "chrome/browser/ui/panels/panel_window_controller_cocoa.h" |
| 18 #include "grit/theme_resources_standard.h" | 18 #include "grit/theme_resources_standard.h" |
| 19 #import "third_party/GTM/AppKit/GTMNSBezierPath+RoundRect.h" | 19 #import "third_party/GTM/AppKit/GTMNSBezierPath+RoundRect.h" |
| 20 #include "ui/gfx/mac/nsimage_cache.h" | 20 #include "ui/gfx/mac/nsimage_cache.h" |
| 21 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" | 21 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h" |
| 22 | 22 |
| 23 const int kRoundedCornerSize = 3; | 23 const int kRoundedCornerSize = 3; |
| 24 const int kButtonPadding = 8; | 24 const int kButtonPadding = 8; |
| 25 const int kIconAndTextPadding = 5; | 25 const int kIconAndTextPadding = 5; |
| 26 | 26 |
| 27 // Distance that user needs to move the mouse in order to start the drag. | |
| 28 // Threshold is needed to differentiate drags from attempts to click the | |
| 29 // titlebar with a twitch of the mouse pointer. | |
| 30 const int kDragThreshold = 3; | |
| 31 | |
| 27 // Used to implement TestingAPI | 32 // Used to implement TestingAPI |
| 28 static NSEvent* MakeMouseEvent(NSEventType type, | 33 static NSEvent* MakeMouseEvent(NSEventType type, |
| 29 NSPoint point, | 34 NSPoint point, |
| 30 int clickCount) { | 35 int clickCount) { |
| 31 return [NSEvent mouseEventWithType:type | 36 return [NSEvent mouseEventWithType:type |
| 32 location:point | 37 location:point |
| 33 modifierFlags:0 | 38 modifierFlags:0 |
| 34 timestamp:0 | 39 timestamp:0 |
| 35 windowNumber:0 | 40 windowNumber:0 |
| 36 context:nil | 41 context:nil |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 310 [self setNeedsDisplay:YES]; | 315 [self setNeedsDisplay:YES]; |
| 311 } | 316 } |
| 312 | 317 |
| 313 - (void)didChangeMainWindow:(NSNotification*)notification { | 318 - (void)didChangeMainWindow:(NSNotification*)notification { |
| 314 [self setNeedsDisplay:YES]; | 319 [self setNeedsDisplay:YES]; |
| 315 [self checkMouseAndUpdateSettingsButtonVisibility]; | 320 [self checkMouseAndUpdateSettingsButtonVisibility]; |
| 316 } | 321 } |
| 317 | 322 |
| 318 - (void)mouseDown:(NSEvent*)event { | 323 - (void)mouseDown:(NSEvent*)event { |
| 319 dragState_ = PANEL_DRAG_CAN_START; | 324 dragState_ = PANEL_DRAG_CAN_START; |
| 325 dragStartLocation_ = [event locationInWindow]; | |
| 320 } | 326 } |
| 321 | 327 |
| 322 - (void)mouseUp:(NSEvent*)event { | 328 - (void)mouseUp:(NSEvent*)event { |
| 323 DCHECK(dragState_ != PANEL_DRAG_IN_PROGRESS); | 329 DCHECK(dragState_ != PANEL_DRAG_IN_PROGRESS); |
| 324 | 330 |
| 325 if ([event clickCount] == 1) | 331 if ([event clickCount] == 1) |
| 326 [controller_ onTitlebarMouseClicked]; | 332 [controller_ onTitlebarMouseClicked]; |
| 327 } | 333 } |
| 328 | 334 |
| 335 - (BOOL)exceedsDragThreshold:(NSPoint)mouseLocation { | |
| 336 float deltaX = dragStartLocation_.x - mouseLocation.x; | |
| 337 float deltaY = dragStartLocation_.y - mouseLocation.y; | |
| 338 return deltaX > kDragThreshold || deltaY > kDragThreshold; | |
|
jennb
2011/11/18 00:36:55
So much simpler! Shaves a few cycles, too. :-D
| |
| 339 } | |
| 340 | |
| 329 - (void)mouseDragged:(NSEvent*)event { | 341 - (void)mouseDragged:(NSEvent*)event { |
| 330 // In addition to events needed to control the drag operation, fetch the right | 342 // In addition to events needed to control the drag operation, fetch the right |
| 331 // mouse click events and key down events and ignore them, to prevent their | 343 // mouse click events and key down events and ignore them, to prevent their |
| 332 // accumulation in the queue and "playing out" when the mouse is released. | 344 // accumulation in the queue and "playing out" when the mouse is released. |
| 333 const NSUInteger mask = | 345 const NSUInteger mask = |
| 334 NSLeftMouseUpMask | NSLeftMouseDraggedMask | NSKeyUpMask | | 346 NSLeftMouseUpMask | NSLeftMouseDraggedMask | NSKeyUpMask | |
| 335 NSRightMouseDownMask | NSKeyDownMask ; | 347 NSRightMouseDownMask | NSKeyDownMask ; |
| 336 BOOL keepGoing = YES; | 348 BOOL keepGoing = YES; |
| 337 | 349 |
| 338 while (keepGoing) { | 350 while (keepGoing) { |
| 339 base::mac::ScopedNSAutoreleasePool autorelease_pool; | 351 base::mac::ScopedNSAutoreleasePool autorelease_pool; |
| 340 | 352 |
| 341 NSEvent* event = [NSApp nextEventMatchingMask:mask | 353 NSEvent* event = [NSApp nextEventMatchingMask:mask |
| 342 untilDate:[NSDate distantFuture] | 354 untilDate:[NSDate distantFuture] |
| 343 inMode:NSDefaultRunLoopMode | 355 inMode:NSDefaultRunLoopMode |
| 344 dequeue:YES]; | 356 dequeue:YES]; |
| 345 | 357 |
| 346 switch ([event type]) { | 358 switch ([event type]) { |
| 347 case NSLeftMouseDragged: | 359 case NSLeftMouseDragged: |
| 348 if (dragState_ == PANEL_DRAG_CAN_START) | 360 if (dragState_ == PANEL_DRAG_CAN_START) { |
| 361 if (![self exceedsDragThreshold:[event locationInWindow]]) | |
| 362 return; // Don't start real drag yet. | |
| 349 [self startDrag]; | 363 [self startDrag]; |
| 364 } | |
| 350 [self dragWithDeltaX:[event deltaX]]; | 365 [self dragWithDeltaX:[event deltaX]]; |
| 351 break; | 366 break; |
| 352 | 367 |
| 353 case NSKeyUp: | 368 case NSKeyUp: |
| 354 if ([event keyCode] == kVK_Escape) { | 369 if ([event keyCode] == kVK_Escape) { |
| 355 [self endDrag:YES]; | 370 [self endDrag:YES]; |
| 356 keepGoing = NO; | 371 keepGoing = NO; |
| 357 } | 372 } |
| 358 break; | 373 break; |
| 359 | 374 |
| 360 case NSLeftMouseUp: | 375 case NSLeftMouseUp: |
| 361 [self endDrag:NO]; | 376 if (dragState_ == PANEL_DRAG_CAN_START) |
| 377 [self mouseUp:event]; // Drag didn't really start, minimize instead. | |
| 378 else | |
| 379 [self endDrag:NO]; | |
| 362 keepGoing = NO; | 380 keepGoing = NO; |
| 363 break; | 381 break; |
| 364 | 382 |
| 365 case NSRightMouseDownMask: | 383 case NSRightMouseDownMask: |
| 366 break; | 384 break; |
| 367 | 385 |
| 368 default: | 386 default: |
| 369 // Dequeue and ignore other mouse and key events so the Chrome context | 387 // Dequeue and ignore other mouse and key events so the Chrome context |
| 370 // menu does not come after right click on a page during Panel | 388 // menu does not come after right click on a page during Panel |
| 371 // rearrangement, or the keystrokes are not 'accumulated' and entered | 389 // rearrangement, or the keystrokes are not 'accumulated' and entered |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 460 } | 478 } |
| 461 | 479 |
| 462 - (void)checkMouseAndUpdateSettingsButtonVisibility { | 480 - (void)checkMouseAndUpdateSettingsButtonVisibility { |
| 463 BOOL mouseOverWindow = NSPointInRect([NSEvent mouseLocation], | 481 BOOL mouseOverWindow = NSPointInRect([NSEvent mouseLocation], |
| 464 [[self window] frame]); | 482 [[self window] frame]); |
| 465 [self updateSettingsButtonVisibility:mouseOverWindow]; | 483 [self updateSettingsButtonVisibility:mouseOverWindow]; |
| 466 } | 484 } |
| 467 | 485 |
| 468 @end | 486 @end |
| 469 | 487 |
| OLD | NEW |