| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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" |
| (...skipping 374 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 } | 385 } |
| 386 | 386 |
| 387 - (void)didChangeMainWindow:(NSNotification*)notification { | 387 - (void)didChangeMainWindow:(NSNotification*)notification { |
| 388 [self setNeedsDisplay:YES]; | 388 [self setNeedsDisplay:YES]; |
| 389 [self checkMouseAndUpdateSettingsButtonVisibility]; | 389 [self checkMouseAndUpdateSettingsButtonVisibility]; |
| 390 } | 390 } |
| 391 | 391 |
| 392 - (void)mouseDown:(NSEvent*)event { | 392 - (void)mouseDown:(NSEvent*)event { |
| 393 if ([controller_ isDraggable]) { | 393 if ([controller_ isDraggable]) { |
| 394 dragState_ = PANEL_DRAG_CAN_START; | 394 dragState_ = PANEL_DRAG_CAN_START; |
| 395 dragStartLocation_ = [event locationInWindow]; | 395 dragStartLocation_ = [NSEvent mouseLocation]; |
| 396 } | 396 } |
| 397 } | 397 } |
| 398 | 398 |
| 399 - (void)mouseUp:(NSEvent*)event { | 399 - (void)mouseUp:(NSEvent*)event { |
| 400 DCHECK(dragState_ != PANEL_DRAG_IN_PROGRESS); | 400 DCHECK(dragState_ != PANEL_DRAG_IN_PROGRESS); |
| 401 | 401 |
| 402 if ([event clickCount] == 1) | 402 if ([event clickCount] == 1) |
| 403 [controller_ onTitlebarMouseClicked]; | 403 [controller_ onTitlebarMouseClicked]; |
| 404 } | 404 } |
| 405 | 405 |
| 406 - (BOOL)exceedsDragThreshold:(NSPoint)mouseLocation { | 406 - (BOOL)exceedsDragThreshold:(NSPoint)mouseLocation { |
| 407 float deltaX = dragStartLocation_.x - mouseLocation.x; | 407 float deltaX = dragStartLocation_.x - mouseLocation.x; |
| 408 float deltaY = dragStartLocation_.y - mouseLocation.y; | 408 float deltaY = dragStartLocation_.y - mouseLocation.y; |
| 409 return deltaX > kDragThreshold || deltaY > kDragThreshold; | 409 return fabs(deltaX) > kDragThreshold || fabs(deltaY) > kDragThreshold; |
| 410 } | 410 } |
| 411 | 411 |
| 412 - (void)mouseDragged:(NSEvent*)event { | 412 - (void)mouseDragged:(NSEvent*)event { |
| 413 if (dragState_ == PANEL_DRAG_SUPPRESSED) | 413 if (dragState_ == PANEL_DRAG_SUPPRESSED) |
| 414 return; | 414 return; |
| 415 | 415 |
| 416 // In addition to events needed to control the drag operation, fetch the right | 416 // In addition to events needed to control the drag operation, fetch the right |
| 417 // mouse click events and key down events and ignore them, to prevent their | 417 // mouse click events and key down events and ignore them, to prevent their |
| 418 // accumulation in the queue and "playing out" when the mouse is released. | 418 // accumulation in the queue and "playing out" when the mouse is released. |
| 419 const NSUInteger mask = | 419 const NSUInteger mask = |
| 420 NSLeftMouseUpMask | NSLeftMouseDraggedMask | NSKeyUpMask | | 420 NSLeftMouseUpMask | NSLeftMouseDraggedMask | NSKeyUpMask | |
| 421 NSRightMouseDownMask | NSKeyDownMask ; | 421 NSRightMouseDownMask | NSKeyDownMask ; |
| 422 BOOL keepGoing = YES; | 422 BOOL keepGoing = YES; |
| 423 | 423 |
| 424 while (keepGoing) { | 424 while (keepGoing) { |
| 425 base::mac::ScopedNSAutoreleasePool autorelease_pool; | 425 base::mac::ScopedNSAutoreleasePool autorelease_pool; |
| 426 | 426 |
| 427 NSEvent* event = [NSApp nextEventMatchingMask:mask | 427 NSEvent* event = [NSApp nextEventMatchingMask:mask |
| 428 untilDate:[NSDate distantFuture] | 428 untilDate:[NSDate distantFuture] |
| 429 inMode:NSDefaultRunLoopMode | 429 inMode:NSDefaultRunLoopMode |
| 430 dequeue:YES]; | 430 dequeue:YES]; |
| 431 | 431 |
| 432 switch ([event type]) { | 432 switch ([event type]) { |
| 433 case NSLeftMouseDragged: | 433 case NSLeftMouseDragged: |
| 434 if (dragState_ == PANEL_DRAG_CAN_START) { | 434 if (dragState_ == PANEL_DRAG_CAN_START) { |
| 435 if (![self exceedsDragThreshold:[event locationInWindow]]) | 435 if (![self exceedsDragThreshold:[NSEvent mouseLocation]]) |
| 436 return; // Don't start real drag yet. | 436 return; // Don't start real drag yet. |
| 437 [self startDrag]; | 437 [self startDrag:dragStartLocation_]; |
| 438 } | 438 } |
| 439 [self dragWithDeltaX:[event deltaX] | 439 if (dragState_ == PANEL_DRAG_IN_PROGRESS) |
| 440 deltaY:[event deltaY]]; | 440 [self drag:[NSEvent mouseLocation]]; |
| 441 break; | 441 break; |
| 442 | 442 |
| 443 case NSKeyUp: | 443 case NSKeyUp: |
| 444 if ([event keyCode] == kVK_Escape) { | 444 if ([event keyCode] == kVK_Escape) { |
| 445 [self endDrag:YES]; | 445 [self endDrag:YES]; |
| 446 keepGoing = NO; | 446 keepGoing = NO; |
| 447 } | 447 } |
| 448 break; | 448 break; |
| 449 | 449 |
| 450 case NSLeftMouseUp: | 450 case NSLeftMouseUp: |
| (...skipping 10 matching lines...) Expand all Loading... |
| 461 default: | 461 default: |
| 462 // Dequeue and ignore other mouse and key events so the Chrome context | 462 // Dequeue and ignore other mouse and key events so the Chrome context |
| 463 // menu does not come after right click on a page during Panel | 463 // menu does not come after right click on a page during Panel |
| 464 // rearrangement, or the keystrokes are not 'accumulated' and entered | 464 // rearrangement, or the keystrokes are not 'accumulated' and entered |
| 465 // at once when the drag ends. | 465 // at once when the drag ends. |
| 466 break; | 466 break; |
| 467 } | 467 } |
| 468 } | 468 } |
| 469 } | 469 } |
| 470 | 470 |
| 471 - (void)startDrag { | 471 - (void)startDrag:(NSPoint)mouseLocation { |
| 472 DCHECK(dragState_ == PANEL_DRAG_CAN_START); | 472 DCHECK(dragState_ == PANEL_DRAG_CAN_START); |
| 473 dragState_ = PANEL_DRAG_IN_PROGRESS; | 473 dragState_ = PANEL_DRAG_IN_PROGRESS; |
| 474 [controller_ startDrag]; | 474 [controller_ startDrag:mouseLocation]; |
| 475 } | 475 } |
| 476 | 476 |
| 477 - (void)endDrag:(BOOL)cancelled { | 477 - (void)endDrag:(BOOL)cancelled { |
| 478 if (dragState_ == PANEL_DRAG_IN_PROGRESS) | 478 if (dragState_ == PANEL_DRAG_IN_PROGRESS) |
| 479 [controller_ endDrag:cancelled]; | 479 [controller_ endDrag:cancelled]; |
| 480 dragState_ = PANEL_DRAG_SUPPRESSED; | 480 dragState_ = PANEL_DRAG_SUPPRESSED; |
| 481 } | 481 } |
| 482 | 482 |
| 483 - (void)dragWithDeltaX:(int)deltaX | 483 - (void)drag:(NSPoint)mouseLocation { |
| 484 deltaY:(int)deltaY { | |
| 485 if (dragState_ != PANEL_DRAG_IN_PROGRESS) | 484 if (dragState_ != PANEL_DRAG_IN_PROGRESS) |
| 486 return; | 485 return; |
| 487 [controller_ dragWithDeltaX:deltaX | 486 [controller_ drag:mouseLocation]; |
| 488 deltaY:deltaY]; | |
| 489 } | 487 } |
| 490 | 488 |
| 491 - (void)drawAttention { | 489 - (void)drawAttention { |
| 492 if (isDrawingAttention_) | 490 if (isDrawingAttention_) |
| 493 return; | 491 return; |
| 494 isDrawingAttention_ = YES; | 492 isDrawingAttention_ = YES; |
| 495 | 493 |
| 496 [self startGlintAnimation]; | 494 [self startGlintAnimation]; |
| 497 } | 495 } |
| 498 | 496 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 562 } | 560 } |
| 563 | 561 |
| 564 - (NSTextField*)title { | 562 - (NSTextField*)title { |
| 565 return title_; | 563 return title_; |
| 566 } | 564 } |
| 567 | 565 |
| 568 - (void)simulateCloseButtonClick { | 566 - (void)simulateCloseButtonClick { |
| 569 [[closeButton_ cell] performClick:closeButton_]; | 567 [[closeButton_ cell] performClick:closeButton_]; |
| 570 } | 568 } |
| 571 | 569 |
| 572 - (void)pressLeftMouseButtonTitlebar { | 570 - (void)pressLeftMouseButtonTitlebar:(NSPoint)mouseLocation { |
| 573 NSEvent* event = MakeMouseEvent(NSLeftMouseDown, NSZeroPoint, 0); | 571 NSEvent* event = MakeMouseEvent(NSLeftMouseDown, |
| 572 [self convertPoint:mouseLocation fromView:nil], 0); |
| 574 [self mouseDown:event]; | 573 [self mouseDown:event]; |
| 575 } | 574 } |
| 576 | 575 |
| 577 - (void)releaseLeftMouseButtonTitlebar { | 576 - (void)releaseLeftMouseButtonTitlebar { |
| 578 NSEvent* event = MakeMouseEvent(NSLeftMouseUp, NSZeroPoint, 1); | 577 NSEvent* event = MakeMouseEvent(NSLeftMouseUp, NSZeroPoint, 1); |
| 579 [self mouseUp:event]; | 578 [self mouseUp:event]; |
| 580 } | 579 } |
| 581 | 580 |
| 582 - (void)dragTitlebarDeltaX:(double)delta_x | 581 - (void)dragTitlebar:(NSPoint)mouseLocation { |
| 583 deltaY:(double)delta_y { | |
| 584 if (dragState_ == PANEL_DRAG_CAN_START) | 582 if (dragState_ == PANEL_DRAG_CAN_START) |
| 585 [self startDrag]; | 583 [self startDrag:dragStartLocation_]; |
| 586 [self dragWithDeltaX:delta_x | 584 [self drag:mouseLocation]; |
| 587 deltaY:delta_y]; | |
| 588 } | 585 } |
| 589 | 586 |
| 590 - (void)cancelDragTitlebar { | 587 - (void)cancelDragTitlebar { |
| 591 [self endDrag:YES]; | 588 [self endDrag:YES]; |
| 592 } | 589 } |
| 593 | 590 |
| 594 - (void)finishDragTitlebar { | 591 - (void)finishDragTitlebar { |
| 595 [self endDrag:NO]; | 592 [self endDrag:NO]; |
| 596 } | 593 } |
| 597 | 594 |
| 598 - (void)updateSettingsButtonVisibility:(BOOL)mouseOverWindow { | 595 - (void)updateSettingsButtonVisibility:(BOOL)mouseOverWindow { |
| 599 // The settings button is visible if the panel is main window or the mouse is | 596 // The settings button is visible if the panel is main window or the mouse is |
| 600 // over it. | 597 // over it. |
| 601 BOOL shouldShowSettingsButton = | 598 BOOL shouldShowSettingsButton = |
| 602 mouseOverWindow || [[self window] isMainWindow]; | 599 mouseOverWindow || [[self window] isMainWindow]; |
| 603 [[settingsButtonWrapper_ animator] | 600 [[settingsButtonWrapper_ animator] |
| 604 setAlphaValue:shouldShowSettingsButton ? 1.0 : 0.0]; | 601 setAlphaValue:shouldShowSettingsButton ? 1.0 : 0.0]; |
| 605 } | 602 } |
| 606 | 603 |
| 607 - (void)checkMouseAndUpdateSettingsButtonVisibility { | 604 - (void)checkMouseAndUpdateSettingsButtonVisibility { |
| 608 BOOL mouseOverWindow = NSPointInRect([NSEvent mouseLocation], | 605 BOOL mouseOverWindow = NSPointInRect([NSEvent mouseLocation], |
| 609 [[self window] frame]); | 606 [[self window] frame]); |
| 610 [self updateSettingsButtonVisibility:mouseOverWindow]; | 607 [self updateSettingsButtonVisibility:mouseOverWindow]; |
| 611 } | 608 } |
| 612 | 609 |
| 613 @end | 610 @end |
| 614 | 611 |
| OLD | NEW |