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/hover_button.h" | |
6 | |
7 @implementation HoverButton | |
8 | |
9 @synthesize hoverState = hoverState_; | |
10 | |
11 - (id)initWithFrame:(NSRect)frameRect { | |
12 if ((self = [super initWithFrame:frameRect])) { | |
13 [self setTrackingEnabled:YES]; | |
14 hoverState_ = kHoverStateNone; | |
15 [self updateTrackingAreas]; | |
16 } | |
17 return self; | |
18 } | |
19 | |
20 - (void)awakeFromNib { | |
21 [self setTrackingEnabled:YES]; | |
22 self.hoverState = kHoverStateNone; | |
23 [self updateTrackingAreas]; | |
24 } | |
25 | |
26 - (void)dealloc { | |
27 [self setTrackingEnabled:NO]; | |
28 [super dealloc]; | |
29 } | |
30 | |
31 - (void)mouseEntered:(NSEvent*)theEvent { | |
32 self.hoverState = kHoverStateMouseOver; | |
33 } | |
34 | |
35 - (void)mouseExited:(NSEvent*)theEvent { | |
36 self.hoverState = kHoverStateNone; | |
37 } | |
38 | |
39 - (void)mouseDown:(NSEvent*)theEvent { | |
40 self.hoverState = kHoverStateMouseDown; | |
41 // The hover button needs to hold onto itself here for a bit. Otherwise, | |
42 // it can be freed while |super mouseDown:| is in its loop, and the | |
43 // |checkImageState| call will crash. | |
44 // http://crbug.com/28220 | |
45 scoped_nsobject<HoverButton> myself([self retain]); | |
46 | |
47 [super mouseDown:theEvent]; | |
48 // We need to check the image state after the mouseDown event loop finishes. | |
49 // It's possible that we won't get a mouseExited event if the button was | |
50 // moved under the mouse during tab resize, instead of the mouse moving over | |
51 // the button. | |
52 // http://crbug.com/31279 | |
53 [self checkImageState]; | |
54 } | |
55 | |
56 - (void)setTrackingEnabled:(BOOL)enabled { | |
57 if (enabled) { | |
58 trackingArea_.reset( | |
59 [[CrTrackingArea alloc] initWithRect:NSZeroRect | |
60 options:NSTrackingMouseEnteredAndExited | | |
61 NSTrackingActiveAlways | | |
62 NSTrackingInVisibleRect | |
63 owner:self | |
64 userInfo:nil]); | |
65 [self addTrackingArea:trackingArea_.get()]; | |
66 | |
67 // If you have a separate window that overlaps the close button, and you | |
68 // move the mouse directly over the close button without entering another | |
69 // part of the tab strip, we don't get any mouseEntered event since the | |
70 // tracking area was disabled when we entered. | |
71 // Done with a delay of 0 because sometimes an event appears to be missed | |
72 // between the activation of the tracking area and the call to | |
73 // checkImageState resulting in the button state being incorrect. | |
74 [self performSelector:@selector(checkImageState) | |
75 withObject:nil | |
76 afterDelay:0]; | |
77 } else { | |
78 if (trackingArea_.get()) { | |
79 [self removeTrackingArea:trackingArea_.get()]; | |
80 trackingArea_.reset(nil); | |
81 } | |
82 } | |
83 } | |
84 | |
85 - (void)updateTrackingAreas { | |
86 [super updateTrackingAreas]; | |
87 [self checkImageState]; | |
88 } | |
89 | |
90 - (void)checkImageState { | |
91 if (!trackingArea_.get()) | |
92 return; | |
93 | |
94 // Update the button's state if the button has moved. | |
95 NSPoint mouseLoc = [[self window] mouseLocationOutsideOfEventStream]; | |
96 mouseLoc = [self convertPoint:mouseLoc fromView:nil]; | |
97 self.hoverState = NSPointInRect(mouseLoc, [self bounds]) ? | |
98 kHoverStateMouseOver : kHoverStateNone; | |
99 } | |
100 | |
101 - (void)setHoverState:(HoverState)state { | |
102 hoverState_ = state; | |
103 [self setNeedsDisplay:YES]; | |
104 } | |
105 | |
106 @end | |
OLD | NEW |