Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1329)

Unified Diff: chrome/browser/ui/cocoa/passwords/credentials_selection_view.mm

Issue 1515553006: Change password bubble for Mac. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Tiny comment update Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..cf408a41267c5f4c6a4103c55ab40dbc60a2e2b0
--- /dev/null
+++ b/chrome/browser/ui/cocoa/passwords/credentials_selection_view.mm
@@ -0,0 +1,115 @@
+// 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.
+
+#import "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 {
+
+NSPopUpButton* CreateUsernamesPopUpButton(
+ const std::vector<const autofill::PasswordForm*>& forms,
+ const base::string16& best_matched_username) {
+ DCHECK(!forms.empty());
+ 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;
+ }
+ }
+
+ ui::SimpleComboboxModel model(usernames);
+ base::scoped_nsobject<NSPopUpButton> button([[BubbleCombobox alloc]
+ initWithFrame:NSZeroRect
+ pullsDown:NO
+ model:&model]);
+ [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];
+ } else {
+ [button selectItemAtIndex:0];
+ }
+
+ [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 = 0;
+
+ [usernamePopUpButton_
+ setFrameSize:NSMakeSize(sizes.first,
+ NSHeight([usernamePopUpButton_ frame]))];
+ [self addSubview:usernamePopUpButton_];
+
+ [passwordField_ setFrameSize:NSMakeSize(sizes.second,
+ NSHeight([passwordField_ frame]))];
+ [self addSubview:passwordField_];
+
+ // Calculate username and password positions. The element with bigger height
+ // will have Y coordinate equal to curY, another one will have Y coordinate
+ // equals to curY + half of their heights difference.
+ CGFloat popUpLabelHeightDifference =
+ NSHeight([usernamePopUpButton_ frame]) -
+ NSHeight([passwordField_ frame]);
+
+ CGFloat usernameY = std::max(curY, curY - popUpLabelHeightDifference / 2);
+ CGFloat passwordY = std::max(curY, curY + popUpLabelHeightDifference / 2);
+
+ [usernamePopUpButton_ setFrameOrigin:NSMakePoint(curX, usernameY)];
+ // Move to the right of the username and add the password.
+ curX = NSMaxX([usernamePopUpButton_ frame]) + kItemLabelSpacing;
+ [passwordField_ setFrameOrigin:NSMakePoint(curX, passwordY)];
+
+ // Move to the top-right of the password.
+ curY = std::max(NSMaxY([passwordField_ frame]),
+ NSMaxY([usernamePopUpButton_ frame]));
+
+ // Update the frame.
+ [self setFrameSize:NSMakeSize(kDesiredRowWidth, curY)];
+ }
+ return self;
+}
+
+- (const autofill::PasswordForm*)getSelectedCredentials {
+ int selected_index = [usernamePopUpButton_ indexOfSelectedItem];
+ CHECK(selected_index >= 0);
+ return model_->local_credentials()[selected_index];
+}
+
+@end

Powered by Google App Engine
This is Rietveld 408576698