OLD | NEW |
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 "ui/base/cocoa/hover_button.h" | 5 #import "ui/base/cocoa/hover_button.h" |
6 | 6 |
7 @implementation HoverButton | 7 @implementation HoverButton |
8 | 8 |
9 @synthesize hoverState = hoverState_; | 9 @synthesize isMouseInside = isMouseInside_; |
10 | 10 |
11 - (id)initWithFrame:(NSRect)frameRect { | 11 - (id)initWithFrame:(NSRect)frameRect { |
12 if ((self = [super initWithFrame:frameRect])) { | 12 if ((self = [super initWithFrame:frameRect])) { |
13 [self setTrackingEnabled:YES]; | 13 [self setTrackingEnabled:YES]; |
14 hoverState_ = kHoverStateNone; | |
15 [self updateTrackingAreas]; | 14 [self updateTrackingAreas]; |
16 } | 15 } |
17 return self; | 16 return self; |
18 } | 17 } |
19 | 18 |
20 - (void)awakeFromNib { | 19 - (void)awakeFromNib { |
21 [self setTrackingEnabled:YES]; | 20 [self setTrackingEnabled:YES]; |
22 self.hoverState = kHoverStateNone; | |
23 [self updateTrackingAreas]; | 21 [self updateTrackingAreas]; |
24 } | 22 } |
25 | 23 |
26 - (void)dealloc { | 24 - (void)dealloc { |
27 [self setTrackingEnabled:NO]; | 25 [self setTrackingEnabled:NO]; |
28 [super dealloc]; | 26 [super dealloc]; |
29 } | 27 } |
30 | 28 |
| 29 - (void)setIsMouseInside:(BOOL)isInside { |
| 30 isMouseInside_ = isInside; |
| 31 [self setNeedsDisplay:YES]; |
| 32 } |
| 33 |
| 34 - (HoverState)hoverState { |
| 35 if (self.state == NSOnState) |
| 36 return kHoverStateMouseDown; |
| 37 if (self.isMouseInside) |
| 38 return kHoverStateMouseOver; |
| 39 return kHoverStateNone; |
| 40 } |
| 41 |
31 - (void)mouseEntered:(NSEvent*)theEvent { | 42 - (void)mouseEntered:(NSEvent*)theEvent { |
32 if (trackingArea_.get()) | 43 if (trackingArea_.get()) |
33 self.hoverState = kHoverStateMouseOver; | 44 self.isMouseInside = YES; |
34 } | 45 } |
35 | 46 |
36 - (void)mouseExited:(NSEvent*)theEvent { | 47 - (void)mouseExited:(NSEvent*)theEvent { |
37 if (trackingArea_.get()) | 48 if (trackingArea_.get()) |
38 self.hoverState = kHoverStateNone; | 49 self.isMouseInside = NO; |
39 } | |
40 | |
41 - (void)mouseDown:(NSEvent*)theEvent { | |
42 self.hoverState = kHoverStateMouseDown; | |
43 // The hover button needs to hold onto itself here for a bit. Otherwise, | |
44 // it can be freed while |super mouseDown:| is in its loop, and the | |
45 // |checkImageState| call will crash. | |
46 // http://crbug.com/28220 | |
47 scoped_nsobject<HoverButton> myself([self retain]); | |
48 | |
49 [super mouseDown:theEvent]; | |
50 // We need to check the image state after the mouseDown event loop finishes. | |
51 // It's possible that we won't get a mouseExited event if the button was | |
52 // moved under the mouse during tab resize, instead of the mouse moving over | |
53 // the button. | |
54 // http://crbug.com/31279 | |
55 [self checkImageState]; | |
56 } | 50 } |
57 | 51 |
58 - (void)setTrackingEnabled:(BOOL)enabled { | 52 - (void)setTrackingEnabled:(BOOL)enabled { |
59 if (enabled) { | 53 if (enabled) { |
60 trackingArea_.reset( | 54 trackingArea_.reset( |
61 [[CrTrackingArea alloc] initWithRect:NSZeroRect | 55 [[CrTrackingArea alloc] initWithRect:NSZeroRect |
62 options:NSTrackingMouseEnteredAndExited | | 56 options:NSTrackingMouseEnteredAndExited | |
63 NSTrackingActiveAlways | | 57 NSTrackingActiveAlways | |
64 NSTrackingInVisibleRect | 58 NSTrackingInVisibleRect |
65 owner:self | 59 owner:self |
(...skipping 23 matching lines...) Expand all Loading... |
89 [self checkImageState]; | 83 [self checkImageState]; |
90 } | 84 } |
91 | 85 |
92 - (void)checkImageState { | 86 - (void)checkImageState { |
93 if (!trackingArea_.get()) | 87 if (!trackingArea_.get()) |
94 return; | 88 return; |
95 | 89 |
96 // Update the button's state if the button has moved. | 90 // Update the button's state if the button has moved. |
97 NSPoint mouseLoc = [[self window] mouseLocationOutsideOfEventStream]; | 91 NSPoint mouseLoc = [[self window] mouseLocationOutsideOfEventStream]; |
98 mouseLoc = [self convertPoint:mouseLoc fromView:nil]; | 92 mouseLoc = [self convertPoint:mouseLoc fromView:nil]; |
99 self.hoverState = NSPointInRect(mouseLoc, [self bounds]) ? | 93 self.isMouseInside = NSPointInRect(mouseLoc, [self bounds]); |
100 kHoverStateMouseOver : kHoverStateNone; | |
101 } | |
102 | |
103 - (void)setHoverState:(HoverState)state { | |
104 hoverState_ = state; | |
105 [self setNeedsDisplay:YES]; | |
106 } | 94 } |
107 | 95 |
108 @end | 96 @end |
OLD | NEW |