Chromium Code Reviews
Help | Chromium Project | Sign in
(73)

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: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
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 @interface MenuTrackedButton (Private)
8 - (void)doHighlight:(BOOL)highlight;
9 - (void)checkMouseInRect;
10 - (NSRect)insetBounds;
11 @end
12
7 @implementation MenuTrackedButton 13 @implementation MenuTrackedButton
8 14
15 - (void)updateTrackingAreas {
16 [super updateTrackingAreas];
17 [self removeTrackingRect:trackingTag_];
18 trackingTag_ = [self addTrackingRect:NSInsetRect([self bounds], 1, 1)
19 owner:self
20 userData:NULL
21 assumeInside:NO];
22 }
23
24 - (void)viewDidMoveToWindow {
25 [self updateTrackingAreas];
26 [self doHighlight:NO];
27 }
28
9 - (void)mouseEntered:(NSEvent*)theEvent { 29 - (void)mouseEntered:(NSEvent*)theEvent {
10 didEnter_ = YES; 30 if (!tracking_) {
31 didEnter_ = YES;
32 }
33 [self doHighlight:YES];
11 [super mouseEntered:theEvent]; 34 [super mouseEntered:theEvent];
12 } 35 }
13 36
14 - (void)mouseExited:(NSEvent*)theEvent { 37 - (void)mouseExited:(NSEvent*)theEvent {
15 didEnter_ = NO; 38 didEnter_ = NO;
16 tracking_ = NO; 39 tracking_ = NO;
40 [self doHighlight:NO];
17 [super mouseExited:theEvent]; 41 [super mouseExited:theEvent];
18 } 42 }
19 43
20 - (void)mouseDragged:(NSEvent*)theEvent { 44 - (void)mouseDragged:(NSEvent*)theEvent {
21 tracking_ = !didEnter_; 45 tracking_ = !didEnter_;
46
47 NSPoint point = [self convertPoint:[theEvent locationInWindow] fromView:nil];
48 BOOL highlight = NSPointInRect(point, [self insetBounds]);
49 [self doHighlight:highlight];
50
51 // If tracking in non-sticky mode, poll the mouse cursor to see if it is still
52 // over the button and thus needs to be highlighted.
53 if (tracking_) {
54 [self performSelector:@selector(checkMouseInRect)
55 withObject:nil
56 afterDelay:0.05 // This is the smallest delay that works.
pink 2010/08/17 17:41:01 do we really need to be checking 20 times a second
Robert Sesek 2010/08/17 18:18:04 It's absolutely necessary to check this frequently
57 inModes:[NSArray arrayWithObject:NSEventTrackingRunLoopMode]];
58 }
22 [super mouseDragged:theEvent]; 59 [super mouseDragged:theEvent];
23 } 60 }
24 61
25 - (void)mouseUp:(NSEvent*)theEvent { 62 - (void)mouseUp:(NSEvent*)theEvent {
63 [self doHighlight:NO];
26 if (!tracking_) { 64 if (!tracking_) {
27 return [super mouseUp:theEvent]; 65 return [super mouseUp:theEvent];
28 } 66 }
29 [self performClick:self]; 67 [self performClick:self];
30 } 68 }
31 69
70 - (void)doHighlight:(BOOL)highlight {
71 [[self cell] setHighlighted:highlight];
72 [self setNeedsDisplay];
73 }
74
75 // Checks if the user's current mouse location is over this button. If it is,
76 // she is merely hovering here. If it is not, then disable the highlight. If
pink 2010/08/17 17:41:01 s/she/they ? we are usually gender-neutral in comm
Robert Sesek 2010/08/17 18:18:04 'They' is plural and would break subject-verb agre
77 // the menu is opened in non-sticky mode, the button does not receive enter/exit
78 // mouse events and thus polling is necessary.
79 - (void)checkMouseInRect {
80 NSPoint point = [NSEvent mouseLocation];
81 point = [[self window] convertScreenToBase:point];
pink 2010/08/17 17:41:01 I don't think you want convertScreenToBase. Check
Robert Sesek 2010/08/17 18:18:04 I was under the impression that the two were equiv
pink 2010/08/17 18:27:53 we've had a lot of bugs crop up from this, i think
82 point = [self convertPointFromBase:point];
Scott Hess 2010/08/17 19:55:07 I find pink's comment mis-leading. I do think you
83 if (!NSPointInRect(point, [self insetBounds])) {
84 [self doHighlight:NO];
85 }
86 }
87
88 // Returns the bounds of the receiver slightly inset to avoid highlighting both
89 // buttons in a pair that overlap.
90 - (NSRect)insetBounds {
91 return NSInsetRect([self bounds], 2, 1);
92 }
93
32 @end 94 @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
RSS Feeds Recent Issues | This issue
This is Rietveld ec887be