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/background_gradient_view.h"
7 #import "chrome/browser/themes/theme_properties.h"
8 #import "chrome/browser/themes/theme_service.h"
9 #import "chrome/browser/ui/cocoa/nsview_additions.h"
10 #import "chrome/browser/ui/cocoa/themed_window.h"
11 #include "grit/theme_resources.h"
13 @interface BackgroundGradientView (Private)
15 - (NSColor*)backgroundImageColor;
18 @implementation BackgroundGradientView
20 @synthesize showsDivider = showsDivider_;
22 - (id)initWithFrame:(NSRect)frameRect {
23 if ((self = [super initWithFrame:frameRect])) {
29 - (id)initWithCoder:(NSCoder*)decoder {
30 if ((self = [super initWithCoder:decoder])) {
37 [[NSNotificationCenter defaultCenter] removeObserver:self];
43 [[NSNotificationCenter defaultCenter]
45 selector:@selector(windowFocusDidChange:)
46 name:NSApplicationWillBecomeActiveNotification
48 [[NSNotificationCenter defaultCenter]
50 selector:@selector(windowFocusDidChange:)
51 name:NSApplicationWillResignActiveNotification
55 - (void)setShowsDivider:(BOOL)show {
56 if (showsDivider_ == show)
59 [self setNeedsDisplay:YES];
62 - (void)drawBackgroundWithOpaque:(BOOL)opaque {
63 const NSRect bounds = [self bounds];
66 // If the background image is semi transparent then we need something
67 // to blend against. Using 20% black gives us a color similar to Windows.
68 [[NSColor colorWithCalibratedWhite:0.2 alpha:1.0] set];
72 [[self backgroundImageColor] set];
73 NSRectFillUsingOperation(bounds, NSCompositeSourceOver);
77 [[self strokeColor] set];
78 NSRect borderRect, contentRect;
79 NSDivideRect(bounds, &borderRect, &contentRect, [self cr_lineWidth],
81 NSRectFillUsingOperation(borderRect, NSCompositeSourceOver);
85 - (NSColor*)strokeColor {
86 NSWindow* window = [self window];
87 if ([window parentWindow])
88 window = [window parentWindow];
90 BOOL isActive = [window isMainWindow];
91 ui::ThemeProvider* themeProvider = [window themeProvider];
93 return [NSColor blackColor];
94 return themeProvider->GetNSColor(
95 isActive ? ThemeProperties::COLOR_TOOLBAR_STROKE :
96 ThemeProperties::COLOR_TOOLBAR_STROKE_INACTIVE);
99 - (NSColor*)backgroundImageColor {
100 ThemeService* themeProvider =
101 static_cast<ThemeService*>([[self window] themeProvider]);
103 return [[self window] backgroundColor];
105 // Themes don't have an inactive image so only look for one if there's no
107 if (![[self window] isMainWindow] && themeProvider->UsingDefaultTheme()) {
108 NSColor* color = themeProvider->GetNSImageColorNamed(
109 IDR_THEME_TOOLBAR_INACTIVE);
114 return themeProvider->GetNSImageColorNamed(IDR_THEME_TOOLBAR);
117 - (void)windowFocusDidChange:(NSNotification*)notification {
118 // The background color depends on the window's focus state.
119 [self cr_recursivelySetNeedsDisplay:YES];
122 - (void)viewWillMoveToWindow:(NSWindow*)window {
124 [[NSNotificationCenter defaultCenter]
126 name:NSWindowDidBecomeKeyNotification
127 object:[self window]];
128 [[NSNotificationCenter defaultCenter]
130 name:NSWindowDidBecomeMainNotification
131 object:[self window]];
134 [[NSNotificationCenter defaultCenter]
136 selector:@selector(windowFocusDidChange:)
137 name:NSWindowDidBecomeKeyNotification
138 object:[self window]];
139 [[NSNotificationCenter defaultCenter]
141 selector:@selector(windowFocusDidChange:)
142 name:NSWindowDidBecomeMainNotification
143 object:[self window]];
145 [super viewWillMoveToWindow:window];
148 - (void)setFrameOrigin:(NSPoint)origin {
149 // The background color depends on the view's vertical position.
150 if (NSMinY([self frame]) != origin.y)
151 [self setNeedsDisplay:YES];
153 [super setFrameOrigin:origin];