1 // Copyright (c) 2012 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 #include "chrome/browser/ui/cocoa/infobars/infobar_gradient_view.h"
7 #include "base/mac/scoped_nsobject.h"
8 #include "chrome/browser/infobars/infobar.h"
9 #import "chrome/browser/themes/theme_properties.h"
10 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
11 #import "chrome/browser/ui/cocoa/infobars/infobar_container_controller.h"
12 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
13 #import "chrome/browser/ui/cocoa/themed_window.h"
14 #include "skia/ext/skia_utils_mac.h"
15 #include "ui/base/theme_provider.h"
17 @implementation InfoBarGradientView
19 @synthesize arrowHeight = arrowHeight_;
20 @synthesize arrowHalfWidth = arrowHalfWidth_;
21 @synthesize arrowX = arrowX_;
22 @synthesize hasTip = hasTip_;
24 - (id)initWithFrame:(NSRect)frame {
25 if ((self = [super initWithFrame:frame])) {
31 - (id)initWithCoder:(NSCoder*)decoder {
32 if ((self = [super initWithCoder:decoder])) {
38 - (void)setInfobarType:(InfoBarDelegate::Type)infobarType {
39 SkColor topColor = GetInfoBarTopColor(infobarType);
40 SkColor bottomColor = GetInfoBarBottomColor(infobarType);
41 base::scoped_nsobject<NSGradient> gradient([[NSGradient alloc]
42 initWithStartingColor:gfx::SkColorToCalibratedNSColor(topColor)
43 endingColor:gfx::SkColorToCalibratedNSColor(bottomColor)]);
44 [self setGradient:gradient];
47 - (NSColor*)strokeColor {
48 ui::ThemeProvider* themeProvider = [[self window] themeProvider];
50 return [NSColor blackColor];
52 BOOL active = [[self window] isMainWindow];
53 return themeProvider->GetNSColor(
54 active ? ThemeProperties::COLOR_TOOLBAR_STROKE :
55 ThemeProperties::COLOR_TOOLBAR_STROKE_INACTIVE);
58 - (void)drawRect:(NSRect)rect {
59 NSRect bounds = [self bounds];
60 bounds.size.height = InfoBar::kDefaultBarTargetHeight;
62 CGFloat tipXOffset = arrowX_ - arrowHalfWidth_;
64 // Around the bounds of the infobar, continue drawing the path into which the
65 // gradient will be drawn.
66 NSBezierPath* infoBarPath = [NSBezierPath bezierPath];
67 [infoBarPath moveToPoint:NSMakePoint(NSMinX(bounds), NSMaxY(bounds))];
71 [infoBarPath lineToPoint:NSMakePoint(tipXOffset, NSMaxY(bounds))];
72 [infoBarPath relativeLineToPoint:NSMakePoint(arrowHalfWidth_,
74 [infoBarPath relativeLineToPoint:NSMakePoint(arrowHalfWidth_,
77 [infoBarPath lineToPoint:NSMakePoint(NSMaxX(bounds), NSMaxY(bounds))];
79 // Save off the top path of the infobar.
80 base::scoped_nsobject<NSBezierPath> topPath([infoBarPath copy]);
82 [infoBarPath lineToPoint:NSMakePoint(NSMaxX(bounds), NSMinY(bounds))];
83 [infoBarPath lineToPoint:NSMakePoint(NSMinX(bounds), NSMinY(bounds))];
84 [infoBarPath closePath];
87 [[self gradient] drawInBezierPath:infoBarPath angle:270];
89 NSColor* strokeColor = [self strokeColor];
93 // Stroke the bottom of the infobar.
94 NSRect borderRect, contentRect;
95 NSDivideRect(bounds, &borderRect, &contentRect, 1, NSMinYEdge);
96 NSRectFillUsingOperation(borderRect, NSCompositeSourceOver);
98 // Re-stroke the top because the tip will have no stroke. This will draw
99 // over the divider drawn by the bottom of the tabstrip.
103 // Add an inner stroke.
104 const CGFloat kHighlightTipHeight = arrowHeight_ - 1;
105 NSBezierPath* highlightPath = [NSBezierPath bezierPath];
106 [highlightPath moveToPoint:NSMakePoint(NSMinX(bounds), NSMaxY(bounds) - 1)];
108 [highlightPath relativeLineToPoint:NSMakePoint(tipXOffset + 1, 0)];
109 [highlightPath relativeLineToPoint:NSMakePoint(arrowHalfWidth_ - 1,
110 kHighlightTipHeight)];
111 [highlightPath relativeLineToPoint:NSMakePoint(arrowHalfWidth_ - 1,
112 -kHighlightTipHeight)];
114 [highlightPath lineToPoint:NSMakePoint(NSMaxX(bounds), NSMaxY(bounds) - 1)];
116 [[NSColor colorWithDeviceWhite:1.0 alpha:1.0] setStroke];
117 [highlightPath stroke];
120 - (BOOL)mouseDownCanMoveWindow {
124 // This view is intentionally not opaque because it overlaps with the findbar.
126 - (BOOL)accessibilityIsIgnored {
130 - (id)accessibilityAttributeValue:(NSString*)attribute {
131 if ([attribute isEqual:NSAccessibilityRoleAttribute])
132 return NSAccessibilityGroupRole;
134 return [super accessibilityAttributeValue:attribute];
137 - (void)setHasTip:(BOOL)hasTip {
138 if (hasTip_ == hasTip)
141 [self setNeedsDisplay:YES];