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 #include "chrome/browser/ui/views/autofill/autofill_dialog_views.h" | 5 #include "chrome/browser/ui/views/autofill/autofill_dialog_views.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
145 // It exists to circumvent shortcomings of GridLayout and BoxLayout (namely that | 145 // It exists to circumvent shortcomings of GridLayout and BoxLayout (namely that |
146 // the former doesn't fully respect child visibility, and that the latter won't | 146 // the former doesn't fully respect child visibility, and that the latter won't |
147 // expand a single child). | 147 // expand a single child). |
148 class SectionRowView : public views::View { | 148 class SectionRowView : public views::View { |
149 public: | 149 public: |
150 SectionRowView() {} | 150 SectionRowView() {} |
151 virtual ~SectionRowView() {} | 151 virtual ~SectionRowView() {} |
152 | 152 |
153 // views::View implementation: | 153 // views::View implementation: |
154 virtual gfx::Size GetPreferredSize() OVERRIDE { | 154 virtual gfx::Size GetPreferredSize() OVERRIDE { |
155 // Only the height matters anyway. | 155 int h = 0; |
156 return child_at(2)->GetPreferredSize(); | 156 int w = 0; |
Ilya Sherman
2013/08/06 08:00:23
nit: Please spell out "height" and "width" (throug
Evan Stade
2013/08/06 19:00:37
I'll change it here, but the views function in vie
Evan Stade
2013/08/06 19:34:53
scratch that, all updated.
| |
157 for (int i = 0; i < child_count(); ++i) { | |
158 if (child_at(i)->visible()) { | |
159 if (w > 0) | |
160 w += kAroundTextPadding; | |
161 | |
162 gfx::Size size = child_at(i)->GetPreferredSize(); | |
163 h = std::max(h, size.height()); | |
164 w += size.width(); | |
165 } | |
166 } | |
167 | |
168 return gfx::Size(w, h); | |
157 } | 169 } |
158 | 170 |
159 virtual void Layout() OVERRIDE { | 171 virtual void Layout() OVERRIDE { |
160 const gfx::Rect bounds = GetContentsBounds(); | 172 const gfx::Rect bounds = GetContentsBounds(); |
161 | 173 |
162 // Icon is left aligned. | 174 // Icon is left aligned. |
163 int start_x = bounds.x(); | 175 int start_x = bounds.x(); |
164 views::View* icon = child_at(0); | 176 views::View* icon = child_at(0); |
165 if (icon->visible()) { | 177 if (icon->visible()) { |
166 icon->SizeToPreferredSize(); | 178 icon->SizeToPreferredSize(); |
(...skipping 812 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
979 ui::MouseEvent AutofillDialogViews::SectionContainer::ProxyEvent( | 991 ui::MouseEvent AutofillDialogViews::SectionContainer::ProxyEvent( |
980 const ui::MouseEvent& event) { | 992 const ui::MouseEvent& event) { |
981 ui::MouseEvent event_copy = event; | 993 ui::MouseEvent event_copy = event; |
982 event_copy.set_location(gfx::Point()); | 994 event_copy.set_location(gfx::Point()); |
983 return event_copy; | 995 return event_copy; |
984 } | 996 } |
985 | 997 |
986 // AutofilDialogViews::SuggestionView ------------------------------------------ | 998 // AutofilDialogViews::SuggestionView ------------------------------------------ |
987 | 999 |
988 AutofillDialogViews::SuggestionView::SuggestionView( | 1000 AutofillDialogViews::SuggestionView::SuggestionView( |
989 const base::string16& edit_label, | |
990 AutofillDialogViews* autofill_dialog) | 1001 AutofillDialogViews* autofill_dialog) |
991 : label_(new views::Label()), | 1002 : label_(new views::Label()), |
992 label_line_2_(new views::Label()), | 1003 label_line_2_(new views::Label()), |
993 icon_(new views::ImageView()), | 1004 icon_(new views::ImageView()), |
994 label_container_(new SectionRowView()), | |
995 decorated_( | 1005 decorated_( |
996 new DecoratedTextfield(base::string16(), | 1006 new DecoratedTextfield(base::string16(), |
997 base::string16(), | 1007 base::string16(), |
998 autofill_dialog)) { | 1008 autofill_dialog)) { |
999 // TODO(estade): Make this the correct color. | 1009 // TODO(estade): Make this the correct color. |
1000 set_border( | 1010 set_border( |
1001 views::Border::CreateSolidSidedBorder(1, 0, 0, 0, SK_ColorLTGRAY)); | 1011 views::Border::CreateSolidSidedBorder(1, 0, 0, 0, SK_ColorLTGRAY)); |
1002 | 1012 |
1013 SectionRowView* label_container = new SectionRowView(); | |
1014 AddChildView(label_container); | |
1015 | |
1003 // Label and icon. | 1016 // Label and icon. |
1017 label_container->AddChildView(icon_); | |
1004 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 1018 label_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
1005 label_container_->AddChildView(icon_); | 1019 label_container->AddChildView(label_); |
1006 label_container_->AddChildView(label_); | 1020 |
1007 label_container_->AddChildView(decorated_); | 1021 // TODO(estade): get the sizing and spacing right on this textfield. |
1008 decorated_->SetVisible(false); | 1022 decorated_->SetVisible(false); |
1009 // TODO(estade): get the sizing and spacing right on this textfield. | |
1010 decorated_->set_default_width_in_chars(10); | 1023 decorated_->set_default_width_in_chars(10); |
1011 AddChildView(label_container_); | 1024 label_container->AddChildView(decorated_); |
1012 | 1025 |
1026 // TODO(estade): need to get the line height right. | |
1013 label_line_2_->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 1027 label_line_2_->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
1014 label_line_2_->SetVisible(false); | 1028 label_line_2_->SetVisible(false); |
1015 label_line_2_->SetMultiLine(true); | 1029 label_line_2_->SetMultiLine(true); |
1016 AddChildView(label_line_2_); | 1030 AddChildView(label_line_2_); |
1017 | 1031 |
1018 // TODO(estade): do something about this '2'. | 1032 // TODO(estade): do something about this '2'. |
1019 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 0, 2, 0)); | 1033 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 0, 2, 0)); |
1020 } | 1034 } |
1021 | 1035 |
1022 AutofillDialogViews::SuggestionView::~SuggestionView() {} | 1036 AutofillDialogViews::SuggestionView::~SuggestionView() {} |
1023 | 1037 |
1024 void AutofillDialogViews::SuggestionView::SetSuggestionText( | 1038 gfx::Size AutofillDialogViews::SuggestionView::GetPreferredSize() { |
1025 const base::string16& text, | 1039 // There's no preferred width. The parent's layout should get the preferred |
1026 gfx::Font::FontStyle text_style) { | 1040 // height from GetHeightForWidth(). |
1027 label_->SetFont(ui::ResourceBundle::GetSharedInstance().GetFont( | 1041 return gfx::Size(); |
1028 ui::ResourceBundle::BaseFont).DeriveFont(0, text_style)); | 1042 } |
1029 | 1043 |
1044 int AutofillDialogViews::SuggestionView::GetHeightForWidth(int w) { | |
1045 int h = 0; | |
1046 CanUseVerticallyCompactText(w, &h); | |
1047 return h; | |
1048 } | |
1049 | |
1050 bool AutofillDialogViews::SuggestionView::CanUseVerticallyCompactText( | |
1051 int available_width, int* resulting_height) { | |
1052 // This calculation may be costly, avoid doing it more than once per width. | |
1053 if (!calculated_heights_.count(available_width)) { | |
1054 // Changing the state of |this| now will lead to extra layouts and | |
1055 // paints we don't want, so create another SuggestionView to calculate | |
1056 // which label we have room to show. | |
1057 SuggestionView sizing_view(NULL); | |
1058 sizing_view.SetLabelText(state_.vertically_compact_text); | |
1059 sizing_view.SetIcon(state_.icon); | |
1060 sizing_view.SetTextfield(state_.extra_text, state_.extra_icon); | |
1061 | |
1062 // Shortcut |sizing_view|'s GetHeightForWidth() to avoid an infinite loop. | |
1063 // Its BoxLayout must do these calculations for us. | |
1064 views::LayoutManager* layout = sizing_view.GetLayoutManager(); | |
1065 if (layout->GetPreferredSize(&sizing_view).width() <= available_width) { | |
1066 calculated_heights_[available_width] = std::make_pair( | |
1067 true, | |
1068 layout->GetPreferredHeightForWidth(&sizing_view, available_width)); | |
1069 } else { | |
1070 sizing_view.SetLabelText(state_.horizontally_compact_text); | |
1071 calculated_heights_[available_width] = std::make_pair( | |
1072 false, | |
1073 layout->GetPreferredHeightForWidth(&sizing_view, available_width)); | |
1074 } | |
1075 } | |
1076 | |
1077 std::pair<bool, int> values = calculated_heights_[available_width]; | |
1078 *resulting_height = values.second; | |
1079 return values.first; | |
1080 } | |
1081 | |
1082 void AutofillDialogViews::SuggestionView::OnBoundsChanged( | |
1083 const gfx::Rect& previous_bounds) { | |
1084 int unused; | |
1085 SetLabelText(CanUseVerticallyCompactText(width(), &unused) ? | |
1086 state_.vertically_compact_text : | |
1087 state_.horizontally_compact_text); | |
1088 } | |
1089 | |
1090 void AutofillDialogViews::SuggestionView::SetState( | |
1091 const SuggestionState& state) { | |
1092 calculated_heights_.clear(); | |
1093 state_ = state; | |
1094 SetVisible(state_.show); | |
1095 // Set to the more compact text for now. |this| will optionally switch to | |
1096 // the more vertically expanded view when the bounds are set. | |
1097 SetLabelText(state_.vertically_compact_text); | |
1098 SetIcon(state_.icon); | |
1099 SetTextfield(state_.extra_text, state_.extra_icon); | |
1100 PreferredSizeChanged(); | |
1101 } | |
1102 | |
1103 void AutofillDialogViews::SuggestionView::SetLabelText( | |
1104 const base::string16& text) { | |
1030 // TODO(estade): does this localize well? | 1105 // TODO(estade): does this localize well? |
1031 base::string16 line_return(ASCIIToUTF16("\n")); | 1106 base::string16 line_return(ASCIIToUTF16("\n")); |
1032 size_t position = text.find(line_return); | 1107 size_t position = text.find(line_return); |
1033 if (position == base::string16::npos) { | 1108 if (position == base::string16::npos) { |
1034 label_->SetText(text); | 1109 label_->SetText(text); |
1035 label_line_2_->SetVisible(false); | 1110 label_line_2_->SetVisible(false); |
1036 } else { | 1111 } else { |
1037 label_->SetText(text.substr(0, position)); | 1112 label_->SetText(text.substr(0, position)); |
1038 label_line_2_->SetText(text.substr(position + line_return.length())); | 1113 label_line_2_->SetText(text.substr(position + line_return.length())); |
1039 label_line_2_->SetVisible(true); | 1114 label_line_2_->SetVisible(true); |
1040 } | 1115 } |
1041 } | 1116 } |
1042 | 1117 |
1043 void AutofillDialogViews::SuggestionView::SetSuggestionIcon( | 1118 void AutofillDialogViews::SuggestionView::SetIcon( |
1044 const gfx::Image& image) { | 1119 const gfx::Image& image) { |
1045 icon_->SetVisible(!image.IsEmpty()); | 1120 icon_->SetVisible(!image.IsEmpty()); |
1046 icon_->SetImage(image.AsImageSkia()); | 1121 icon_->SetImage(image.AsImageSkia()); |
1047 } | 1122 } |
1048 | 1123 |
1049 void AutofillDialogViews::SuggestionView::ShowTextfield( | 1124 void AutofillDialogViews::SuggestionView::SetTextfield( |
1050 const base::string16& placeholder_text, | 1125 const base::string16& placeholder_text, |
1051 const gfx::Image& icon) { | 1126 const gfx::Image& icon) { |
1052 decorated_->set_placeholder_text(placeholder_text); | 1127 decorated_->set_placeholder_text(placeholder_text); |
1053 decorated_->SetIcon(icon); | 1128 decorated_->SetIcon(icon); |
1054 decorated_->SetVisible(true); | 1129 decorated_->SetVisible(!placeholder_text.empty()); |
1055 // The textfield will increase the height of the first row and cause the | |
1056 // label to be aligned properly, so the border is not necessary. | |
1057 label_->set_border(NULL); | |
1058 } | 1130 } |
1059 | 1131 |
1060 // AutofillDialogViews::AutocheckoutStepsArea --------------------------------- | 1132 // AutofillDialogViews::AutocheckoutStepsArea --------------------------------- |
1061 | 1133 |
1062 AutofillDialogViews::AutocheckoutStepsArea::AutocheckoutStepsArea() { | 1134 AutofillDialogViews::AutocheckoutStepsArea::AutocheckoutStepsArea() { |
1063 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, | 1135 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, |
1064 kAutocheckoutStepsAreaPadding, | 1136 kAutocheckoutStepsAreaPadding, |
1065 0, | 1137 0, |
1066 kAutocheckoutStepInset)); | 1138 kAutocheckoutStepInset)); |
1067 } | 1139 } |
(...skipping 721 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1789 | 1861 |
1790 views::View* AutofillDialogViews::CreateInputsContainer(DialogSection section) { | 1862 views::View* AutofillDialogViews::CreateInputsContainer(DialogSection section) { |
1791 // The |info_view| holds |manual_inputs| and |suggested_info|, allowing the | 1863 // The |info_view| holds |manual_inputs| and |suggested_info|, allowing the |
1792 // dialog to toggle which is shown. | 1864 // dialog to toggle which is shown. |
1793 views::View* info_view = new views::View(); | 1865 views::View* info_view = new views::View(); |
1794 info_view->SetLayoutManager( | 1866 info_view->SetLayoutManager( |
1795 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0)); | 1867 new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0)); |
1796 | 1868 |
1797 views::View* manual_inputs = InitInputsView(section); | 1869 views::View* manual_inputs = InitInputsView(section); |
1798 info_view->AddChildView(manual_inputs); | 1870 info_view->AddChildView(manual_inputs); |
1799 SuggestionView* suggested_info = | 1871 SuggestionView* suggested_info = new SuggestionView(this); |
1800 new SuggestionView(controller_->EditSuggestionText(), this); | |
1801 info_view->AddChildView(suggested_info); | 1872 info_view->AddChildView(suggested_info); |
1802 | 1873 |
1803 // TODO(estade): It might be slightly more OO if this button were created | 1874 // TODO(estade): It might be slightly more OO if this button were created |
1804 // and listened to by the section container. | 1875 // and listened to by the section container. |
1805 views::ImageButton* menu_button = new views::ImageButton(this); | 1876 views::ImageButton* menu_button = new views::ImageButton(this); |
1806 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 1877 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
1807 menu_button->SetImage(views::Button::STATE_NORMAL, | 1878 menu_button->SetImage(views::Button::STATE_NORMAL, |
1808 rb.GetImageSkiaNamed(IDR_AUTOFILL_DIALOG_MENU_BUTTON)); | 1879 rb.GetImageSkiaNamed(IDR_AUTOFILL_DIALOG_MENU_BUTTON)); |
1809 menu_button->SetImage(views::Button::STATE_PRESSED, | 1880 menu_button->SetImage(views::Button::STATE_PRESSED, |
1810 rb.GetImageSkiaNamed(IDR_AUTOFILL_DIALOG_MENU_BUTTON_P)); | 1881 rb.GetImageSkiaNamed(IDR_AUTOFILL_DIALOG_MENU_BUTTON_P)); |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1946 } | 2017 } |
1947 } | 2018 } |
1948 } | 2019 } |
1949 | 2020 |
1950 UpdateDetailsGroupState(*group); | 2021 UpdateDetailsGroupState(*group); |
1951 } | 2022 } |
1952 | 2023 |
1953 void AutofillDialogViews::UpdateDetailsGroupState(const DetailsGroup& group) { | 2024 void AutofillDialogViews::UpdateDetailsGroupState(const DetailsGroup& group) { |
1954 const SuggestionState& suggestion_state = | 2025 const SuggestionState& suggestion_state = |
1955 controller_->SuggestionStateForSection(group.section); | 2026 controller_->SuggestionStateForSection(group.section); |
1956 bool show_suggestions = !suggestion_state.text.empty(); | 2027 group.suggested_info->SetState(suggestion_state); |
1957 group.suggested_info->SetVisible(show_suggestions); | 2028 group.manual_input->SetVisible(!suggestion_state.show); |
1958 group.suggested_info->SetSuggestionText(suggestion_state.text, | |
1959 suggestion_state.text_style); | |
1960 group.suggested_info->SetSuggestionIcon(suggestion_state.icon); | |
1961 | |
1962 if (!suggestion_state.extra_text.empty()) { | |
1963 group.suggested_info->ShowTextfield( | |
1964 suggestion_state.extra_text, | |
1965 suggestion_state.extra_icon); | |
1966 } | |
1967 | |
1968 group.manual_input->SetVisible(!show_suggestions); | |
1969 | 2029 |
1970 // Show or hide the "Save in chrome" checkbox. If nothing is in editing mode, | 2030 // Show or hide the "Save in chrome" checkbox. If nothing is in editing mode, |
1971 // hide. If the controller tells us not to show it, likewise hide. | 2031 // hide. If the controller tells us not to show it, likewise hide. |
1972 UpdateSaveInChromeCheckbox(); | 2032 UpdateSaveInChromeCheckbox(); |
1973 | 2033 |
1974 const bool has_menu = !!controller_->MenuModelForSection(group.section); | 2034 const bool has_menu = !!controller_->MenuModelForSection(group.section); |
1975 | 2035 |
1976 if (group.suggested_button) | 2036 if (group.suggested_button) |
1977 group.suggested_button->SetVisible(has_menu); | 2037 group.suggested_button->SetVisible(has_menu); |
1978 | 2038 |
1979 if (group.container) { | 2039 if (group.container) { |
1980 group.container->SetForwardMouseEvents(has_menu && show_suggestions); | 2040 group.container->SetForwardMouseEvents( |
2041 has_menu && suggestion_state.show); | |
1981 group.container->SetVisible(controller_->SectionIsActive(group.section)); | 2042 group.container->SetVisible(controller_->SectionIsActive(group.section)); |
1982 if (group.container->visible()) | 2043 if (group.container->visible()) |
1983 ValidateGroup(group, VALIDATE_EDIT); | 2044 ValidateGroup(group, VALIDATE_EDIT); |
1984 } | 2045 } |
1985 | 2046 |
1986 ContentsPreferredSizeChanged(); | 2047 ContentsPreferredSizeChanged(); |
1987 } | 2048 } |
1988 | 2049 |
1989 template<class T> | 2050 template<class T> |
1990 void AutofillDialogViews::SetValidityForInput( | 2051 void AutofillDialogViews::SetValidityForInput( |
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2267 AutofillDialogViews::DetailsGroup::DetailsGroup(DialogSection section) | 2328 AutofillDialogViews::DetailsGroup::DetailsGroup(DialogSection section) |
2268 : section(section), | 2329 : section(section), |
2269 container(NULL), | 2330 container(NULL), |
2270 manual_input(NULL), | 2331 manual_input(NULL), |
2271 suggested_info(NULL), | 2332 suggested_info(NULL), |
2272 suggested_button(NULL) {} | 2333 suggested_button(NULL) {} |
2273 | 2334 |
2274 AutofillDialogViews::DetailsGroup::~DetailsGroup() {} | 2335 AutofillDialogViews::DetailsGroup::~DetailsGroup() {} |
2275 | 2336 |
2276 } // namespace autofill | 2337 } // namespace autofill |
OLD | NEW |