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

Side by Side Diff: chrome/browser/cocoa/menu_tracked_button.mm

Issue 3137013: [Mac] Implement highlight-on-hover for the Wrench menu buttons. (Closed) Base URL: http://src.chromium.org/git/chromium.git
Patch Set: Addressed all comments Created 10 years, 4 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
« no previous file with comments | « chrome/browser/cocoa/menu_tracked_button.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 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 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/cocoa/menu_tracked_button.h" 5 #import "chrome/browser/cocoa/menu_tracked_button.h"
6 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
7 @implementation MenuTrackedButton 16 @implementation MenuTrackedButton
8 17
18 - (void)updateTrackingAreas {
19 [super updateTrackingAreas];
20 [self removeTrackingRect:trackingTag_];
21 trackingTag_ = [self addTrackingRect:NSInsetRect([self bounds], 1, 1)
22 owner:self
23 userData:NULL
24 assumeInside:NO];
25 }
26
27 - (void)viewDidMoveToWindow {
28 [self updateTrackingAreas];
29 [self doHighlight:NO];
30 }
31
9 - (void)mouseEntered:(NSEvent*)theEvent { 32 - (void)mouseEntered:(NSEvent*)theEvent {
10 didEnter_ = YES; 33 if (!tracking_) {
34 didEnter_ = YES;
35 }
36 [self doHighlight:YES];
11 [super mouseEntered:theEvent]; 37 [super mouseEntered:theEvent];
12 } 38 }
13 39
14 - (void)mouseExited:(NSEvent*)theEvent { 40 - (void)mouseExited:(NSEvent*)theEvent {
15 didEnter_ = NO; 41 didEnter_ = NO;
16 tracking_ = NO; 42 tracking_ = NO;
43 [self doHighlight:NO];
17 [super mouseExited:theEvent]; 44 [super mouseExited:theEvent];
18 } 45 }
19 46
20 - (void)mouseDragged:(NSEvent*)theEvent { 47 - (void)mouseDragged:(NSEvent*)theEvent {
21 tracking_ = !didEnter_; 48 tracking_ = !didEnter_;
49
50 NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil];
51 BOOL highlight = NSPointInRect(point, [self insetBounds]);
52 [self doHighlight:highlight];
53
54 // If tracking in non-sticky mode, poll the mouse cursor to see if it is still
55 // over the button and thus needs to be highlighted. The delay is the largest
56 // value that works; smaller values make the selector fire too close to
57 // immediately/now for the mouse to have moved off the receiver.
58 if (tracking_ && [self shouldHighlightOnHover]) {
59 [self performSelector:@selector(checkMouseInRect)
60 withObject:nil
61 afterDelay:0.05
62 inModes:[NSArray arrayWithObject:NSEventTrackingRunLoopMode]];
63 }
22 [super mouseDragged:theEvent]; 64 [super mouseDragged:theEvent];
23 } 65 }
24 66
25 - (void)mouseUp:(NSEvent*)theEvent { 67 - (void)mouseUp:(NSEvent*)theEvent {
68 [self doHighlight:NO];
26 if (!tracking_) { 69 if (!tracking_) {
27 return [super mouseUp:theEvent]; 70 return [super mouseUp:theEvent];
28 } 71 }
29 [self performClick:self]; 72 [self performClick:self];
30 } 73 }
31 74
75 - (void)doHighlight:(BOOL)highlight {
76 if (![self shouldHighlightOnHover]) {
77 return;
78 }
79 [[self cell] setHighlighted:highlight];
80 [self setNeedsDisplay];
81 }
82
83 // Checks if the user's current mouse location is over this button. If it is,
84 // the user is merely hovering here. If it is not, then disable the highlight.
85 // If the menu is opened in non-sticky mode, the button does not receive enter/
86 // exit mouse events and thus polling is necessary.
87 - (void)checkMouseInRect {
88 NSPoint point = [NSEvent mouseLocation];
89 point = [[self window] convertScreenToBase:point];
90 point = [self convertPoint:point fromView:nil];
91 if (!NSPointInRect(point, [self insetBounds])) {
92 [self doHighlight:NO];
93 }
94 }
95
96 // Returns the bounds of the receiver slightly inset to avoid highlighting both
97 // buttons in a pair that overlap.
98 - (NSRect)insetBounds {
99 return NSInsetRect([self bounds], 2, 1);
100 }
101
102 - (BOOL)shouldHighlightOnHover {
103 // Apple does not define NSAppKitVersionNumber10_5 when using the 10.5 SDK.
104 // The Internets have come up with this solution.
105 #ifndef NSAppKitVersionNumber10_5
106 #define NSAppKitVersionNumber10_5 949
107 #endif
108
109 // There's a cell drawing bug in 10.5 that was fixed on 10.6. Hover states
110 // look terrible due to this, so disable highlighting on 10.5.
111 return std::floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_5;
112 }
113
32 @end 114 @end
OLDNEW
« no previous file with comments | « chrome/browser/cocoa/menu_tracked_button.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698