Index: chrome/browser/ui/cocoa/passwords/credentials_selection_view.mm |
diff --git a/chrome/browser/ui/cocoa/passwords/credentials_selection_view.mm b/chrome/browser/ui/cocoa/passwords/credentials_selection_view.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8829cb28fb20c93c13f71c56a647a2e44a6ce833 |
--- /dev/null |
+++ b/chrome/browser/ui/cocoa/passwords/credentials_selection_view.mm |
@@ -0,0 +1,105 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/ui/cocoa/passwords/credentials_selection_view.h" |
+ |
+#include "chrome/browser/ui/chrome_style.h" |
+#import "chrome/browser/ui/cocoa/bubble_combobox.h" |
+#import "chrome/browser/ui/cocoa/passwords/passwords_bubble_utils.h" |
+#include "chrome/browser/ui/passwords/manage_passwords_bubble_model.h" |
+#include "ui/base/models/simple_combobox_model.h" |
+ |
+namespace { |
+ |
+const CGFloat kVerticalComboboxLabelSpacing = 6; |
+ |
+NSPopUpButton* CreateUsernamesPopUpButton( |
+ const std::vector<const autofill::PasswordForm*>& forms, |
+ const base::string16& best_matched_username) { |
+ std::vector<base::string16> usernames; |
+ size_t best_matched_username_index = forms.size(); |
+ size_t preffered_form_index = forms.size(); |
+ |
+ for (size_t index = 0; index < forms.size(); ++index) { |
+ usernames.push_back(forms[index]->username_value); |
+ if (forms[index]->username_value == best_matched_username) { |
+ best_matched_username_index = index; |
+ } |
+ if (forms[index]->preferred) { |
+ preffered_form_index = index; |
+ } |
+ } |
+ |
+ scoped_ptr<ui::SimpleComboboxModel> model( |
vasilii
2015/12/15 15:58:01
Looks like without the scoped pointer it will work
dvadym
2015/12/16 14:16:30
Done.
|
+ new ui::SimpleComboboxModel(usernames)); |
+ base::scoped_nsobject<NSPopUpButton> button([[BubbleCombobox alloc] |
+ initWithFrame:NSZeroRect |
+ pullsDown:NO |
+ model:model.get()]); |
+ [button setFont:LabelFont()]; |
+ |
+ if (best_matched_username_index < forms.size()) { |
+ [button selectItemAtIndex:best_matched_username_index]; |
+ } else if (preffered_form_index < forms.size()) { |
+ [button selectItemAtIndex:preffered_form_index]; |
+ } |
+ |
+ [button sizeToFit]; |
+ return button.autorelease(); |
+} |
+ |
+} // namespace |
+ |
+@implementation CredentialsSelectionView |
+ |
+- (id)initWithModel:(ManagePasswordsBubbleModel*)model { |
+ if ((self = [super init])) { |
+ model_ = model; |
+ |
+ // Create the pop up button with usernames and the password field. |
+ usernamePopUpButton_.reset([CreateUsernamesPopUpButton( |
+ model_->local_credentials().get(), |
+ model_->pending_password().username_value) retain]); |
+ passwordField_.reset( |
+ [PasswordLabel(model_->pending_password().password_value) retain]); |
+ |
+ // Calculate desired widths of username and password. |
+ CGFloat firstWidth = NSMaxX([usernamePopUpButton_ frame]); |
+ CGFloat secondWidth = NSMaxX([passwordField_ frame]); |
+ std::pair<CGFloat, CGFloat> sizes = GetResizedColumns( |
+ kDesiredRowWidth, std::make_pair(firstWidth, secondWidth)); |
+ CGFloat curX = 0; |
+ CGFloat curY = kRelatedControlVerticalSpacing; |
vasilii
2015/12/15 15:58:01
I'm not sure about this. You hardcode the margin h
dvadym
2015/12/16 14:16:30
Done, I've set curY to 0
|
+ |
+ [usernamePopUpButton_ |
+ setFrameSize:NSMakeSize(sizes.first, |
+ NSHeight([usernamePopUpButton_ frame]))]; |
+ [self addSubview:usernamePopUpButton_]; |
+ [usernamePopUpButton_ setFrameOrigin:NSMakePoint(curX, curY)]; |
+ |
+ // Move to the right of the username and add the password. |
+ curX = NSMaxX([usernamePopUpButton_ frame]) + kItemLabelSpacing; |
+ [passwordField_ setFrameSize:NSMakeSize(sizes.second, |
+ NSHeight([passwordField_ frame]))]; |
+ |
+ [passwordField_ |
+ setFrameOrigin:NSMakePoint(curX, curY + kVerticalComboboxLabelSpacing)]; |
vasilii
2015/12/15 15:58:01
I don't know how it looks like if the user changes
dvadym
2015/12/16 14:16:31
Thanks, I've replaced this constant by difference
|
+ [self addSubview:passwordField_]; |
+ |
+ // Move to the top-right of the password. |
+ curY = NSMaxY([passwordField_ frame]) + kRelatedControlVerticalSpacing; |
+ |
+ // Update the frame. |
+ [self setFrameSize:NSMakeSize(kDesiredRowWidth, curY)]; |
+ } |
+ return self; |
+} |
+ |
+- (const autofill::PasswordForm*)getSelectedCredentials { |
+ size_t selected_index = |
+ static_cast<size_t>([usernamePopUpButton_ indexOfSelectedItem]); |
vasilii
2015/12/15 15:58:01
It can return -1.
dvadym
2015/12/16 14:16:31
Done.
|
+ return model_->local_credentials()[selected_index]; |
+} |
+ |
+@end |