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 // Square of a distance that user needs to move the mouse in order to start the | |
28 // drag. Threshold is needed to differentiate drags from attempts to click the | |
29 // titlebar with a twitch of the mouse pointer. | |
30 const int kDragThreasholdSquared = 9; | |
jennb
2011/11/17 23:45:40
typo: Threshold
What's the logic behind using the
Dmitry Titov
2011/11/18 00:15:11
I'm computing a distance and don't want to do sqrt
| |
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)overDragThreshold:(NSPoint)mouseLocation { | |
jennb
2011/11/17 23:45:40
exceedsDragThreshold? I'm not sure what overDragTh
Dmitry Titov
2011/11/18 00:15:11
Done.
| |
336 float deltaX = dragStartLocation_.x - mouseLocation.x; | |
337 float deltaY = dragStartLocation_.y - mouseLocation.y; | |
338 return deltaX*deltaX + deltaY*deltaY > kDragThreasholdSquared; | |
jennb
2011/11/17 23:45:40
Please use parens rather than dropping the spacing
Dmitry Titov
2011/11/18 00:15:11
Done.
| |
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) { |
349 [self startDrag]; | 361 if ([self overDragThreshold:[event locationInWindow]]) |
jennb
2011/11/17 23:45:40
nit: How about reversing this conditional to make
Dmitry Titov
2011/11/18 00:15:11
Done.
| |
362 [self startDrag]; | |
363 else | |
364 return; // Don't start real drag yet. | |
365 } | |
350 [self dragWithDeltaX:[event deltaX]]; | 366 [self dragWithDeltaX:[event deltaX]]; |
351 break; | 367 break; |
352 | 368 |
353 case NSKeyUp: | 369 case NSKeyUp: |
354 if ([event keyCode] == kVK_Escape) { | 370 if ([event keyCode] == kVK_Escape) { |
355 [self endDrag:YES]; | 371 [self endDrag:YES]; |
356 keepGoing = NO; | 372 keepGoing = NO; |
357 } | 373 } |
358 break; | 374 break; |
359 | 375 |
360 case NSLeftMouseUp: | 376 case NSLeftMouseUp: |
377 if (dragState_ == PANEL_DRAG_CAN_START) { // Drag didn't really start. | |
378 [self mouseUp:event]; | |
379 return; | |
jennb
2011/11/17 23:45:40
nit: I want to drop this return and add else claus
Dmitry Titov
2011/11/18 00:15:11
Done.
| |
380 } | |
361 [self endDrag:NO]; | 381 [self endDrag:NO]; |
362 keepGoing = NO; | 382 keepGoing = NO; |
363 break; | 383 break; |
364 | 384 |
365 case NSRightMouseDownMask: | 385 case NSRightMouseDownMask: |
366 break; | 386 break; |
367 | 387 |
368 default: | 388 default: |
369 // Dequeue and ignore other mouse and key events so the Chrome context | 389 // 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 | 390 // menu does not come after right click on a page during Panel |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
460 } | 480 } |
461 | 481 |
462 - (void)checkMouseAndUpdateSettingsButtonVisibility { | 482 - (void)checkMouseAndUpdateSettingsButtonVisibility { |
463 BOOL mouseOverWindow = NSPointInRect([NSEvent mouseLocation], | 483 BOOL mouseOverWindow = NSPointInRect([NSEvent mouseLocation], |
464 [[self window] frame]); | 484 [[self window] frame]); |
465 [self updateSettingsButtonVisibility:mouseOverWindow]; | 485 [self updateSettingsButtonVisibility:mouseOverWindow]; |
466 } | 486 } |
467 | 487 |
468 @end | 488 @end |
469 | 489 |
OLD | NEW |