blob: 1ed51139bb6977ad8fe5d425c1e7689f9622e6b5 [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/background_gradient_view.h"
6
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00007#import "chrome/browser/themes/theme_properties.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +00008#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"
12
13@interface BackgroundGradientView (Private)
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010014- (void)commonInit;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000015- (NSColor*)backgroundImageColor;
16@end
17
18@implementation BackgroundGradientView
Ben Murdocheb525c52013-07-10 11:40:50 +010019
Torne (Richard Coles)58218062012-11-14 11:43:16 +000020@synthesize showsDivider = showsDivider_;
21
22- (id)initWithFrame:(NSRect)frameRect {
23 if ((self = [super initWithFrame:frameRect])) {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010024 [self commonInit];
Torne (Richard Coles)58218062012-11-14 11:43:16 +000025 }
26 return self;
27}
28
29- (id)initWithCoder:(NSCoder*)decoder {
30 if ((self = [super initWithCoder:decoder])) {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010031 [self commonInit];
Torne (Richard Coles)58218062012-11-14 11:43:16 +000032 }
33 return self;
34}
35
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010036- (void)dealloc {
37 [[NSNotificationCenter defaultCenter] removeObserver:self];
38 [super dealloc];
39}
40
41- (void)commonInit {
42 showsDivider_ = YES;
43 [[NSNotificationCenter defaultCenter]
44 addObserver:self
45 selector:@selector(windowFocusDidChange:)
46 name:NSApplicationWillBecomeActiveNotification
47 object:NSApp];
48 [[NSNotificationCenter defaultCenter]
49 addObserver:self
50 selector:@selector(windowFocusDidChange:)
51 name:NSApplicationWillResignActiveNotification
52 object:NSApp];
53}
54
Torne (Richard Coles)58218062012-11-14 11:43:16 +000055- (void)setShowsDivider:(BOOL)show {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000056 if (showsDivider_ == show)
57 return;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000058 showsDivider_ = show;
59 [self setNeedsDisplay:YES];
60}
61
62- (void)drawBackgroundWithOpaque:(BOOL)opaque {
63 const NSRect bounds = [self bounds];
64
65 if (opaque) {
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];
69 NSRectFill(bounds);
70 }
71
72 [[self backgroundImageColor] set];
73 NSRectFillUsingOperation(bounds, NSCompositeSourceOver);
74
75 if (showsDivider_) {
76 // Draw bottom stroke
77 [[self strokeColor] set];
78 NSRect borderRect, contentRect;
79 NSDivideRect(bounds, &borderRect, &contentRect, [self cr_lineWidth],
80 NSMinYEdge);
81 NSRectFillUsingOperation(borderRect, NSCompositeSourceOver);
82 }
83}
84
85- (NSColor*)strokeColor {
Ben Murdocheb525c52013-07-10 11:40:50 +010086 NSWindow* window = [self window];
87 if ([window parentWindow])
88 window = [window parentWindow];
89
90 BOOL isActive = [window isMainWindow];
91 ui::ThemeProvider* themeProvider = [window themeProvider];
Torne (Richard Coles)58218062012-11-14 11:43:16 +000092 if (!themeProvider)
93 return [NSColor blackColor];
94 return themeProvider->GetNSColor(
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000095 isActive ? ThemeProperties::COLOR_TOOLBAR_STROKE :
Ben Murdochbb1529c2013-08-08 10:24:53 +010096 ThemeProperties::COLOR_TOOLBAR_STROKE_INACTIVE);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000097}
98
99- (NSColor*)backgroundImageColor {
100 ThemeService* themeProvider =
101 static_cast<ThemeService*>([[self window] themeProvider]);
102 if (!themeProvider)
103 return [[self window] backgroundColor];
104
105 // Themes don't have an inactive image so only look for one if there's no
106 // theme.
107 if (![[self window] isMainWindow] && themeProvider->UsingDefaultTheme()) {
108 NSColor* color = themeProvider->GetNSImageColorNamed(
Ben Murdochbb1529c2013-08-08 10:24:53 +0100109 IDR_THEME_TOOLBAR_INACTIVE);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000110 if (color)
111 return color;
112 }
113
Ben Murdochbb1529c2013-08-08 10:24:53 +0100114 return themeProvider->GetNSImageColorNamed(IDR_THEME_TOOLBAR);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000115}
116
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100117- (void)windowFocusDidChange:(NSNotification*)notification {
118 // The background color depends on the window's focus state.
Ben Murdocheb525c52013-07-10 11:40:50 +0100119 [self cr_recursivelySetNeedsDisplay:YES];
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100120}
121
122- (void)viewWillMoveToWindow:(NSWindow*)window {
123 if ([self window]) {
124 [[NSNotificationCenter defaultCenter]
125 removeObserver:self
126 name:NSWindowDidBecomeKeyNotification
127 object:[self window]];
128 [[NSNotificationCenter defaultCenter]
129 removeObserver:self
130 name:NSWindowDidBecomeMainNotification
131 object:[self window]];
132 }
133 if (window) {
134 [[NSNotificationCenter defaultCenter]
135 addObserver:self
136 selector:@selector(windowFocusDidChange:)
137 name:NSWindowDidBecomeKeyNotification
138 object:[self window]];
139 [[NSNotificationCenter defaultCenter]
140 addObserver:self
141 selector:@selector(windowFocusDidChange:)
142 name:NSWindowDidBecomeMainNotification
143 object:[self window]];
144 }
145 [super viewWillMoveToWindow:window];
146}
147
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];
152
153 [super setFrameOrigin:origin];
154}
155
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000156@end