| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/cocoa/web_contents_modal_dialog_host_cocoa.h" | 5 #include "chrome/browser/ui/cocoa/web_contents_modal_dialog_host_cocoa.h" |
| 6 | 6 |
| 7 #include "chrome/browser/ui/browser_finder.h" | 7 #include "chrome/browser/ui/browser_finder.h" |
| 8 #include "chrome/browser/ui/tab_dialogs.h" | 8 #include "chrome/browser/ui/tab_dialogs.h" |
| 9 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 9 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 10 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet_con
troller.h" | 10 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_sheet_con
troller.h" |
| 11 #include "ui/gfx/geometry/point.h" | 11 #include "ui/gfx/geometry/point.h" |
| 12 #include "ui/gfx/geometry/size.h" | 12 #include "ui/gfx/geometry/size.h" |
| 13 #import "ui/gfx/mac/coordinate_conversion.h" |
| 13 | 14 |
| 14 WebContentsModalDialogHostCocoa::WebContentsModalDialogHostCocoa( | 15 WebContentsModalDialogHostCocoa::WebContentsModalDialogHostCocoa( |
| 15 ConstrainedWindowSheetController* sheet_controller) | 16 ConstrainedWindowSheetController* sheet_controller) |
| 16 : sheet_controller_(sheet_controller) { | 17 : sheet_controller_(sheet_controller) { |
| 17 } | 18 } |
| 18 | 19 |
| 19 WebContentsModalDialogHostCocoa::~WebContentsModalDialogHostCocoa() { | 20 WebContentsModalDialogHostCocoa::~WebContentsModalDialogHostCocoa() { |
| 20 // Toolkit-Views calls OnHostDestroying on observers here, but the Cocoa host | 21 for (auto& observer : observers_) |
| 21 // doesn't need to be observed. | 22 observer.OnHostDestroying(); |
| 23 } |
| 24 |
| 25 void WebContentsModalDialogHostCocoa::OnPositionRequiresUpdate() { |
| 26 for (auto& observer : observers_) |
| 27 observer.OnPositionRequiresUpdate(); |
| 22 } | 28 } |
| 23 | 29 |
| 24 gfx::NativeView WebContentsModalDialogHostCocoa::GetHostView() const { | 30 gfx::NativeView WebContentsModalDialogHostCocoa::GetHostView() const { |
| 25 // To avoid the constrained window controller having to know about the browser | 31 // To avoid the constrained window controller having to know about the browser |
| 26 // view layout, use the active tab in the parent window. | 32 // view layout, use the active tab in the parent window. |
| 27 NSWindow* parent_window = [sheet_controller_ parentWindow]; | 33 NSWindow* parent_window = [sheet_controller_ parentWindow]; |
| 28 Browser* browser = chrome::FindBrowserWithWindow(parent_window); | 34 Browser* browser = chrome::FindBrowserWithWindow(parent_window); |
| 29 // This could be null for packaged app windows, but this dialog host is | 35 // This could be null for packaged app windows, but this dialog host is |
| 30 // currently only used for browsers. | 36 // currently only used for browsers. |
| 31 DCHECK(browser); | 37 DCHECK(browser); |
| 32 content::WebContents* web_contents = | 38 content::WebContents* web_contents = |
| 33 browser->tab_strip_model()->GetActiveWebContents(); | 39 browser->tab_strip_model()->GetActiveWebContents(); |
| 34 DCHECK(web_contents); | 40 DCHECK(web_contents); |
| 35 TabDialogs* tab_dialogs = TabDialogs::FromWebContents(web_contents); | 41 TabDialogs* tab_dialogs = TabDialogs::FromWebContents(web_contents); |
| 36 DCHECK(tab_dialogs); | 42 DCHECK(tab_dialogs); |
| 37 | 43 |
| 38 // Note this returns the WebContents' superview, so it doesn't really matter | 44 // Note this returns the WebContents' superview, so it doesn't really matter |
| 39 // which WebContents inside the browser we actually chose above. | 45 // which WebContents inside the browser we actually chose above. |
| 40 return tab_dialogs->GetDialogParentView(); | 46 return tab_dialogs->GetDialogParentView(); |
| 41 } | 47 } |
| 42 | 48 |
| 43 gfx::Point WebContentsModalDialogHostCocoa::GetDialogPosition( | 49 gfx::Point WebContentsModalDialogHostCocoa::GetDialogPosition( |
| 44 const gfx::Size& size) { | 50 const gfx::Size& size) { |
| 45 // Dialogs are always re-positioned by the constrained window sheet controller | 51 // Web-modals go via [sheet_controller_ originForSheet:..]. This is invoked |
| 46 // so nothing interesting to return yet. | 52 // for window-modals, so use the NSWindow sheet APIs. |
| 47 return gfx::Point(); | 53 NSWindow* native_parent = [sheet_controller_ parentWindow]; |
| 54 NSRect proposed_rect = [[native_parent contentView] bounds]; |
| 55 proposed_rect.origin.y = proposed_rect.size.height; |
| 56 proposed_rect.size.height = 0; |
| 57 |
| 58 id parent_delegate = [native_parent delegate]; |
| 59 if ([parent_delegate |
| 60 respondsToSelector:@selector(window:willPositionSheet:usingRect:)]) { |
| 61 proposed_rect = [parent_delegate window:native_parent |
| 62 willPositionSheet:nil |
| 63 usingRect:proposed_rect]; |
| 64 } |
| 65 |
| 66 // Center horizontally and flip vertically. Note that origin.x may go |
| 67 // negative. (That means it can also go offscreen.) |
| 68 return gfx::Point(NSMidX(proposed_rect) - size.width() / 2, |
| 69 NSHeight([native_parent frame]) - proposed_rect.origin.y); |
| 70 } |
| 71 |
| 72 gfx::Point WebContentsModalDialogHostCocoa::GetHostPosition() const { |
| 73 return gfx::ScreenRectFromNSRect([[sheet_controller_ parentWindow] frame]) |
| 74 .origin(); |
| 48 } | 75 } |
| 49 | 76 |
| 50 void WebContentsModalDialogHostCocoa::AddObserver( | 77 void WebContentsModalDialogHostCocoa::AddObserver( |
| 51 web_modal::ModalDialogHostObserver* observer) { | 78 web_modal::ModalDialogHostObserver* observer) { |
| 52 NOTREACHED(); | 79 observers_.AddObserver(observer); |
| 53 } | 80 } |
| 54 void WebContentsModalDialogHostCocoa::RemoveObserver( | 81 void WebContentsModalDialogHostCocoa::RemoveObserver( |
| 55 web_modal::ModalDialogHostObserver* observer) { | 82 web_modal::ModalDialogHostObserver* observer) { |
| 56 NOTREACHED(); | 83 observers_.RemoveObserver(observer); |
| 57 } | 84 } |
| 58 | 85 |
| 59 gfx::Size WebContentsModalDialogHostCocoa::GetMaximumDialogSize() { | 86 gfx::Size WebContentsModalDialogHostCocoa::GetMaximumDialogSize() { |
| 60 // The dialog should try to fit within the overlay for the web contents. | 87 // The dialog should try to fit within the overlay for the web contents. |
| 61 // Note that, for things like print preview, this is just a suggested maximum. | 88 // Note that, for things like print preview, this is just a suggested maximum. |
| 62 return gfx::Size( | 89 return gfx::Size( |
| 63 [sheet_controller_ overlayWindowSizeForParentView:GetHostView()]); | 90 [sheet_controller_ overlayWindowSizeForParentView:GetHostView()]); |
| 64 } | 91 } |
| OLD | NEW |