blob: c86f4639ac521adca7e0c337c9bad523110c0d37 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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.
4
5#include "chrome/browser/ui/cocoa/infobars/infobar_gradient_view.h"
6
Ben Murdocheb525c52013-07-10 11:40:50 +01007#include "base/mac/scoped_nsobject.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +00008#include "chrome/browser/infobars/infobar.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00009#import "chrome/browser/themes/theme_properties.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000010#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"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000015#include "ui/base/theme_provider.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000016
17namespace {
18
19const CGFloat kTipWidth = 23;
20
21} // namespace
22
23@implementation InfoBarGradientView
24
25@synthesize tipApex = tipApex_;
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010026@synthesize hasTip = hasTip_;
27
28- (id)initWithFrame:(NSRect)frame {
29 if ((self = [super initWithFrame:frame])) {
30 hasTip_ = YES;
31 }
32 return self;
33}
34
35- (id)initWithCoder:(NSCoder*)decoder {
36 if ((self = [super initWithCoder:decoder])) {
37 hasTip_ = YES;
38 }
39 return self;
40}
Torne (Richard Coles)58218062012-11-14 11:43:16 +000041
42- (void)setInfobarType:(InfoBarDelegate::Type)infobarType {
43 SkColor topColor = GetInfoBarTopColor(infobarType);
44 SkColor bottomColor = GetInfoBarBottomColor(infobarType);
Ben Murdocheb525c52013-07-10 11:40:50 +010045 base::scoped_nsobject<NSGradient> gradient([[NSGradient alloc]
Torne (Richard Coles)58218062012-11-14 11:43:16 +000046 initWithStartingColor:gfx::SkColorToCalibratedNSColor(topColor)
47 endingColor:gfx::SkColorToCalibratedNSColor(bottomColor)]);
48 [self setGradient:gradient];
49}
50
51- (NSColor*)strokeColor {
52 ui::ThemeProvider* themeProvider = [[self window] themeProvider];
53 if (!themeProvider)
54 return [NSColor blackColor];
55
56 BOOL active = [[self window] isMainWindow];
57 return themeProvider->GetNSColor(
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000058 active ? ThemeProperties::COLOR_TOOLBAR_STROKE :
Ben Murdochbb1529c2013-08-08 10:24:53 +010059 ThemeProperties::COLOR_TOOLBAR_STROKE_INACTIVE);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000060}
61
62- (void)drawRect:(NSRect)rect {
63 NSRect bounds = [self bounds];
64 bounds.size.height = infobars::kBaseHeight;
65
66 const CGFloat kHalfWidth = kTipWidth / 2.0;
67 NSPoint localApex = [self convertPoint:self.tipApex fromView:nil];
68 CGFloat tipXOffset = localApex.x - kHalfWidth;
69
70 // Around the bounds of the infobar, continue drawing the path into which the
71 // gradient will be drawn.
72 NSBezierPath* infoBarPath = [NSBezierPath bezierPath];
73 [infoBarPath moveToPoint:NSMakePoint(NSMinX(bounds), NSMaxY(bounds))];
74
75 // Draw the tip.
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010076 if (hasTip_) {
77 [infoBarPath lineToPoint:NSMakePoint(tipXOffset, NSMaxY(bounds))];
78 [infoBarPath relativeLineToPoint:NSMakePoint(kHalfWidth,
79 infobars::kTipHeight)];
80 [infoBarPath relativeLineToPoint:NSMakePoint(kHalfWidth,
81 -infobars::kTipHeight)];
82 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +000083 [infoBarPath lineToPoint:NSMakePoint(NSMaxX(bounds), NSMaxY(bounds))];
84
85 // Save off the top path of the infobar.
Ben Murdocheb525c52013-07-10 11:40:50 +010086 base::scoped_nsobject<NSBezierPath> topPath([infoBarPath copy]);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000087
88 [infoBarPath lineToPoint:NSMakePoint(NSMaxX(bounds), NSMinY(bounds))];
89 [infoBarPath lineToPoint:NSMakePoint(NSMinX(bounds), NSMinY(bounds))];
90 [infoBarPath closePath];
91
92 // Draw the gradient.
93 [[self gradient] drawInBezierPath:infoBarPath angle:270];
94
95 NSColor* strokeColor = [self strokeColor];
96 if (strokeColor) {
97 [strokeColor set];
98
99 // Stroke the bottom of the infobar.
100 NSRect borderRect, contentRect;
101 NSDivideRect(bounds, &borderRect, &contentRect, 1, NSMinYEdge);
102 NSRectFillUsingOperation(borderRect, NSCompositeSourceOver);
103
104 // Re-stroke the top because the tip will have no stroke. This will draw
105 // over the divider drawn by the bottom of the tabstrip.
106 [topPath stroke];
107 }
108
109 // Add an inner stroke.
110 const CGFloat kHighlightTipHeight = infobars::kTipHeight - 1;
111 NSBezierPath* highlightPath = [NSBezierPath bezierPath];
112 [highlightPath moveToPoint:NSMakePoint(NSMinX(bounds), NSMaxY(bounds) - 1)];
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100113 if (hasTip_) {
114 [highlightPath relativeLineToPoint:NSMakePoint(tipXOffset + 1, 0)];
115 [highlightPath relativeLineToPoint:NSMakePoint(kHalfWidth - 1,
116 kHighlightTipHeight)];
117 [highlightPath relativeLineToPoint:NSMakePoint(kHalfWidth - 1,
118 -kHighlightTipHeight)];
119 }
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000120 [highlightPath lineToPoint:NSMakePoint(NSMaxX(bounds), NSMaxY(bounds) - 1)];
121
122 [[NSColor colorWithDeviceWhite:1.0 alpha:1.0] setStroke];
123 [highlightPath stroke];
124}
125
126- (BOOL)mouseDownCanMoveWindow {
127 return NO;
128}
129
130// This view is intentionally not opaque because it overlaps with the findbar.
131
132- (BOOL)accessibilityIsIgnored {
133 return NO;
134}
135
136- (id)accessibilityAttributeValue:(NSString*)attribute {
137 if ([attribute isEqual:NSAccessibilityRoleAttribute])
138 return NSAccessibilityGroupRole;
139
140 return [super accessibilityAttributeValue:attribute];
141}
142
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100143- (void)setHasTip:(BOOL)hasTip {
144 if (hasTip_ == hasTip)
145 return;
146 hasTip_ = hasTip;
147 [self setNeedsDisplay:YES];
148}
149
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000150@end