Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / constrained_window / constrained_window_custom_window.mm
blob102b19854dbe24042634fb7379d47d6a7a999de5
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
23                               styleMask:windowStyle
24                                 backing:NSBackingStoreBuffered
25                                   defer:NO])) {
26     base::scoped_nsobject<NSView> contentView(
27         [[ConstrainedWindowCustomWindowContentView alloc]
28             initWithFrame:NSZeroRect]);
29     [self setContentView:contentView];
30   }
31   return self;
34 - (id)initWithContentRect:(NSRect)contentRect
35                 styleMask:(NSUInteger)windowStyle
36                   backing:(NSBackingStoreType)bufferingType
37                     defer:(BOOL)deferCreation {
38   if ((self = [super initWithContentRect:contentRect
39                                styleMask:windowStyle
40                                  backing:bufferingType
41                                    defer:NO])) {
42     [self setHasShadow:YES];
43     [self setBackgroundColor:gfx::SkColorToCalibratedNSColor(
44         chrome_style::GetBackgroundColor())];
45     [self setOpaque:NO];
46     [self setReleasedWhenClosed:NO];
47   }
48   return self;
51 - (BOOL)canBecomeKeyWindow {
52   return YES;
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.
62   if (!sheetController)
63     return windowContent;
65   NSRect frame;
66   frame.origin = [sheetController originForSheet:sheet
67                                   withWindowSize:windowContent.size];
68   frame.size = windowContent.size;
69   return frame;
72 @end
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:
88       NSCompositeCopy];
89   [[NSColor clearColor] set];
90   [path fill];
92   [[self window] invalidateShadow];
95 @end