Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/passwords/autosignin_prompt_view_controller.h" | 5 #import "chrome/browser/ui/cocoa/passwords/autosignin_prompt_view_controller.h" |
| 6 | 6 |
| 7 #include <Carbon/Carbon.h> | |
| 8 | |
| 7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 8 #include "base/mac/scoped_nsobject.h" | 10 #include "base/mac/scoped_nsobject.h" |
| 9 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
| 12 #import "chrome/browser/ui/cocoa/key_equivalent_constants.h" | |
| 10 #import "chrome/browser/ui/cocoa/passwords/passwords_bubble_utils.h" | 13 #import "chrome/browser/ui/cocoa/passwords/passwords_bubble_utils.h" |
| 11 #include "chrome/browser/ui/passwords/password_dialog_controller.h" | 14 #include "chrome/browser/ui/passwords/password_dialog_controller.h" |
| 12 #include "chrome/browser/ui/passwords/password_dialog_prompts.h" | 15 #include "chrome/browser/ui/passwords/password_dialog_prompts.h" |
| 13 #include "chrome/grit/generated_resources.h" | 16 #include "chrome/grit/generated_resources.h" |
| 14 #include "ui/base/cocoa/controls/hyperlink_text_view.h" | 17 #include "ui/base/cocoa/controls/hyperlink_text_view.h" |
| 15 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
| 16 | 19 |
| 17 namespace { | 20 namespace { |
| 18 | 21 |
| 19 // Returns a NSRegularControlSize button. It's used for improving the contrast | 22 // Returns a NSRegularControlSize button. It's used for improving the contrast |
| 20 // due to Accessabilty standards. | 23 // due to Accessabilty standards. |
| 21 NSButton* BiggerDialogButton(NSString* title) { | 24 NSButton* BiggerDialogButton(NSString* title) { |
| 22 base::scoped_nsobject<NSButton> button( | 25 base::scoped_nsobject<NSButton> button( |
| 23 [[NSButton alloc] initWithFrame:NSZeroRect]); | 26 [[NSButton alloc] initWithFrame:NSZeroRect]); |
| 24 CGFloat fontSize = [NSFont systemFontSizeForControlSize:NSRegularControlSize]; | 27 CGFloat fontSize = [NSFont systemFontSizeForControlSize:NSRegularControlSize]; |
| 25 [button setFont:[NSFont systemFontOfSize:fontSize]]; | 28 [button setFont:[NSFont systemFontOfSize:fontSize]]; |
| 26 [button setTitle:title]; | 29 [button setTitle:title]; |
| 27 [button setBezelStyle:NSRoundedBezelStyle]; | 30 [button setBezelStyle:NSRoundedBezelStyle]; |
| 28 [[button cell] setControlSize:NSRegularControlSize]; | 31 [[button cell] setControlSize:NSRegularControlSize]; |
| 29 [button sizeToFit]; | 32 [button sizeToFit]; |
| 30 return button.autorelease(); | 33 return button.autorelease(); |
| 31 } | 34 } |
| 32 | 35 |
| 33 } // namespace | 36 } // namespace |
| 34 | 37 |
| 38 @interface AutoSigninPromptView : NSView | |
| 39 @property (nonatomic, copy) BOOL (^escHandler)(NSEvent* theEvent); | |
|
vabr (Chromium)
2016/05/09 11:16:17
Is this settable so that it can be a no-op before
vasilii
2016/05/09 11:45:54
It's done to avoid back pointers to the bridge or
| |
| 40 @end | |
| 41 | |
| 42 @implementation AutoSigninPromptView | |
| 43 @synthesize escHandler = _escHandler; | |
| 44 | |
| 45 -(void)dealloc { | |
| 46 [_escHandler release]; | |
| 47 [super dealloc]; | |
| 48 } | |
| 49 | |
| 50 - (BOOL)performKeyEquivalent:(NSEvent*)theEvent { | |
| 51 if (_escHandler(theEvent)) | |
| 52 return TRUE; | |
|
vabr (Chromium)
2016/05/09 11:16:17
TRUE or YES?
vasilii
2016/05/09 11:45:54
YES!
| |
| 53 return [super performKeyEquivalent:theEvent]; | |
| 54 } | |
| 55 | |
| 56 @end | |
| 57 | |
| 35 @interface AutoSigninPromptViewController () { | 58 @interface AutoSigninPromptViewController () { |
| 36 NSButton* _okButton; | 59 NSButton* _okButton; |
| 37 NSButton* _turnOffButton; | 60 NSButton* _turnOffButton; |
| 38 HyperlinkTextView* _contentText; | 61 HyperlinkTextView* _contentText; |
| 39 } | 62 } |
| 40 - (void)onOkClicked:(id)sender; | 63 - (void)onOkClicked:(id)sender; |
| 41 - (void)onTurnOffClicked:(id)sender; | 64 - (void)onTurnOffClicked:(id)sender; |
| 65 - (BOOL)handleEscPress:(NSEvent*)theEvent; | |
| 42 @end | 66 @end |
| 43 | 67 |
| 44 @implementation AutoSigninPromptViewController | 68 @implementation AutoSigninPromptViewController |
| 45 @synthesize bridge = _bridge; | 69 @synthesize bridge = _bridge; |
| 46 | 70 |
| 47 - (instancetype)initWithBridge:(PasswordPromptBridgeInterface*)bridge { | 71 - (instancetype)initWithBridge:(PasswordPromptBridgeInterface*)bridge { |
| 48 DCHECK(bridge); | 72 DCHECK(bridge); |
| 49 if (self = [super initWithNibName:nil bundle:nil]) { | 73 if (self = [super initWithNibName:nil bundle:nil]) { |
| 50 _bridge = bridge; | 74 _bridge = bridge; |
| 51 } | 75 } |
| 52 return self; | 76 return self; |
| 53 } | 77 } |
| 54 | 78 |
| 55 // ------------------------------------ | 79 // ------------------------------------ |
| 56 // | Title | | 80 // | Title | |
| 57 // | | | 81 // | | |
| 58 // | Auto Signin is cool. | | 82 // | Auto Signin is cool. | |
| 59 // | | | 83 // | | |
| 60 // | [ Turn Off ] [ OK ] | | 84 // | [ Turn Off ] [ OK ] | |
| 61 // ------------------------------------ | 85 // ------------------------------------ |
| 62 - (void)loadView { | 86 - (void)loadView { |
| 63 base::scoped_nsobject<NSView> view([[NSView alloc] initWithFrame:NSZeroRect]); | 87 base::scoped_nsobject<AutoSigninPromptView> view( |
| 88 [[AutoSigninPromptView alloc] initWithFrame:NSZeroRect]); | |
| 89 __block AutoSigninPromptViewController* weakSelf = self; | |
| 90 [view setEscHandler:^(NSEvent* theEvent) { | |
| 91 return [weakSelf handleEscPress:theEvent]; | |
| 92 }]; | |
| 93 | |
| 64 | 94 |
| 65 // Title. | 95 // Title. |
| 66 base::string16 titleText = | 96 base::string16 titleText = |
| 67 self.bridge->GetDialogController()->GetAutoSigninPromoTitle(); | 97 self.bridge->GetDialogController()->GetAutoSigninPromoTitle(); |
| 68 NSTextView* titleView = | 98 NSTextView* titleView = |
| 69 TitleDialogLabelWithLink(titleText, gfx::Range(), self); | 99 TitleDialogLabelWithLink(titleText, gfx::Range(), self); |
| 70 // The text container by default track the view's width only. Set the width to | 100 // The text container by default track the view's width only. Set the width to |
| 71 // a big number so the container is resized too. | 101 // a big number so the container is resized too. |
| 72 [titleView setFrameSize:NSMakeSize(MAXFLOAT, 0)]; | 102 [titleView setFrameSize:NSMakeSize(MAXFLOAT, 0)]; |
| 73 // Now force the view to track the text's size and resize it. | 103 // Now force the view to track the text's size and resize it. |
| 74 [titleView setHorizontallyResizable:YES]; | 104 [titleView setHorizontallyResizable:YES]; |
| 75 [titleView setVerticallyResizable:YES]; | 105 [titleView setVerticallyResizable:YES]; |
| 76 [titleView sizeToFit]; | 106 [titleView sizeToFit]; |
| 77 | 107 |
| 78 // Content. | 108 // Content. |
| 79 std::pair<base::string16, gfx::Range> contentText = | 109 std::pair<base::string16, gfx::Range> contentText = |
| 80 self.bridge->GetDialogController()->GetAutoSigninText(); | 110 self.bridge->GetDialogController()->GetAutoSigninText(); |
| 81 _contentText = LabelWithLink(contentText.first, kAutoSigninTextColor, | 111 _contentText = LabelWithLink(contentText.first, kAutoSigninTextColor, |
| 82 contentText.second, self); | 112 contentText.second, self); |
| 83 [_contentText setVerticallyResizable:YES]; | 113 [_contentText setVerticallyResizable:YES]; |
| 84 | 114 |
| 85 // Buttons. | 115 // Buttons. |
| 86 _okButton = | 116 _okButton = |
| 87 BiggerDialogButton(l10n_util::GetNSString(IDS_AUTO_SIGNIN_FIRST_RUN_OK)); | 117 BiggerDialogButton(l10n_util::GetNSString(IDS_AUTO_SIGNIN_FIRST_RUN_OK)); |
| 88 [_okButton setTarget:self]; | 118 [_okButton setTarget:self]; |
| 89 [_okButton setAction:@selector(onOkClicked:)]; | 119 [_okButton setAction:@selector(onOkClicked:)]; |
| 90 [_okButton setKeyEquivalent:@"\r"]; | 120 [_okButton setKeyEquivalent:kKeyEquivalentReturn]; |
| 91 [view addSubview:_okButton]; | 121 [view addSubview:_okButton]; |
| 92 | 122 |
| 93 _turnOffButton = BiggerDialogButton( | 123 _turnOffButton = BiggerDialogButton( |
| 94 l10n_util::GetNSString(IDS_AUTO_SIGNIN_FIRST_RUN_TURN_OFF)); | 124 l10n_util::GetNSString(IDS_AUTO_SIGNIN_FIRST_RUN_TURN_OFF)); |
| 95 [_turnOffButton setTarget:self]; | 125 [_turnOffButton setTarget:self]; |
| 96 [_turnOffButton setAction:@selector(onTurnOffClicked:)]; | 126 [_turnOffButton setAction:@selector(onTurnOffClicked:)]; |
| 97 [view addSubview:_turnOffButton]; | 127 [view addSubview:_turnOffButton]; |
| 98 | 128 |
| 129 // Invisible button to handle ESC. | |
| 130 base::scoped_nsobject<NSButton> cancel_button( | |
| 131 [[NSButton alloc] initWithFrame:NSZeroRect]); | |
| 132 [cancel_button setTarget:self]; | |
| 133 [cancel_button setAction:@selector(onEscClicked:)]; | |
| 134 [cancel_button setKeyEquivalent:kKeyEquivalentEscape]; | |
| 135 [view addSubview:cancel_button]; | |
| 136 | |
| 99 // Layout. | 137 // Layout. |
| 100 // Compute the bubble width using the title and the buttons. | 138 // Compute the bubble width using the title and the buttons. |
| 101 const CGFloat contentWidth = kDesiredBubbleWidth; | 139 const CGFloat contentWidth = kDesiredBubbleWidth; |
| 102 CGFloat curX = contentWidth - kFramePadding; | 140 CGFloat curX = contentWidth - kFramePadding; |
| 103 CGFloat curY = kFramePadding; | 141 CGFloat curY = kFramePadding; |
| 104 [_okButton setFrameOrigin:NSMakePoint(curX - NSWidth([_okButton frame]), | 142 [_okButton setFrameOrigin:NSMakePoint(curX - NSWidth([_okButton frame]), |
| 105 curY)]; | 143 curY)]; |
| 106 curX -= (NSWidth([_okButton frame]) + kRelatedControlHorizontalPadding); | 144 curX -= (NSWidth([_okButton frame]) + kRelatedControlHorizontalPadding); |
| 107 [_turnOffButton setFrameOrigin:NSMakePoint( | 145 [_turnOffButton setFrameOrigin:NSMakePoint( |
| 108 curX - NSWidth([_turnOffButton frame]), curY)]; | 146 curX - NSWidth([_turnOffButton frame]), curY)]; |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 135 - (void)onOkClicked:(id)sender { | 173 - (void)onOkClicked:(id)sender { |
| 136 if (_bridge && _bridge->GetDialogController()) | 174 if (_bridge && _bridge->GetDialogController()) |
| 137 _bridge->GetDialogController()->OnAutoSigninOK(); | 175 _bridge->GetDialogController()->OnAutoSigninOK(); |
| 138 } | 176 } |
| 139 | 177 |
| 140 - (void)onTurnOffClicked:(id)sender { | 178 - (void)onTurnOffClicked:(id)sender { |
| 141 if (_bridge && _bridge->GetDialogController()) | 179 if (_bridge && _bridge->GetDialogController()) |
| 142 _bridge->GetDialogController()->OnAutoSigninTurnOff(); | 180 _bridge->GetDialogController()->OnAutoSigninTurnOff(); |
| 143 } | 181 } |
| 144 | 182 |
| 183 - (BOOL)handleEscPress:(NSEvent*)theEvent { | |
| 184 if ([theEvent keyCode] == kVK_Escape) { | |
| 185 if (_bridge) | |
| 186 _bridge->PerformClose(); | |
| 187 return TRUE; | |
|
vabr (Chromium)
2016/05/09 11:16:17
ditto here and on line 189: YES/NO instead of TRUE
vasilii
2016/05/09 11:45:54
Done.
| |
| 188 } | |
| 189 return FALSE; | |
| 190 } | |
| 191 | |
| 145 @end | 192 @end |
| 146 | 193 |
| 147 @implementation AutoSigninPromptViewController(Testing) | 194 @implementation AutoSigninPromptViewController(Testing) |
| 148 - (NSTextView*)contentText { | 195 - (NSTextView*)contentText { |
| 149 return _contentText; | 196 return _contentText; |
| 150 } | 197 } |
| 151 | 198 |
| 152 - (NSButton*)okButton { | 199 - (NSButton*)okButton { |
| 153 return _okButton; | 200 return _okButton; |
| 154 } | 201 } |
| 155 | 202 |
| 156 - (NSButton*)turnOffButton { | 203 - (NSButton*)turnOffButton { |
| 157 return _turnOffButton; | 204 return _turnOffButton; |
| 158 } | 205 } |
| 159 @end | 206 @end |
| OLD | NEW |