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 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_window.h"
7 #import "base/mac/scoped_nsobject.h"
8 #import "chrome/browser/ui/chrome_style.h"
9 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet_controller.h"
10 #include "skia/ext/skia_utils_mac.h"
11 #include "ui/gfx/scoped_ns_graphics_context_save_gstate_mac.h"
13 @implementation ConstrainedWindowCustomWindow
15 - (id)initWithContentRect:(NSRect)contentRect {
16 if ((self = [self initWithContentRect:contentRect
17 styleMask:NSBorderlessWindowMask
18 backing:NSBackingStoreBuffered
20 base::scoped_nsobject<NSView> contentView(
21 [[ConstrainedWindowCustomWindowContentView alloc]
22 initWithFrame:NSZeroRect]);
23 [self setContentView:contentView];
28 - (id)initWithContentRect:(NSRect)contentRect
29 styleMask:(NSUInteger)windowStyle
30 backing:(NSBackingStoreType)bufferingType
31 defer:(BOOL)deferCreation {
32 if ((self = [super initWithContentRect:contentRect
33 styleMask:NSBorderlessWindowMask
36 [self setHasShadow:YES];
37 [self setBackgroundColor:gfx::SkColorToCalibratedNSColor(
38 chrome_style::GetBackgroundColor())];
40 [self setReleasedWhenClosed:NO];
45 - (BOOL)canBecomeKeyWindow {
49 - (NSRect)frameRectForContentRect:(NSRect)windowContent {
50 id<ConstrainedWindowSheet> sheet = [ConstrainedWindowSheetController
51 sheetForOverlayWindow:[self parentWindow]];
52 ConstrainedWindowSheetController* sheetController =
53 [ConstrainedWindowSheetController controllerForSheet:sheet];
55 // Sheet controller may be nil if this window hasn't been shown yet.
60 frame.origin = [sheetController originForSheet:sheet
61 withWindowSize:windowContent.size];
62 frame.size = windowContent.size;
68 @implementation ConstrainedWindowCustomWindowContentView
70 - (void)drawRect:(NSRect)rect {
71 gfx::ScopedNSGraphicsContextSaveGState state;
73 // Draw symmetric difference between rect path and oval path as "clear".
74 NSBezierPath* ovalPath = [NSBezierPath
75 bezierPathWithRoundedRect:[self bounds]
76 xRadius:chrome_style::kBorderRadius
77 yRadius:chrome_style::kBorderRadius];
78 NSBezierPath* path = [NSBezierPath bezierPathWithRect:[self bounds]];
79 [path appendBezierPath:ovalPath];
80 [path setWindingRule:NSEvenOddWindingRule];
81 [[NSGraphicsContext currentContext] setCompositingOperation:
83 [[NSColor clearColor] set];
86 [[self window] invalidateShadow];