| 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/textfield_controller.h" | 13 #include "ui/views/controls/textfield/textfield_controller.h" |
| 14 #include "ui/views/view_targeter.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 |
| 27 DecoratedTextfield::DecoratedTextfield( | 28 DecoratedTextfield::DecoratedTextfield( |
| 28 const base::string16& default_value, | 29 const base::string16& default_value, |
| 29 const base::string16& placeholder, | 30 const base::string16& placeholder, |
| 30 views::TextfieldController* controller) | 31 views::TextfieldController* controller) |
| 31 : invalid_(false), | 32 : invalid_(false), |
| 32 editable_(true) { | 33 editable_(true) { |
| 33 UpdateBackground(); | 34 UpdateBackground(); |
| 34 UpdateBorder(); | 35 UpdateBorder(); |
| 35 | 36 |
| 36 set_placeholder_text(placeholder); | 37 set_placeholder_text(placeholder); |
| 37 SetText(default_value); | 38 SetText(default_value); |
| 38 set_controller(controller); | 39 set_controller(controller); |
| 40 |
| 41 SetEventTargeter( |
| 42 scoped_ptr<views::ViewTargeter>(new views::ViewTargeter(this))); |
| 39 } | 43 } |
| 40 | 44 |
| 41 DecoratedTextfield::~DecoratedTextfield() {} | 45 DecoratedTextfield::~DecoratedTextfield() {} |
| 42 | 46 |
| 43 void DecoratedTextfield::SetInvalid(bool invalid) { | 47 void DecoratedTextfield::SetInvalid(bool invalid) { |
| 44 if (invalid_ == invalid) | 48 if (invalid_ == invalid) |
| 45 return; | 49 return; |
| 46 | 50 |
| 47 invalid_ = invalid; | 51 invalid_ = invalid; |
| 48 UpdateBorder(); | 52 UpdateBorder(); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 } | 96 } |
| 93 | 97 |
| 94 base::string16 DecoratedTextfield::GetPlaceholderText() const { | 98 base::string16 DecoratedTextfield::GetPlaceholderText() const { |
| 95 return editable_ ? views::Textfield::GetPlaceholderText() : base::string16(); | 99 return editable_ ? views::Textfield::GetPlaceholderText() : base::string16(); |
| 96 } | 100 } |
| 97 | 101 |
| 98 const char* DecoratedTextfield::GetClassName() const { | 102 const char* DecoratedTextfield::GetClassName() const { |
| 99 return kViewClassName; | 103 return kViewClassName; |
| 100 } | 104 } |
| 101 | 105 |
| 102 views::View* DecoratedTextfield::GetEventHandlerForRect(const gfx::Rect& rect) { | |
| 103 views::View* handler = views::Textfield::GetEventHandlerForRect(rect); | |
| 104 if (handler->GetClassName() == TooltipIcon::kViewClassName) | |
| 105 return handler; | |
| 106 return this; | |
| 107 } | |
| 108 | |
| 109 gfx::Size DecoratedTextfield::GetPreferredSize() const { | 106 gfx::Size DecoratedTextfield::GetPreferredSize() const { |
| 110 static const int height = | 107 static const int height = |
| 111 views::LabelButton(NULL, base::string16()).GetPreferredSize().height(); | 108 views::LabelButton(NULL, base::string16()).GetPreferredSize().height(); |
| 112 const gfx::Size size = views::Textfield::GetPreferredSize(); | 109 const gfx::Size size = views::Textfield::GetPreferredSize(); |
| 113 return gfx::Size(size.width(), std::max(size.height(), height)); | 110 return gfx::Size(size.width(), std::max(size.height(), height)); |
| 114 } | 111 } |
| 115 | 112 |
| 116 void DecoratedTextfield::Layout() { | 113 void DecoratedTextfield::Layout() { |
| 117 views::Textfield::Layout(); | 114 views::Textfield::Layout(); |
| 118 | 115 |
| 119 if (icon_view_ && icon_view_->visible()) { | 116 if (icon_view_ && icon_view_->visible()) { |
| 120 gfx::Rect bounds = GetContentsBounds(); | 117 gfx::Rect bounds = GetContentsBounds(); |
| 121 gfx::Size icon_size = icon_view_->GetPreferredSize(); | 118 gfx::Size icon_size = icon_view_->GetPreferredSize(); |
| 122 int x = base::i18n::IsRTL() ? | 119 int x = base::i18n::IsRTL() ? |
| 123 bounds.x() - icon_size.width() - kTextfieldIconPadding : | 120 bounds.x() - icon_size.width() - kTextfieldIconPadding : |
| 124 bounds.right() + kTextfieldIconPadding; | 121 bounds.right() + kTextfieldIconPadding; |
| 125 // Vertically centered. | 122 // Vertically centered. |
| 126 int y = bounds.y() + (bounds.height() - icon_size.height()) / 2; | 123 int y = bounds.y() + (bounds.height() - icon_size.height()) / 2; |
| 127 gfx::Rect icon_bounds(x, y, icon_size.width(), icon_size.height()); | 124 gfx::Rect icon_bounds(x, y, icon_size.width(), icon_size.height()); |
| 128 icon_bounds.set_x(GetMirroredXForRect(icon_bounds)); | 125 icon_bounds.set_x(GetMirroredXForRect(icon_bounds)); |
| 129 icon_view_->SetBoundsRect(icon_bounds); | 126 icon_view_->SetBoundsRect(icon_bounds); |
| 130 } | 127 } |
| 131 } | 128 } |
| 132 | 129 |
| 130 views::View* DecoratedTextfield::TargetForRect(views::View* root, |
| 131 const gfx::Rect& rect) { |
| 132 CHECK_EQ(root, this); |
| 133 |
| 134 views::View* handler = views::ViewTargeterDelegate::TargetForRect(root, rect); |
| 135 if (handler->GetClassName() == TooltipIcon::kViewClassName) |
| 136 return handler; |
| 137 return this; |
| 138 } |
| 139 |
| 133 void DecoratedTextfield::UpdateBackground() { | 140 void DecoratedTextfield::UpdateBackground() { |
| 134 if (editable_) | 141 if (editable_) |
| 135 UseDefaultBackgroundColor(); | 142 UseDefaultBackgroundColor(); |
| 136 else | 143 else |
| 137 SetBackgroundColor(SK_ColorTRANSPARENT); | 144 SetBackgroundColor(SK_ColorTRANSPARENT); |
| 138 set_background( | 145 set_background( |
| 139 views::Background::CreateSolidBackground(GetBackgroundColor())); | 146 views::Background::CreateSolidBackground(GetBackgroundColor())); |
| 140 } | 147 } |
| 141 | 148 |
| 142 void DecoratedTextfield::UpdateBorder() { | 149 void DecoratedTextfield::UpdateBorder() { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 159 } | 166 } |
| 160 | 167 |
| 161 void DecoratedTextfield::IconChanged() { | 168 void DecoratedTextfield::IconChanged() { |
| 162 // Don't show the icon if nothing else is showing. | 169 // Don't show the icon if nothing else is showing. |
| 163 icon_view_->SetVisible(editable_ || !text().empty()); | 170 icon_view_->SetVisible(editable_ || !text().empty()); |
| 164 UpdateBorder(); | 171 UpdateBorder(); |
| 165 Layout(); | 172 Layout(); |
| 166 } | 173 } |
| 167 | 174 |
| 168 } // namespace autofill | 175 } // namespace autofill |
| OLD | NEW |