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 return [self initWithContentRect:contentRect
17 styleMask:NSBorderlessWindowMask];
20 - (id)initWithContentRect:(NSRect)contentRect
21 styleMask:(NSUInteger)windowStyle {
22 if ((self = [self initWithContentRect:contentRect
24 backing:NSBackingStoreBuffered
26 base::scoped_nsobject<NSView> contentView(
27 [[ConstrainedWindowCustomWindowContentView alloc]
28 initWithFrame:NSZeroRect]);
29 [self setContentView:contentView];
34 - (id)initWithContentRect:(NSRect)contentRect
35 styleMask:(NSUInteger)windowStyle
36 backing:(NSBackingStoreType)bufferingType
37 defer:(BOOL)deferCreation {
38 if ((self = [super initWithContentRect:contentRect
42 [self setHasShadow:YES];
43 [self setBackgroundColor:gfx::SkColorToCalibratedNSColor(
44 chrome_style::GetBackgroundColor())];
46 [self setReleasedWhenClosed:NO];
51 - (BOOL)canBecomeKeyWindow {
55 - (NSRect)frameRectForContentRect:(NSRect)windowContent {
56 id<ConstrainedWindowSheet> sheet = [ConstrainedWindowSheetController
57 sheetForOverlayWindow:[self parentWindow]];
58 ConstrainedWindowSheetController* sheetController =
59 [ConstrainedWindowSheetController controllerForSheet:sheet];
61 // Sheet controller may be nil if this window hasn't been shown yet.
66 frame.origin = [sheetController originForSheet:sheet
67 withWindowSize:windowContent.size];
68 frame.size = windowContent.size;
74 @implementation ConstrainedWindowCustomWindowContentView
76 - (void)drawRect:(NSRect)rect {
77 gfx::ScopedNSGraphicsContextSaveGState state;
79 // Draw symmetric difference between rect path and oval path as "clear".
80 NSBezierPath* ovalPath = [NSBezierPath
81 bezierPathWithRoundedRect:[self bounds]
82 xRadius:chrome_style::kBorderRadius
83 yRadius:chrome_style::kBorderRadius];
84 NSBezierPath* path = [NSBezierPath bezierPathWithRect:[self bounds]];
85 [path appendBezierPath:ovalPath];
86 [path setWindingRule:NSEvenOddWindingRule];
87 [[NSGraphicsContext currentContext] setCompositingOperation:
89 [[NSColor clearColor] set];
92 [[self window] invalidateShadow];