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 "autofill_popup_view_views.h" | 5 #include "autofill_popup_view_views.h" |
6 | 6 |
7 #include "chrome/browser/ui/views/autofill/autofill_external_delegate_views.h" | 7 #include "chrome/browser/ui/views/autofill/autofill_external_delegate_views.h" |
8 #include "content/public/browser/render_view_host.h" | 8 #include "content/public/browser/render_view_host.h" |
9 #include "content/public/browser/web_contents.h" | 9 #include "content/public/browser/web_contents.h" |
10 #include "content/public/browser/web_contents_view.h" | 10 #include "content/public/browser/web_contents_view.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 } | 38 } |
39 | 39 |
40 AutofillPopupViewViews::~AutofillPopupViewViews() { | 40 AutofillPopupViewViews::~AutofillPopupViewViews() { |
41 external_delegate_->InvalidateView(); | 41 external_delegate_->InvalidateView(); |
42 } | 42 } |
43 | 43 |
44 void AutofillPopupViewViews::Hide() { | 44 void AutofillPopupViewViews::Hide() { |
45 if (GetWidget()) | 45 if (GetWidget()) |
46 GetWidget()->Close(); | 46 GetWidget()->Close(); |
47 web_contents_->GetRenderViewHost()->RemoveKeyboardListener(this); | 47 web_contents_->GetRenderViewHost()->RemoveKeyboardListener(this); |
48 | |
49 views::Widget* browser_widget = | |
50 views::Widget::GetTopLevelWidgetForNativeView( | |
51 web_contents_->GetView()->GetTopLevelNativeWindow()); | |
52 browser_widget->RemoveObserver(this); | |
53 } | 48 } |
54 | 49 |
55 void AutofillPopupViewViews::OnPaint(gfx::Canvas* canvas) { | 50 void AutofillPopupViewViews::OnPaint(gfx::Canvas* canvas) { |
56 canvas->DrawColor(kPopupBackground); | 51 canvas->DrawColor(kPopupBackground); |
57 OnPaintBorder(canvas); | 52 OnPaintBorder(canvas); |
58 | 53 |
59 for (size_t i = 0; i < autofill_values().size(); ++i) { | 54 for (size_t i = 0; i < autofill_values().size(); ++i) { |
60 gfx::Rect line_rect = GetRectForRow(i, width()); | 55 gfx::Rect line_rect = GetRectForRow(i, width()); |
61 | 56 |
62 if (autofill_unique_ids()[i] == WebAutofillClient::MenuItemIDSeparator) | 57 if (autofill_unique_ids()[i] == WebAutofillClient::MenuItemIDSeparator) |
63 canvas->DrawRect(line_rect, kLabelTextColor); | 58 canvas->DrawRect(line_rect, kLabelTextColor); |
64 else | 59 else |
65 DrawAutofillEntry(canvas, i, line_rect); | 60 DrawAutofillEntry(canvas, i, line_rect); |
66 } | 61 } |
67 } | 62 } |
68 | 63 |
69 void AutofillPopupViewViews::OnWidgetBoundsChanged(views::Widget* widget, | |
70 const gfx::Rect& new_bounds) { | |
71 external_delegate()->HideAutofillPopup(); | |
72 } | |
73 | |
74 bool AutofillPopupViewViews::HandleKeyPressEvent(ui::KeyEvent* event) { | 64 bool AutofillPopupViewViews::HandleKeyPressEvent(ui::KeyEvent* event) { |
75 switch (event->key_code()) { | 65 switch (event->key_code()) { |
76 case ui::VKEY_UP: | 66 case ui::VKEY_UP: |
77 SelectPreviousLine(); | 67 SelectPreviousLine(); |
78 return true; | 68 return true; |
79 case ui::VKEY_DOWN: | 69 case ui::VKEY_DOWN: |
80 SelectNextLine(); | 70 SelectNextLine(); |
81 return true; | 71 return true; |
82 case ui::VKEY_PRIOR: | 72 case ui::VKEY_PRIOR: |
83 SetSelectedLine(0); | 73 SetSelectedLine(0); |
84 return true; | 74 return true; |
85 case ui::VKEY_NEXT: | 75 case ui::VKEY_NEXT: |
86 SetSelectedLine(autofill_values().size() - 1); | 76 SetSelectedLine(autofill_values().size() - 1); |
87 return true; | 77 return true; |
88 case ui::VKEY_ESCAPE: | 78 case ui::VKEY_ESCAPE: |
89 external_delegate()->HideAutofillPopup(); | 79 if (external_delegate()) { |
| 80 external_delegate()->HideAutofillPopup(); |
| 81 } else { |
| 82 Hide(); |
| 83 } |
90 return true; | 84 return true; |
91 case ui::VKEY_DELETE: | 85 case ui::VKEY_DELETE: |
92 return event->IsShiftDown() && RemoveSelectedLine(); | 86 return event->IsShiftDown() && RemoveSelectedLine(); |
93 case ui::VKEY_RETURN: | 87 case ui::VKEY_RETURN: |
94 return AcceptSelectedLine(); | 88 return AcceptSelectedLine(); |
95 default: | 89 default: |
96 return false; | 90 return false; |
97 } | 91 } |
98 } | 92 } |
99 | 93 |
100 void AutofillPopupViewViews::ShowInternal() { | 94 void AutofillPopupViewViews::ShowInternal() { |
101 if (!GetWidget()) { | 95 if (!GetWidget()) { |
102 // The widget is destroyed by the corresponding NativeWidget, so we use | 96 // The widget is destroyed by the corresponding NativeWidget, so we use |
103 // a weak pointer to hold the reference and don't have to worry about | 97 // a weak pointer to hold the reference and don't have to worry about |
104 // deletion. | 98 // deletion. |
105 views::Widget* widget = new views::Widget; | 99 views::Widget* widget = new views::Widget; |
106 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); | 100 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); |
107 params.delegate = this; | 101 params.delegate = this; |
108 params.can_activate = false; | 102 params.can_activate = false; |
109 params.transparent = true; | 103 params.transparent = true; |
110 params.parent = web_contents_->GetView()->GetTopLevelNativeWindow(); | 104 params.parent = web_contents_->GetView()->GetTopLevelNativeWindow(); |
111 widget->Init(params); | 105 widget->Init(params); |
112 widget->SetContentsView(this); | 106 widget->SetContentsView(this); |
113 widget->Show(); | 107 widget->Show(); |
114 | 108 |
115 gfx::Rect client_area; | 109 gfx::Rect client_area; |
116 web_contents_->GetContainerBounds(&client_area); | 110 web_contents_->GetContainerBounds(&client_area); |
117 widget->SetBounds(client_area); | 111 widget->SetBounds(client_area); |
118 | |
119 // Setup an observer to check for when the browser moves or changes size, | |
120 // since the popup should always be hidden in those cases. | |
121 views::Widget* browser_widget = | |
122 views::Widget::GetTopLevelWidgetForNativeView( | |
123 web_contents_->GetView()->GetTopLevelNativeWindow()); | |
124 browser_widget->AddObserver(this); | |
125 } | 112 } |
126 | 113 |
127 set_border(views::Border::CreateSolidBorder(kBorderThickness, kBorderColor)); | 114 set_border(views::Border::CreateSolidBorder(kBorderThickness, kBorderColor)); |
128 | 115 |
129 UpdateBoundsAndRedrawPopup(); | 116 UpdateBoundsAndRedrawPopup(); |
130 | 117 |
131 web_contents_->GetRenderViewHost()->AddKeyboardListener(this); | 118 web_contents_->GetRenderViewHost()->AddKeyboardListener(this); |
132 } | 119 } |
133 | 120 |
134 void AutofillPopupViewViews::InvalidateRow(size_t row) { | 121 void AutofillPopupViewViews::InvalidateRow(size_t row) { |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 canvas->DrawStringInt( | 184 canvas->DrawStringInt( |
198 autofill_labels()[index], | 185 autofill_labels()[index], |
199 label_font(), | 186 label_font(), |
200 kLabelTextColor, | 187 kLabelTextColor, |
201 x_align_left + kEndPadding, | 188 x_align_left + kEndPadding, |
202 entry_rect.y(), | 189 entry_rect.y(), |
203 canvas->GetStringWidth(autofill_labels()[index], label_font()), | 190 canvas->GetStringWidth(autofill_labels()[index], label_font()), |
204 entry_rect.height(), | 191 entry_rect.height(), |
205 gfx::Canvas::TEXT_ALIGN_CENTER); | 192 gfx::Canvas::TEXT_ALIGN_CENTER); |
206 } | 193 } |
OLD | NEW |