1 // Copyright (c) 2011 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.
5 #import "chrome/browser/ui/cocoa/find_bar/find_bar_view_cocoa.h"
7 #import "chrome/browser/ui/cocoa/url_drop_target.h"
8 #import "chrome/browser/ui/cocoa/view_id_util.h"
9 #import "ui/base/cocoa/nsview_additions.h"
10 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
13 CGFloat kCurveSize = 8;
16 @implementation FindBarView
18 - (id)initWithFrame:(NSRect)frame {
19 if ((self = [super initWithFrame:frame])) {
20 // Give this view its own layer so that it can appear over the web contents
21 // view's layer. Layer squashing is not helpful for this view because
22 // NSTextField will correctly anti-alias text on 10.8 and beyond.
23 [self setWantsLayer:YES];
28 - (void)awakeFromNib {
29 // Register for all the drag types handled by the RWHVCocoa.
30 [self registerForDraggedTypes:[URLDropTargetHandler handledDragTypes]];
33 - (void)drawRect:(NSRect)dirtyRect {
34 const CGFloat lineWidth = [self cr_lineWidth];
35 const CGFloat halfLineWidth = lineWidth / 2.0;
37 // TODO(rohitrao): Make this prettier.
38 NSRect rect = NSInsetRect([self bounds], halfLineWidth, halfLineWidth);
39 rect = NSOffsetRect(rect, 0, lineWidth);
41 NSPoint topLeft = NSMakePoint(NSMinX(rect), NSMaxY(rect));
42 NSPoint topRight = NSMakePoint(NSMaxX(rect), NSMaxY(rect));
44 NSMakePoint(NSMinX(rect) + kCurveSize, NSMaxY(rect) - kCurveSize);
46 NSMakePoint(NSMinX(rect) + kCurveSize, NSMinY(rect) + kCurveSize);
48 NSMakePoint(NSMaxX(rect) - kCurveSize, NSMinY(rect) + kCurveSize);
50 NSMakePoint(NSMaxX(rect) - kCurveSize, NSMaxY(rect) - kCurveSize);
52 NSMakePoint(NSMinX(rect) + (2 * kCurveSize), NSMinY(rect));
54 NSMakePoint(NSMaxX(rect) - (2 * kCurveSize), NSMinY(rect));
56 NSBezierPath* path = [NSBezierPath bezierPath];
57 [path moveToPoint:topLeft];
58 [path curveToPoint:midLeft1
59 controlPoint1:NSMakePoint(midLeft1.x, topLeft.y)
60 controlPoint2:NSMakePoint(midLeft1.x, topLeft.y)];
61 [path lineToPoint:midLeft2];
62 [path curveToPoint:bottomLeft
63 controlPoint1:NSMakePoint(midLeft2.x, bottomLeft.y)
64 controlPoint2:NSMakePoint(midLeft2.x, bottomLeft.y)];
66 [path lineToPoint:bottomRight];
67 [path curveToPoint:midRight1
68 controlPoint1:NSMakePoint(midRight1.x, bottomLeft.y)
69 controlPoint2:NSMakePoint(midRight1.x, bottomLeft.y)];
70 [path lineToPoint:midRight2];
71 [path curveToPoint:topRight
72 controlPoint1:NSMakePoint(midRight2.x, topLeft.y)
73 controlPoint2:NSMakePoint(midRight2.x, topLeft.y)];
76 gfx::ScopedNSGraphicsContextSaveGState scopedGState;
78 [self drawBackground:dirtyRect];
81 [[self strokeColor] set];
82 [path setLineWidth:lineWidth];
86 // The findbar is mostly opaque, but has an 8px transparent border on the left
87 // and right sides (see |kCurveSize|). This is an artifact of the way it is
88 // drawn. We override hitTest to return nil for points in this transparent
90 - (NSView*)hitTest:(NSPoint)point {
91 NSView* hitView = [super hitTest:point];
92 if (hitView == self) {
93 // |rect| is approximately equivalent to the opaque area of the findbar.
94 NSRect rect = NSInsetRect([self bounds], kCurveSize, 0);
95 if (!NSMouseInRect(point, rect, [self isFlipped]))
102 // Eat all mouse events, to prevent clicks from falling through to views below.
103 - (void)mouseDown:(NSEvent *)theEvent {
106 - (void)rightMouseDown:(NSEvent *)theEvent {
109 - (void)otherMouseDown:(NSEvent *)theEvent {
112 - (void)mouseUp:(NSEvent *)theEvent {
115 - (void)rightMouseUp:(NSEvent *)theEvent {
118 - (void)otherMouseUp:(NSEvent *)theEvent {
121 - (void)mouseMoved:(NSEvent *)theEvent {
124 - (void)mouseDragged:(NSEvent *)theEvent {
127 - (void)rightMouseDragged:(NSEvent *)theEvent {
130 - (void)otherMouseDragged:(NSEvent *)theEvent {
133 // Eat drag operations, to prevent drags from going through to the views below.
134 - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)info {
135 return NSDragOperationNone;
138 // Specifies that mouse events over this view should be ignored by the
140 - (BOOL)nonWebContentView {