Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: chrome/browser/ui/cocoa/constrained_web_dialog_delegate_mac.mm

Issue 1430023002: Enable AutoResize for Constrained Web Dialogs for Mac. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Changes per erikchen@'s comments. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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/webui/constrained_web_dialog_delegate_base.h" 5 #include "chrome/browser/ui/webui/constrained_web_dialog_delegate_base.h"
6 6
7 #import <Cocoa/Cocoa.h> 7 #import <Cocoa/Cocoa.h>
8 8
9 #include "base/mac/scoped_nsobject.h" 9 #include "base/mac/scoped_nsobject.h"
10 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_wi ndow.h" 10 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_wi ndow.h"
11 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h" 11 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h"
12 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_web_dialo g_sheet.h" 12 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_web_dialo g_sheet.h"
13 #include "chrome/browser/ui/webui/chrome_web_contents_handler.h"
14 #include "content/public/browser/render_view_host.h"
13 #include "content/public/browser/web_contents.h" 15 #include "content/public/browser/web_contents.h"
16 #include "content/public/browser/web_contents_observer.h"
14 #include "ui/gfx/geometry/size.h" 17 #include "ui/gfx/geometry/size.h"
15 #include "ui/web_dialogs/web_dialog_delegate.h" 18 #include "ui/web_dialogs/web_dialog_delegate.h"
16 #include "ui/web_dialogs/web_dialog_ui.h" 19 #include "ui/web_dialogs/web_dialog_ui.h"
17 #include "ui/web_dialogs/web_dialog_web_contents_delegate.h" 20 #include "ui/web_dialogs/web_dialog_web_contents_delegate.h"
18 21
19 using content::WebContents; 22 using content::WebContents;
20 using ui::WebDialogDelegate; 23 using ui::WebDialogDelegate;
21 using ui::WebDialogWebContentsDelegate; 24 using ui::WebDialogWebContentsDelegate;
22 25
23 namespace { 26 namespace {
24 27
28 class ConstrainedWebDialogDelegateMac;
29
30 // This class is to trigger a resize to the dialog window when
31 // ResizeDueToAutoResize() is invoked.
32 class WebDialogWebContentsDelegateMac
33 : public ui::WebDialogWebContentsDelegate {
34 public:
35 WebDialogWebContentsDelegateMac(content::BrowserContext* browser_context,
36 content::WebContentsObserver* observer,
37 ConstrainedWebDialogDelegateBase* delegate)
38 : ui::WebDialogWebContentsDelegate(browser_context,
39 new ChromeWebContentsHandler()),
40 observer_(observer),
41 delegate_(delegate) {
42 }
43 ~WebDialogWebContentsDelegateMac() override {}
44
45 void ResizeDueToAutoResize(content::WebContents* source,
46 const gfx::Size& preferred_size) override {
47 if (!observer_->web_contents())
48 return;
49 delegate_->ResizeToGivenSize(preferred_size);
50 }
51
52 private:
53 // These members must outlive the instance.
54 content::WebContentsObserver* const observer_;
55 ConstrainedWebDialogDelegateBase* delegate_;
56
57 DISALLOW_COPY_AND_ASSIGN(WebDialogWebContentsDelegateMac);
58 };
59
25 class ConstrainedWebDialogDelegateMac 60 class ConstrainedWebDialogDelegateMac
26 : public ConstrainedWebDialogDelegateBase { 61 : public ConstrainedWebDialogDelegateBase {
27 public: 62 public:
28 ConstrainedWebDialogDelegateMac( 63 ConstrainedWebDialogDelegateMac(
29 content::BrowserContext* browser_context, 64 content::BrowserContext* browser_context,
30 WebDialogDelegate* delegate) 65 WebDialogDelegate* delegate,
31 : ConstrainedWebDialogDelegateBase(browser_context, delegate, NULL) {} 66 content::WebContentsObserver* observer)
67 : ConstrainedWebDialogDelegateBase(browser_context, delegate,
68 new WebDialogWebContentsDelegateMac(browser_context, observer,
69 this)) {}
32 70
33 // WebDialogWebContentsDelegate interface. 71 // WebDialogWebContentsDelegate interface.
34 void CloseContents(WebContents* source) override { 72 void CloseContents(WebContents* source) override {
35 window_->CloseWebContentsModalDialog(); 73 window_->CloseWebContentsModalDialog();
36 } 74 }
37 75
76 // ConstrainedWebDialogDelegateBase:
77 void ResizeToGivenSize(const gfx::Size size) override {
78 NSSize updated_preferred_size = NSMakeSize(size.width(),
79 size.height());
80 [window_->sheet() resizeWithNewSize:updated_preferred_size];
81 }
82
38 void set_window(ConstrainedWindowMac* window) { window_ = window; } 83 void set_window(ConstrainedWindowMac* window) { window_ = window; }
39 ConstrainedWindowMac* window() const { return window_; } 84 ConstrainedWindowMac* window() const { return window_; }
40 85
41 private: 86 private:
42 // Weak, owned by ConstrainedWebDialogDelegateViewMac. 87 // Weak, owned by ConstrainedWebDialogDelegateViewMac.
43 ConstrainedWindowMac* window_; 88 ConstrainedWindowMac* window_;
44 89
45 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateMac); 90 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateMac);
46 }; 91 };
47 92
48 } // namespace 93 } // namespace
49 94
50 class ConstrainedWebDialogDelegateViewMac : 95 class ConstrainedWebDialogDelegateViewMac :
51 public ConstrainedWindowMacDelegate, 96 public ConstrainedWindowMacDelegate,
52 public ConstrainedWebDialogDelegate { 97 public ConstrainedWebDialogDelegate,
98 public content::WebContentsObserver {
53 99
54 public: 100 public:
55 ConstrainedWebDialogDelegateViewMac( 101 ConstrainedWebDialogDelegateViewMac(
56 content::BrowserContext* browser_context, 102 content::BrowserContext* browser_context,
57 WebDialogDelegate* delegate, 103 WebDialogDelegate* delegate,
58 content::WebContents* web_contents); 104 content::WebContents* web_contents,
105 const gfx::Size& min_size,
106 const gfx::Size& max_size);
59 ~ConstrainedWebDialogDelegateViewMac() override {} 107 ~ConstrainedWebDialogDelegateViewMac() override {}
60 108
61 // ConstrainedWebDialogDelegate interface 109 // ConstrainedWebDialogDelegate interface
62 const WebDialogDelegate* GetWebDialogDelegate() const override { 110 const WebDialogDelegate* GetWebDialogDelegate() const override {
63 return impl_->GetWebDialogDelegate(); 111 return impl_->GetWebDialogDelegate();
64 } 112 }
65 WebDialogDelegate* GetWebDialogDelegate() override { 113 WebDialogDelegate* GetWebDialogDelegate() override {
66 return impl_->GetWebDialogDelegate(); 114 return impl_->GetWebDialogDelegate();
67 } 115 }
68 void OnDialogCloseFromWebUI() override { 116 void OnDialogCloseFromWebUI() override {
69 return impl_->OnDialogCloseFromWebUI(); 117 return impl_->OnDialogCloseFromWebUI();
70 } 118 }
71 void ReleaseWebContentsOnDialogClose() override { 119 void ReleaseWebContentsOnDialogClose() override {
72 return impl_->ReleaseWebContentsOnDialogClose(); 120 return impl_->ReleaseWebContentsOnDialogClose();
73 } 121 }
74 gfx::NativeWindow GetNativeDialog() override { return window_; } 122 gfx::NativeWindow GetNativeDialog() override { return window_; }
75 WebContents* GetWebContents() override { return impl_->GetWebContents(); } 123 WebContents* GetWebContents() override { return impl_->GetWebContents(); }
76 gfx::Size GetMinimumSize() const override { 124 gfx::Size GetMinimumSize() const override {
77 NOTIMPLEMENTED(); 125 return min_size_;
78 return gfx::Size();
79 } 126 }
80 gfx::Size GetMaximumSize() const override { 127 gfx::Size GetMaximumSize() const override {
81 NOTIMPLEMENTED(); 128 return max_size_;
82 return gfx::Size();
83 } 129 }
84 gfx::Size GetPreferredSize() const override { 130 gfx::Size GetPreferredSize() const override {
85 NOTIMPLEMENTED(); 131 gfx::Size size;
86 return gfx::Size(); 132 if (!impl_->closed_via_webui()) {
133 NSRect frame = [window_ frame];
134 size = gfx::Size(frame.size.width, frame.size.height);
135 }
136 return size;
137 }
138
139 // content::WebContentsObserver:
140 void RenderViewCreated(content::RenderViewHost* render_view_host) override {
141 DCHECK(IsDialogAutoResizable());
142 EnableAutoResize();
143 }
144 void RenderViewHostChanged(content::RenderViewHost* old_host,
145 content::RenderViewHost* new_host) override {
146 DCHECK(IsDialogAutoResizable());
147 EnableAutoResize();
148 }
149 void DocumentOnLoadCompletedInMainFrame() override {
150 DCHECK(IsDialogAutoResizable());
151 EnableAutoResize();
152 if (GetWebContents())
153 constrained_window_->ShowWebContentsModalDialog();
87 } 154 }
88 155
89 // ConstrainedWindowMacDelegate interface 156 // ConstrainedWindowMacDelegate interface
90 void OnConstrainedWindowClosed(ConstrainedWindowMac* window) override { 157 void OnConstrainedWindowClosed(ConstrainedWindowMac* window) override {
91 if (!impl_->closed_via_webui()) 158 if (!impl_->closed_via_webui())
92 GetWebDialogDelegate()->OnDialogClosed(""); 159 GetWebDialogDelegate()->OnDialogClosed("");
93 delete this; 160 delete this;
94 } 161 }
95 162
96 private: 163 private:
164 void EnableAutoResize() {
165 if (!GetWebContents())
166 return;
167
168 content::RenderViewHost* render_view_host =
169 GetWebContents()->GetRenderViewHost();
170 render_view_host->EnableAutoResize(min_size_, max_size_);
171 }
172
173 // Whether or not the dialog is autoresizable is determined based on whether
174 // |max_size_| was specified.
175 bool IsDialogAutoResizable() {
176 return !max_size_.IsEmpty();
177 }
178
97 scoped_ptr<ConstrainedWebDialogDelegateMac> impl_; 179 scoped_ptr<ConstrainedWebDialogDelegateMac> impl_;
98 scoped_ptr<ConstrainedWindowMac> constrained_window_; 180 scoped_ptr<ConstrainedWindowMac> constrained_window_;
99 base::scoped_nsobject<NSWindow> window_; 181 base::scoped_nsobject<NSWindow> window_;
100 182
183 // Minimum and maximum sizes to determine dialog bounds for auto-resizing.
184 const gfx::Size min_size_;
185 const gfx::Size max_size_;
186
101 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewMac); 187 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewMac);
102 }; 188 };
103 189
104 ConstrainedWebDialogDelegateViewMac::ConstrainedWebDialogDelegateViewMac( 190 ConstrainedWebDialogDelegateViewMac::ConstrainedWebDialogDelegateViewMac(
105 content::BrowserContext* browser_context, 191 content::BrowserContext* browser_context,
106 WebDialogDelegate* delegate, 192 WebDialogDelegate* delegate,
107 content::WebContents* web_contents) 193 content::WebContents* web_contents,
108 : impl_(new ConstrainedWebDialogDelegateMac(browser_context, delegate)) { 194 const gfx::Size& min_size,
195 const gfx::Size& max_size)
196 : content::WebContentsObserver(web_contents),
197 impl_(new ConstrainedWebDialogDelegateMac(browser_context, delegate,
198 this)),
199 min_size_(min_size),
200 max_size_(max_size) {
201 if (IsDialogAutoResizable())
202 Observe(GetWebContents());
203
109 // Create a window to hold web_contents in the constrained sheet: 204 // Create a window to hold web_contents in the constrained sheet:
110 gfx::Size size; 205 gfx::Size size;
111 delegate->GetDialogSize(&size); 206 delegate->GetDialogSize(&size);
112 NSRect frame = NSMakeRect(0, 0, size.width(), size.height()); 207 NSRect frame = NSMakeRect(0, 0, size.width(), size.height());
113 208
114 window_.reset( 209 window_.reset([[ConstrainedWindowCustomWindow alloc]
115 [[ConstrainedWindowCustomWindow alloc] initWithContentRect:frame]); 210 initWithContentRect:frame]);
116 [GetWebContents()->GetNativeView() setFrame:frame]; 211 [GetWebContents()->GetNativeView() setFrame:frame];
117 [GetWebContents()->GetNativeView() setAutoresizingMask: 212 [GetWebContents()->GetNativeView() setAutoresizingMask:
118 NSViewWidthSizable|NSViewHeightSizable]; 213 NSViewWidthSizable|NSViewHeightSizable];
119 [[window_ contentView] addSubview:GetWebContents()->GetNativeView()]; 214 [[window_ contentView] addSubview:GetWebContents()->GetNativeView()];
120 215
121 base::scoped_nsobject<WebDialogConstrainedWindowSheet> sheet( 216 base::scoped_nsobject<WebDialogConstrainedWindowSheet> sheet(
122 [[WebDialogConstrainedWindowSheet alloc] initWithCustomWindow:window_ 217 [[WebDialogConstrainedWindowSheet alloc] initWithCustomWindow:window_
123 webDialogDelegate:delegate]); 218 webDialogDelegate:delegate]);
124 constrained_window_.reset(new ConstrainedWindowMac( 219
125 this, web_contents, sheet)); 220 if (IsDialogAutoResizable()) {
221 constrained_window_.reset(CreateWebModalDialogMac(
222 this, web_contents, sheet));
223 } else {
224 constrained_window_.reset(CreateAndShowWebModalDialogMac(
225 this, web_contents, sheet));
226 }
126 227
127 impl_->set_window(constrained_window_.get()); 228 impl_->set_window(constrained_window_.get());
128 } 229 }
129 230
130 ConstrainedWebDialogDelegate* ShowConstrainedWebDialog( 231 ConstrainedWebDialogDelegate* ShowConstrainedWebDialog(
131 content::BrowserContext* browser_context, 232 content::BrowserContext* browser_context,
132 WebDialogDelegate* delegate, 233 WebDialogDelegate* delegate,
133 content::WebContents* web_contents) { 234 content::WebContents* web_contents) {
134 // Deleted when the dialog closes. 235 // Deleted when the dialog closes.
135 ConstrainedWebDialogDelegateViewMac* constrained_delegate = 236 ConstrainedWebDialogDelegateViewMac* constrained_delegate =
136 new ConstrainedWebDialogDelegateViewMac( 237 new ConstrainedWebDialogDelegateViewMac(
137 browser_context, delegate, web_contents); 238 browser_context, delegate, web_contents,
239 gfx::Size(), gfx::Size());
138 return constrained_delegate; 240 return constrained_delegate;
139 } 241 }
242
243 ConstrainedWebDialogDelegate* ShowConstrainedWebDialogWithAutoResize(
244 content::BrowserContext* browser_context,
245 WebDialogDelegate* delegate,
246 content::WebContents* web_contents,
247 const gfx::Size& min_size,
248 const gfx::Size& max_size) {
249 DCHECK(!min_size.IsEmpty());
250 DCHECK(!max_size.IsEmpty());
251 // Deleted when the dialog closes.
252 ConstrainedWebDialogDelegateViewMac* constrained_delegate =
253 new ConstrainedWebDialogDelegateViewMac(
254 browser_context, delegate, web_contents,
255 min_size, max_size);
256 return constrained_delegate;
257 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698