Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / frame / browser_frame_mac.mm
blobd28a4da4bae421fe9cc3406d82b383b6947a356f
1 // Copyright 2014 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 #include "chrome/browser/ui/views/frame/browser_frame_mac.h"
7 #import "chrome/browser/ui/cocoa/browser_window_command_handler.h"
8 #import "chrome/browser/ui/cocoa/chrome_command_dispatcher_delegate.h"
9 #include "chrome/browser/ui/views/frame/browser_frame.h"
10 #include "chrome/browser/ui/views/frame/browser_shutdown.h"
11 #include "chrome/browser/ui/views/frame/browser_view.h"
12 #import "chrome/browser/ui/views/frame/native_widget_mac_frameless_nswindow.h"
13 #include "components/web_modal/web_contents_modal_dialog_host.h"
14 #import "ui/base/cocoa/window_size_constants.h"
16 BrowserFrameMac::BrowserFrameMac(BrowserFrame* browser_frame,
17                                  BrowserView* browser_view)
18     : views::NativeWidgetMac(browser_frame),
19       browser_view_(browser_view),
20       command_dispatcher_delegate_(
21           [[ChromeCommandDispatcherDelegate alloc] init]) {}
23 BrowserFrameMac::~BrowserFrameMac() {
26 ////////////////////////////////////////////////////////////////////////////////
27 // BrowserFrameMac, views::NativeWidgetMac implementation:
29 void BrowserFrameMac::OnWindowWillClose() {
30   // Destroy any remaining WebContents early on. This is consistent with Aura.
31   // See comment in DesktopBrowserFrameAura::OnHostClosed().
32   DestroyBrowserWebContents(browser_view_->browser());
33   NativeWidgetMac::OnWindowWillClose();
36 int BrowserFrameMac::SheetPositionY() {
37   web_modal::WebContentsModalDialogHost* dialog_host =
38       browser_view_->GetWebContentsModalDialogHost();
39   NSView* view = dialog_host->GetHostView();
40   // Get the position of the host view relative to the window since
41   // ModalDialogHost::GetDialogPosition() is relative to the host view.
42   int host_view_y =
43       [view convertPoint:NSMakePoint(0, NSHeight([view frame])) toView:nil].y;
44   return host_view_y - dialog_host->GetDialogPosition(gfx::Size()).y();
47 void BrowserFrameMac::InitNativeWidget(
48     const views::Widget::InitParams& params) {
49   views::NativeWidgetMac::InitNativeWidget(params);
51   // Our content view draws on top of the titlebar area, but we want the window
52   // control buttons to draw on top of the content view.
53   // We do this by setting the content view's z-order below the buttons, and
54   // by giving the root view a layer so that the buttons get their own layers.
55   NSView* content_view = [GetNativeWindow() contentView];
56   NSView* root_view = [content_view superview];
57   [content_view removeFromSuperview];
58   [root_view setWantsLayer:YES];
59   [root_view addSubview:content_view positioned:NSWindowBelow relativeTo:nil];
62 NativeWidgetMacNSWindow* BrowserFrameMac::CreateNSWindow(
63     const views::Widget::InitParams& params) {
64   NSUInteger style_mask = NSTitledWindowMask | NSClosableWindowMask |
65                           NSMiniaturizableWindowMask | NSResizableWindowMask |
66                           NSTexturedBackgroundWindowMask;
67   base::scoped_nsobject<NativeWidgetMacFramelessNSWindow> ns_window(
68       [[NativeWidgetMacFramelessNSWindow alloc]
69           initWithContentRect:ui::kWindowSizeDeterminedLater
70                     styleMask:style_mask
71                       backing:NSBackingStoreBuffered
72                         defer:NO]);
73   [ns_window setCommandDispatcherDelegate:command_dispatcher_delegate_];
74   [ns_window setCommandHandler:[[[BrowserWindowCommandHandler alloc] init]
75                                    autorelease]];
76   return ns_window.autorelease();
79 ////////////////////////////////////////////////////////////////////////////////
80 // BrowserFrameMac, NativeBrowserFrame implementation:
82 views::Widget::InitParams BrowserFrameMac::GetWidgetParams() {
83   views::Widget::InitParams params;
84   params.native_widget = this;
85   return params;
88 bool BrowserFrameMac::UseCustomFrame() const {
89   return false;
92 bool BrowserFrameMac::UsesNativeSystemMenu() const {
93   return true;
96 bool BrowserFrameMac::ShouldSaveWindowPlacement() const {
97   return true;
100 void BrowserFrameMac::GetWindowPlacement(
101     gfx::Rect* bounds,
102     ui::WindowShowState* show_state) const {
103   return NativeWidgetMac::GetWindowPlacement(bounds, show_state);
106 int BrowserFrameMac::GetMinimizeButtonOffset() const {
107   NOTIMPLEMENTED();
108   return 0;