summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/new_tab_button.mm
blob: bc924855916470a93f7d27c028fcf7742c0f3458 (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
// Copyright (c) 2010 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/cocoa/new_tab_button.h"

@implementation NewTabButton

// Approximate the shape. It doesn't need to be perfect. This will need to be
// updated if the size or shape of the icon ever changes.
// TODO(pinkerton): use a click mask image instead of hard-coding points.
- (NSBezierPath*)pathForButton {
  if (imagePath_.get())
    return imagePath_.get();

  // Cache the path as it doesn't change (the coordinates are local to this
  // view). There's not much point making constants for these, as they are
  // custom.
  imagePath_.reset([[NSBezierPath bezierPath] retain]);
  [imagePath_ moveToPoint:NSMakePoint(9, 7)];
  [imagePath_ lineToPoint:NSMakePoint(26, 7)];
  [imagePath_ lineToPoint:NSMakePoint(33, 23)];
  [imagePath_ lineToPoint:NSMakePoint(14, 23)];
  [imagePath_ lineToPoint:NSMakePoint(9, 7)];
  return imagePath_;
}

- (BOOL)pointIsOverButton:(NSPoint)point {
  NSPoint localPoint = [self convertPoint:point fromView:[self superview]];
  NSBezierPath* buttonPath = [self pathForButton];
  return [buttonPath containsPoint:localPoint];
}

// Override to only accept clicks within the bounds of the defined path, not
// the entire bounding box. |aPoint| is in the superview's coordinate system.
- (NSView*)hitTest:(NSPoint)aPoint {
  if ([self pointIsOverButton:aPoint])
    return [super hitTest:aPoint];
  return nil;
}

@end