OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/cocoa/autofill/autofill_dialog_cocoa.h" |
6 | 6 |
7 #import <Cocoa/Cocoa.h> | 7 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_button.h" |
8 | 8 #include "chrome/browser/ui/chrome_style.h" |
| 9 #include "base/mac/bundle_locations.h" |
9 #include "base/memory/scoped_nsobject.h" | 10 #include "base/memory/scoped_nsobject.h" |
| 11 #include "chrome/browser/ui/chrome_style.h" |
10 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_sh
eet.h" | 12 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_sh
eet.h" |
11 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_wi
ndow.h" | 13 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_custom_wi
ndow.h" |
12 #import "chrome/browser/ui/cocoa/constrained_window/constrained_window_mac.h" | 14 #import "chrome/browser/ui/cocoa/key_equivalent_constants.h" |
13 #include "content/public/browser/web_contents.h" | 15 #include "grit/generated_resources.h" |
14 #include "content/public/browser/web_contents_view.h" | 16 #import "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" |
15 #include "ui/gfx/size.h" | 17 #include "ui/base/cocoa/window_size_constants.h" |
16 #include "ui/web_dialogs/web_dialog_delegate.h" | 18 #include "ui/base/l10n/l10n_util.h" |
17 #include "ui/web_dialogs/web_dialog_ui.h" | |
18 #include "ui/web_dialogs/web_dialog_web_contents_delegate.h" | |
19 | |
20 using content::WebContents; | |
21 using ui::WebDialogDelegate; | |
22 using ui::WebDialogWebContentsDelegate; | |
23 | 19 |
24 namespace { | 20 namespace { |
25 | 21 |
26 class ConstrainedWebDialogDelegateMac | 22 const CGFloat kButtonGap = 6; |
27 : public ConstrainedWebDialogDelegateBase { | |
28 public: | |
29 ConstrainedWebDialogDelegateMac( | |
30 content::BrowserContext* browser_context, | |
31 WebDialogDelegate* delegate, | |
32 WebDialogWebContentsDelegate* tab_delegate) | |
33 : ConstrainedWebDialogDelegateBase( | |
34 browser_context, delegate, tab_delegate) {} | |
35 | |
36 // WebDialogWebContentsDelegate interface. | |
37 virtual void CloseContents(WebContents* source) OVERRIDE { | |
38 window_->CloseWebContentsModalDialog(); | |
39 } | |
40 | |
41 void set_window(ConstrainedWindowMac* window) { window_ = window; } | |
42 ConstrainedWindowMac* window() const { return window_; } | |
43 | |
44 private: | |
45 // Weak, owned by ConstrainedWebDialogDelegateViewMac. | |
46 ConstrainedWindowMac* window_; | |
47 | |
48 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateMac); | |
49 }; | |
50 | 23 |
51 } // namespace | 24 } // namespace |
52 | 25 |
53 class ConstrainedWebDialogDelegateViewMac : | 26 namespace autofill { |
54 public ConstrainedWindowMacDelegate, | |
55 public ConstrainedWebDialogDelegate { | |
56 | 27 |
57 public: | 28 // static |
58 ConstrainedWebDialogDelegateViewMac( | 29 AutofillDialogView* AutofillDialogView::Create( |
59 content::BrowserContext* browser_context, | 30 AutofillDialogController* controller) { |
60 WebDialogDelegate* delegate, | 31 return new AutofillDialogCocoa(controller); |
61 WebDialogWebContentsDelegate* tab_delegate, | 32 } |
62 content::WebContents* web_contents); | |
63 virtual ~ConstrainedWebDialogDelegateViewMac() {} | |
64 | 33 |
65 // ConstrainedWebDialogDelegate interface | 34 AutofillDialogCocoa::AutofillDialogCocoa(AutofillDialogController* controller) |
66 virtual const WebDialogDelegate* | 35 : controller_(controller) { |
67 GetWebDialogDelegate() const OVERRIDE { | 36 sheet_controller_.reset([[AutofillDialogWindowController alloc] |
68 return impl_->GetWebDialogDelegate(); | 37 initWithWebContents:controller_->web_contents() |
69 } | 38 autofillDialog:this]); |
70 virtual WebDialogDelegate* GetWebDialogDelegate() OVERRIDE { | |
71 return impl_->GetWebDialogDelegate(); | |
72 } | |
73 virtual void OnDialogCloseFromWebUI() OVERRIDE { | |
74 return impl_->OnDialogCloseFromWebUI(); | |
75 } | |
76 virtual void ReleaseWebContentsOnDialogClose() OVERRIDE { | |
77 return impl_->ReleaseWebContentsOnDialogClose(); | |
78 } | |
79 virtual NativeWebContentsModalDialog GetNativeDialog() OVERRIDE { | |
80 return constrained_window_->GetNativeDialog(); | |
81 } | |
82 virtual WebContents* GetWebContents() OVERRIDE { | |
83 return impl_->GetWebContents(); | |
84 } | |
85 | |
86 // ConstrainedWindowMacDelegate interface | |
87 virtual void OnConstrainedWindowClosed( | |
88 ConstrainedWindowMac* window) OVERRIDE { | |
89 if (!impl_->closed_via_webui()) | |
90 GetWebDialogDelegate()->OnDialogClosed(""); | |
91 delete this; | |
92 } | |
93 | |
94 private: | |
95 scoped_ptr<ConstrainedWebDialogDelegateMac> impl_; | |
96 scoped_ptr<ConstrainedWindowMac> constrained_window_; | |
97 scoped_nsobject<NSWindow> window_; | |
98 | |
99 DISALLOW_COPY_AND_ASSIGN(ConstrainedWebDialogDelegateViewMac); | |
100 }; | |
101 | |
102 ConstrainedWebDialogDelegateViewMac::ConstrainedWebDialogDelegateViewMac( | |
103 content::BrowserContext* browser_context, | |
104 WebDialogDelegate* delegate, | |
105 WebDialogWebContentsDelegate* tab_delegate, | |
106 content::WebContents* web_contents) | |
107 : impl_(new ConstrainedWebDialogDelegateMac(browser_context, | |
108 delegate, | |
109 tab_delegate)) { | |
110 // Create a window to hold web_contents in the constrained sheet: | |
111 gfx::Size size; | |
112 delegate->GetDialogSize(&size); | |
113 NSRect frame = NSMakeRect(0, 0, size.width(), size.height()); | |
114 | |
115 window_.reset( | |
116 [[ConstrainedWindowCustomWindow alloc] initWithContentRect:frame]); | |
117 [GetWebContents()->GetView()->GetNativeView() setFrame:frame]; | |
118 [[window_ contentView] | |
119 addSubview:GetWebContents()->GetView()->GetNativeView()]; | |
120 | |
121 scoped_nsobject<CustomConstrainedWindowSheet> sheet( | 39 scoped_nsobject<CustomConstrainedWindowSheet> sheet( |
122 [[CustomConstrainedWindowSheet alloc] | 40 [[CustomConstrainedWindowSheet alloc] |
123 initWithCustomWindow:window_]); | 41 initWithCustomWindow:[sheet_controller_ window]]); |
124 constrained_window_.reset(new ConstrainedWindowMac( | 42 constrained_window_.reset( |
125 this, web_contents, sheet)); | 43 new ConstrainedWindowMac(this, controller_->web_contents(), sheet)); |
126 | |
127 impl_->set_window(constrained_window_.get()); | |
128 } | 44 } |
129 | 45 |
130 ConstrainedWebDialogDelegate* CreateConstrainedWebDialog( | 46 AutofillDialogCocoa::~AutofillDialogCocoa() { |
131 content::BrowserContext* browser_context, | |
132 WebDialogDelegate* delegate, | |
133 WebDialogWebContentsDelegate* tab_delegate, | |
134 content::WebContents* web_contents) { | |
135 // Deleted when the dialog closes. | |
136 ConstrainedWebDialogDelegateViewMac* constrained_delegate = | |
137 new ConstrainedWebDialogDelegateViewMac( | |
138 browser_context, delegate, tab_delegate, web_contents); | |
139 return constrained_delegate; | |
140 } | 47 } |
| 48 |
| 49 void AutofillDialogCocoa::Show() { |
| 50 } |
| 51 |
| 52 void AutofillDialogCocoa::Hide() { |
| 53 } |
| 54 |
| 55 void AutofillDialogCocoa::UpdateAccountChooser() { |
| 56 } |
| 57 |
| 58 void AutofillDialogCocoa::UpdateButtonStrip() { |
| 59 } |
| 60 |
| 61 void AutofillDialogCocoa::UpdateNotificationArea() { |
| 62 } |
| 63 |
| 64 void AutofillDialogCocoa::UpdateSection(DialogSection section, |
| 65 UserInputAction action) { |
| 66 } |
| 67 |
| 68 void AutofillDialogCocoa::GetUserInput(DialogSection section, |
| 69 DetailOutputMap* output) { |
| 70 } |
| 71 |
| 72 string16 AutofillDialogCocoa::GetCvc() { |
| 73 return string16(); |
| 74 } |
| 75 |
| 76 bool AutofillDialogCocoa::UseBillingForShipping() { |
| 77 return false; |
| 78 } |
| 79 |
| 80 bool AutofillDialogCocoa::SaveDetailsLocally() { |
| 81 return false; |
| 82 } |
| 83 |
| 84 const content::NavigationController* AutofillDialogCocoa::ShowSignIn() { |
| 85 return NULL; |
| 86 } |
| 87 |
| 88 void AutofillDialogCocoa::HideSignIn() {} |
| 89 |
| 90 void AutofillDialogCocoa::UpdateProgressBar(double value) {} |
| 91 |
| 92 void AutofillDialogCocoa::ModelChanged() {} |
| 93 |
| 94 void AutofillDialogCocoa::SubmitForTesting() {} |
| 95 |
| 96 void AutofillDialogCocoa::CancelForTesting() { |
| 97 PerformClose(); |
| 98 } |
| 99 |
| 100 void AutofillDialogCocoa::OnConstrainedWindowClosed( |
| 101 ConstrainedWindowMac* window) { |
| 102 constrained_window_.reset(); |
| 103 // |this| belongs to |controller_|, so no self-destruction here. |
| 104 controller_->ViewClosed(); |
| 105 } |
| 106 |
| 107 void AutofillDialogCocoa::PerformClose() { |
| 108 controller_->OnCancel(); |
| 109 constrained_window_->CloseWebContentsModalDialog(); |
| 110 } |
| 111 |
| 112 } // autofill |
| 113 |
| 114 #pragma mark Window Controller |
| 115 |
| 116 @implementation AutofillDialogWindowController |
| 117 |
| 118 - (id)initWithWebContents:(content::WebContents*)webContents |
| 119 autofillDialog:(autofill::AutofillDialogCocoa*)autofillDialog { |
| 120 DCHECK(webContents); |
| 121 |
| 122 // TODO(groby): Should be ui::kWindowSizeDeterminedLater |
| 123 NSRect frame = NSMakeRect(0, 0, 550, 600); |
| 124 scoped_nsobject<ConstrainedWindowCustomWindow> window( |
| 125 [[ConstrainedWindowCustomWindow alloc] initWithContentRect:frame]); |
| 126 |
| 127 if ((self = [super initWithWindow:window])) { |
| 128 webContents_ = webContents; |
| 129 autofillDialog_ = autofillDialog; |
| 130 |
| 131 [self buildWindowButtons]; |
| 132 [self layoutButtons]; |
| 133 } |
| 134 return self; |
| 135 } |
| 136 |
| 137 - (IBAction)closeSheet:(id)sender { |
| 138 autofillDialog_->PerformClose(); |
| 139 } |
| 140 |
| 141 - (void)buildWindowButtons { |
| 142 if (buttonContainer_.get()) |
| 143 return; |
| 144 |
| 145 buttonContainer_.reset([[GTMWidthBasedTweaker alloc] initWithFrame: |
| 146 ui::kWindowSizeDeterminedLater]); |
| 147 [buttonContainer_ |
| 148 setAutoresizingMask:(NSViewMinXMargin | NSViewMinYMargin)]; |
| 149 |
| 150 scoped_nsobject<NSButton> button( |
| 151 [[ConstrainedWindowButton alloc] initWithFrame:NSZeroRect]); |
| 152 [button setTitle:l10n_util::GetNSStringWithFixup(IDS_CANCEL)]; |
| 153 [button setKeyEquivalent:kKeyEquivalentEscape]; |
| 154 [button setTarget:self]; |
| 155 [button setAction:@selector(closeSheet:)]; |
| 156 [button sizeToFit]; |
| 157 [buttonContainer_ addSubview:button]; |
| 158 |
| 159 CGFloat nextX = NSMaxX([button frame]) + kButtonGap; |
| 160 button.reset([[ConstrainedWindowButton alloc] initWithFrame:NSZeroRect]); |
| 161 [button setFrameOrigin:NSMakePoint(nextX, 0)]; |
| 162 [button setTitle:l10n_util::GetNSStringWithFixup( |
| 163 IDS_AUTOFILL_DIALOG_SUBMIT_BUTTON)]; |
| 164 [button setKeyEquivalent:kKeyEquivalentReturn]; |
| 165 [button setTarget:self]; |
| 166 [button setAction:@selector(closeSheet:)]; |
| 167 [button sizeToFit]; |
| 168 [buttonContainer_ addSubview:button]; |
| 169 |
| 170 const CGFloat dialogOffset = NSWidth([[self window] frame]) - |
| 171 chrome_style::kHorizontalPadding - NSMaxX([button frame]); |
| 172 [buttonContainer_ setFrame: |
| 173 NSMakeRect(dialogOffset, chrome_style::kClientBottomPadding, |
| 174 NSMaxX([button frame]), NSMaxY([button frame]))]; |
| 175 |
| 176 [[[self window] contentView] addSubview:buttonContainer_]; |
| 177 } |
| 178 |
| 179 - (void)layoutButtons { |
| 180 scoped_nsobject<GTMUILocalizerAndLayoutTweaker> layoutTweaker( |
| 181 [[GTMUILocalizerAndLayoutTweaker alloc] init]); |
| 182 [layoutTweaker tweakUI:buttonContainer_]; |
| 183 } |
| 184 |
| 185 @end |
OLD | NEW |