| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/decorated_textfield.h" | 5 #include "chrome/browser/ui/views/autofill/decorated_textfield.h" |
| 6 | 6 |
| 7 #include "chrome/browser/ui/autofill/autofill_dialog_types.h" | 7 #include "chrome/browser/ui/autofill/autofill_dialog_types.h" |
| 8 #include "chrome/browser/ui/views/autofill/tooltip_icon.h" | 8 #include "chrome/browser/ui/views/autofill/tooltip_icon.h" |
| 9 #include "ui/gfx/canvas.h" | 9 #include "ui/gfx/canvas.h" |
| 10 #include "ui/views/background.h" | 10 #include "ui/views/background.h" |
| 11 #include "ui/views/controls/button/label_button.h" | 11 #include "ui/views/controls/button/label_button.h" |
| 12 #include "ui/views/controls/focusable_border.h" | 12 #include "ui/views/controls/focusable_border.h" |
| 13 #include "ui/views/controls/textfield/native_textfield_views.h" |
| 13 #include "ui/views/controls/textfield/textfield_controller.h" | 14 #include "ui/views/controls/textfield/textfield_controller.h" |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 // Padding around icons inside DecoratedTextfields. | 18 // Padding around icons inside DecoratedTextfields. |
| 18 const int kTextfieldIconPadding = 3; | 19 const int kTextfieldIconPadding = 3; |
| 19 | 20 |
| 20 } // namespace | 21 } // namespace |
| 21 | 22 |
| 22 namespace autofill { | 23 namespace autofill { |
| 23 | 24 |
| 24 // static | 25 // static |
| 25 const char DecoratedTextfield::kViewClassName[] = "autofill/DecoratedTextfield"; | 26 const char DecoratedTextfield::kViewClassName[] = "autofill/DecoratedTextfield"; |
| 26 | 27 |
| 28 const int DecoratedTextfield::kMagicInsetNumber = 6; |
| 29 |
| 27 DecoratedTextfield::DecoratedTextfield( | 30 DecoratedTextfield::DecoratedTextfield( |
| 28 const base::string16& default_value, | 31 const base::string16& default_value, |
| 29 const base::string16& placeholder, | 32 const base::string16& placeholder, |
| 30 views::TextfieldController* controller) | 33 views::TextfieldController* controller) |
| 31 : invalid_(false), | 34 : border_(new views::FocusableBorder()), |
| 35 invalid_(false), |
| 32 editable_(true) { | 36 editable_(true) { |
| 33 UpdateBackground(); | 37 UpdateBackground(); |
| 34 UpdateBorder(); | 38 |
| 39 set_border(border_); |
| 40 // Removes the border from |native_wrapper_|. |
| 41 RemoveBorder(); |
| 35 | 42 |
| 36 set_placeholder_text(placeholder); | 43 set_placeholder_text(placeholder); |
| 37 SetText(default_value); | 44 SetText(default_value); |
| 38 SetController(controller); | 45 SetController(controller); |
| 46 SetHorizontalMargins(0, 0); |
| 39 } | 47 } |
| 40 | 48 |
| 41 DecoratedTextfield::~DecoratedTextfield() {} | 49 DecoratedTextfield::~DecoratedTextfield() {} |
| 42 | 50 |
| 43 void DecoratedTextfield::SetInvalid(bool invalid) { | 51 void DecoratedTextfield::SetInvalid(bool invalid) { |
| 44 if (invalid_ == invalid) | 52 invalid_ = invalid; |
| 53 if (!editable_) |
| 45 return; | 54 return; |
| 46 | 55 |
| 47 invalid_ = invalid; | 56 if (invalid) |
| 48 UpdateBorder(); | 57 border_->SetColor(kWarningColor); |
| 58 else |
| 59 border_->UseDefaultColor(); |
| 49 SchedulePaint(); | 60 SchedulePaint(); |
| 50 } | 61 } |
| 51 | 62 |
| 52 void DecoratedTextfield::SetEditable(bool editable) { | 63 void DecoratedTextfield::SetEditable(bool editable) { |
| 53 if (editable_ == editable) | 64 if (editable_ == editable) |
| 54 return; | 65 return; |
| 55 | 66 |
| 56 editable_ = editable; | 67 editable_ = editable; |
| 57 UpdateBorder(); | 68 if (editable) { |
| 69 SetInvalid(invalid_); |
| 70 UseDefaultBackgroundColor(); |
| 71 } else { |
| 72 border_->SetColor(SK_ColorTRANSPARENT); |
| 73 SetBackgroundColor(SK_ColorTRANSPARENT); |
| 74 } |
| 75 |
| 58 UpdateBackground(); | 76 UpdateBackground(); |
| 59 SetEnabled(editable); | 77 SetEnabled(editable); |
| 60 IconChanged(); | 78 IconChanged(); |
| 61 } | 79 } |
| 62 | 80 |
| 63 void DecoratedTextfield::SetIcon(const gfx::Image& icon) { | 81 void DecoratedTextfield::SetIcon(const gfx::Image& icon) { |
| 64 if (!icon_view_ && icon.IsEmpty()) | 82 if (!icon_view_ && icon.IsEmpty()) |
| 65 return; | 83 return; |
| 66 | 84 |
| 67 if (icon_view_) | 85 if (icon_view_) |
| (...skipping 18 matching lines...) Expand all Loading... |
| 86 | 104 |
| 87 if (!text.empty()) { | 105 if (!text.empty()) { |
| 88 icon_view_.reset(new TooltipIcon(text)); | 106 icon_view_.reset(new TooltipIcon(text)); |
| 89 AddChildView(icon_view_.get()); | 107 AddChildView(icon_view_.get()); |
| 90 } | 108 } |
| 91 | 109 |
| 92 IconChanged(); | 110 IconChanged(); |
| 93 } | 111 } |
| 94 | 112 |
| 95 base::string16 DecoratedTextfield::GetPlaceholderText() const { | 113 base::string16 DecoratedTextfield::GetPlaceholderText() const { |
| 96 return editable_ ? views::Textfield::GetPlaceholderText() : base::string16(); | 114 if (!editable_) |
| 115 return base::string16(); |
| 116 |
| 117 return views::Textfield::GetPlaceholderText(); |
| 97 } | 118 } |
| 98 | 119 |
| 99 const char* DecoratedTextfield::GetClassName() const { | 120 const char* DecoratedTextfield::GetClassName() const { |
| 100 return kViewClassName; | 121 return kViewClassName; |
| 101 } | 122 } |
| 102 | 123 |
| 103 views::View* DecoratedTextfield::GetEventHandlerForRect(const gfx::Rect& rect) { | 124 views::View* DecoratedTextfield::GetEventHandlerForRect(const gfx::Rect& rect) { |
| 104 views::View* handler = views::Textfield::GetEventHandlerForRect(rect); | 125 views::View* handler = views::Textfield::GetEventHandlerForRect(rect); |
| 105 if (handler->GetClassName() == TooltipIcon::kViewClassName) | 126 if (handler->GetClassName() == TooltipIcon::kViewClassName) |
| 106 return handler; | 127 return handler; |
| 107 return this; | 128 return textfield_view_; |
| 129 } |
| 130 |
| 131 void DecoratedTextfield::OnFocus() { |
| 132 views::Textfield::OnFocus(); |
| 133 SchedulePaint(); |
| 134 } |
| 135 |
| 136 void DecoratedTextfield::OnBlur() { |
| 137 views::Textfield::OnBlur(); |
| 138 SchedulePaint(); |
| 108 } | 139 } |
| 109 | 140 |
| 110 gfx::Size DecoratedTextfield::GetPreferredSize() { | 141 gfx::Size DecoratedTextfield::GetPreferredSize() { |
| 111 static const int height = | 142 int w = views::Textfield::GetPreferredSize().width(); |
| 112 views::LabelButton(NULL, base::string16()).GetPreferredSize().height(); | 143 views::LabelButton button(NULL, base::string16()); |
| 113 const gfx::Size size = views::Textfield::GetPreferredSize(); | 144 button.SetStyle(views::Button::STYLE_BUTTON); |
| 114 return gfx::Size(size.width(), std::max(size.height(), height)); | 145 int h = button.GetPreferredSize().height(); |
| 146 return gfx::Size(w, h - kMagicInsetNumber); |
| 115 } | 147 } |
| 116 | 148 |
| 117 void DecoratedTextfield::Layout() { | 149 void DecoratedTextfield::Layout() { |
| 118 views::Textfield::Layout(); | 150 views::Textfield::Layout(); |
| 119 | 151 |
| 120 if (icon_view_ && icon_view_->visible()) { | 152 if (icon_view_ && icon_view_->visible()) { |
| 121 gfx::Rect bounds = GetContentsBounds(); | 153 gfx::Rect bounds = GetContentsBounds(); |
| 122 gfx::Size icon_size = icon_view_->GetPreferredSize(); | 154 gfx::Size icon_size = icon_view_->GetPreferredSize(); |
| 123 int x = base::i18n::IsRTL() ? | 155 int x = base::i18n::IsRTL() ? |
| 124 kTextfieldIconPadding : | 156 kTextfieldIconPadding : |
| 125 bounds.right() - icon_size.width() - kTextfieldIconPadding; | 157 bounds.right() - icon_size.width() - kTextfieldIconPadding; |
| 126 // Vertically centered. | 158 // Vertically centered. |
| 127 int y = bounds.y() + (bounds.height() - icon_size.height()) / 2; | 159 int y = bounds.y() + (bounds.height() - icon_size.height()) / 2; |
| 128 icon_view_->SetBounds(x, y, icon_size.width(), icon_size.height()); | 160 icon_view_->SetBounds(x, |
| 161 y, |
| 162 icon_size.width(), |
| 163 icon_size.height()); |
| 129 } | 164 } |
| 130 } | 165 } |
| 131 | 166 |
| 132 void DecoratedTextfield::UpdateBackground() { | 167 void DecoratedTextfield::UpdateBackground() { |
| 133 if (editable_) | |
| 134 UseDefaultBackgroundColor(); | |
| 135 else | |
| 136 SetBackgroundColor(SK_ColorTRANSPARENT); | |
| 137 set_background( | 168 set_background( |
| 138 views::Background::CreateSolidBackground(GetBackgroundColor())); | 169 views::Background::CreateSolidBackground(GetBackgroundColor())); |
| 139 } | 170 } |
| 140 | 171 |
| 141 void DecoratedTextfield::UpdateBorder() { | |
| 142 views::FocusableBorder* border = new views::FocusableBorder(); | |
| 143 if (invalid_) | |
| 144 border->SetColor(kWarningColor); | |
| 145 else if (!editable_) | |
| 146 border->SetColor(SK_ColorTRANSPARENT); | |
| 147 | |
| 148 const gfx::Insets insets = GetInsets(); | |
| 149 int left = icon_view_ ? | |
| 150 icon_view_->GetPreferredSize().width() + 2 * kTextfieldIconPadding : 0; | |
| 151 int right = 0; | |
| 152 if (base::i18n::IsRTL()) | |
| 153 std::swap(left, right); | |
| 154 border->SetInsets(insets.top(), insets.left() + left, insets.bottom(), | |
| 155 insets.right() + right); | |
| 156 set_border(border); | |
| 157 } | |
| 158 | |
| 159 void DecoratedTextfield::IconChanged() { | 172 void DecoratedTextfield::IconChanged() { |
| 160 // Don't show the icon if nothing else is showing. | 173 // Don't show the icon if nothing else is showing. |
| 161 const bool visible = editable_ || !text().empty(); | 174 icon_view_->SetVisible(editable_ || !text().empty()); |
| 162 if (icon_view_->visible() == visible) | |
| 163 return; | |
| 164 | 175 |
| 165 icon_view_->SetVisible(visible); | 176 int icon_space = icon_view_ ? |
| 166 UpdateBorder(); | 177 icon_view_->GetPreferredSize().width() + 2 * kTextfieldIconPadding : 0; |
| 178 |
| 179 bool is_rtl = base::i18n::IsRTL(); |
| 180 SetHorizontalMargins(is_rtl ? icon_space : 0, is_rtl ? 0 : icon_space); |
| 181 |
| 182 Layout(); |
| 167 SchedulePaint(); | 183 SchedulePaint(); |
| 168 } | 184 } |
| 169 | 185 |
| 170 } // namespace autofill | 186 } // namespace autofill |
| OLD | NEW |