OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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/autofill/autofill_tooltip_controller.h" | |
6 | |
7 #include "base/mac/foundation_util.h" | |
8 #import "chrome/browser/ui/cocoa/autofill/autofill_bubble_controller.h" | |
9 #import "ui/base/cocoa/hover_image_button.h" | |
10 | |
11 namespace { | |
12 | |
13 CGFloat kTooltipDelay = 0.1; // Delay time before tooltip shows/hides. | |
Robert Sesek
2013/12/02 17:16:53
NSTimeInterval
groby-ooo-7-16
2013/12/02 20:41:57
Done.
| |
14 CGFloat kTooltipInset = 10; // How far to inset tooltip contents. | |
15 | |
16 } | |
Robert Sesek
2013/12/02 17:16:53
// namespace
groby-ooo-7-16
2013/12/02 20:41:57
Killed namespace entirely - turns out consts at fi
| |
17 | |
18 #pragma mark AutofillTooltip | |
19 | |
20 // The actual tooltip control - based on HoverButton, which comes with free | |
21 // hover handling. | |
22 @interface AutofillTooltip : HoverButton { | |
23 @private | |
24 id<AutofillTooltipDelegate> tooltipDelegate_; | |
25 } | |
26 | |
27 @property(nonatomic, assign) id<AutofillTooltipDelegate> tooltipDelegate; | |
Robert Sesek
2013/12/02 17:16:53
assign, nonatomic
groby-ooo-7-16
2013/12/02 20:41:57
Done.
| |
28 | |
29 @end | |
30 | |
31 | |
32 @implementation AutofillTooltip | |
33 | |
34 @synthesize tooltipDelegate = tooltipDelegate_; | |
35 | |
36 - (void)drawRect:(NSRect)rect { | |
37 [[self image] drawInRect:rect | |
38 fromRect:NSZeroRect | |
39 operation:NSCompositeSourceOver | |
40 fraction:1.0 | |
41 respectFlipped:YES | |
42 hints:nil]; | |
43 } | |
44 | |
45 - (void)setHoverState:(HoverState)state { | |
46 HoverState oldHoverState = [self hoverState]; | |
47 [super setHoverState:state]; | |
48 if (state != oldHoverState) { | |
49 switch (state) { | |
50 case kHoverStateNone: | |
51 [tooltipDelegate_ didEndHover]; | |
52 break; | |
53 case kHoverStateMouseOver: | |
54 [tooltipDelegate_ didBeginHover]; | |
55 break; | |
56 case kHoverStateMouseDown: | |
57 break; | |
58 } | |
59 } | |
60 } | |
61 | |
62 @end | |
63 | |
64 #pragma mark AutofillTooltipController | |
65 | |
66 @implementation AutofillTooltipController | |
67 | |
68 @synthesize message = message_; | |
69 | |
70 - (id)init { | |
71 if ((self = [super init])) { | |
72 view_.reset([[AutofillTooltip alloc] init]); | |
73 [self setView:view_]; | |
74 [view_ setTooltipDelegate:self]; | |
75 } | |
76 return self; | |
77 } | |
78 | |
79 - (void)dealloc { | |
80 [[NSNotificationCenter defaultCenter] | |
81 removeObserver:self | |
82 name:NSWindowWillCloseNotification | |
83 object:[bubbleController_ window]]; | |
84 [super dealloc]; | |
85 } | |
86 | |
87 - (void)setImage:(NSImage*)image { | |
88 [view_ setImage:image]; | |
89 [view_ setFrameSize:[image size]]; | |
90 } | |
91 | |
92 - (void)didBeginHover { | |
93 [NSObject cancelPreviousPerformRequestsWithTarget:self]; | |
94 // Start a timer to display the tooltip, unless it's already displayed. | |
95 if (!bubbleController_) { | |
96 [self performSelector:@selector(displayHover) | |
97 withObject:nil | |
98 afterDelay:kTooltipDelay]; | |
99 } | |
100 } | |
101 | |
102 - (void)tooltipWindowWillClose:(NSNotification*)notification { | |
103 bubbleController_ = nil; | |
104 } | |
105 | |
106 - (void)displayHover { | |
107 [bubbleController_ close]; | |
108 bubbleController_ = | |
109 [[AutofillBubbleController alloc] | |
110 initWithParentWindow:[[self view] window] | |
111 message:[self message] | |
112 inset:NSMakeSize(kTooltipInset, kTooltipInset)]; | |
113 | |
114 // Handle bubble self-deleting. | |
115 NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; | |
116 [center addObserver:self | |
117 selector:@selector(tooltipWindowWillClose:) | |
118 name:NSWindowWillCloseNotification | |
119 object:[bubbleController_ window]]; | |
120 | |
121 // Compute anchor point (in window coords - views might be flipped). | |
122 NSRect viewRect = [view_ convertRect:[view_ bounds] toView:nil]; | |
123 NSPoint anchorPoint = NSMakePoint(NSMidX(viewRect), NSMinY(viewRect)); | |
124 [bubbleController_ setAnchorPoint: | |
125 [[[self view] window] convertBaseToScreen:anchorPoint]]; | |
126 [bubbleController_ showWindow:self]; | |
127 } | |
128 | |
129 - (void)hideHover { | |
130 [bubbleController_ close]; | |
131 } | |
132 | |
133 - (void)didEndHover { | |
134 [NSObject cancelPreviousPerformRequestsWithTarget:self]; | |
135 // Start a timer to display the tooltip, unless it's already hidden. | |
136 if (bubbleController_) { | |
137 [self performSelector:@selector(hideHover) | |
138 withObject:nil | |
139 afterDelay:kTooltipDelay]; | |
140 } | |
141 } | |
142 | |
143 @end | |
144 | |
Robert Sesek
2013/12/02 17:16:53
nit: extra blank line
groby-ooo-7-16
2013/12/02 20:41:57
Done.
| |
OLD | NEW |