summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/new_tab_button.mm
blob: 2062681e26f7e0a9b6c3cb48001f7a47979886d3 (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
// 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/new_tab_button.h"
#import "chrome/browser/ui/cocoa/image_button_cell.h"

// A simple override of the ImageButtonCell to disable handling of
// -mouseEntered.
@interface NewTabButtonCell : ImageButtonCell

- (void)mouseEntered:(NSEvent*)theEvent;

@end

@implementation NewTabButtonCell

- (void)mouseEntered:(NSEvent*)theEvent {
  // Ignore this since the NTB enter is handled by the TabStripController.
}

@end


@implementation NewTabButton

+ (Class)cellClass {
  return [NewTabButtonCell class];
}

// 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