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 // Delay time before tooltip shows/hides. |
| 12 const NSTimeInterval kTooltipDelay = 0.1; |
| 13 |
| 14 // How far to inset tooltip contents. |
| 15 CGFloat kTooltipInset = 10; |
| 16 |
| 17 #pragma mark AutofillTooltip |
| 18 |
| 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_; |
| 24 } |
| 25 |
| 26 @property(assign, nonatomic) id<AutofillTooltipDelegate> tooltipDelegate; |
| 27 |
| 28 @end |
| 29 |
| 30 |
| 31 @implementation AutofillTooltip |
| 32 |
| 33 @synthesize tooltipDelegate = tooltipDelegate_; |
| 34 |
| 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]; |
| 42 } |
| 43 |
| 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 } |
| 59 } |
| 60 |
| 61 @end |
| 62 |
| 63 #pragma mark AutofillTooltipController |
| 64 |
| 65 @implementation AutofillTooltipController |
| 66 |
| 67 @synthesize message = message_; |
| 68 |
| 69 - (id)init { |
| 70 if ((self = [super init])) { |
| 71 view_.reset([[AutofillTooltip alloc] init]); |
| 72 [self setView:view_]; |
| 73 [view_ setTooltipDelegate:self]; |
| 74 } |
| 75 return self; |
| 76 } |
| 77 |
| 78 - (void)dealloc { |
| 79 [[NSNotificationCenter defaultCenter] |
| 80 removeObserver:self |
| 81 name:NSWindowWillCloseNotification |
| 82 object:[bubbleController_ window]]; |
| 83 [super dealloc]; |
| 84 } |
| 85 |
| 86 - (void)setImage:(NSImage*)image { |
| 87 [view_ setImage:image]; |
| 88 [view_ setFrameSize:[image size]]; |
| 89 } |
| 90 |
| 91 - (void)didBeginHover { |
| 92 [NSObject cancelPreviousPerformRequestsWithTarget:self]; |
| 93 // Start a timer to display the tooltip, unless it's already displayed. |
| 94 if (!bubbleController_) { |
| 95 [self performSelector:@selector(displayHover) |
| 96 withObject:nil |
| 97 afterDelay:kTooltipDelay]; |
| 98 } |
| 99 } |
| 100 |
| 101 - (void)tooltipWindowWillClose:(NSNotification*)notification { |
| 102 bubbleController_ = nil; |
| 103 } |
| 104 |
| 105 - (void)displayHover { |
| 106 [bubbleController_ close]; |
| 107 bubbleController_ = |
| 108 [[AutofillBubbleController alloc] |
| 109 initWithParentWindow:[[self view] window] |
| 110 message:[self message] |
| 111 inset:NSMakeSize(kTooltipInset, kTooltipInset)]; |
| 112 |
| 113 // Handle bubble self-deleting. |
| 114 NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; |
| 115 [center addObserver:self |
| 116 selector:@selector(tooltipWindowWillClose:) |
| 117 name:NSWindowWillCloseNotification |
| 118 object:[bubbleController_ window]]; |
| 119 |
| 120 // Compute anchor point (in window coords - views might be flipped). |
| 121 NSRect viewRect = [view_ convertRect:[view_ bounds] toView:nil]; |
| 122 NSPoint anchorPoint = NSMakePoint(NSMidX(viewRect), NSMinY(viewRect)); |
| 123 [bubbleController_ setAnchorPoint: |
| 124 [[[self view] window] convertBaseToScreen:anchorPoint]]; |
| 125 [bubbleController_ showWindow:self]; |
| 126 } |
| 127 |
| 128 - (void)hideHover { |
| 129 [bubbleController_ close]; |
| 130 } |
| 131 |
| 132 - (void)didEndHover { |
| 133 [NSObject cancelPreviousPerformRequestsWithTarget:self]; |
| 134 // Start a timer to display the tooltip, unless it's already hidden. |
| 135 if (bubbleController_) { |
| 136 [self performSelector:@selector(hideHover) |
| 137 withObject:nil |
| 138 afterDelay:kTooltipDelay]; |
| 139 } |
| 140 } |
| 141 |
| 142 @end |
OLD | NEW |