Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / views / autofill / autofill_popup_view_views.cc
blob2fa7ba5d84a0f4b331f0435a4bb0e8783ffe1383
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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/autofill/autofill_popup_view_views.h"
7 #include "chrome/browser/ui/autofill/autofill_popup_controller.h"
8 #include "components/autofill/core/browser/popup_item_ids.h"
9 #include "components/autofill/core/browser/suggestion.h"
10 #include "ui/base/resource/resource_bundle.h"
11 #include "ui/events/keycodes/keyboard_codes.h"
12 #include "ui/gfx/canvas.h"
13 #include "ui/gfx/geometry/point.h"
14 #include "ui/gfx/geometry/rect.h"
15 #include "ui/gfx/image/image.h"
16 #include "ui/gfx/native_widget_types.h"
17 #include "ui/gfx/text_utils.h"
18 #include "ui/views/border.h"
19 #include "ui/views/widget/widget.h"
21 namespace autofill {
23 AutofillPopupViewViews::AutofillPopupViewViews(
24 AutofillPopupController* controller,
25 views::Widget* parent_widget)
26 : AutofillPopupBaseView(controller, parent_widget),
27 controller_(controller) {}
29 AutofillPopupViewViews::~AutofillPopupViewViews() {}
31 void AutofillPopupViewViews::Show() {
32 DoShow();
35 void AutofillPopupViewViews::Hide() {
36 // The controller is no longer valid after it hides us.
37 controller_ = NULL;
39 DoHide();
42 void AutofillPopupViewViews::UpdateBoundsAndRedrawPopup() {
43 DoUpdateBoundsAndRedrawPopup();
46 void AutofillPopupViewViews::OnPaint(gfx::Canvas* canvas) {
47 if (!controller_)
48 return;
50 canvas->DrawColor(kPopupBackground);
51 OnPaintBorder(canvas);
53 for (size_t i = 0; i < controller_->GetLineCount(); ++i) {
54 gfx::Rect line_rect = controller_->GetRowBounds(i);
56 if (controller_->GetSuggestionAt(i).frontend_id ==
57 POPUP_ITEM_ID_SEPARATOR) {
58 canvas->FillRect(line_rect, kItemTextColor);
59 } else {
60 DrawAutofillEntry(canvas, i, line_rect);
65 void AutofillPopupViewViews::InvalidateRow(size_t row) {
66 SchedulePaintInRect(controller_->GetRowBounds(row));
69 void AutofillPopupViewViews::DrawAutofillEntry(gfx::Canvas* canvas,
70 int index,
71 const gfx::Rect& entry_rect) {
72 if (controller_->selected_line() == index)
73 canvas->FillRect(entry_rect, kHoveredBackgroundColor);
75 const bool is_rtl = controller_->IsRTL();
76 const int text_align =
77 is_rtl ? gfx::Canvas::TEXT_ALIGN_RIGHT : gfx::Canvas::TEXT_ALIGN_LEFT;
78 gfx::Rect value_rect = entry_rect;
79 value_rect.Inset(kEndPadding, 0);
80 canvas->DrawStringRectWithFlags(
81 controller_->GetElidedValueAt(index),
82 controller_->GetValueFontListForRow(index),
83 controller_->IsWarning(index) ? kWarningTextColor : kValueTextColor,
84 value_rect, text_align);
86 // Use this to figure out where all the other Autofill items should be placed.
87 int x_align_left = is_rtl ? kEndPadding : entry_rect.right() - kEndPadding;
89 // Draw the Autofill icon, if one exists
90 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
91 int row_height = controller_->GetRowBounds(index).height();
92 if (!controller_->GetSuggestionAt(index).icon.empty()) {
93 int icon = controller_->GetIconResourceID(
94 controller_->GetSuggestionAt(index).icon);
95 DCHECK_NE(-1, icon);
96 const gfx::ImageSkia* image = rb.GetImageSkiaNamed(icon);
97 int icon_y = entry_rect.y() + (row_height - image->height()) / 2;
99 x_align_left += is_rtl ? 0 : -image->width();
101 canvas->DrawImageInt(*image, x_align_left, icon_y);
103 x_align_left += is_rtl ? image->width() + kIconPadding : -kIconPadding;
106 // Draw the label text.
107 const int label_width =
108 gfx::GetStringWidth(controller_->GetElidedLabelAt(index),
109 controller_->GetLabelFontList());
110 if (!is_rtl)
111 x_align_left -= label_width;
113 canvas->DrawStringRectWithFlags(
114 controller_->GetElidedLabelAt(index), controller_->GetLabelFontList(),
115 kItemTextColor,
116 gfx::Rect(x_align_left, entry_rect.y(), label_width, entry_rect.height()),
117 text_align);
120 AutofillPopupView* AutofillPopupView::Create(
121 AutofillPopupController* controller) {
122 views::Widget* observing_widget =
123 views::Widget::GetTopLevelWidgetForNativeView(
124 controller->container_view());
126 // If the top level widget can't be found, cancel the popup since we can't
127 // fully set it up.
128 if (!observing_widget)
129 return NULL;
131 return new AutofillPopupViewViews(controller, observing_widget);
134 } // namespace autofill