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 "chrome/browser/ui/autofill/autofill_popup_controller.h" | 7 #include "chrome/browser/ui/autofill/autofill_popup_controller.h" |
8 #include "chrome/browser/ui/autofill/autofill_popup_layout_model.h" | 8 #include "chrome/browser/ui/autofill/autofill_popup_layout_model.h" |
| 9 #include "chrome/grit/generated_resources.h" |
9 #include "components/autofill/core/browser/popup_item_ids.h" | 10 #include "components/autofill/core/browser/popup_item_ids.h" |
10 #include "components/autofill/core/browser/suggestion.h" | 11 #include "components/autofill/core/browser/suggestion.h" |
| 12 #include "ui/accessibility/ax_node_data.h" |
| 13 #include "ui/base/l10n/l10n_util.h" |
11 #include "ui/events/keycodes/keyboard_codes.h" | 14 #include "ui/events/keycodes/keyboard_codes.h" |
12 #include "ui/gfx/canvas.h" | 15 #include "ui/gfx/canvas.h" |
13 #include "ui/gfx/geometry/point.h" | 16 #include "ui/gfx/geometry/point.h" |
14 #include "ui/gfx/geometry/rect.h" | 17 #include "ui/gfx/geometry/rect.h" |
15 #include "ui/gfx/image/image.h" | 18 #include "ui/gfx/image/image.h" |
16 #include "ui/gfx/native_widget_types.h" | 19 #include "ui/gfx/native_widget_types.h" |
17 #include "ui/gfx/text_utils.h" | 20 #include "ui/gfx/text_utils.h" |
18 #include "ui/views/border.h" | 21 #include "ui/views/border.h" |
| 22 #include "ui/views/view.h" |
19 #include "ui/views/widget/widget.h" | 23 #include "ui/views/widget/widget.h" |
20 | 24 |
21 namespace autofill { | 25 namespace autofill { |
22 | 26 |
| 27 namespace { |
| 28 |
| 29 // Child view only for triggering accessibility events. Rendering is handled |
| 30 // by |AutofillPopupViewViews|. |
| 31 class AutofillPopupChildView : public views::View { |
| 32 public: |
| 33 // Internal class name. |
| 34 static const char kViewClassName[]; |
| 35 |
| 36 // |controller| should not be NULL. |
| 37 AutofillPopupChildView(AutofillPopupController* controller, size_t index) |
| 38 : controller_(controller), index_(index) { |
| 39 SetFocusBehavior(FocusBehavior::ALWAYS); |
| 40 } |
| 41 |
| 42 private: |
| 43 ~AutofillPopupChildView() override {} |
| 44 |
| 45 // views::Views implementation |
| 46 const char* GetClassName() const override { return kViewClassName; } |
| 47 |
| 48 void GetAccessibleNodeData(ui::AXNodeData* node_data) override { |
| 49 node_data->role = ui::AX_ROLE_MENU_ITEM; |
| 50 node_data->SetName(controller_->GetSuggestionAt(index_).value); |
| 51 } |
| 52 |
| 53 AutofillPopupController* controller_; // Weak reference. |
| 54 const size_t index_; |
| 55 |
| 56 DISALLOW_COPY_AND_ASSIGN(AutofillPopupChildView); |
| 57 }; |
| 58 |
| 59 // static |
| 60 const char AutofillPopupChildView::kViewClassName[] = "AutofillPopupChildView"; |
| 61 |
| 62 } // namespace |
| 63 |
| 64 // static |
| 65 constexpr int AutofillPopupView::kNoSelection; |
| 66 |
23 AutofillPopupViewViews::AutofillPopupViewViews( | 67 AutofillPopupViewViews::AutofillPopupViewViews( |
24 AutofillPopupController* controller, | 68 AutofillPopupController* controller, |
25 views::Widget* parent_widget) | 69 views::Widget* parent_widget) |
26 : AutofillPopupBaseView(controller, parent_widget), | 70 : AutofillPopupBaseView(controller, parent_widget), |
27 controller_(controller) {} | 71 controller_(controller) { |
| 72 CreateChildViews(); |
| 73 SetFocusBehavior(FocusBehavior::ALWAYS); |
| 74 } |
28 | 75 |
29 AutofillPopupViewViews::~AutofillPopupViewViews() {} | 76 AutofillPopupViewViews::~AutofillPopupViewViews() {} |
30 | 77 |
31 void AutofillPopupViewViews::Show() { | 78 void AutofillPopupViewViews::Show() { |
32 DoShow(); | 79 DoShow(); |
| 80 NotifyAccessibilityEvent(ui::AX_EVENT_MENU_START, true); |
33 } | 81 } |
34 | 82 |
35 void AutofillPopupViewViews::Hide() { | 83 void AutofillPopupViewViews::Hide() { |
36 // The controller is no longer valid after it hides us. | 84 // The controller is no longer valid after it hides us. |
37 controller_ = NULL; | 85 controller_ = NULL; |
38 | |
39 DoHide(); | 86 DoHide(); |
| 87 NotifyAccessibilityEvent(ui::AX_EVENT_MENU_END, true); |
40 } | 88 } |
41 | 89 |
42 void AutofillPopupViewViews::UpdateBoundsAndRedrawPopup() { | 90 void AutofillPopupViewViews::OnSuggestionsChanged() { |
| 91 // We recreate the child views so we can be sure the |controller_|'s |
| 92 // |GetLineCount()| will match the number of child views. Otherwise, |
| 93 // the number of suggestions i.e. |GetLineCount()| may not match 1x1 with the |
| 94 // child views. See crbug.com/697466. |
| 95 RemoveAllChildViews(true /* delete_children */); |
| 96 CreateChildViews(); |
43 DoUpdateBoundsAndRedrawPopup(); | 97 DoUpdateBoundsAndRedrawPopup(); |
44 } | 98 } |
45 | 99 |
46 void AutofillPopupViewViews::OnPaint(gfx::Canvas* canvas) { | 100 void AutofillPopupViewViews::OnPaint(gfx::Canvas* canvas) { |
47 if (!controller_) | 101 if (!controller_) |
48 return; | 102 return; |
49 | 103 |
50 canvas->DrawColor(GetNativeTheme()->GetSystemColor( | 104 canvas->DrawColor(GetNativeTheme()->GetSystemColor( |
51 ui::NativeTheme::kColorId_ResultsTableNormalBackground)); | 105 ui::NativeTheme::kColorId_ResultsTableNormalBackground)); |
52 OnPaintBorder(canvas); | 106 OnPaintBorder(canvas); |
53 | 107 |
| 108 DCHECK_EQ(controller_->GetLineCount(), static_cast<size_t>(child_count())); |
54 for (size_t i = 0; i < controller_->GetLineCount(); ++i) { | 109 for (size_t i = 0; i < controller_->GetLineCount(); ++i) { |
55 gfx::Rect line_rect = controller_->layout_model().GetRowBounds(i); | 110 gfx::Rect line_rect = controller_->layout_model().GetRowBounds(i); |
56 | 111 |
57 if (controller_->GetSuggestionAt(i).frontend_id == | 112 if (controller_->GetSuggestionAt(i).frontend_id == |
58 POPUP_ITEM_ID_SEPARATOR) { | 113 POPUP_ITEM_ID_SEPARATOR) { |
59 canvas->FillRect( | 114 canvas->FillRect( |
60 line_rect, | 115 line_rect, |
61 GetNativeTheme()->GetSystemColor( | 116 GetNativeTheme()->GetSystemColor( |
62 ui::NativeTheme::kColorId_ResultsTableNormalDimmedText)); | 117 ui::NativeTheme::kColorId_ResultsTableNormalDimmedText)); |
63 } else { | 118 } else { |
64 DrawAutofillEntry(canvas, i, line_rect); | 119 DrawAutofillEntry(canvas, i, line_rect); |
65 } | 120 } |
66 } | 121 } |
67 } | 122 } |
68 | 123 |
69 void AutofillPopupViewViews::InvalidateRow(size_t row) { | 124 void AutofillPopupViewViews::OnSelectedRowChanged(int previous_row_selection, |
70 SchedulePaintInRect(controller_->layout_model().GetRowBounds(row)); | 125 int current_row_selection) { |
| 126 if (previous_row_selection != kNoSelection && |
| 127 previous_row_selection < static_cast<int>(controller_->GetLineCount())) { |
| 128 SchedulePaintInRect( |
| 129 controller_->layout_model().GetRowBounds(previous_row_selection)); |
| 130 } |
| 131 |
| 132 if (current_row_selection != kNoSelection) { |
| 133 SchedulePaintInRect( |
| 134 controller_->layout_model().GetRowBounds(current_row_selection)); |
| 135 DCHECK_LT(current_row_selection, child_count()); |
| 136 views::View* child = child_at(current_row_selection); |
| 137 DCHECK_EQ(child->GetClassName(), AutofillPopupChildView::kViewClassName); |
| 138 AutofillPopupChildView* child_view = |
| 139 static_cast<AutofillPopupChildView*>(child); |
| 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); |
| 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 |