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