autofill_popup_view_views.cc revision 6e8cce623b6e4fe0c9e4af605d675dd9d0338c38
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. 4 5#include "chrome/browser/ui/views/autofill/autofill_popup_view_views.h" 6 7#include "chrome/browser/ui/autofill/autofill_popup_controller.h" 8#include "components/autofill/core/browser/popup_item_ids.h" 9#include "ui/base/resource/resource_bundle.h" 10#include "ui/events/keycodes/keyboard_codes.h" 11#include "ui/gfx/canvas.h" 12#include "ui/gfx/image/image.h" 13#include "ui/gfx/native_widget_types.h" 14#include "ui/gfx/point.h" 15#include "ui/gfx/rect.h" 16#include "ui/gfx/text_utils.h" 17#include "ui/views/border.h" 18#include "ui/views/widget/widget.h" 19 20namespace autofill { 21 22AutofillPopupViewViews::AutofillPopupViewViews( 23 AutofillPopupController* controller, views::Widget* observing_widget) 24 : AutofillPopupBaseView(controller, observing_widget), 25 controller_(controller) {} 26 27AutofillPopupViewViews::~AutofillPopupViewViews() {} 28 29void AutofillPopupViewViews::Show() { 30 DoShow(); 31} 32 33void AutofillPopupViewViews::Hide() { 34 // The controller is no longer valid after it hides us. 35 controller_ = NULL; 36 37 DoHide(); 38} 39 40void AutofillPopupViewViews::UpdateBoundsAndRedrawPopup() { 41 DoUpdateBoundsAndRedrawPopup(); 42} 43 44void AutofillPopupViewViews::OnPaint(gfx::Canvas* canvas) { 45 if (!controller_) 46 return; 47 48 canvas->DrawColor(kPopupBackground); 49 OnPaintBorder(canvas); 50 51 for (size_t i = 0; i < controller_->names().size(); ++i) { 52 gfx::Rect line_rect = controller_->GetRowBounds(i); 53 54 if (controller_->identifiers()[i] == POPUP_ITEM_ID_SEPARATOR) { 55 canvas->FillRect(line_rect, kItemTextColor); 56 } else { 57 DrawAutofillEntry(canvas, i, line_rect); 58 } 59 } 60} 61 62void AutofillPopupViewViews::InvalidateRow(size_t row) { 63 SchedulePaintInRect(controller_->GetRowBounds(row)); 64} 65 66void AutofillPopupViewViews::DrawAutofillEntry(gfx::Canvas* canvas, 67 int index, 68 const gfx::Rect& entry_rect) { 69 if (controller_->selected_line() == index) 70 canvas->FillRect(entry_rect, kHoveredBackgroundColor); 71 72 const bool is_rtl = controller_->IsRTL(); 73 const int value_text_width = 74 gfx::GetStringWidth(controller_->names()[index], 75 controller_->GetNameFontListForRow(index)); 76 const int value_content_x = is_rtl ? 77 entry_rect.width() - value_text_width - kEndPadding : kEndPadding; 78 79 canvas->DrawStringRectWithFlags( 80 controller_->names()[index], 81 controller_->GetNameFontListForRow(index), 82 controller_->IsWarning(index) ? kWarningTextColor : kValueTextColor, 83 gfx::Rect(value_content_x, 84 entry_rect.y(), 85 value_text_width, 86 entry_rect.height()), 87 gfx::Canvas::TEXT_ALIGN_CENTER); 88 89 // Use this to figure out where all the other Autofill items should be placed. 90 int x_align_left = is_rtl ? kEndPadding : entry_rect.width() - kEndPadding; 91 92 // Draw the Autofill icon, if one exists 93 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 94 int row_height = controller_->GetRowBounds(index).height(); 95 if (!controller_->icons()[index].empty()) { 96 int icon = controller_->GetIconResourceID(controller_->icons()[index]); 97 DCHECK_NE(-1, icon); 98 const gfx::ImageSkia* image = rb.GetImageSkiaNamed(icon); 99 int icon_y = entry_rect.y() + (row_height - image->height()) / 2; 100 101 x_align_left += is_rtl ? 0 : -image->width(); 102 103 canvas->DrawImageInt(*image, x_align_left, icon_y); 104 105 x_align_left += is_rtl ? image->width() + kIconPadding : -kIconPadding; 106 } 107 108 // Draw the name text. 109 const int subtext_width = 110 gfx::GetStringWidth(controller_->subtexts()[index], 111 controller_->subtext_font_list()); 112 if (!is_rtl) 113 x_align_left -= subtext_width; 114 115 canvas->DrawStringRectWithFlags( 116 controller_->subtexts()[index], 117 controller_->subtext_font_list(), 118 kItemTextColor, 119 gfx::Rect(x_align_left, 120 entry_rect.y(), 121 subtext_width, 122 entry_rect.height()), 123 gfx::Canvas::TEXT_ALIGN_CENTER); 124} 125 126AutofillPopupView* AutofillPopupView::Create( 127 AutofillPopupController* controller) { 128 views::Widget* observing_widget = 129 views::Widget::GetTopLevelWidgetForNativeView( 130 controller->container_view()); 131 132 // If the top level widget can't be found, cancel the popup since we can't 133 // fully set it up. 134 if (!observing_widget) 135 return NULL; 136 137 return new AutofillPopupViewViews(controller, observing_widget); 138} 139 140} // namespace autofill 141