blob: f063f8000d597c14318fc105153145e0ffe4bce0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "chrome/browser/ui/cocoa/hover_close_button.h"
#include "base/memory/scoped_nsobject.h"
#include "base/memory/scoped_ptr.h"
#import "chrome/browser/ui/cocoa/animation_utils.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#import "third_party/GTM/AppKit/GTMKeyValueAnimation.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
namespace {
const CGFloat kFramesPerSecond = 16; // Determined experimentally to look good.
const CGFloat kCloseAnimationDuration = 0.1;
// Images that are used for all close buttons. Set up in +initialize.
NSImage* gHoverNoneImage = nil;
NSImage* gHoverMouseOverImage = nil;
NSImage* gHoverMouseDownImage = nil;
// Strings that are used for all close buttons. Set up in +initialize.
NSString* gTooltip = nil;
NSString* gDescription = nil;
// If this string is changed, the setter (currently setFadeOutValue:) must
// be changed as well to match.
NSString* const kFadeOutValueKeyPath = @"fadeOutValue";
} // namespace
@interface HoverCloseButton ()
// Common initialization routine called from initWithFrame and awakeFromNib.
- (void)commonInit;
// Called by |fadeOutAnimation_| when animated value changes.
- (void)setFadeOutValue:(CGFloat)value;
@end
@implementation HoverCloseButton
+ (void)initialize {
if ([self class] == [HoverCloseButton class]) {
ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
gHoverNoneImage = bundle.GetNativeImageNamed(IDR_TAB_CLOSE).ToNSImage();
gHoverMouseOverImage =
bundle.GetNativeImageNamed(IDR_TAB_CLOSE_H).ToNSImage();
gHoverMouseDownImage =
bundle.GetNativeImageNamed(IDR_TAB_CLOSE_P).ToNSImage();
// Grab some strings that are used by all close buttons.
gDescription = [l10n_util::GetNSStringWithFixup(IDS_ACCNAME_CLOSE) copy];
gTooltip = [l10n_util::GetNSStringWithFixup(IDS_TOOLTIP_CLOSE_TAB) copy];
}
}
- (id)initWithFrame:(NSRect)frameRect {
if ((self = [super initWithFrame:frameRect])) {
[self commonInit];
}
return self;
}
- (void)awakeFromNib {
[super awakeFromNib];
[self commonInit];
}
- (void)removeFromSuperview {
// -stopAnimation will call the animationDidStop: delegate method
// which will release our animation.
[fadeOutAnimation_ stopAnimation];
[super removeFromSuperview];
}
- (void)animationDidStop:(NSAnimation*)animation {
DCHECK(animation == fadeOutAnimation_);
[fadeOutAnimation_ setDelegate:nil];
[fadeOutAnimation_ release];
fadeOutAnimation_ = nil;
}
- (void)animationDidEnd:(NSAnimation*)animation {
[self animationDidStop:animation];
}
- (void)drawRect:(NSRect)dirtyRect {
// Close boxes align left horizontally, and align center vertically.
// http:crbug.com/14739 requires this.
NSRect imageRect = NSZeroRect;
imageRect.size = [gHoverMouseOverImage size];
NSRect destRect = [self bounds];
destRect.origin.y = floor((NSHeight(destRect) / 2)
- (NSHeight(imageRect) / 2));
destRect.size = imageRect.size;
switch(self.hoverState) {
case kHoverStateMouseOver:
[gHoverMouseOverImage drawInRect:destRect
fromRect:imageRect
operation:NSCompositeSourceOver
fraction:1.0
respectFlipped:YES
hints:nil];
break;
case kHoverStateMouseDown:
[gHoverMouseDownImage drawInRect:destRect
fromRect:imageRect
operation:NSCompositeSourceOver
fraction:1.0
respectFlipped:YES
hints:nil];
break;
default:
case kHoverStateNone: {
CGFloat value = 1.0;
if (fadeOutAnimation_) {
value = [fadeOutAnimation_ currentValue];
NSImage* previousImage = nil;
if (previousState_ == kHoverStateMouseOver) {
previousImage = gHoverMouseOverImage;
} else {
previousImage = gHoverMouseDownImage;
}
[previousImage drawInRect:destRect
fromRect:imageRect
operation:NSCompositeSourceOver
fraction:1.0 - value
respectFlipped:YES
hints:nil];
}
[gHoverNoneImage drawInRect:destRect
fromRect:imageRect
operation:NSCompositeSourceOver
fraction:value
respectFlipped:YES
hints:nil];
break;
}
}
}
- (void)setFadeOutValue:(CGFloat)value {
[self setNeedsDisplay];
}
- (void)setHoverState:(HoverState)state {
if (state != self.hoverState) {
previousState_ = self.hoverState;
[super setHoverState:state];
// Only animate the HoverStateNone case.
if (state == kHoverStateNone) {
DCHECK(fadeOutAnimation_ == nil);
fadeOutAnimation_ =
[[GTMKeyValueAnimation alloc] initWithTarget:self
keyPath:kFadeOutValueKeyPath];
[fadeOutAnimation_ setDuration:kCloseAnimationDuration];
[fadeOutAnimation_ setFrameRate:kFramesPerSecond];
[fadeOutAnimation_ setDelegate:self];
[fadeOutAnimation_ startAnimation];
} else {
// -stopAnimation will call the animationDidStop: delegate method
// which will clean up the animation.
[fadeOutAnimation_ stopAnimation];
}
}
}
- (void)commonInit {
// Set accessibility description.
NSCell* cell = [self cell];
[cell accessibilitySetOverrideValue:gDescription
forAttribute:NSAccessibilityDescriptionAttribute];
// Add a tooltip. Using 'owner:self' means that
// -view:stringForToolTip:point:userData: will be called to provide the
// tooltip contents immediately before showing it.
[self addToolTipRect:[self bounds] owner:self userData:NULL];
// Initialize previousState.
previousState_ = kHoverStateNone;
}
// Called each time a tooltip is about to be shown.
- (NSString*)view:(NSView*)view
stringForToolTip:(NSToolTipTag)tag
point:(NSPoint)point
userData:(void*)userData {
if (self.hoverState == kHoverStateMouseOver) {
// In some cases (e.g. the download tray), the button is still in the
// hover state, but is outside the bounds of its parent and not visible.
// Don't show the tooltip in that case.
NSRect buttonRect = [self frame];
NSRect parentRect = [[self superview] bounds];
if (NSIntersectsRect(buttonRect, parentRect))
return gTooltip;
}
return nil; // Do not show the tooltip.
}
@end
|