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/omnibox/omnibox_popup_contents_view.h"
9 #include "chrome/browser/search/search.h"
10 #include "chrome/browser/themes/theme_properties.h"
11 #include "chrome/browser/ui/omnibox/omnibox_view.h"
12 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
13 #include "chrome/browser/ui/views/omnibox/omnibox_result_view.h"
14 #include "ui/base/theme_provider.h"
15 #include "ui/compositor/clip_transform_recorder.h"
16 #include "ui/compositor/paint_recorder.h"
17 #include "ui/gfx/canvas.h"
18 #include "ui/gfx/image/image.h"
19 #include "ui/gfx/path.h"
20 #include "ui/resources/grit/ui_resources.h"
21 #include "ui/views/controls/image_view.h"
22 #include "ui/views/resources/grit/views_resources.h"
23 #include "ui/views/view_targeter.h"
24 #include "ui/views/widget/widget.h"
25 #include "ui/views/window/non_client_view.h"
29 // This is the number of pixels in the border image interior to the actual
31 const int kBorderInterior
= 6;
35 class OmniboxPopupContentsView::AutocompletePopupWidget
36 : public views::Widget
,
37 public base::SupportsWeakPtr
<AutocompletePopupWidget
> {
39 AutocompletePopupWidget() {}
40 ~AutocompletePopupWidget() override
{}
43 DISALLOW_COPY_AND_ASSIGN(AutocompletePopupWidget
);
46 ////////////////////////////////////////////////////////////////////////////////
47 // OmniboxPopupContentsView, public:
49 OmniboxPopupView
* OmniboxPopupContentsView::Create(
50 const gfx::FontList
& font_list
,
51 OmniboxView
* omnibox_view
,
52 OmniboxEditModel
* edit_model
,
53 LocationBarView
* location_bar_view
) {
54 OmniboxPopupContentsView
* view
= NULL
;
55 view
= new OmniboxPopupContentsView(
56 font_list
, omnibox_view
, edit_model
, location_bar_view
);
61 OmniboxPopupContentsView::OmniboxPopupContentsView(
62 const gfx::FontList
& font_list
,
63 OmniboxView
* omnibox_view
,
64 OmniboxEditModel
* edit_model
,
65 LocationBarView
* location_bar_view
)
66 : model_(new OmniboxPopupModel(this, edit_model
)),
67 omnibox_view_(omnibox_view
),
68 location_bar_view_(location_bar_view
),
69 font_list_(font_list
),
70 ignore_mouse_drag_(false),
71 size_animation_(this),
74 // The contents is owned by the LocationBarView.
75 set_owned_by_client();
77 ui::ThemeProvider
* theme
= location_bar_view_
->GetThemeProvider();
78 bottom_shadow_
= theme
->GetImageSkiaNamed(IDR_BUBBLE_B
);
81 scoped_ptr
<views::ViewTargeter
>(new views::ViewTargeter(this)));
84 void OmniboxPopupContentsView::Init() {
85 // This can't be done in the constructor as at that point we aren't
86 // necessarily our final class yet, and we may have subclasses
87 // overriding CreateResultView.
88 for (size_t i
= 0; i
< AutocompleteResult::kMaxMatches
; ++i
) {
89 OmniboxResultView
* result_view
= CreateResultView(i
, font_list_
);
90 result_view
->SetVisible(false);
91 AddChildViewAt(result_view
, static_cast<int>(i
));
95 OmniboxPopupContentsView::~OmniboxPopupContentsView() {
96 // We don't need to do anything with |popup_| here. The OS either has already
97 // closed the window, in which case it's been deleted, or it will soon, in
98 // which case there's nothing we need to do.
101 gfx::Rect
OmniboxPopupContentsView::GetPopupBounds() const {
102 if (!size_animation_
.is_animating())
103 return target_bounds_
;
105 gfx::Rect current_frame_bounds
= start_bounds_
;
106 int total_height_delta
= target_bounds_
.height() - start_bounds_
.height();
107 // Round |current_height_delta| instead of truncating so we won't leave single
108 // white pixels at the bottom of the popup as long when animating very small
109 // height differences.
110 int current_height_delta
= static_cast<int>(
111 size_animation_
.GetCurrentValue() * total_height_delta
- 0.5);
112 current_frame_bounds
.set_height(
113 current_frame_bounds
.height() + current_height_delta
);
114 return current_frame_bounds
;
117 void OmniboxPopupContentsView::LayoutChildren() {
118 gfx::Rect contents_rect
= GetContentsBounds();
121 left_margin_
, views::NonClientFrameView::kClientEdgeThickness
+
122 OmniboxResultView::kMinimumTextVerticalPadding
,
123 right_margin_
, OmniboxResultView::kMinimumTextVerticalPadding
);
124 int top
= contents_rect
.y();
125 for (size_t i
= 0; i
< AutocompleteResult::kMaxMatches
; ++i
) {
126 View
* v
= child_at(i
);
128 v
->SetBounds(contents_rect
.x(), top
, contents_rect
.width(),
129 v
->GetPreferredSize().height());
130 top
= v
->bounds().bottom();
135 ////////////////////////////////////////////////////////////////////////////////
136 // OmniboxPopupContentsView, OmniboxPopupView overrides:
138 bool OmniboxPopupContentsView::IsOpen() const {
139 return popup_
!= NULL
;
142 void OmniboxPopupContentsView::InvalidateLine(size_t line
) {
143 OmniboxResultView
* result
= result_view_at(line
);
144 result
->Invalidate();
146 if (HasMatchAt(line
) && GetMatchAtIndex(line
).associated_keyword
.get()) {
147 result
->ShowKeyword(IsSelectedIndex(line
) &&
148 model_
->selected_line_state() == OmniboxPopupModel::KEYWORD
);
152 void OmniboxPopupContentsView::UpdatePopupAppearance() {
153 if (model_
->result().empty() || omnibox_view_
->IsImeShowingPopup()) {
154 // No matches or the IME is showing a popup window which may overlap
155 // the omnibox popup window. Close any existing popup.
156 if (popup_
!= NULL
) {
157 size_animation_
.Stop();
159 // NOTE: Do NOT use CloseNow() here, as we may be deep in a callstack
160 // triggered by the popup receiving a message (e.g. LBUTTONUP), and
161 // destroying the popup would cause us to read garbage when we unwind back
163 popup_
->Close(); // This will eventually delete the popup.
169 // Update the match cached by each row, in the process of doing so make sure
170 // we have enough row views.
171 const size_t result_size
= model_
->result().size();
172 max_match_contents_width_
= 0;
173 for (size_t i
= 0; i
< result_size
; ++i
) {
174 OmniboxResultView
* view
= result_view_at(i
);
175 const AutocompleteMatch
& match
= GetMatchAtIndex(i
);
176 view
->SetMatch(match
);
177 view
->SetVisible(true);
178 if (match
.answer
&& !model_
->answer_bitmap().isNull()) {
179 view
->SetAnswerImage(
180 gfx::ImageSkia::CreateFrom1xBitmap(model_
->answer_bitmap()));
182 if (match
.type
== AutocompleteMatchType::SEARCH_SUGGEST_TAIL
) {
183 max_match_contents_width_
= std::max(
184 max_match_contents_width_
, view
->GetMatchContentsWidth());
188 for (size_t i
= result_size
; i
< AutocompleteResult::kMaxMatches
; ++i
)
189 child_at(i
)->SetVisible(false);
191 gfx::Point top_left_screen_coord
;
193 location_bar_view_
->GetOmniboxPopupPositioningInfo(
194 &top_left_screen_coord
, &width
, &left_margin_
, &right_margin_
);
195 gfx::Rect
new_target_bounds(top_left_screen_coord
,
196 gfx::Size(width
, CalculatePopupHeight()));
198 // If we're animating and our target height changes, reset the animation.
199 // NOTE: If we just reset blindly on _every_ update, then when the user types
200 // rapidly we could get "stuck" trying repeatedly to animate shrinking by the
201 // last few pixels to get to one visible result.
202 if (new_target_bounds
.height() != target_bounds_
.height())
203 size_animation_
.Reset();
204 target_bounds_
= new_target_bounds
;
206 if (popup_
== NULL
) {
207 views::Widget
* popup_parent
= location_bar_view_
->GetWidget();
209 // If the popup is currently closed, we need to create it.
210 popup_
= (new AutocompletePopupWidget
)->AsWeakPtr();
211 // On Windows use TYPE_MENU to ensure that this window uses the software
212 // compositor which avoids the UI thread blocking issue during command
213 // buffer creation. We can revert this change once http://crbug.com/125248
216 views::Widget::InitParams
params(views::Widget::InitParams::TYPE_MENU
);
217 // The menu style assumes a top most window. We don't want that in this
219 params
.keep_on_top
= false;
221 views::Widget::InitParams
params(views::Widget::InitParams::TYPE_POPUP
);
223 params
.opacity
= views::Widget::InitParams::TRANSLUCENT_WINDOW
;
224 params
.parent
= popup_parent
->GetNativeView();
225 params
.bounds
= GetPopupBounds();
226 params
.context
= popup_parent
->GetNativeWindow();
227 popup_
->Init(params
);
228 // Third-party software such as DigitalPersona identity verification can
229 // hook the underlying window creation methods and use SendMessage to
230 // synchronously change focus/activation, resulting in the popup being
231 // destroyed by the time control returns here. Bail out in this case to
232 // avoid a NULL dereference.
235 popup_
->SetVisibilityAnimationTransition(views::Widget::ANIMATE_NONE
);
236 popup_
->SetContentsView(this);
237 popup_
->StackAbove(omnibox_view_
->GetRelativeWindowForPopup());
239 // For some IMEs GetRelativeWindowForPopup triggers the omnibox to lose
240 // focus, thereby closing (and destroying) the popup.
241 // TODO(sky): this won't be needed once we close the omnibox on input
245 popup_
->ShowInactive();
247 // Animate the popup shrinking, but don't animate growing larger since that
248 // would make the popup feel less responsive.
249 start_bounds_
= GetWidget()->GetWindowBoundsInScreen();
250 if (target_bounds_
.height() < start_bounds_
.height())
251 size_animation_
.Show();
253 start_bounds_
= target_bounds_
;
254 popup_
->SetBounds(GetPopupBounds());
260 gfx::Rect
OmniboxPopupContentsView::GetTargetBounds() {
261 return target_bounds_
;
264 void OmniboxPopupContentsView::PaintUpdatesNow() {
265 // TODO(beng): remove this from the interface.
268 void OmniboxPopupContentsView::OnDragCanceled() {
269 ignore_mouse_drag_
= true;
272 ////////////////////////////////////////////////////////////////////////////////
273 // OmniboxPopupContentsView, OmniboxResultViewModel implementation:
275 bool OmniboxPopupContentsView::IsSelectedIndex(size_t index
) const {
276 return index
== model_
->selected_line();
279 bool OmniboxPopupContentsView::IsHoveredIndex(size_t index
) const {
280 return index
== model_
->hovered_line();
283 gfx::Image
OmniboxPopupContentsView::GetIconIfExtensionMatch(
284 size_t index
) const {
285 if (!HasMatchAt(index
))
287 return model_
->GetIconIfExtensionMatch(GetMatchAtIndex(index
));
290 bool OmniboxPopupContentsView::IsStarredMatch(
291 const AutocompleteMatch
& match
) const {
292 return model_
->IsStarredMatch(match
);
295 ////////////////////////////////////////////////////////////////////////////////
296 // OmniboxPopupContentsView, AnimationDelegate implementation:
298 void OmniboxPopupContentsView::AnimationProgressed(
299 const gfx::Animation
* animation
) {
300 // We should only be running the animation when the popup is already visible.
301 DCHECK(popup_
!= NULL
);
302 popup_
->SetBounds(GetPopupBounds());
305 ////////////////////////////////////////////////////////////////////////////////
306 // OmniboxPopupContentsView, views::View overrides:
308 void OmniboxPopupContentsView::Layout() {
309 // Size our children to the available content area.
312 // We need to manually schedule a paint here since we are a layered window and
313 // won't implicitly require painting until we ask for one.
317 views::View
* OmniboxPopupContentsView::GetTooltipHandlerForPoint(
318 const gfx::Point
& point
) {
322 bool OmniboxPopupContentsView::OnMousePressed(
323 const ui::MouseEvent
& event
) {
324 ignore_mouse_drag_
= false; // See comment on |ignore_mouse_drag_| in header.
325 if (event
.IsLeftMouseButton() || event
.IsMiddleMouseButton())
326 UpdateLineEvent(event
, event
.IsLeftMouseButton());
330 bool OmniboxPopupContentsView::OnMouseDragged(
331 const ui::MouseEvent
& event
) {
332 if (event
.IsLeftMouseButton() || event
.IsMiddleMouseButton())
333 UpdateLineEvent(event
, !ignore_mouse_drag_
&& event
.IsLeftMouseButton());
337 void OmniboxPopupContentsView::OnMouseReleased(
338 const ui::MouseEvent
& event
) {
339 if (ignore_mouse_drag_
) {
340 OnMouseCaptureLost();
344 if (event
.IsOnlyMiddleMouseButton() || event
.IsOnlyLeftMouseButton()) {
345 OpenSelectedLine(event
, event
.IsOnlyLeftMouseButton() ? CURRENT_TAB
:
350 void OmniboxPopupContentsView::OnMouseCaptureLost() {
351 ignore_mouse_drag_
= false;
354 void OmniboxPopupContentsView::OnMouseMoved(
355 const ui::MouseEvent
& event
) {
356 model_
->SetHoveredLine(GetIndexForPoint(event
.location()));
359 void OmniboxPopupContentsView::OnMouseEntered(
360 const ui::MouseEvent
& event
) {
361 model_
->SetHoveredLine(GetIndexForPoint(event
.location()));
364 void OmniboxPopupContentsView::OnMouseExited(
365 const ui::MouseEvent
& event
) {
366 model_
->SetHoveredLine(OmniboxPopupModel::kNoMatch
);
369 void OmniboxPopupContentsView::OnGestureEvent(ui::GestureEvent
* event
) {
370 switch (event
->type()) {
371 case ui::ET_GESTURE_TAP_DOWN
:
372 case ui::ET_GESTURE_SCROLL_BEGIN
:
373 case ui::ET_GESTURE_SCROLL_UPDATE
:
374 UpdateLineEvent(*event
, true);
376 case ui::ET_GESTURE_TAP
:
377 case ui::ET_GESTURE_SCROLL_END
:
378 OpenSelectedLine(*event
, CURRENT_TAB
);
386 ////////////////////////////////////////////////////////////////////////////////
387 // OmniboxPopupContentsView, protected:
389 int OmniboxPopupContentsView::CalculatePopupHeight() {
390 DCHECK_GE(static_cast<size_t>(child_count()), model_
->result().size());
391 int popup_height
= 0;
392 for (size_t i
= 0; i
< model_
->result().size(); ++i
)
393 popup_height
+= child_at(i
)->GetPreferredSize().height();
395 // Add enough space on the top and bottom so it looks like there is the same
396 // amount of space between the text and the popup border as there is in the
397 // interior between each row of text.
399 // The * 2 accounts for vertical padding used at the top and bottom.
400 return popup_height
+
401 views::NonClientFrameView::kClientEdgeThickness
+ // Top border.
402 OmniboxResultView::kMinimumTextVerticalPadding
* 2 + // Padding.
403 bottom_shadow_
->height() - kBorderInterior
; // Bottom border.
406 OmniboxResultView
* OmniboxPopupContentsView::CreateResultView(
408 const gfx::FontList
& font_list
) {
409 return new OmniboxResultView(this, model_index
, location_bar_view_
,
413 ////////////////////////////////////////////////////////////////////////////////
414 // OmniboxPopupContentsView, views::View overrides, private:
416 const char* OmniboxPopupContentsView::GetClassName() const {
417 return "OmniboxPopupContentsView";
420 void OmniboxPopupContentsView::OnPaint(gfx::Canvas
* canvas
) {
423 gfx::Rect(0, 0, width(), views::NonClientFrameView::kClientEdgeThickness
),
424 ThemeProperties::GetDefaultColor(
425 ThemeProperties::COLOR_TOOLBAR_SEPARATOR
));
428 canvas
->TileImageInt(*bottom_shadow_
, 0, height() - bottom_shadow_
->height(),
429 width(), bottom_shadow_
->height());
432 void OmniboxPopupContentsView::PaintChildren(const ui::PaintContext
& context
) {
433 gfx::Rect contents_bounds
= GetContentsBounds();
434 contents_bounds
.Inset(0, views::NonClientFrameView::kClientEdgeThickness
, 0,
435 bottom_shadow_
->height() - kBorderInterior
);
437 ui::ClipTransformRecorder
clip_transform_recorder(context
);
438 clip_transform_recorder
.ClipRect(contents_bounds
);
440 ui::PaintRecorder
recorder(context
, size());
441 SkColor background_color
= result_view_at(0)->GetColor(
442 OmniboxResultView::NORMAL
, OmniboxResultView::BACKGROUND
);
443 recorder
.canvas()->DrawColor(background_color
);
445 View::PaintChildren(context
);
448 ////////////////////////////////////////////////////////////////////////////////
449 // OmniboxPopupContentsView, private:
451 views::View
* OmniboxPopupContentsView::TargetForRect(views::View
* root
,
452 const gfx::Rect
& rect
) {
453 CHECK_EQ(root
, this);
457 bool OmniboxPopupContentsView::HasMatchAt(size_t index
) const {
458 return index
< model_
->result().size();
461 const AutocompleteMatch
& OmniboxPopupContentsView::GetMatchAtIndex(
462 size_t index
) const {
463 return model_
->result().match_at(index
);
466 size_t OmniboxPopupContentsView::GetIndexForPoint(
467 const gfx::Point
& point
) {
468 if (!HitTestPoint(point
))
469 return OmniboxPopupModel::kNoMatch
;
471 int nb_match
= model_
->result().size();
472 DCHECK(nb_match
<= child_count());
473 for (int i
= 0; i
< nb_match
; ++i
) {
474 views::View
* child
= child_at(i
);
475 gfx::Point
point_in_child_coords(point
);
476 View::ConvertPointToTarget(this, child
, &point_in_child_coords
);
477 if (child
->visible() && child
->HitTestPoint(point_in_child_coords
))
480 return OmniboxPopupModel::kNoMatch
;
483 void OmniboxPopupContentsView::UpdateLineEvent(
484 const ui::LocatedEvent
& event
,
485 bool should_set_selected_line
) {
486 size_t index
= GetIndexForPoint(event
.location());
487 model_
->SetHoveredLine(index
);
488 if (HasMatchAt(index
) && should_set_selected_line
)
489 model_
->SetSelectedLine(index
, false, false);
492 void OmniboxPopupContentsView::OpenSelectedLine(
493 const ui::LocatedEvent
& event
,
494 WindowOpenDisposition disposition
) {
495 size_t index
= GetIndexForPoint(event
.location());
496 if (!HasMatchAt(index
))
498 omnibox_view_
->OpenMatch(model_
->result().match_at(index
), disposition
,
499 GURL(), base::string16(), index
);
502 OmniboxResultView
* OmniboxPopupContentsView::result_view_at(size_t i
) {
503 return static_cast<OmniboxResultView
*>(child_at(static_cast<int>(i
)));