1 // Copyright 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.
5 #import "chrome/browser/ui/cocoa/autofill/autofill_tooltip_controller.h"
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"
11 // Delay time before tooltip shows/hides.
12 const NSTimeInterval kTooltipDelay = 0.1;
14 // How far to inset tooltip contents.
15 CGFloat kTooltipInset = 10;
17 #pragma mark AutofillTooltip
19 // The actual tooltip control - based on HoverButton, which comes with free
21 @interface AutofillTooltip : HoverButton {
23 id<AutofillTooltipDelegate> tooltipDelegate_;
26 @property(assign, nonatomic) id<AutofillTooltipDelegate> tooltipDelegate;
31 @implementation AutofillTooltip
33 @synthesize tooltipDelegate = tooltipDelegate_;
35 - (void)drawRect:(NSRect)rect {
36 [[self image] drawInRect:rect
38 operation:NSCompositeSourceOver
44 - (void)setHoverState:(HoverState)state {
45 HoverState oldHoverState = [self hoverState];
46 [super setHoverState:state];
47 if (state != oldHoverState) {
50 [tooltipDelegate_ didEndHover];
52 case kHoverStateMouseOver:
53 [tooltipDelegate_ didBeginHover];
55 case kHoverStateMouseDown:
63 #pragma mark AutofillTooltipController
65 @implementation AutofillTooltipController
67 @synthesize message = message_;
69 - (id)initWithArrowLocation:(info_bubble::BubbleArrowLocation)arrowLocation {
70 if ((self = [super init])) {
71 arrowLocation_ = arrowLocation;
72 view_.reset([[AutofillTooltip alloc] init]);
74 [view_ setTooltipDelegate:self];
80 [[NSNotificationCenter defaultCenter]
82 name:NSWindowWillCloseNotification
83 object:[bubbleController_ window]];
87 - (void)setImage:(NSImage*)image {
88 [view_ setImage:image];
89 [view_ setFrameSize:[image size]];
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)
98 afterDelay:kTooltipDelay];
102 - (void)tooltipWindowWillClose:(NSNotification*)notification {
103 bubbleController_ = nil;
106 - (void)displayHover {
107 [bubbleController_ close];
109 [[AutofillBubbleController alloc]
110 initWithParentWindow:[[self view] window]
111 message:[self message]
112 inset:NSMakeSize(kTooltipInset, kTooltipInset)
113 arrowLocation:arrowLocation_];
115 // Handle bubble self-deleting.
116 NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
117 [center addObserver:self
118 selector:@selector(tooltipWindowWillClose:)
119 name:NSWindowWillCloseNotification
120 object:[bubbleController_ window]];
122 // Compute anchor point (in window coords - views might be flipped).
123 NSRect viewRect = [view_ convertRect:[view_ bounds] toView:nil];
124 NSPoint anchorPoint = NSMakePoint(NSMidX(viewRect), NSMinY(viewRect));
125 [bubbleController_ setAnchorPoint:
126 [[[self view] window] convertBaseToScreen:anchorPoint]];
127 [bubbleController_ showWindow:self];
131 [bubbleController_ close];
134 - (void)didEndHover {
135 [NSObject cancelPreviousPerformRequestsWithTarget:self];
136 // Start a timer to display the tooltip, unless it's already hidden.
137 if (bubbleController_) {
138 [self performSelector:@selector(hideHover)
140 afterDelay:kTooltipDelay];