| 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 explicit AutofillPopupChildView(const Suggestion& suggestion) |
| 35 : suggestion_(suggestion) { |
| 36 SetFocusBehavior(FocusBehavior::ALWAYS); |
| 37 } |
| 38 |
| 39 private: |
| 40 ~AutofillPopupChildView() override {} |
| 41 |
| 42 // views::Views implementation |
| 43 void GetAccessibleNodeData(ui::AXNodeData* node_data) override { |
| 44 node_data->role = ui::AX_ROLE_MENU_ITEM; |
| 45 node_data->SetName(suggestion_.value); |
| 46 } |
| 47 |
| 48 const Suggestion& suggestion_; |
| 49 |
| 50 DISALLOW_COPY_AND_ASSIGN(AutofillPopupChildView); |
| 51 }; |
| 52 |
| 53 } // namespace |
| 54 |
| 23 AutofillPopupViewViews::AutofillPopupViewViews( | 55 AutofillPopupViewViews::AutofillPopupViewViews( |
| 24 AutofillPopupController* controller, | 56 AutofillPopupController* controller, |
| 25 views::Widget* parent_widget) | 57 views::Widget* parent_widget) |
| 26 : AutofillPopupBaseView(controller, parent_widget), | 58 : AutofillPopupBaseView(controller, parent_widget), |
| 27 controller_(controller) {} | 59 controller_(controller) { |
| 60 CreateChildViews(); |
| 61 SetFocusBehavior(FocusBehavior::ALWAYS); |
| 62 } |
| 28 | 63 |
| 29 AutofillPopupViewViews::~AutofillPopupViewViews() {} | 64 AutofillPopupViewViews::~AutofillPopupViewViews() {} |
| 30 | 65 |
| 31 void AutofillPopupViewViews::Show() { | 66 void AutofillPopupViewViews::Show() { |
| 32 DoShow(); | 67 DoShow(); |
| 68 NotifyAccessibilityEvent(ui::AX_EVENT_MENU_START, true); |
| 33 } | 69 } |
| 34 | 70 |
| 35 void AutofillPopupViewViews::Hide() { | 71 void AutofillPopupViewViews::Hide() { |
| 36 // The controller is no longer valid after it hides us. | 72 // The controller is no longer valid after it hides us. |
| 37 controller_ = NULL; | 73 controller_ = NULL; |
| 38 | |
| 39 DoHide(); | 74 DoHide(); |
| 75 NotifyAccessibilityEvent(ui::AX_EVENT_MENU_END, true); |
| 40 } | 76 } |
| 41 | 77 |
| 42 void AutofillPopupViewViews::UpdateBoundsAndRedrawPopup() { | 78 void AutofillPopupViewViews::OnSuggestionsChanged() { |
| 79 // We recreate the child views so we can be sure the |controller_|'s |
| 80 // |GetLineCount()| will match the number of child views. Otherwise, |
| 81 // the number of suggestions i.e. |GetLineCount()| may not match 1x1 with the |
| 82 // child views. See crbug.com/697466. |
| 83 CreateChildViews(); |
| 43 DoUpdateBoundsAndRedrawPopup(); | 84 DoUpdateBoundsAndRedrawPopup(); |
| 44 } | 85 } |
| 45 | 86 |
| 46 void AutofillPopupViewViews::OnPaint(gfx::Canvas* canvas) { | 87 void AutofillPopupViewViews::OnPaint(gfx::Canvas* canvas) { |
| 47 if (!controller_) | 88 if (!controller_) |
| 48 return; | 89 return; |
| 49 | 90 |
| 50 canvas->DrawColor(GetNativeTheme()->GetSystemColor( | 91 canvas->DrawColor(GetNativeTheme()->GetSystemColor( |
| 51 ui::NativeTheme::kColorId_ResultsTableNormalBackground)); | 92 ui::NativeTheme::kColorId_ResultsTableNormalBackground)); |
| 52 OnPaintBorder(canvas); | 93 OnPaintBorder(canvas); |
| 53 | 94 |
| 54 for (size_t i = 0; i < controller_->GetLineCount(); ++i) { | 95 DCHECK_EQ(controller_->GetLineCount(), child_count()); |
| 96 for (int i = 0; i < controller_->GetLineCount(); ++i) { |
| 55 gfx::Rect line_rect = controller_->layout_model().GetRowBounds(i); | 97 gfx::Rect line_rect = controller_->layout_model().GetRowBounds(i); |
| 56 | 98 |
| 57 if (controller_->GetSuggestionAt(i).frontend_id == | 99 if (controller_->GetSuggestionAt(i).frontend_id == |
| 58 POPUP_ITEM_ID_SEPARATOR) { | 100 POPUP_ITEM_ID_SEPARATOR) { |
| 59 canvas->FillRect( | 101 canvas->FillRect( |
| 60 line_rect, | 102 line_rect, |
| 61 GetNativeTheme()->GetSystemColor( | 103 GetNativeTheme()->GetSystemColor( |
| 62 ui::NativeTheme::kColorId_ResultsTableNormalDimmedText)); | 104 ui::NativeTheme::kColorId_ResultsTableNormalDimmedText)); |
| 63 } else { | 105 } else { |
| 64 DrawAutofillEntry(canvas, i, line_rect); | 106 DrawAutofillEntry(canvas, i, line_rect); |
| 65 } | 107 } |
| 66 } | 108 } |
| 67 } | 109 } |
| 68 | 110 |
| 69 void AutofillPopupViewViews::InvalidateRow(size_t row) { | 111 void AutofillPopupViewViews::OnSelectedRowChanged( |
| 70 SchedulePaintInRect(controller_->layout_model().GetRowBounds(row)); | 112 base::Optional<int> previous_row_selection, |
| 113 base::Optional<int> current_row_selection) { |
| 114 SchedulePaint(); |
| 115 |
| 116 if (current_row_selection) { |
| 117 DCHECK_LT(*current_row_selection, child_count()); |
| 118 child_at(*current_row_selection) |
| 119 ->NotifyAccessibilityEvent(ui::AX_EVENT_SELECTION, true); |
| 120 } |
| 71 } | 121 } |
| 72 | 122 |
| 73 /** | 123 /** |
| 74 * Autofill entries in ltr. | 124 * Autofill entries in ltr. |
| 75 * | 125 * |
| 76 * ............................................................................ | 126 * ............................................................................ |
| 77 * . ICON | HTTP WARNING MESSAGE VALUE | LABEL . | 127 * . ICON | HTTP WARNING MESSAGE VALUE | LABEL . |
| 78 * ............................................................................ | 128 * ............................................................................ |
| 79 * . OTHER AUTOFILL ENTRY VALUE | LABEL | ICON . | 129 * . OTHER AUTOFILL ENTRY VALUE | LABEL | ICON . |
| 80 * ............................................................................ | 130 * ............................................................................ |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 controller_->GetElidedLabelAt(index), | 232 controller_->GetElidedLabelAt(index), |
| 183 controller_->layout_model().GetLabelFontListForRow(index), | 233 controller_->layout_model().GetLabelFontListForRow(index), |
| 184 GetNativeTheme()->GetSystemColor( | 234 GetNativeTheme()->GetSystemColor( |
| 185 ui::NativeTheme::kColorId_ResultsTableNormalDimmedText), | 235 ui::NativeTheme::kColorId_ResultsTableNormalDimmedText), |
| 186 gfx::Rect(label_x_align_left, entry_rect.y(), label_width, | 236 gfx::Rect(label_x_align_left, entry_rect.y(), label_width, |
| 187 entry_rect.height()), | 237 entry_rect.height()), |
| 188 text_align); | 238 text_align); |
| 189 } | 239 } |
| 190 } | 240 } |
| 191 | 241 |
| 242 void AutofillPopupViewViews::GetAccessibleNodeData(ui::AXNodeData* node_data) { |
| 243 node_data->role = ui::AX_ROLE_MENU; |
| 244 node_data->SetName( |
| 245 l10n_util::GetStringUTF16(IDS_AUTOFILL_POPUP_ACCESSIBLE_NODE_DATA)); |
| 246 } |
| 247 |
| 248 void AutofillPopupViewViews::CreateChildViews() { |
| 249 RemoveAllChildViews(true /* delete_children */); |
| 250 |
| 251 for (int i = 0; i < controller_->GetLineCount(); ++i) { |
| 252 AddChildView(new AutofillPopupChildView(controller_->GetSuggestionAt(i))); |
| 253 } |
| 254 } |
| 255 |
| 192 AutofillPopupView* AutofillPopupView::Create( | 256 AutofillPopupView* AutofillPopupView::Create( |
| 193 AutofillPopupController* controller) { | 257 AutofillPopupController* controller) { |
| 194 views::Widget* observing_widget = | 258 views::Widget* observing_widget = |
| 195 views::Widget::GetTopLevelWidgetForNativeView( | 259 views::Widget::GetTopLevelWidgetForNativeView( |
| 196 controller->container_view()); | 260 controller->container_view()); |
| 197 | 261 |
| 198 // If the top level widget can't be found, cancel the popup since we can't | 262 // If the top level widget can't be found, cancel the popup since we can't |
| 199 // fully set it up. | 263 // fully set it up. |
| 200 if (!observing_widget) | 264 if (!observing_widget) |
| 201 return NULL; | 265 return NULL; |
| 202 | 266 |
| 203 return new AutofillPopupViewViews(controller, observing_widget); | 267 return new AutofillPopupViewViews(controller, observing_widget); |
| 204 } | 268 } |
| 205 | 269 |
| 206 } // namespace autofill | 270 } // namespace autofill |
| OLD | NEW |