Chromium Code Reviews| 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 #include "grit/generated_resources.h" |
| 13 #include "content/public/browser/web_contents.h" | 15 #import "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" |
| 14 #include "content/public/browser/web_contents_view.h" | 16 #include "ui/base/cocoa/window_size_constants.h" |
| 15 #include "ui/gfx/size.h" | 17 #include "ui/base/l10n/l10n_util.h" |
| 16 #include "ui/web_dialogs/web_dialog_delegate.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 | 18 |
| 24 namespace { | 19 namespace { |
| 25 | 20 |
| 26 class ConstrainedWebDialogDelegateMac | 21 // TODO(groby): Should this be shared with ConstrainedWindowAlert? |
|
sail
2013/04/10 02:36:56
We could move this to constrained_window_control_u
groby-ooo-7-16
2013/04/12 03:13:40
Done.
| |
| 27 : public ConstrainedWebDialogDelegateBase { | 22 const CGFloat kButtonGap = 6; |
| 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]); |
|
sail
2013/04/10 02:36:56
align on ":"
groby-ooo-7-16
2013/04/12 03:13:40
Done.
| |
| 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 } | |
| 66 | |
| 67 void AutofillDialogCocoa::GetUserInput(DialogSection section, | |
| 68 DetailOutputMap* output) { | |
| 69 } | |
| 70 | |
| 71 string16 AutofillDialogCocoa::GetCvc() { | |
| 72 return string16(); | |
| 73 } | |
| 74 | |
| 75 bool AutofillDialogCocoa::UseBillingForShipping() { | |
| 76 return false; | |
| 77 } | |
| 78 | |
| 79 bool AutofillDialogCocoa::SaveDetailsInWallet() { | |
| 80 return false; | |
| 81 } | |
| 82 | |
| 83 bool AutofillDialogCocoa::SaveDetailsLocally() { | |
| 84 return false; | |
| 85 } | |
| 86 | |
| 87 const content::NavigationController* AutofillDialogCocoa::ShowSignIn() { | |
| 88 return NULL; | |
| 89 } | |
| 90 | |
| 91 void AutofillDialogCocoa::HideSignIn() {} | |
| 92 | |
| 93 void AutofillDialogCocoa::UpdateProgressBar(double value) {} | |
| 94 | |
| 95 void AutofillDialogCocoa::ModelChanged() {} | |
| 96 | |
| 97 void AutofillDialogCocoa::SubmitForTesting() {} | |
| 98 | |
| 99 void AutofillDialogCocoa::CancelForTesting() {} | |
| 100 | |
| 101 void AutofillDialogCocoa::OnConstrainedWindowClosed( | |
| 102 ConstrainedWindowMac* window) { | |
| 103 constrained_window_.reset(); | |
| 104 // |this| belongs to |controller_|, so no self-destruction here. | |
| 105 controller_->ViewClosed(); | |
| 106 } | |
| 107 | |
| 108 void AutofillDialogCocoa::PerformClose() { | |
| 109 controller_->OnCancel(); | |
| 110 constrained_window_->CloseWebContentsModalDialog(); | |
| 111 } | |
| 112 | |
| 113 } // autofill | |
| 114 | |
| 115 #pragma mark Window Controller | |
| 116 | |
| 117 @implementation AutofillDialogWindowController | |
|
sail
2013/04/10 02:36:56
new line after
groby-ooo-7-16
2013/04/12 03:13:40
Done.
| |
| 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: | |
| 152 ui::kWindowSizeDeterminedLater]); | |
|
sail
2013/04/10 02:36:56
This definitely works. An alternative is to use NS
groby-ooo-7-16
2013/04/12 03:13:40
I take it NSControls can be created with zero size
| |
| 153 [button setTitle:l10n_util::GetNSString(IDS_CANCEL)]; | |
|
sail
2013/04/10 02:36:56
GetNSStringWithFixup (more below)
groby-ooo-7-16
2013/04/12 03:13:40
Done.
| |
| 154 [button setKeyEquivalent:@"\e"]; | |
|
sail
2013/04/10 02:36:56
use kKeyEquivalentEscape?
groby-ooo-7-16
2013/04/12 03:13:40
Done.
| |
| 155 [button setTarget:self]; | |
| 156 [button setAction:@selector(closeSheet:)]; | |
| 157 [buttonContainer_ addSubview:button]; | |
| 158 | |
| 159 button.reset([[ConstrainedWindowButton alloc] initWithFrame: | |
| 160 ui::kWindowSizeDeterminedLater]); | |
| 161 [button setFrameOrigin:NSMakePoint(NSWidth([button frame]) + kButtonGap, 0)]; | |
| 162 [button | |
| 163 setTitle:l10n_util::GetNSString(IDS_AUTOFILL_DIALOG_SUBMIT_BUTTON)]; | |
| 164 [button setKeyEquivalent:@"\r"]; | |
|
sail
2013/04/10 02:36:56
use kKeyEquivalentReturn?
groby-ooo-7-16
2013/04/12 03:13:40
Done.
| |
| 165 [button setTarget:self]; | |
| 166 [button setAction:@selector(closeSheet:)]; | |
| 167 [buttonContainer_ addSubview:button]; | |
| 168 | |
| 169 const CGFloat dialogOffset = NSWidth([[self window] frame]) - | |
| 170 chrome_style::kHorizontalPadding - NSMaxX([button frame]); | |
| 171 [buttonContainer_ setFrame: | |
| 172 NSMakeRect(dialogOffset, chrome_style::kClientBottomPadding, | |
| 173 NSMaxX([button frame]), NSMaxY([button frame]))]; | |
| 174 | |
| 175 [[[self window] contentView] addSubview:buttonContainer_]; | |
| 176 } | |
| 177 | |
| 178 - (void)layoutButtons { | |
| 179 GTMUILocalizerAndLayoutTweaker* layoutTweaker = | |
|
sail
2013/04/10 02:36:56
scoped_nsobject?
groby-ooo-7-16
2013/04/12 03:13:40
Done.
| |
| 180 [[[GTMUILocalizerAndLayoutTweaker alloc] init] autorelease]; | |
| 181 [layoutTweaker tweakUI:buttonContainer_]; | |
| 182 NSRect frame = [buttonContainer_ frame]; | |
| 183 frame.size.height = | |
| 184 NSHeight([[[buttonContainer_ subviews] objectAtIndex:0] frame]); | |
| 185 [buttonContainer_ setFrame:frame]; | |
| 186 } | |
|
sail
2013/04/10 02:36:56
new line after?
groby-ooo-7-16
2013/04/12 03:13:40
Done.
| |
| 187 @end | |
| OLD | NEW |