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

Side by Side Diff: chrome/browser/ui/panels/panel_titlebar_view_cocoa.mm

Issue 9616037: Change panel drag related methods to use mouse location in screen coordinates. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Patch to reland Created 8 years, 9 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 = fabs(dragStartLocation_.x - mouseLocation.x);
408 float deltaY = dragStartLocation_.y - mouseLocation.y; 409 float deltaY = fabs(dragStartLocation_.y - mouseLocation.y);
409 return deltaX > kDragThreshold || deltaY > kDragThreshold; 410 return deltaX > kDragThreshold || 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 Cocoa's 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 DCHECK(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
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 // Convert from Cocoa's screen coordinates to base coordinates since the mouse
577 // event takes base coordinates.
578 NSEvent* event = MakeMouseEvent(
579 NSLeftMouseDown, [[self window] convertScreenToBase:mouseLocation], 0);
574 [self mouseDown:event]; 580 [self mouseDown:event];
575 } 581 }
576 582
577 - (void)releaseLeftMouseButtonTitlebar { 583 - (void)releaseLeftMouseButtonTitlebar {
578 NSEvent* event = MakeMouseEvent(NSLeftMouseUp, NSZeroPoint, 1); 584 NSEvent* event = MakeMouseEvent(NSLeftMouseUp, NSZeroPoint, 1);
579 [self mouseUp:event]; 585 [self mouseUp:event];
580 } 586 }
581 587
582 - (void)dragTitlebarDeltaX:(double)delta_x 588 - (void)dragTitlebar:(NSPoint)mouseLocation {
583 deltaY:(double)delta_y {
584 if (dragState_ == PANEL_DRAG_CAN_START) 589 if (dragState_ == PANEL_DRAG_CAN_START)
585 [self startDrag]; 590 [self startDrag:dragStartLocation_];
586 [self dragWithDeltaX:delta_x 591 // No need to do any conversion since |mouseLocation| is already in Cocoa's
587 deltaY:delta_y]; 592 // screen coordinates.
593 [self drag:mouseLocation];
588 } 594 }
589 595
590 - (void)cancelDragTitlebar { 596 - (void)cancelDragTitlebar {
591 [self endDrag:YES]; 597 [self endDrag:YES];
592 } 598 }
593 599
594 - (void)finishDragTitlebar { 600 - (void)finishDragTitlebar {
595 [self endDrag:NO]; 601 [self endDrag:NO];
596 } 602 }
597 603
598 - (void)updateSettingsButtonVisibility:(BOOL)mouseOverWindow { 604 - (void)updateSettingsButtonVisibility:(BOOL)mouseOverWindow {
599 // The settings button is visible if the panel is main window or the mouse is 605 // The settings button is visible if the panel is main window or the mouse is
600 // over it. 606 // over it.
601 BOOL shouldShowSettingsButton = 607 BOOL shouldShowSettingsButton =
602 mouseOverWindow || [[self window] isMainWindow]; 608 mouseOverWindow || [[self window] isMainWindow];
603 [[settingsButtonWrapper_ animator] 609 [[settingsButtonWrapper_ animator]
604 setAlphaValue:shouldShowSettingsButton ? 1.0 : 0.0]; 610 setAlphaValue:shouldShowSettingsButton ? 1.0 : 0.0];
605 } 611 }
606 612
607 - (void)checkMouseAndUpdateSettingsButtonVisibility { 613 - (void)checkMouseAndUpdateSettingsButtonVisibility {
608 BOOL mouseOverWindow = NSPointInRect([NSEvent mouseLocation], 614 BOOL mouseOverWindow = NSPointInRect([NSEvent mouseLocation],
609 [[self window] frame]); 615 [[self window] frame]);
610 [self updateSettingsButtonVisibility:mouseOverWindow]; 616 [self updateSettingsButtonVisibility:mouseOverWindow];
611 } 617 }
612 618
613 @end 619 @end
614 620
OLDNEW
« no previous file with comments | « chrome/browser/ui/panels/panel_titlebar_view_cocoa.h ('k') | chrome/browser/ui/panels/panel_utils_cocoa.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698