Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 #import "chrome/browser/ui/cocoa/one_click_signin_view_controller.h" | 5 #import "chrome/browser/ui/cocoa/one_click_signin_view_controller.h" |
| 6 | 6 |
| 7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/mac/bundle_locations.h" | 9 #include "base/mac/bundle_locations.h" |
| 10 #include "base/strings/sys_string_conversions.h" | |
| 10 #import "chrome/browser/ui/chrome_style.h" | 11 #import "chrome/browser/ui/chrome_style.h" |
| 11 #import "chrome/browser/ui/cocoa/hyperlink_text_view.h" | 12 #import "chrome/browser/ui/cocoa/hyperlink_text_view.h" |
| 12 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
| 13 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
| 14 #include "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
| 15 #include "skia/ext/skia_utils_mac.h" | 16 #include "skia/ext/skia_utils_mac.h" |
| 16 #import "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" | 17 #import "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" |
| 17 #include "ui/base/l10n/l10n_util_mac.h" | 18 #include "ui/base/l10n/l10n_util_mac.h" |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 // Shift the origin of |view|'s frame by the given amount in the | 22 // Shift the origin of |view|'s frame by the given amount in the |
| 22 // positive y direction (up). | 23 // positive y direction (up). |
| 23 void ShiftOriginY(NSView* view, CGFloat amount) { | 24 void ShiftOriginY(NSView* view, CGFloat amount) { |
| 24 NSPoint origin = [view frame].origin; | 25 NSPoint origin = [view frame].origin; |
| 25 origin.y += amount; | 26 origin.y += amount; |
| 26 [view setFrameOrigin:origin]; | 27 [view setFrameOrigin:origin]; |
| 27 } | 28 } |
| 28 | 29 |
| 29 } // namespace | 30 } // namespace |
| 30 | 31 |
| 31 @interface OneClickSigninViewController () | 32 @interface OneClickSigninViewController () |
| 32 - (CGFloat)initializeInformativeTextView; | 33 - (CGFloat)initializeInformativeTextView; |
| 33 - (void)close; | 34 - (void)close; |
| 34 @end | 35 @end |
| 35 | 36 |
| 36 @implementation OneClickSigninViewController | 37 @implementation OneClickSigninViewController |
| 37 | 38 |
| 39 | |
| 38 - (id)initWithNibName:(NSString*)nibName | 40 - (id)initWithNibName:(NSString*)nibName |
| 39 webContents:(content::WebContents*)webContents | 41 webContents:(content::WebContents*)webContents |
| 40 syncCallback:(const BrowserWindow::StartSyncCallback&)syncCallback | 42 syncCallback:(const BrowserWindow::StartSyncCallback&)syncCallback |
| 41 closeCallback:(const base::Closure&)closeCallback { | 43 closeCallback:(const base::Closure&)closeCallback |
| 44 isSyncDialog:(bool)isSyncDialog | |
| 45 errorMessage:(const string16&)errorMessage { | |
| 42 if ((self = [super initWithNibName:nibName | 46 if ((self = [super initWithNibName:nibName |
| 43 bundle:base::mac::FrameworkBundle()])) { | 47 bundle:base::mac::FrameworkBundle()])) { |
| 44 webContents_ = webContents; | 48 webContents_ = webContents; |
| 45 startSyncCallback_ = syncCallback; | 49 startSyncCallback_ = syncCallback; |
| 46 closeCallback_ = closeCallback; | 50 closeCallback_ = closeCallback; |
| 47 DCHECK(!startSyncCallback_.is_null()); | 51 isSyncDialog_ = isSyncDialog; |
| 52 errorMessage_ = base::SysUTF16ToNSString(errorMessage); | |
|
Alexei Svitkine (slow)
2013/04/19 19:54:15
Make the ctor param type NSString* and pass in nil
noms (inactive)
2013/04/22 15:28:07
I have changed the parameter to NSString* as it's
| |
| 53 | |
| 54 if (isSyncDialog_) | |
| 55 DCHECK(!startSyncCallback_.is_null()); | |
| 48 } | 56 } |
| 49 return self; | 57 return self; |
| 50 } | 58 } |
| 51 | 59 |
| 52 - (void)viewWillClose { | 60 - (void)viewWillClose { |
| 53 if (!startSyncCallback_.is_null()) { | 61 if (isSyncDialog_ && !startSyncCallback_.is_null()) { |
|
Alexei Svitkine (slow)
2013/04/19 19:54:15
If |isSyncDialog_| is YES, then you're already DCH
noms (inactive)
2013/04/22 15:28:07
Hmm, but you only DCHECK in the constructor, and t
Alexei Svitkine (slow)
2013/04/22 18:16:51
I guess you can add a DCHECK() here too, if you wa
noms (inactive)
2013/04/23 18:52:48
Ok, did some analysis: viewWillClose is called aft
Alexei Svitkine (slow)
2013/04/23 19:43:24
Okay, I understand the need for the NULL check giv
| |
| 54 base::ResetAndReturn(&startSyncCallback_).Run( | 62 base::ResetAndReturn(&startSyncCallback_).Run( |
| 55 OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS); | 63 OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS); |
| 56 } | 64 } |
| 57 } | 65 } |
| 58 | 66 |
| 59 - (IBAction)ok:(id)sender { | 67 - (IBAction)ok:(id)sender { |
| 60 base::ResetAndReturn(&startSyncCallback_).Run( | 68 if (isSyncDialog_) { |
| 69 base::ResetAndReturn(&startSyncCallback_).Run( | |
| 61 OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS); | 70 OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS); |
| 71 } | |
| 62 [self close]; | 72 [self close]; |
| 63 } | 73 } |
| 64 | 74 |
| 65 - (IBAction)onClickUndo:(id)sender { | 75 - (IBAction)onClickUndo:(id)sender { |
| 66 base::ResetAndReturn(&startSyncCallback_).Run( | 76 if (isSyncDialog_) { |
| 67 OneClickSigninSyncStarter::UNDO_SYNC); | 77 base::ResetAndReturn(&startSyncCallback_).Run( |
| 78 OneClickSigninSyncStarter::UNDO_SYNC); | |
| 79 } | |
| 68 [self close]; | 80 [self close]; |
| 69 } | 81 } |
| 70 | 82 |
| 71 - (IBAction)onClickAdvancedLink:(id)sender { | 83 - (IBAction)onClickAdvancedLink:(id)sender { |
| 72 base::ResetAndReturn(&startSyncCallback_).Run( | 84 if (isSyncDialog_) { |
| 73 OneClickSigninSyncStarter::CONFIGURE_SYNC_FIRST); | 85 base::ResetAndReturn(&startSyncCallback_).Run( |
| 86 OneClickSigninSyncStarter::CONFIGURE_SYNC_FIRST); | |
| 87 } else { | |
| 88 content::OpenURLParams params(GURL(chrome::kChromeUISettingsURL), | |
| 89 content::Referrer(), CURRENT_TAB, | |
| 90 content::PAGE_TRANSITION_LINK, false); | |
| 91 webContents_->OpenURL(params); | |
| 92 } | |
| 74 [self close]; | 93 [self close]; |
| 75 } | 94 } |
| 76 | 95 |
| 77 - (void)awakeFromNib { | 96 - (void)awakeFromNib { |
| 78 // Lay out the text controls from the bottom up. | 97 // Lay out the text controls from the bottom up. |
| 79 CGFloat totalYOffset = 0.0; | 98 CGFloat totalYOffset = 0.0; |
| 80 | 99 |
| 81 totalYOffset += | 100 totalYOffset += |
| 82 [GTMUILocalizerAndLayoutTweaker sizeToFitView:advancedLink_].height; | 101 [GTMUILocalizerAndLayoutTweaker sizeToFitView:advancedLink_].height; |
| 83 [[advancedLink_ cell] setTextColor: | 102 [[advancedLink_ cell] setTextColor: |
| 84 gfx::SkColorToCalibratedNSColor(chrome_style::GetLinkColor())]; | 103 gfx::SkColorToCalibratedNSColor(chrome_style::GetLinkColor())]; |
| 85 | 104 |
| 86 if (informativePlaceholderTextField_) { | 105 if (informativePlaceholderTextField_) { |
| 87 ShiftOriginY(informativePlaceholderTextField_, totalYOffset); | 106 ShiftOriginY(informativePlaceholderTextField_, totalYOffset); |
| 88 totalYOffset += [self initializeInformativeTextView]; | 107 totalYOffset += [self initializeInformativeTextView]; |
| 89 } | 108 } |
| 90 | 109 |
| 91 ShiftOriginY(messageTextField_, totalYOffset); | 110 ShiftOriginY(messageTextField_, totalYOffset); |
| 92 totalYOffset += | 111 totalYOffset += |
| 93 [GTMUILocalizerAndLayoutTweaker | 112 [GTMUILocalizerAndLayoutTweaker |
| 94 sizeToFitFixedWidthTextField:messageTextField_]; | 113 sizeToFitFixedWidthTextField:messageTextField_]; |
| 95 | 114 |
| 96 if (closeButton_) | 115 if (closeButton_) |
| 97 ShiftOriginY(closeButton_, totalYOffset); | 116 ShiftOriginY(closeButton_, totalYOffset); |
| 98 | 117 |
| 99 NSSize delta = NSMakeSize(0.0, totalYOffset); | 118 NSSize delta = NSMakeSize(0.0, totalYOffset); |
| 100 | 119 |
| 120 if (!isSyncDialog_ && [errorMessage_ length] != 0) | |
| 121 [messageTextField_ setStringValue:errorMessage_]; | |
| 122 | |
| 101 // Resize bubble and window to hold the controls. | 123 // Resize bubble and window to hold the controls. |
| 102 [GTMUILocalizerAndLayoutTweaker | 124 [GTMUILocalizerAndLayoutTweaker |
| 103 resizeViewWithoutAutoResizingSubViews:[self view] | 125 resizeViewWithoutAutoResizingSubViews:[self view] |
| 104 delta:delta]; | 126 delta:delta]; |
| 105 } | 127 } |
| 106 | 128 |
| 107 - (CGFloat)initializeInformativeTextView { | 129 - (CGFloat)initializeInformativeTextView { |
| 108 NSRect oldFrame = [informativePlaceholderTextField_ frame]; | 130 NSRect oldFrame = [informativePlaceholderTextField_ frame]; |
| 109 | 131 |
| 110 // Replace the placeholder NSTextField with the real label NSTextView. The | 132 // Replace the placeholder NSTextField with the real label NSTextView. The |
| 111 // former doesn't show links in a nice way, but the latter can't be added in | 133 // former doesn't show links in a nice way, but the latter can't be added in |
| 112 // a xib without a containing scroll view, so create the NSTextView | 134 // a xib without a containing scroll view, so create the NSTextView |
| 113 // programmatically. | 135 // programmatically. |
| 114 informativeTextView_.reset( | 136 informativeTextView_.reset( |
| 115 [[HyperlinkTextView alloc] initWithFrame:oldFrame]); | 137 [[HyperlinkTextView alloc] initWithFrame:oldFrame]); |
| 116 [informativeTextView_.get() setAutoresizingMask: | 138 [informativeTextView_.get() setAutoresizingMask: |
| 117 [informativePlaceholderTextField_ autoresizingMask]]; | 139 [informativePlaceholderTextField_ autoresizingMask]]; |
| 118 [informativeTextView_.get() setDelegate:self]; | 140 [informativeTextView_.get() setDelegate:self]; |
| 119 | 141 |
| 120 // Set the text. | 142 // Set the text. |
| 121 NSString* learnMoreText = l10n_util::GetNSStringWithFixup(IDS_LEARN_MORE); | 143 NSString* learnMoreText = l10n_util::GetNSStringWithFixup(IDS_LEARN_MORE); |
| 122 NSString* messageText = | 144 NSString* messageText; |
| 123 l10n_util::GetNSStringWithFixup(IDS_ONE_CLICK_SIGNIN_DIALOG_MESSAGE); | 145 |
| 124 messageText = [messageText stringByAppendingString:@" "]; | 146 NSFont* font; |
| 125 NSFont* font = ui::ResourceBundle::GetSharedInstance().GetFont( | 147 |
| 148 // the non-modal bubble already has a text content | |
|
Alexei Svitkine (slow)
2013/04/19 19:54:15
Comments should be full sentences.
noms (inactive)
2013/04/22 15:28:07
Done.
| |
| 149 // and only needs the Learn More link (in a smaller font) | |
| 150 if (isSyncDialog_) { | |
| 151 messageText = l10n_util::GetNSStringWithFixup(IDS_ONE_CLICK_SIGNIN_DIALOG_ME SSAGE); | |
|
Alexei Svitkine (slow)
2013/04/19 19:54:15
Wrap this so it doesn't excede 80 chars.
noms (inactive)
2013/04/22 15:28:07
Done.
| |
| 152 messageText = [messageText stringByAppendingString:@" "]; | |
| 153 font = ui::ResourceBundle::GetSharedInstance().GetFont( | |
| 126 chrome_style::kTextFontStyle).GetNativeFont(); | 154 chrome_style::kTextFontStyle).GetNativeFont(); |
| 155 } else { | |
| 156 messageText = @""; | |
| 157 font = ui::ResourceBundle::GetSharedInstance().GetFont( | |
| 158 ui::ResourceBundle::SmallFont).GetNativeFont(); | |
|
Alexei Svitkine (slow)
2013/04/19 19:54:15
I suggest just having a fontSize local variable th
noms (inactive)
2013/04/22 15:28:07
Done.
| |
| 159 } | |
| 160 | |
| 127 NSColor* linkColor = | 161 NSColor* linkColor = |
| 128 gfx::SkColorToCalibratedNSColor(chrome_style::GetLinkColor()); | 162 gfx::SkColorToCalibratedNSColor(chrome_style::GetLinkColor()); |
| 129 [informativeTextView_ setMessageAndLink:messageText | 163 [informativeTextView_ setMessageAndLink:messageText |
| 130 withLink:learnMoreText | 164 withLink:learnMoreText |
| 131 atOffset:[messageText length] | 165 atOffset:[messageText length] |
| 132 font:font | 166 font:font |
| 133 messageColor:[NSColor blackColor] | 167 messageColor:[NSColor blackColor] |
| 134 linkColor:linkColor]; | 168 linkColor:linkColor]; |
| 135 | 169 |
| 136 // Size to fit. | 170 // Size to fit. |
| 137 [[informativePlaceholderTextField_ cell] setAttributedStringValue: | 171 [[informativePlaceholderTextField_ cell] setAttributedStringValue: |
| 138 [informativeTextView_ attributedString]]; | 172 [informativeTextView_ attributedString]]; |
| 139 [GTMUILocalizerAndLayoutTweaker | 173 [GTMUILocalizerAndLayoutTweaker |
| 140 sizeToFitFixedWidthTextField:informativePlaceholderTextField_]; | 174 sizeToFitFixedWidthTextField:informativePlaceholderTextField_]; |
| 141 NSRect newFrame = [informativePlaceholderTextField_ frame]; | 175 NSRect newFrame = [informativePlaceholderTextField_ frame]; |
| 142 [informativeTextView_ setFrame:newFrame]; | 176 [informativeTextView_ setFrame:newFrame]; |
| 143 | 177 |
| 144 // Swap placeholder. | 178 // Swap placeholder. |
| 145 [[informativePlaceholderTextField_ superview] | 179 [[informativePlaceholderTextField_ superview] |
| 146 replaceSubview:informativePlaceholderTextField_ | 180 replaceSubview:informativePlaceholderTextField_ |
| 147 with:informativeTextView_.get()]; | 181 with:informativeTextView_.get()]; |
| 148 informativePlaceholderTextField_ = nil; // Now released. | 182 informativePlaceholderTextField_ = nil; // Now released. |
| 149 | 183 |
| 150 return NSHeight(newFrame) - NSHeight(oldFrame); | 184 return NSHeight(newFrame) - NSHeight(oldFrame); |
| 151 } | 185 } |
| 152 | 186 |
| 153 - (BOOL)textView:(NSTextView*)textView | 187 - (BOOL)textView:(NSTextView*)textView |
| 154 clickedOnLink:(id)link | 188 clickedOnLink:(id)link |
| 155 atIndex:(NSUInteger)charIndex { | 189 atIndex:(NSUInteger)charIndex { |
| 190 WindowOpenDisposition location = isSyncDialog_ ? | |
| 191 NEW_WINDOW : NEW_FOREGROUND_TAB; | |
| 192 | |
| 156 content::OpenURLParams params(GURL(chrome::kChromeSyncLearnMoreURL), | 193 content::OpenURLParams params(GURL(chrome::kChromeSyncLearnMoreURL), |
| 157 content::Referrer(), NEW_WINDOW, | 194 content::Referrer(), location, |
| 158 content::PAGE_TRANSITION_LINK, false); | 195 content::PAGE_TRANSITION_LINK, false); |
| 159 webContents_->OpenURL(params); | 196 webContents_->OpenURL(params); |
| 160 return YES; | 197 return YES; |
| 161 } | 198 } |
| 162 | 199 |
| 163 - (void)close { | 200 - (void)close { |
| 164 base::ResetAndReturn(&closeCallback_).Run(); | 201 base::ResetAndReturn(&closeCallback_).Run(); |
| 165 } | 202 } |
| 166 | 203 |
| 167 @end | 204 @end |
| OLD | NEW |