Chromium Code Reviews| 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_popup_view_views.h" | 5 #include "chrome/browser/ui/views/autofill/autofill_popup_view_views.h" |
| 6 | 6 |
| 7 #include "base/optional.h" | |
| 7 #include "chrome/browser/ui/autofill/autofill_popup_controller.h" | 8 #include "chrome/browser/ui/autofill/autofill_popup_controller.h" |
| 8 #include "chrome/browser/ui/autofill/autofill_popup_layout_model.h" | 9 #include "chrome/browser/ui/autofill/autofill_popup_layout_model.h" |
| 10 #include "chrome/grit/generated_resources.h" | |
| 9 #include "components/autofill/core/browser/popup_item_ids.h" | 11 #include "components/autofill/core/browser/popup_item_ids.h" |
| 10 #include "components/autofill/core/browser/suggestion.h" | 12 #include "components/autofill/core/browser/suggestion.h" |
| 13 #include "ui/accessibility/ax_node_data.h" | |
| 14 #include "ui/base/l10n/l10n_util.h" | |
| 11 #include "ui/events/keycodes/keyboard_codes.h" | 15 #include "ui/events/keycodes/keyboard_codes.h" |
| 12 #include "ui/gfx/canvas.h" | 16 #include "ui/gfx/canvas.h" |
| 13 #include "ui/gfx/geometry/point.h" | 17 #include "ui/gfx/geometry/point.h" |
| 14 #include "ui/gfx/geometry/rect.h" | 18 #include "ui/gfx/geometry/rect.h" |
| 15 #include "ui/gfx/image/image.h" | 19 #include "ui/gfx/image/image.h" |
| 16 #include "ui/gfx/native_widget_types.h" | 20 #include "ui/gfx/native_widget_types.h" |
| 17 #include "ui/gfx/text_utils.h" | 21 #include "ui/gfx/text_utils.h" |
| 18 #include "ui/views/border.h" | 22 #include "ui/views/border.h" |
| 23 #include "ui/views/view.h" | |
| 19 #include "ui/views/widget/widget.h" | 24 #include "ui/views/widget/widget.h" |
| 20 | 25 |
| 21 namespace autofill { | 26 namespace autofill { |
| 22 | 27 |
| 28 namespace { | |
| 29 | |
| 30 // Child view only for triggering accessibility events. Rendering is handled | |
| 31 // by |AutofillPopupViewViews|. | |
| 32 class AutofillPopupChildView : public views::View { | |
| 33 public: | |
| 34 // Internal class name. | |
| 35 static const char kViewClassName[]; | |
| 36 | |
| 37 // |controller| should not be NULL. | |
|
Evan Stade
2017/03/16 17:39:00
in this case you can probably make controller_ (an
csashi
2017/03/16 22:42:06
Done.
| |
| 38 AutofillPopupChildView(AutofillPopupController* controller, size_t index) | |
| 39 : controller_(controller), index_(index) { | |
| 40 SetFocusBehavior(FocusBehavior::ALWAYS); | |
| 41 } | |
| 42 | |
| 43 private: | |
| 44 ~AutofillPopupChildView() override {} | |
| 45 | |
| 46 // views::Views implementation | |
| 47 const char* GetClassName() const override { return kViewClassName; } | |
| 48 | |
| 49 void GetAccessibleNodeData(ui::AXNodeData* node_data) override { | |
| 50 node_data->role = ui::AX_ROLE_MENU_ITEM; | |
| 51 node_data->SetName(controller_->GetSuggestionAt(index_).value); | |
| 52 } | |
| 53 | |
| 54 AutofillPopupController* controller_; // Weak reference. | |
| 55 const size_t index_; | |
| 56 | |
| 57 DISALLOW_COPY_AND_ASSIGN(AutofillPopupChildView); | |
| 58 }; | |
| 59 | |
| 60 // static | |
| 61 const char AutofillPopupChildView::kViewClassName[] = "AutofillPopupChildView"; | |
| 62 | |
| 63 } // namespace | |
| 64 | |
| 23 AutofillPopupViewViews::AutofillPopupViewViews( | 65 AutofillPopupViewViews::AutofillPopupViewViews( |
| 24 AutofillPopupController* controller, | 66 AutofillPopupController* controller, |
| 25 views::Widget* parent_widget) | 67 views::Widget* parent_widget) |
| 26 : AutofillPopupBaseView(controller, parent_widget), | 68 : AutofillPopupBaseView(controller, parent_widget), |
| 27 controller_(controller) {} | 69 controller_(controller) { |
| 70 CreateChildViews(); | |
| 71 SetFocusBehavior(FocusBehavior::ALWAYS); | |
| 72 } | |
| 28 | 73 |
| 29 AutofillPopupViewViews::~AutofillPopupViewViews() {} | 74 AutofillPopupViewViews::~AutofillPopupViewViews() {} |
| 30 | 75 |
| 31 void AutofillPopupViewViews::Show() { | 76 void AutofillPopupViewViews::Show() { |
| 32 DoShow(); | 77 DoShow(); |
| 78 NotifyAccessibilityEvent(ui::AX_EVENT_MENU_START, true); | |
| 33 } | 79 } |
| 34 | 80 |
| 35 void AutofillPopupViewViews::Hide() { | 81 void AutofillPopupViewViews::Hide() { |
| 36 // The controller is no longer valid after it hides us. | 82 // The controller is no longer valid after it hides us. |
| 37 controller_ = NULL; | 83 controller_ = NULL; |
| 38 | |
| 39 DoHide(); | 84 DoHide(); |
| 85 NotifyAccessibilityEvent(ui::AX_EVENT_MENU_END, true); | |
| 40 } | 86 } |
| 41 | 87 |
| 42 void AutofillPopupViewViews::UpdateBoundsAndRedrawPopup() { | 88 void AutofillPopupViewViews::OnSuggestionsChanged() { |
| 89 // We recreate the child views so we can be sure the |controller_|'s | |
| 90 // |GetLineCount()| will match the number of child views. Otherwise, | |
| 91 // the number of suggestions i.e. |GetLineCount()| may not match 1x1 with the | |
| 92 // child views. See crbug.com/697466. | |
| 93 RemoveAllChildViews(true /* delete_children */); | |
|
Evan Stade
2017/03/16 17:39:00
nit: seems like this should be inside CreateChildV
csashi
2017/03/16 22:42:06
Done.
| |
| 94 CreateChildViews(); | |
| 43 DoUpdateBoundsAndRedrawPopup(); | 95 DoUpdateBoundsAndRedrawPopup(); |
| 44 } | 96 } |
| 45 | 97 |
| 46 void AutofillPopupViewViews::OnPaint(gfx::Canvas* canvas) { | 98 void AutofillPopupViewViews::OnPaint(gfx::Canvas* canvas) { |
| 47 if (!controller_) | 99 if (!controller_) |
| 48 return; | 100 return; |
| 49 | 101 |
| 50 canvas->DrawColor(GetNativeTheme()->GetSystemColor( | 102 canvas->DrawColor(GetNativeTheme()->GetSystemColor( |
| 51 ui::NativeTheme::kColorId_ResultsTableNormalBackground)); | 103 ui::NativeTheme::kColorId_ResultsTableNormalBackground)); |
| 52 OnPaintBorder(canvas); | 104 OnPaintBorder(canvas); |
| 53 | 105 |
| 106 DCHECK_EQ(controller_->GetLineCount(), static_cast<size_t>(child_count())); | |
| 54 for (size_t i = 0; i < controller_->GetLineCount(); ++i) { | 107 for (size_t i = 0; i < controller_->GetLineCount(); ++i) { |
| 55 gfx::Rect line_rect = controller_->layout_model().GetRowBounds(i); | 108 gfx::Rect line_rect = controller_->layout_model().GetRowBounds(i); |
| 56 | 109 |
| 57 if (controller_->GetSuggestionAt(i).frontend_id == | 110 if (controller_->GetSuggestionAt(i).frontend_id == |
| 58 POPUP_ITEM_ID_SEPARATOR) { | 111 POPUP_ITEM_ID_SEPARATOR) { |
| 59 canvas->FillRect( | 112 canvas->FillRect( |
| 60 line_rect, | 113 line_rect, |
| 61 GetNativeTheme()->GetSystemColor( | 114 GetNativeTheme()->GetSystemColor( |
| 62 ui::NativeTheme::kColorId_ResultsTableNormalDimmedText)); | 115 ui::NativeTheme::kColorId_ResultsTableNormalDimmedText)); |
| 63 } else { | 116 } else { |
| 64 DrawAutofillEntry(canvas, i, line_rect); | 117 DrawAutofillEntry(canvas, i, line_rect); |
| 65 } | 118 } |
| 66 } | 119 } |
| 67 } | 120 } |
| 68 | 121 |
| 69 void AutofillPopupViewViews::InvalidateRow(size_t row) { | 122 void AutofillPopupViewViews::OnSelectedRowChanged( |
| 70 SchedulePaintInRect(controller_->layout_model().GetRowBounds(row)); | 123 base::Optional<size_t> previous_row_selection, |
| 124 base::Optional<size_t> current_row_selection) { | |
| 125 if (previous_row_selection && | |
| 126 previous_row_selection < controller_->GetLineCount()) { | |
|
Evan Stade
2017/03/16 17:39:00
why is this second check required?
csashi
2017/03/16 22:42:06
Because the previous_row_selection could be for a
Evan Stade
2017/03/17 14:11:55
but that check looks like it turned into a dcheck
csashi
2017/03/17 16:30:35
The dcheck in the controller is for the newly sele
Evan Stade
2017/03/17 19:08:54
fair enough, but I think somewhere in Show() you s
csashi
2017/03/17 21:55:21
Done.
| |
| 127 SchedulePaintInRect(controller_->layout_model().GetRowBounds( | |
| 128 previous_row_selection.value())); | |
| 129 } | |
| 130 | |
| 131 if (current_row_selection) { | |
| 132 SchedulePaintInRect(controller_->layout_model().GetRowBounds( | |
| 133 current_row_selection.value())); | |
|
Evan Stade
2017/03/16 17:39:00
s/current_row_selection.value()/*current_row_selec
csashi
2017/03/16 22:42:06
Done.
| |
| 134 DCHECK_LT(current_row_selection.value(), | |
| 135 static_cast<size_t>(child_count())); | |
| 136 views::View* child = child_at(current_row_selection.value()); | |
| 137 DCHECK_EQ(child->GetClassName(), AutofillPopupChildView::kViewClassName); | |
|
Evan Stade
2017/03/16 17:39:00
not sure this is necessary
csashi
2017/03/16 22:42:06
Acknowledged. A previous reviewer asked that I add
Evan Stade
2017/03/17 14:11:55
I mean, lots of things would break then right, lik
csashi
2017/03/17 16:30:35
Done.
| |
| 138 AutofillPopupChildView* child_view = | |
| 139 static_cast<AutofillPopupChildView*>(child); | |
|
Evan Stade
2017/03/17 14:11:55
nit: do you need this cast?
csashi
2017/03/17 16:30:35
Done.
| |
| 140 child_view->NotifyAccessibilityEvent(ui::AX_EVENT_SELECTION, true); | |
| 141 } | |
| 71 } | 142 } |
| 72 | 143 |
| 73 /** | 144 /** |
| 74 * Autofill entries in ltr. | 145 * Autofill entries in ltr. |
| 75 * | 146 * |
| 76 * ............................................................................ | 147 * ............................................................................ |
| 77 * . ICON | HTTP WARNING MESSAGE VALUE | LABEL . | 148 * . ICON | HTTP WARNING MESSAGE VALUE | LABEL . |
| 78 * ............................................................................ | 149 * ............................................................................ |
| 79 * . OTHER AUTOFILL ENTRY VALUE | LABEL | ICON . | 150 * . OTHER AUTOFILL ENTRY VALUE | LABEL | ICON . |
| 80 * ............................................................................ | 151 * ............................................................................ |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 182 controller_->GetElidedLabelAt(index), | 253 controller_->GetElidedLabelAt(index), |
| 183 controller_->layout_model().GetLabelFontListForRow(index), | 254 controller_->layout_model().GetLabelFontListForRow(index), |
| 184 GetNativeTheme()->GetSystemColor( | 255 GetNativeTheme()->GetSystemColor( |
| 185 ui::NativeTheme::kColorId_ResultsTableNormalDimmedText), | 256 ui::NativeTheme::kColorId_ResultsTableNormalDimmedText), |
| 186 gfx::Rect(label_x_align_left, entry_rect.y(), label_width, | 257 gfx::Rect(label_x_align_left, entry_rect.y(), label_width, |
| 187 entry_rect.height()), | 258 entry_rect.height()), |
| 188 text_align); | 259 text_align); |
| 189 } | 260 } |
| 190 } | 261 } |
| 191 | 262 |
| 263 void AutofillPopupViewViews::GetAccessibleNodeData(ui::AXNodeData* node_data) { | |
| 264 node_data->role = ui::AX_ROLE_MENU; | |
| 265 node_data->SetName( | |
| 266 l10n_util::GetStringUTF16(IDS_AUTOFILL_POPUP_ACCESSIBLE_NODE_DATA)); | |
| 267 } | |
| 268 | |
| 269 void AutofillPopupViewViews::CreateChildViews() { | |
| 270 for (size_t i = 0; i < controller_->GetLineCount(); ++i) { | |
| 271 AutofillPopupChildView* child = new AutofillPopupChildView(controller_, i); | |
| 272 AddChildView(child); | |
|
Evan Stade
2017/03/16 17:39:00
nit: combine these two lines.
csashi
2017/03/16 22:42:06
Done.
| |
| 273 } | |
| 274 } | |
| 275 | |
| 192 AutofillPopupView* AutofillPopupView::Create( | 276 AutofillPopupView* AutofillPopupView::Create( |
| 193 AutofillPopupController* controller) { | 277 AutofillPopupController* controller) { |
| 194 views::Widget* observing_widget = | 278 views::Widget* observing_widget = |
| 195 views::Widget::GetTopLevelWidgetForNativeView( | 279 views::Widget::GetTopLevelWidgetForNativeView( |
| 196 controller->container_view()); | 280 controller->container_view()); |
| 197 | 281 |
| 198 // If the top level widget can't be found, cancel the popup since we can't | 282 // If the top level widget can't be found, cancel the popup since we can't |
| 199 // fully set it up. | 283 // fully set it up. |
| 200 if (!observing_widget) | 284 if (!observing_widget) |
| 201 return NULL; | 285 return NULL; |
| 202 | 286 |
| 203 return new AutofillPopupViewViews(controller, observing_widget); | 287 return new AutofillPopupViewViews(controller, observing_widget); |
| 204 } | 288 } |
| 205 | 289 |
| 206 } // namespace autofill | 290 } // namespace autofill |
| OLD | NEW |