summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/hover_close_button.mm
blob: b50cada4ebd6c84c2210635989110d4a4c69b728 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright (c) 2011 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"
#import "third_party/GTM/AppKit/GTMKeyValueAnimation.h"
#import "third_party/molokocacao/NSBezierPath+MCAdditions.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"

namespace  {
const CGFloat kButtonWidth = 16;
const CGFloat kFramesPerSecond = 16; // Determined experimentally to look good.
const CGFloat kCircleRadius = 0.415 * kButtonWidth;
const CGFloat kCircleHoverWhite = 0.565;
const CGFloat kCircleClickWhite = 0.396;
const CGFloat kXShadowAlpha = 0.75;
const CGFloat kXShadowCircleAlpha = 0.1;
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;

// Returns an autoreleased NSImage of the close button in a given state.
+ (NSImage*)imageForBounds:(NSRect)bounds
                     xPath:(NSBezierPath*)xPath
                circlePath:(NSBezierPath*)circlePath
                hoverState:(HoverState)hoverState;
@end

@implementation HoverCloseButton

+ (void)initialize {
  if ([self class] == [HoverCloseButton class]) {
    // Set up the paths for our images. They are centered around the origin.
    NSRect bounds = NSMakeRect(0, 0, kButtonWidth, kButtonWidth);
    NSBezierPath* circlePath = [NSBezierPath bezierPath];
    [circlePath appendBezierPathWithArcWithCenter:NSZeroPoint
                                           radius:kCircleRadius
                                       startAngle:0.0
                                         endAngle:365.0];

    // Construct an 'x' by drawing two intersecting rectangles in the shape of a
    // cross and then rotating the path by 45 degrees.
    NSBezierPath* xPath = [NSBezierPath bezierPath];
    [xPath appendBezierPathWithRect:NSMakeRect(-4.5, -1.0, 9.0, 2.0)];
    [xPath appendBezierPathWithRect:NSMakeRect(-1.0, -4.5, 2.0, 9.0)];

    NSAffineTransform* transform = [NSAffineTransform transform];
    [transform rotateByDegrees:45.0];
    [xPath transformUsingAffineTransform:transform];

    // Move the paths into the center of the given bounds rectangle.
    transform = [NSAffineTransform transform];
    NSPoint xCenter = NSMakePoint(NSWidth(bounds) / 2.0,
                                  NSHeight(bounds) / 2.0);
    [transform translateXBy:xCenter.x yBy:xCenter.y];
    [circlePath transformUsingAffineTransform:transform];
    [xPath transformUsingAffineTransform:transform];

    NSImage* image = [self imageForBounds:bounds
                                    xPath:xPath
                               circlePath:circlePath
                               hoverState:kHoverStateNone];
    gHoverNoneImage = [image retain];
    image = [self imageForBounds:bounds
                           xPath:xPath
                      circlePath:circlePath
                      hoverState:kHoverStateMouseOver];
    gHoverMouseOverImage = [image retain];
    image = [self imageForBounds:bounds
                           xPath:xPath
                      circlePath:circlePath
                      hoverState:kHoverStateMouseDown];
    gHoverMouseDownImage = [image retain];

    // 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];
      break;

    case kHoverStateMouseDown:
      [gHoverMouseDownImage drawInRect:destRect
                              fromRect:imageRect
                             operation:NSCompositeSourceOver
                              fraction:1.0];
      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];
      }
      [gHoverNoneImage drawInRect:destRect
                         fromRect:imageRect
                        operation:NSCompositeSourceOver
                         fraction:value];
      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.
  [self setToolTip:gTooltip];

  // Initialize previousState.
  previousState_ = kHoverStateNone;
}

+ (NSImage*)imageForBounds:(NSRect)bounds
                     xPath:(NSBezierPath*)xPath
                circlePath:(NSBezierPath*)circlePath
                hoverState:(HoverState)hoverState {
  gfx::ScopedNSGraphicsContextSaveGState graphicsStateSaver;

  scoped_nsobject<NSBitmapImageRep> imageRep(
      [[NSBitmapImageRep alloc]
        initWithBitmapDataPlanes:NULL
                      pixelsWide:NSWidth(bounds)
                      pixelsHigh:NSHeight(bounds)
                   bitsPerSample:8
                 samplesPerPixel:4
                        hasAlpha:YES
                        isPlanar:NO
                  colorSpaceName:NSCalibratedRGBColorSpace
                    bitmapFormat:0
                     bytesPerRow:kButtonWidth * 4
                    bitsPerPixel:32]);
  NSGraphicsContext* gc =
      [NSGraphicsContext graphicsContextWithBitmapImageRep:imageRep];
  [NSGraphicsContext setCurrentContext:gc];

  [[NSColor clearColor] set];
  NSRectFill(bounds);

  // If the user is hovering over the button, a light/dark gray circle is drawn
  // behind the 'x'.
  if (hoverState != kHoverStateNone) {
    // Adjust the darkness of the circle depending on whether it is being
    // clicked.
    CGFloat white = (hoverState == kHoverStateMouseOver) ?
        kCircleHoverWhite : kCircleClickWhite;
    [[NSColor colorWithCalibratedWhite:white alpha:1.0] set];
    [circlePath fill];
  }

  [[NSColor whiteColor] set];
  [xPath fill];

  // Give the 'x' an inner shadow for depth. If the button is in a hover state
  // (circle behind it), then adjust the shadow accordingly (not as harsh).
  NSShadow* shadow = [[[NSShadow alloc] init] autorelease];
  CGFloat alpha = (hoverState != kHoverStateNone) ?
      kXShadowCircleAlpha : kXShadowAlpha;
  [shadow setShadowColor:[NSColor colorWithCalibratedWhite:0.15 alpha:alpha]];
  [shadow setShadowOffset:NSMakeSize(0.0, 0.0)];
  [shadow setShadowBlurRadius:2.5];
  [xPath fillWithInnerShadow:shadow];
  NSImage* image = [[[NSImage alloc] initWithSize:bounds.size] autorelease];
  [image addRepresentation:imageRep];
  return image;
}

@end