| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #import "chrome/browser/ui/cocoa/menu_tracked_button.h" | |
| 6 | |
| 7 #include <cmath> | |
| 8 | |
| 9 @interface MenuTrackedButton (Private) | |
| 10 - (void)doHighlight:(BOOL)highlight; | |
| 11 - (void)checkMouseInRect; | |
| 12 - (NSRect)insetBounds; | |
| 13 - (BOOL)shouldHighlightOnHover; | |
| 14 @end | |
| 15 | |
| 16 @implementation MenuTrackedButton | |
| 17 | |
| 18 @synthesize tracking = tracking_; | |
| 19 | |
| 20 - (void)updateTrackingAreas { | |
| 21 [super updateTrackingAreas]; | |
| 22 [self removeTrackingRect:trackingTag_]; | |
| 23 trackingTag_ = [self addTrackingRect:NSInsetRect([self bounds], 1, 1) | |
| 24 owner:self | |
| 25 userData:NULL | |
| 26 assumeInside:NO]; | |
| 27 } | |
| 28 | |
| 29 - (void)viewDidMoveToWindow { | |
| 30 [self updateTrackingAreas]; | |
| 31 [self doHighlight:NO]; | |
| 32 } | |
| 33 | |
| 34 - (void)mouseEntered:(NSEvent*)theEvent { | |
| 35 if (!tracking_) { | |
| 36 didEnter_ = YES; | |
| 37 } | |
| 38 [self doHighlight:YES]; | |
| 39 [super mouseEntered:theEvent]; | |
| 40 } | |
| 41 | |
| 42 - (void)mouseExited:(NSEvent*)theEvent { | |
| 43 didEnter_ = NO; | |
| 44 tracking_ = NO; | |
| 45 [self doHighlight:NO]; | |
| 46 [super mouseExited:theEvent]; | |
| 47 } | |
| 48 | |
| 49 - (void)mouseDragged:(NSEvent*)theEvent { | |
| 50 tracking_ = !didEnter_; | |
| 51 | |
| 52 NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil]; | |
| 53 BOOL highlight = NSPointInRect(point, [self insetBounds]); | |
| 54 [self doHighlight:highlight]; | |
| 55 | |
| 56 // If tracking in non-sticky mode, poll the mouse cursor to see if it is still | |
| 57 // over the button and thus needs to be highlighted. The delay is the | |
| 58 // smallest that still produces the effect while minimizing jank. Smaller | |
| 59 // values make the selector fire too close to immediately/now for the mouse to | |
| 60 // have moved off the receiver, and larger values produce lag. | |
| 61 if (tracking_ && [self shouldHighlightOnHover]) { | |
| 62 [self performSelector:@selector(checkMouseInRect) | |
| 63 withObject:nil | |
| 64 afterDelay:0.05 | |
| 65 inModes:[NSArray arrayWithObject:NSEventTrackingRunLoopMode]]; | |
| 66 } | |
| 67 [super mouseDragged:theEvent]; | |
| 68 } | |
| 69 | |
| 70 - (void)mouseUp:(NSEvent*)theEvent { | |
| 71 [self doHighlight:NO]; | |
| 72 if (!tracking_) { | |
| 73 return [super mouseUp:theEvent]; | |
| 74 } | |
| 75 [self performClick:self]; | |
| 76 tracking_ = NO; | |
| 77 } | |
| 78 | |
| 79 - (void)doHighlight:(BOOL)highlight { | |
| 80 if (![self shouldHighlightOnHover]) { | |
| 81 return; | |
| 82 } | |
| 83 [[self cell] setHighlighted:highlight]; | |
| 84 [self setNeedsDisplay]; | |
| 85 } | |
| 86 | |
| 87 // Checks if the user's current mouse location is over this button. If it is, | |
| 88 // the user is merely hovering here. If it is not, then disable the highlight. | |
| 89 // If the menu is opened in non-sticky mode, the button does not receive enter/ | |
| 90 // exit mouse events and thus polling is necessary. | |
| 91 - (void)checkMouseInRect { | |
| 92 NSPoint point = [NSEvent mouseLocation]; | |
| 93 point = [[self window] convertScreenToBase:point]; | |
| 94 point = [self convertPoint:point fromView:nil]; | |
| 95 if (!NSPointInRect(point, [self insetBounds])) { | |
| 96 [self doHighlight:NO]; | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 // Returns the bounds of the receiver slightly inset to avoid highlighting both | |
| 101 // buttons in a pair that overlap. | |
| 102 - (NSRect)insetBounds { | |
| 103 return NSInsetRect([self bounds], 2, 1); | |
| 104 } | |
| 105 | |
| 106 - (BOOL)shouldHighlightOnHover { | |
| 107 // Apple does not define NSAppKitVersionNumber10_5 when using the 10.5 SDK. | |
| 108 // The Internets have come up with this solution. | |
| 109 #ifndef NSAppKitVersionNumber10_5 | |
| 110 #define NSAppKitVersionNumber10_5 949 | |
| 111 #endif | |
| 112 | |
| 113 // There's a cell drawing bug in 10.5 that was fixed on 10.6. Hover states | |
| 114 // look terrible due to this, so disable highlighting on 10.5. | |
| 115 return std::floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5; | |
| 116 } | |
| 117 | |
| 118 @end | |
| OLD | NEW |