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 #import "chrome/browser/themes/theme_properties.h"
9 #import "chrome/browser/ui/cocoa/browser_window_controller.h"
10 #import "chrome/browser/ui/cocoa/infobars/infobar_container_controller.h"
11 #import "chrome/browser/ui/cocoa/location_bar/location_bar_view_mac.h"
12 #import "chrome/browser/ui/cocoa/nsview_additions.h"
13 #import "chrome/browser/ui/cocoa/themed_window.h"
14 #include "components/infobars/core/infobar.h"
15 #include "skia/ext/skia_utils_mac.h"
16 #include "ui/base/theme_provider.h"
18 @implementation InfoBarGradientView
20 @synthesize arrowHeight = arrowHeight_;
21 @synthesize arrowHalfWidth = arrowHalfWidth_;
22 @synthesize arrowX = arrowX_;
23 @synthesize hasTip = hasTip_;
25 - (id)initWithFrame:(NSRect)frame {
26 if ((self = [super initWithFrame:frame])) {
32 - (id)initWithCoder:(NSCoder*)decoder {
33 if ((self = [super initWithCoder:decoder])) {
39 - (void)setInfobarType:(infobars::InfoBarDelegate::Type)infobarType {
40 SkColor topColor = infobars::InfoBar::GetTopColor(infobarType);
41 SkColor bottomColor = infobars::InfoBar::GetBottomColor(infobarType);
42 base::scoped_nsobject<NSGradient> gradient([[NSGradient alloc]
43 initWithStartingColor:gfx::SkColorToCalibratedNSColor(topColor)
44 endingColor:gfx::SkColorToCalibratedNSColor(bottomColor)]);
45 [self setGradient:gradient];
48 - (NSColor*)strokeColor {
49 ui::ThemeProvider* themeProvider = [[self window] themeProvider];
51 return [NSColor blackColor];
53 BOOL active = [[self window] isMainWindow];
54 return themeProvider->GetNSColor(
55 active ? ThemeProperties::COLOR_TOOLBAR_STROKE :
56 ThemeProperties::COLOR_TOOLBAR_STROKE_INACTIVE);
59 - (void)drawRect:(NSRect)rect {
60 NSRect bounds = [self bounds];
61 bounds.size.height = infobars::InfoBar::kDefaultBarTargetHeight;
63 CGFloat tipXOffset = arrowX_ - arrowHalfWidth_;
65 // Around the bounds of the infobar, continue drawing the path into which the
66 // gradient will be drawn.
67 NSBezierPath* infoBarPath = [NSBezierPath bezierPath];
68 [infoBarPath moveToPoint:NSMakePoint(NSMinX(bounds), NSMaxY(bounds))];
72 [infoBarPath lineToPoint:NSMakePoint(tipXOffset, NSMaxY(bounds))];
73 [infoBarPath relativeLineToPoint:NSMakePoint(arrowHalfWidth_,
75 [infoBarPath relativeLineToPoint:NSMakePoint(arrowHalfWidth_,
78 [infoBarPath lineToPoint:NSMakePoint(NSMaxX(bounds), NSMaxY(bounds))];
80 // Save off the top path of the infobar.
81 base::scoped_nsobject<NSBezierPath> topPath([infoBarPath copy]);
83 [infoBarPath lineToPoint:NSMakePoint(NSMaxX(bounds), NSMinY(bounds))];
84 [infoBarPath lineToPoint:NSMakePoint(NSMinX(bounds), NSMinY(bounds))];
85 [infoBarPath closePath];
88 [[self gradient] drawInBezierPath:infoBarPath angle:270];
90 NSColor* strokeColor = [self strokeColor];
94 // Stroke the bottom of the infobar.
95 NSRect borderRect, contentRect;
96 NSDivideRect(bounds, &borderRect, &contentRect, 1, NSMinYEdge);
97 NSRectFillUsingOperation(borderRect, NSCompositeSourceOver);
99 // Re-stroke the top because the tip will have no stroke. This will draw
100 // over the divider drawn by the bottom of the tabstrip.
104 // Add an inner stroke.
105 const CGFloat kHighlightTipHeight = arrowHeight_ - 1;
106 NSBezierPath* highlightPath = [NSBezierPath bezierPath];
107 [highlightPath moveToPoint:NSMakePoint(NSMinX(bounds), NSMaxY(bounds) - 1)];
109 [highlightPath relativeLineToPoint:NSMakePoint(tipXOffset + 1, 0)];
110 [highlightPath relativeLineToPoint:NSMakePoint(arrowHalfWidth_ - 1,
111 kHighlightTipHeight)];
112 [highlightPath relativeLineToPoint:NSMakePoint(arrowHalfWidth_ - 1,
113 -kHighlightTipHeight)];
115 [highlightPath lineToPoint:NSMakePoint(NSMaxX(bounds), NSMaxY(bounds) - 1)];
117 [[NSColor colorWithDeviceWhite:1.0 alpha:1.0] setStroke];
118 [highlightPath stroke];
121 - (BOOL)mouseDownCanMoveWindow {
125 // This view is intentionally not opaque because it overlaps with the findbar.
127 - (BOOL)accessibilityIsIgnored {
131 - (id)accessibilityAttributeValue:(NSString*)attribute {
132 if ([attribute isEqual:NSAccessibilityRoleAttribute])
133 return NSAccessibilityGroupRole;
135 return [super accessibilityAttributeValue:attribute];
138 - (void)setHasTip:(BOOL)hasTip {
139 if (hasTip_ == hasTip)
142 [self setNeedsDisplay:YES];