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.h"
7 #import "chrome/browser/ui/cocoa/nsview_additions.h"
8 #import "chrome/browser/ui/cocoa/themed_window.h"
9 #import "chrome/browser/ui/cocoa/url_drop_target.h"
10 #import "chrome/browser/ui/cocoa/view_id_util.h"
11 #import "ui/base/cocoa/nsgraphics_context_additions.h"
12 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
15 CGFloat kCurveSize = 8;
18 @implementation FindBarView
20 - (id)initWithFrame:(NSRect)frame {
21 if ((self = [super initWithFrame:frame])) {
22 // Give this view its own layer so that it can appear over the web contents
23 // view's layer. Layer squashing is not necessary for this view because
24 // NSTextField will correctly anti-alias text on 10.8 and beyond.
25 [self cr_setWantsLayer:YES];
30 - (void)awakeFromNib {
31 // Register for all the drag types handled by the RWHVCocoa.
32 [self registerForDraggedTypes:[URLDropTargetHandler handledDragTypes]];
35 - (void)drawRect:(NSRect)rect {
36 const CGFloat lineWidth = [self cr_lineWidth];
37 const CGFloat halfLineWidth = lineWidth / 2.0;
39 // TODO(rohitrao): Make this prettier.
40 rect = NSInsetRect([self bounds], halfLineWidth, halfLineWidth);
41 rect = NSOffsetRect(rect, 0, lineWidth);
43 NSPoint topLeft = NSMakePoint(NSMinX(rect), NSMaxY(rect));
44 NSPoint topRight = NSMakePoint(NSMaxX(rect), NSMaxY(rect));
46 NSMakePoint(NSMinX(rect) + kCurveSize, NSMaxY(rect) - kCurveSize);
48 NSMakePoint(NSMinX(rect) + kCurveSize, NSMinY(rect) + kCurveSize);
50 NSMakePoint(NSMaxX(rect) - kCurveSize, NSMinY(rect) + kCurveSize);
52 NSMakePoint(NSMaxX(rect) - kCurveSize, NSMaxY(rect) - kCurveSize);
54 NSMakePoint(NSMinX(rect) + (2 * kCurveSize), NSMinY(rect));
56 NSMakePoint(NSMaxX(rect) - (2 * kCurveSize), NSMinY(rect));
58 NSBezierPath* path = [NSBezierPath bezierPath];
59 [path moveToPoint:topLeft];
60 [path curveToPoint:midLeft1
61 controlPoint1:NSMakePoint(midLeft1.x, topLeft.y)
62 controlPoint2:NSMakePoint(midLeft1.x, topLeft.y)];
63 [path lineToPoint:midLeft2];
64 [path curveToPoint:bottomLeft
65 controlPoint1:NSMakePoint(midLeft2.x, bottomLeft.y)
66 controlPoint2:NSMakePoint(midLeft2.x, bottomLeft.y)];
68 [path lineToPoint:bottomRight];
69 [path curveToPoint:midRight1
70 controlPoint1:NSMakePoint(midRight1.x, bottomLeft.y)
71 controlPoint2:NSMakePoint(midRight1.x, bottomLeft.y)];
72 [path lineToPoint:midRight2];
73 [path curveToPoint:topRight
74 controlPoint1:NSMakePoint(midRight2.x, topLeft.y)
75 controlPoint2:NSMakePoint(midRight2.x, topLeft.y)];
78 gfx::ScopedNSGraphicsContextSaveGState scopedGState;
79 NSGraphicsContext* context = [NSGraphicsContext currentContext];
82 // Set the pattern phase
83 NSPoint position = [[self window]
84 themeImagePositionForAlignment:THEME_IMAGE_ALIGN_WITH_TAB_STRIP];
85 [context cr_setPatternPhase:position forView:self];
87 [super drawBackgroundWithOpaque:YES];
90 [[self strokeColor] set];
91 [path setLineWidth:lineWidth];
95 // The findbar is mostly opaque, but has an 8px transparent border on the left
96 // and right sides (see |kCurveSize|). This is an artifact of the way it is
97 // drawn. We override hitTest to return nil for points in this transparent
99 - (NSView*)hitTest:(NSPoint)point {
100 NSView* hitView = [super hitTest:point];
101 if (hitView == self) {
102 // |rect| is approximately equivalent to the opaque area of the findbar.
103 NSRect rect = NSInsetRect([self bounds], kCurveSize, 0);
104 if (!NSMouseInRect(point, rect, [self isFlipped]))
111 // Eat all mouse events, to prevent clicks from falling through to views below.
112 - (void)mouseDown:(NSEvent *)theEvent {
115 - (void)rightMouseDown:(NSEvent *)theEvent {
118 - (void)otherMouseDown:(NSEvent *)theEvent {
121 - (void)mouseUp:(NSEvent *)theEvent {
124 - (void)rightMouseUp:(NSEvent *)theEvent {
127 - (void)otherMouseUp:(NSEvent *)theEvent {
130 - (void)mouseMoved:(NSEvent *)theEvent {
133 - (void)mouseDragged:(NSEvent *)theEvent {
136 - (void)rightMouseDragged:(NSEvent *)theEvent {
139 - (void)otherMouseDragged:(NSEvent *)theEvent {
142 // Eat drag operations, to prevent drags from going through to the views below.
143 - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)info {
144 return NSDragOperationNone;
147 // Specifies that mouse events over this view should be ignored by the
149 - (BOOL)nonWebContentView {