Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / autofill / autofill_tooltip_controller.mm
blob9850b71575496b9588e850faf4709e7662a3398e
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
20 // hover handling.
21 @interface AutofillTooltip : HoverButton {
22  @private
23   id<AutofillTooltipDelegate> tooltipDelegate_;
26 @property(assign, nonatomic) id<AutofillTooltipDelegate> tooltipDelegate;
28 @end
31 @implementation AutofillTooltip
33 @synthesize tooltipDelegate = tooltipDelegate_;
35 - (void)drawRect:(NSRect)rect {
36  [[self image] drawInRect:rect
37                  fromRect:NSZeroRect
38                 operation:NSCompositeSourceOver
39                  fraction:1.0
40            respectFlipped:YES
41                     hints:nil];
44 - (void)setHoverState:(HoverState)state {
45   HoverState oldHoverState = [self hoverState];
46   [super setHoverState:state];
47   if (state != oldHoverState) {
48     switch (state) {
49       case kHoverStateNone:
50         [tooltipDelegate_ didEndHover];
51         break;
52       case kHoverStateMouseOver:
53         [tooltipDelegate_ didBeginHover];
54         break;
55       case kHoverStateMouseDown:
56         break;
57     }
58   }
61 @end
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]);
73     [self setView:view_];
74     [view_ setTooltipDelegate:self];
75   }
76   return self;
79 - (void)dealloc {
80   [[NSNotificationCenter defaultCenter]
81       removeObserver:self
82                 name:NSWindowWillCloseNotification
83               object:[bubbleController_ window]];
84   [super dealloc];
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)
97                withObject:nil
98                afterDelay:kTooltipDelay];
99   }
102 - (void)tooltipWindowWillClose:(NSNotification*)notification {
103   bubbleController_ = nil;
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                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];
130 - (void)hideHover {
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)
139                withObject:nil
140                afterDelay:kTooltipDelay];
141   }
144 @end