OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #import "chrome/browser/ui/cocoa/passwords/credentials_selection_view.h" | |
6 | |
7 #include <stddef.h> | |
8 | |
9 #import "chrome/browser/ui/cocoa/bubble_combobox.h" | |
10 #include "chrome/browser/ui/cocoa/chrome_style.h" | |
11 #import "chrome/browser/ui/cocoa/passwords/passwords_bubble_utils.h" | |
12 #include "chrome/browser/ui/passwords/manage_passwords_bubble_model.h" | |
13 #include "ui/base/models/simple_combobox_model.h" | |
14 | |
15 namespace { | |
16 | |
17 NSPopUpButton* CreateUsernamesPopUpButton( | |
18 const std::vector<autofill::PasswordForm>& forms, | |
19 const base::string16& best_matched_username) { | |
20 DCHECK(!forms.empty()); | |
21 std::vector<base::string16> usernames; | |
22 size_t best_matched_username_index = forms.size(); | |
23 size_t preffered_form_index = forms.size(); | |
24 | |
25 for (size_t index = 0; index < forms.size(); ++index) { | |
26 usernames.push_back(forms[index].username_value); | |
27 if (forms[index].username_value == best_matched_username) { | |
28 best_matched_username_index = index; | |
29 } | |
30 if (forms[index].preferred) { | |
31 preffered_form_index = index; | |
32 } | |
33 } | |
34 | |
35 ui::SimpleComboboxModel model(usernames); | |
36 base::scoped_nsobject<NSPopUpButton> button([[BubbleCombobox alloc] | |
37 initWithFrame:NSZeroRect | |
38 pullsDown:NO | |
39 model:&model]); | |
40 [button setFont:LabelFont()]; | |
41 | |
42 if (best_matched_username_index < forms.size()) { | |
43 [button selectItemAtIndex:best_matched_username_index]; | |
44 } else if (preffered_form_index < forms.size()) { | |
45 [button selectItemAtIndex:preffered_form_index]; | |
46 } else { | |
47 [button selectItemAtIndex:0]; | |
48 } | |
49 | |
50 [button sizeToFit]; | |
51 return button.autorelease(); | |
52 } | |
53 | |
54 } // namespace | |
55 | |
56 @implementation CredentialsSelectionView | |
57 | |
58 - (id)initWithModel:(ManagePasswordsBubbleModel*)model { | |
59 if ((self = [super init])) { | |
60 model_ = model; | |
61 | |
62 // Create the pop up button with usernames and the password field. | |
63 usernamePopUpButton_.reset([CreateUsernamesPopUpButton( | |
64 model_->local_credentials(), | |
65 model_->pending_password().username_value) retain]); | |
66 passwordField_.reset( | |
67 [PasswordLabel(model_->pending_password().password_value) retain]); | |
68 | |
69 // Calculate desired widths of username and password. | |
70 CGFloat firstWidth = NSMaxX([usernamePopUpButton_ frame]); | |
71 CGFloat secondWidth = NSMaxX([passwordField_ frame]); | |
72 std::pair<CGFloat, CGFloat> sizes = GetResizedColumns( | |
73 kDesiredRowWidth, std::make_pair(firstWidth, secondWidth)); | |
74 CGFloat curX = 0; | |
75 CGFloat curY = 0; | |
76 | |
77 [usernamePopUpButton_ | |
78 setFrameSize:NSMakeSize(sizes.first, | |
79 NSHeight([usernamePopUpButton_ frame]))]; | |
80 [self addSubview:usernamePopUpButton_]; | |
81 | |
82 [passwordField_ setFrameSize:NSMakeSize(sizes.second, | |
83 NSHeight([passwordField_ frame]))]; | |
84 [self addSubview:passwordField_]; | |
85 | |
86 // Calculate username and password positions. The element with bigger height | |
87 // will have Y coordinate equal to curY, another one will have Y coordinate | |
88 // equals to curY + half of their heights difference. | |
89 CGFloat popUpLabelHeightDifference = | |
90 NSHeight([usernamePopUpButton_ frame]) - | |
91 NSHeight([passwordField_ frame]); | |
92 | |
93 CGFloat usernameY = std::max(curY, curY - popUpLabelHeightDifference / 2); | |
94 CGFloat passwordY = std::max(curY, curY + popUpLabelHeightDifference / 2); | |
95 | |
96 [usernamePopUpButton_ setFrameOrigin:NSMakePoint(curX, usernameY)]; | |
97 // Move to the right of the username and add the password. | |
98 curX = NSMaxX([usernamePopUpButton_ frame]) + kItemLabelSpacing; | |
99 [passwordField_ setFrameOrigin:NSMakePoint(curX, passwordY)]; | |
100 | |
101 // Move to the top-right of the password. | |
102 curY = std::max(NSMaxY([passwordField_ frame]), | |
103 NSMaxY([usernamePopUpButton_ frame])); | |
104 | |
105 // Update the frame. | |
106 [self setFrameSize:NSMakeSize(kDesiredRowWidth, curY)]; | |
107 } | |
108 return self; | |
109 } | |
110 | |
111 - (const autofill::PasswordForm*)getSelectedCredentials { | |
112 int selected_index = [usernamePopUpButton_ indexOfSelectedItem]; | |
113 CHECK(selected_index >= 0); | |
114 return &model_->local_credentials()[selected_index]; | |
115 } | |
116 | |
117 @end | |
OLD | NEW |