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.
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
71 backing:NSBackingStoreBuffered
73 [ns_window setCommandDispatcherDelegate:command_dispatcher_delegate_];
74 [ns_window setCommandHandler:[[[BrowserWindowCommandHandler alloc] init]
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;
88 bool BrowserFrameMac::UseCustomFrame() const {
92 bool BrowserFrameMac::UsesNativeSystemMenu() const {
96 bool BrowserFrameMac::ShouldSaveWindowPlacement() const {
100 void BrowserFrameMac::GetWindowPlacement(
102 ui::WindowShowState* show_state) const {
103 return NativeWidgetMac::GetWindowPlacement(bounds, show_state);
106 int BrowserFrameMac::GetMinimizeButtonOffset() const {