1 // Copyright 2015 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 #import "chrome/browser/ui/cocoa/apps/titlebar_background_view.h"
7 #include "base/logging.h"
8 #import "skia/ext/skia_utils_mac.h"
10 @interface TitlebarBackgroundView ()
11 - (void)setColor:(NSColor*)color inactiveColor:(NSColor*)inactiveColor;
14 @implementation TitlebarBackgroundView
16 + (TitlebarBackgroundView*)addToNSWindow:(NSWindow*)window
17 activeColor:(SkColor)activeColor
18 inactiveColor:(SkColor)inactiveColor {
20 // AppKit only officially supports adding subviews to the window's
21 // contentView and not its superview (an NSNextStepFrame). The 10.10 SDK
22 // allows adding an NSTitlebarAccessoryViewController to a window, but the
23 // view can only be placed above the window control buttons, so we'd have to
25 NSView* window_view = [[window contentView] superview];
27 NSHeight([window_view bounds]) - NSHeight([[window contentView] bounds]);
28 base::scoped_nsobject<TitlebarBackgroundView> titlebar_background_view(
29 [[TitlebarBackgroundView alloc]
30 initWithFrame:NSMakeRect(0, NSMaxY([window_view bounds]) - height,
31 NSWidth([window_view bounds]), height)]);
32 [titlebar_background_view
33 setAutoresizingMask:NSViewWidthSizable | NSViewMinYMargin];
34 [window_view addSubview:titlebar_background_view
35 positioned:NSWindowBelow
38 [titlebar_background_view setColor:gfx::SkColorToSRGBNSColor(activeColor)
39 inactiveColor:gfx::SkColorToSRGBNSColor(inactiveColor)];
40 return titlebar_background_view.autorelease();
43 - (void)drawRect:(NSRect)rect {
44 // Only the top corners are rounded. For simplicity, round all 4 corners but
45 // draw the bottom corners outside of the visible bounds.
46 CGFloat cornerRadius = 4.0;
47 NSRect roundedRect = [self bounds];
48 roundedRect.origin.y -= cornerRadius;
49 roundedRect.size.height += cornerRadius;
50 [[NSBezierPath bezierPathWithRoundedRect:roundedRect
52 yRadius:cornerRadius] addClip];
53 if ([[self window] isMainWindow] || [[self window] isKeyWindow])
60 - (void)setColor:(NSColor*)color inactiveColor:(NSColor*)inactiveColor {
61 color_.reset([color retain]);
62 inactiveColor_.reset([inactiveColor retain]);