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