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
3 // found in the LICENSE file.
5 #import "chrome/browser/ui/cocoa/wrench_menu/menu_tracked_button.h"
7 @interface MenuTrackedButton (Private)
8 - (void)doHighlight:(BOOL)highlight;
9 - (void)checkMouseInRect;
10 - (NSRect)insetBounds;
13 @implementation MenuTrackedButton
15 @synthesize tracking = tracking_;
17 - (void)updateTrackingAreas {
18 [super updateTrackingAreas];
19 [self removeTrackingRect:trackingTag_];
20 trackingTag_ = [self addTrackingRect:NSInsetRect([self bounds], 1, 1)
26 - (void)viewDidMoveToWindow {
27 [self updateTrackingAreas];
28 [self doHighlight:NO];
31 - (void)mouseEntered:(NSEvent*)theEvent {
35 [self doHighlight:YES];
36 [super mouseEntered:theEvent];
39 - (void)mouseExited:(NSEvent*)theEvent {
42 [self doHighlight:NO];
43 [super mouseExited:theEvent];
46 - (void)mouseDragged:(NSEvent*)theEvent {
47 tracking_ = !didEnter_;
49 NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil];
50 BOOL highlight = NSPointInRect(point, [self insetBounds]);
51 [self doHighlight:highlight];
53 // If tracking in non-sticky mode, poll the mouse cursor to see if it is still
54 // over the button and thus needs to be highlighted. The delay is the
55 // smallest that still produces the effect while minimizing jank. Smaller
56 // values make the selector fire too close to immediately/now for the mouse to
57 // have moved off the receiver, and larger values produce lag.
59 [self performSelector:@selector(checkMouseInRect)
62 inModes:[NSArray arrayWithObject:NSEventTrackingRunLoopMode]];
64 [super mouseDragged:theEvent];
67 - (void)mouseUp:(NSEvent*)theEvent {
68 [self doHighlight:NO];
70 return [super mouseUp:theEvent];
72 [self performClick:self];
76 - (void)doHighlight:(BOOL)highlight {
77 [[self cell] setHighlighted:highlight];
78 [self setNeedsDisplay];
81 // Checks if the user's current mouse location is over this button. If it is,
82 // the user is merely hovering here. If it is not, then disable the highlight.
83 // If the menu is opened in non-sticky mode, the button does not receive enter/
84 // exit mouse events and thus polling is necessary.
85 - (void)checkMouseInRect {
86 NSPoint point = [NSEvent mouseLocation];
87 point = [[self window] convertScreenToBase:point];
88 point = [self convertPoint:point fromView:nil];
89 if (!NSPointInRect(point, [self insetBounds])) {
90 [self doHighlight:NO];
94 // Returns the bounds of the receiver slightly inset to avoid highlighting both
95 // buttons in a pair that overlap.
96 - (NSRect)insetBounds {
97 return NSInsetRect([self bounds], 2, 1);