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 "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; | |
|
Evan Stade
2017/03/15 15:18:58
I'm confused why you need this line or what it's d
csashi
2017/03/15 21:31:09
It is a constant to indicate no selection. I tried
Evan Stade
2017/03/16 01:23:44
I understand the purpose of kNoSelection, but it's
csashi
2017/03/16 01:50:55
Don't we need an initializer for static variable?
csashi
2017/03/16 01:53:41
Removed. I must have misunderstood.
csashi
2017/03/16 02:18:23
Sorry for the flip-flop. I put it back based on th
| |
| 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 // Currently, child views get the data from 'controller_'. If the child views | |
| 96 // save additional state corresponding to the suggestion, we must recreate the | |
| 97 // child views even when |GetLineCount()| == |child_count()| because the | |
| 98 // suggestion content may change. | |
|
Evan Stade
2017/03/15 15:18:58
I think we should remove this conditional and the
csashi
2017/03/15 21:31:08
Done.
| |
| 99 if (controller_->GetLineCount() != static_cast<size_t>(child_count())) { | |
| 100 RemoveAllChildViews(true /* delete_children */); | |
| 101 CreateChildViews(); | |
| 102 } | |
| 43 DoUpdateBoundsAndRedrawPopup(); | 103 DoUpdateBoundsAndRedrawPopup(); |
| 44 } | 104 } |
| 45 | 105 |
| 46 void AutofillPopupViewViews::OnPaint(gfx::Canvas* canvas) { | 106 void AutofillPopupViewViews::OnPaint(gfx::Canvas* canvas) { |
| 47 if (!controller_) | 107 if (!controller_) |
| 48 return; | 108 return; |
| 49 | 109 |
| 50 canvas->DrawColor(GetNativeTheme()->GetSystemColor( | 110 canvas->DrawColor(GetNativeTheme()->GetSystemColor( |
| 51 ui::NativeTheme::kColorId_ResultsTableNormalBackground)); | 111 ui::NativeTheme::kColorId_ResultsTableNormalBackground)); |
| 52 OnPaintBorder(canvas); | 112 OnPaintBorder(canvas); |
| 53 | 113 |
| 114 DCHECK_EQ(controller_->GetLineCount(), static_cast<size_t>(child_count())); | |
| 54 for (size_t i = 0; i < controller_->GetLineCount(); ++i) { | 115 for (size_t i = 0; i < controller_->GetLineCount(); ++i) { |
| 55 gfx::Rect line_rect = controller_->layout_model().GetRowBounds(i); | 116 gfx::Rect line_rect = controller_->layout_model().GetRowBounds(i); |
| 56 | 117 |
| 57 if (controller_->GetSuggestionAt(i).frontend_id == | 118 if (controller_->GetSuggestionAt(i).frontend_id == |
| 58 POPUP_ITEM_ID_SEPARATOR) { | 119 POPUP_ITEM_ID_SEPARATOR) { |
| 59 canvas->FillRect( | 120 canvas->FillRect( |
| 60 line_rect, | 121 line_rect, |
| 61 GetNativeTheme()->GetSystemColor( | 122 GetNativeTheme()->GetSystemColor( |
| 62 ui::NativeTheme::kColorId_ResultsTableNormalDimmedText)); | 123 ui::NativeTheme::kColorId_ResultsTableNormalDimmedText)); |
| 63 } else { | 124 } else { |
| 64 DrawAutofillEntry(canvas, i, line_rect); | 125 DrawAutofillEntry(canvas, i, line_rect); |
| 65 } | 126 } |
| 66 } | 127 } |
| 67 } | 128 } |
| 68 | 129 |
| 69 void AutofillPopupViewViews::InvalidateRow(size_t row) { | 130 void AutofillPopupViewViews::OnSelectedRowChanged( |
| 70 SchedulePaintInRect(controller_->layout_model().GetRowBounds(row)); | 131 size_t previous_row_selection, |
| 132 size_t current_row_selection) { | |
| 133 if (previous_row_selection != static_cast<size_t>(kNoSelection) && | |
|
Evan Stade
2017/03/15 15:18:58
it seems off that you're casting -1 to a size_t. E
csashi
2017/03/15 21:31:08
Done. I did not use optional because of following
| |
| 134 previous_row_selection < controller_->GetLineCount()) | |
| 135 SchedulePaintInRect( | |
| 136 controller_->layout_model().GetRowBounds(previous_row_selection)); | |
|
Evan Stade
2017/03/15 15:18:58
nit: please use curlies and add a newline after
csashi
2017/03/15 21:31:08
Done.
| |
| 137 if (current_row_selection != static_cast<size_t>(kNoSelection)) { | |
| 138 DCHECK_GE(current_row_selection, static_cast<size_t>(0)); | |
|
Evan Stade
2017/03/15 15:18:58
is it even possible for this check to fail? size_t
csashi
2017/03/15 21:31:08
Done.
| |
| 139 DCHECK_LT(current_row_selection, controller_->GetLineCount()); | |
|
Evan Stade
2017/03/15 15:18:58
I think these DCHECKs are a little over the top an
csashi
2017/03/15 21:31:08
Done. Agreed. I did not know suggestions could ch
| |
| 140 SchedulePaintInRect( | |
| 141 controller_->layout_model().GetRowBounds(current_row_selection)); | |
| 142 DCHECK_LT(current_row_selection, static_cast<size_t>(child_count())); | |
| 143 views::View* child = child_at(current_row_selection); | |
| 144 DCHECK(child); | |
|
Evan Stade
2017/03/15 15:18:58
nit: this is redundant with the check on L142
I t
csashi
2017/03/15 21:31:08
Done.
| |
| 145 DCHECK_EQ(child->GetClassName(), AutofillPopupChildView::kViewClassName); | |
| 146 AutofillPopupChildView* child_view = | |
| 147 static_cast<AutofillPopupChildView*>(child); | |
| 148 child_view->NotifyAccessibilityEvent(ui::AX_EVENT_SELECTION, true); | |
| 149 } | |
| 71 } | 150 } |
| 72 | 151 |
| 73 /** | 152 /** |
| 74 * Autofill entries in ltr. | 153 * Autofill entries in ltr. |
| 75 * | 154 * |
| 76 * ............................................................................ | 155 * ............................................................................ |
| 77 * . ICON | HTTP WARNING MESSAGE VALUE | LABEL . | 156 * . ICON | HTTP WARNING MESSAGE VALUE | LABEL . |
| 78 * ............................................................................ | 157 * ............................................................................ |
| 79 * . OTHER AUTOFILL ENTRY VALUE | LABEL | ICON . | 158 * . OTHER AUTOFILL ENTRY VALUE | LABEL | ICON . |
| 80 * ............................................................................ | 159 * ............................................................................ |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 182 controller_->GetElidedLabelAt(index), | 261 controller_->GetElidedLabelAt(index), |
| 183 controller_->layout_model().GetLabelFontListForRow(index), | 262 controller_->layout_model().GetLabelFontListForRow(index), |
| 184 GetNativeTheme()->GetSystemColor( | 263 GetNativeTheme()->GetSystemColor( |
| 185 ui::NativeTheme::kColorId_ResultsTableNormalDimmedText), | 264 ui::NativeTheme::kColorId_ResultsTableNormalDimmedText), |
| 186 gfx::Rect(label_x_align_left, entry_rect.y(), label_width, | 265 gfx::Rect(label_x_align_left, entry_rect.y(), label_width, |
| 187 entry_rect.height()), | 266 entry_rect.height()), |
| 188 text_align); | 267 text_align); |
| 189 } | 268 } |
| 190 } | 269 } |
| 191 | 270 |
| 271 void AutofillPopupViewViews::GetAccessibleNodeData(ui::AXNodeData* node_data) { | |
| 272 node_data->role = ui::AX_ROLE_MENU; | |
| 273 node_data->SetName( | |
| 274 l10n_util::GetStringUTF16(IDS_AUTOFILL_POPUP_ACCESSIBLE_NODE_DATA)); | |
| 275 } | |
| 276 | |
| 277 void AutofillPopupViewViews::CreateChildViews() { | |
| 278 for (size_t i = 0; i < controller_->GetLineCount(); ++i) { | |
| 279 AutofillPopupChildView* child = new AutofillPopupChildView(controller_, i); | |
| 280 AddChildView(child); | |
| 281 } | |
| 282 } | |
| 283 | |
| 192 AutofillPopupView* AutofillPopupView::Create( | 284 AutofillPopupView* AutofillPopupView::Create( |
| 193 AutofillPopupController* controller) { | 285 AutofillPopupController* controller) { |
| 194 views::Widget* observing_widget = | 286 views::Widget* observing_widget = |
| 195 views::Widget::GetTopLevelWidgetForNativeView( | 287 views::Widget::GetTopLevelWidgetForNativeView( |
| 196 controller->container_view()); | 288 controller->container_view()); |
| 197 | 289 |
| 198 // If the top level widget can't be found, cancel the popup since we can't | 290 // If the top level widget can't be found, cancel the popup since we can't |
| 199 // fully set it up. | 291 // fully set it up. |
| 200 if (!observing_widget) | 292 if (!observing_widget) |
| 201 return NULL; | 293 return NULL; |
| 202 | 294 |
| 203 return new AutofillPopupViewViews(controller, observing_widget); | 295 return new AutofillPopupViewViews(controller, observing_widget); |
| 204 } | 296 } |
| 205 | 297 |
| 206 } // namespace autofill | 298 } // namespace autofill |
| OLD | NEW |