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/themed_window.h"
13 #include "chrome/browser/ui/infobar_container_delegate.h"
14 #include "components/infobars/core/infobar.h"
15 #include "skia/ext/skia_utils_mac.h"
16 #import "ui/base/cocoa/nsview_additions.h"
17 #include "ui/base/theme_provider.h"
19 @implementation InfoBarGradientView
21 @synthesize arrowHeight = arrowHeight_;
22 @synthesize arrowHalfWidth = arrowHalfWidth_;
23 @synthesize arrowX = arrowX_;
24 @synthesize hasTip = hasTip_;
26 - (id)initWithFrame:(NSRect)frame {
27 if ((self = [super initWithFrame:frame])) {
33 - (id)initWithCoder:(NSCoder*)decoder {
34 if ((self = [super initWithCoder:decoder])) {
40 - (void)setInfobarType:(infobars::InfoBarDelegate::Type)infobarType {
41 SkColor topColor = infobars::InfoBar::GetTopColor(infobarType);
42 SkColor bottomColor = infobars::InfoBar::GetBottomColor(infobarType);
43 base::scoped_nsobject<NSGradient> gradient([[NSGradient alloc]
44 initWithStartingColor:gfx::SkColorToCalibratedNSColor(topColor)
45 endingColor:gfx::SkColorToCalibratedNSColor(bottomColor)]);
46 [self setGradient:gradient];
49 - (NSColor*)strokeColor {
50 ui::ThemeProvider* themeProvider = [[self window] themeProvider];
52 return [NSColor blackColor];
54 BOOL active = [[self window] isMainWindow];
55 return themeProvider->GetNSColor(
56 active ? ThemeProperties::COLOR_TOOLBAR_STROKE :
57 ThemeProperties::COLOR_TOOLBAR_STROKE_INACTIVE);
60 - (void)drawRect:(NSRect)rect {
61 NSRect bounds = [self bounds];
62 bounds.size.height = InfoBarContainerDelegate::kDefaultBarTargetHeight;
64 CGFloat tipXOffset = arrowX_ - arrowHalfWidth_;
66 // Around the bounds of the infobar, continue drawing the path into which the
67 // gradient will be drawn.
68 NSBezierPath* infoBarPath = [NSBezierPath bezierPath];
69 [infoBarPath moveToPoint:NSMakePoint(NSMinX(bounds), NSMaxY(bounds))];
73 [infoBarPath lineToPoint:NSMakePoint(tipXOffset, NSMaxY(bounds))];
74 [infoBarPath relativeLineToPoint:NSMakePoint(arrowHalfWidth_,
76 [infoBarPath relativeLineToPoint:NSMakePoint(arrowHalfWidth_,
79 [infoBarPath lineToPoint:NSMakePoint(NSMaxX(bounds), NSMaxY(bounds))];
81 // Save off the top path of the infobar.
82 base::scoped_nsobject<NSBezierPath> topPath([infoBarPath copy]);
84 [infoBarPath lineToPoint:NSMakePoint(NSMaxX(bounds), NSMinY(bounds))];
85 [infoBarPath lineToPoint:NSMakePoint(NSMinX(bounds), NSMinY(bounds))];
86 [infoBarPath closePath];
89 [[self gradient] drawInBezierPath:infoBarPath angle:270];
91 NSColor* strokeColor = [self strokeColor];
95 // Stroke the bottom of the infobar.
96 NSRect borderRect, contentRect;
97 NSDivideRect(bounds, &borderRect, &contentRect, 1, NSMinYEdge);
98 NSRectFillUsingOperation(borderRect, NSCompositeSourceOver);
100 // Re-stroke the top because the tip will have no stroke. This will draw
101 // over the divider drawn by the bottom of the tabstrip.
105 // Add an inner stroke.
106 const CGFloat kHighlightTipHeight = arrowHeight_ - 1;
107 NSBezierPath* highlightPath = [NSBezierPath bezierPath];
108 [highlightPath moveToPoint:NSMakePoint(NSMinX(bounds), NSMaxY(bounds) - 1)];
110 [highlightPath relativeLineToPoint:NSMakePoint(tipXOffset + 1, 0)];
111 [highlightPath relativeLineToPoint:NSMakePoint(arrowHalfWidth_ - 1,
112 kHighlightTipHeight)];
113 [highlightPath relativeLineToPoint:NSMakePoint(arrowHalfWidth_ - 1,
114 -kHighlightTipHeight)];
116 [highlightPath lineToPoint:NSMakePoint(NSMaxX(bounds), NSMaxY(bounds) - 1)];
118 [[NSColor colorWithDeviceWhite:1.0 alpha:1.0] setStroke];
119 [highlightPath stroke];
122 - (BOOL)mouseDownCanMoveWindow {
126 // This view is intentionally not opaque because it overlaps with the findbar.
128 - (BOOL)accessibilityIsIgnored {
132 - (id)accessibilityAttributeValue:(NSString*)attribute {
133 if ([attribute isEqual:NSAccessibilityRoleAttribute])
134 return NSAccessibilityGroupRole;
136 return [super accessibilityAttributeValue:attribute];
139 - (void)setHasTip:(BOOL)hasTip {
140 if (hasTip_ == hasTip)
143 [self setNeedsDisplay:YES];