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/views/location_bar/location_bar_view.h"
12 #include "chrome/browser/ui/views/omnibox/omnibox_result_view.h"
13 #include "components/omnibox/browser/omnibox_view.h"
14 #include "grit/theme_resources.h"
15 #include "ui/base/resource/material_design/material_design_controller.h"
16 #include "ui/base/theme_provider.h"
17 #include "ui/compositor/clip_transform_recorder.h"
18 #include "ui/compositor/paint_recorder.h"
19 #include "ui/gfx/canvas.h"
20 #include "ui/gfx/image/image.h"
21 #include "ui/gfx/path.h"
22 #include "ui/resources/grit/ui_resources.h"
23 #include "ui/views/controls/image_view.h"
24 #include "ui/views/resources/grit/views_resources.h"
25 #include "ui/views/view_targeter.h"
26 #include "ui/views/widget/widget.h"
27 #include "ui/views/window/non_client_view.h"
29 class OmniboxPopupContentsView::AutocompletePopupWidget
30 : public views::Widget
,
31 public base::SupportsWeakPtr
<AutocompletePopupWidget
> {
33 AutocompletePopupWidget() {}
34 ~AutocompletePopupWidget() override
{}
37 DISALLOW_COPY_AND_ASSIGN(AutocompletePopupWidget
);
40 ////////////////////////////////////////////////////////////////////////////////
41 // OmniboxPopupContentsView, public:
43 OmniboxPopupView
* OmniboxPopupContentsView::Create(
44 const gfx::FontList
& font_list
,
45 OmniboxView
* omnibox_view
,
46 OmniboxEditModel
* edit_model
,
47 LocationBarView
* location_bar_view
) {
48 OmniboxPopupContentsView
* view
= NULL
;
49 view
= new OmniboxPopupContentsView(
50 font_list
, omnibox_view
, edit_model
, location_bar_view
);
55 OmniboxPopupContentsView::OmniboxPopupContentsView(
56 const gfx::FontList
& font_list
,
57 OmniboxView
* omnibox_view
,
58 OmniboxEditModel
* edit_model
,
59 LocationBarView
* location_bar_view
)
60 : model_(new OmniboxPopupModel(this, edit_model
)),
61 omnibox_view_(omnibox_view
),
62 location_bar_view_(location_bar_view
),
63 font_list_(font_list
),
64 ignore_mouse_drag_(false),
65 size_animation_(this),
68 // The contents is owned by the LocationBarView.
69 set_owned_by_client();
71 ui::ThemeProvider
* theme
= location_bar_view_
->GetThemeProvider();
72 if (ui::MaterialDesignController::IsModeMaterial()) {
73 top_shadow_
= theme
->GetImageSkiaNamed(IDR_OMNIBOX_DROPDOWN_SHADOW_TOP
);
75 theme
->GetImageSkiaNamed(IDR_OMNIBOX_DROPDOWN_SHADOW_BOTTOM
);
77 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 ui::ThemeProvider
* theme_provider
= location_bar_view_
->GetThemeProvider();
119 const int min_vertical_padding
= theme_provider
->GetDisplayProperty(
120 ThemeProperties::PROPERTY_OMNIBOX_DROPDOWN_MIN_TEXT_VERTICAL_PADDING
);
122 gfx::Rect contents_rect
= GetContentsBounds();
124 0, views::NonClientFrameView::kClientEdgeThickness
+ min_vertical_padding
,
125 0, min_vertical_padding
);
127 // In the non-material dropdown, the colored/clickable regions within the
128 // dropdown are only as wide as the location bar. In the material version,
129 // these are full width, and OmniboxResultView instead insets the icons/text
130 // inside to be aligned with the location bar.
131 if (!ui::MaterialDesignController::IsModeMaterial())
132 contents_rect
.Inset(start_margin_
, 0, end_margin_
, 0);
134 int top
= contents_rect
.y();
135 for (size_t i
= 0; i
< AutocompleteResult::kMaxMatches
; ++i
) {
136 View
* v
= child_at(i
);
138 v
->SetBounds(contents_rect
.x(), top
, contents_rect
.width(),
139 v
->GetPreferredSize().height());
140 top
= v
->bounds().bottom();
145 ////////////////////////////////////////////////////////////////////////////////
146 // OmniboxPopupContentsView, OmniboxPopupView overrides:
148 bool OmniboxPopupContentsView::IsOpen() const {
149 return popup_
!= NULL
;
152 void OmniboxPopupContentsView::InvalidateLine(size_t line
) {
153 OmniboxResultView
* result
= result_view_at(line
);
154 result
->Invalidate();
156 if (HasMatchAt(line
) && GetMatchAtIndex(line
).associated_keyword
.get()) {
157 result
->ShowKeyword(IsSelectedIndex(line
) &&
158 model_
->selected_line_state() == OmniboxPopupModel::KEYWORD
);
162 void OmniboxPopupContentsView::UpdatePopupAppearance() {
163 if (model_
->result().empty() || omnibox_view_
->IsImeShowingPopup()) {
164 // No matches or the IME is showing a popup window which may overlap
165 // the omnibox popup window. Close any existing popup.
166 if (popup_
!= NULL
) {
167 size_animation_
.Stop();
169 // NOTE: Do NOT use CloseNow() here, as we may be deep in a callstack
170 // triggered by the popup receiving a message (e.g. LBUTTONUP), and
171 // destroying the popup would cause us to read garbage when we unwind back
173 popup_
->Close(); // This will eventually delete the popup.
179 // Update the match cached by each row, in the process of doing so make sure
180 // we have enough row views.
181 const size_t result_size
= model_
->result().size();
182 max_match_contents_width_
= 0;
183 for (size_t i
= 0; i
< result_size
; ++i
) {
184 OmniboxResultView
* view
= result_view_at(i
);
185 const AutocompleteMatch
& match
= GetMatchAtIndex(i
);
186 view
->SetMatch(match
);
187 view
->SetVisible(true);
188 if (match
.answer
&& !model_
->answer_bitmap().isNull()) {
189 view
->SetAnswerImage(
190 gfx::ImageSkia::CreateFrom1xBitmap(model_
->answer_bitmap()));
192 if (match
.type
== AutocompleteMatchType::SEARCH_SUGGEST_TAIL
) {
193 max_match_contents_width_
= std::max(
194 max_match_contents_width_
, view
->GetMatchContentsWidth());
198 for (size_t i
= result_size
; i
< AutocompleteResult::kMaxMatches
; ++i
)
199 child_at(i
)->SetVisible(false);
201 // In non-material mode, we want the popup to appear as if it's overlaying
202 // the top of the page content, i.e., is flush against the client edge at the
203 // bottom of the toolbar. However, if the bookmarks bar is attached, we want
204 // to draw over it (so as not to push the results below it), but that means
205 // the toolbar won't be drawing a client edge separating itself from the
206 // popup. So we unconditionally overlap the toolbar by the thickness of the
207 // client edge and draw our own edge (see OnPaint()), which fixes the
208 // attached bookmark bar case without breaking the other case.
209 int top_edge_overlap
= views::NonClientFrameView::kClientEdgeThickness
;
210 if (ui::MaterialDesignController::IsModeMaterial()) {
211 // In material mode, we cover the bookmark bar similarly, but instead of
212 // appearing below the client edge, we want the popup to appear to overlay
213 // the bottom of the toolbar. So instead of drawing a client edge atop the
214 // popup, we shift the popup to completely cover the client edge, and then
215 // draw an additional semitransparent shadow above that. So the total
216 // overlap necessary is the client edge thickness plus the shadow height.
217 top_edge_overlap
+= top_shadow_
->height();
220 gfx::Point top_left_screen_coord
;
222 location_bar_view_
->GetOmniboxPopupPositioningInfo(
223 &top_left_screen_coord
, &width
, &start_margin_
,
224 &end_margin_
, top_edge_overlap
);
225 gfx::Rect
new_target_bounds(top_left_screen_coord
,
226 gfx::Size(width
, CalculatePopupHeight()));
228 // If we're animating and our target height changes, reset the animation.
229 // NOTE: If we just reset blindly on _every_ update, then when the user types
230 // rapidly we could get "stuck" trying repeatedly to animate shrinking by the
231 // last few pixels to get to one visible result.
232 if (new_target_bounds
.height() != target_bounds_
.height())
233 size_animation_
.Reset();
234 target_bounds_
= new_target_bounds
;
236 if (popup_
== NULL
) {
237 views::Widget
* popup_parent
= location_bar_view_
->GetWidget();
239 // If the popup is currently closed, we need to create it.
240 popup_
= (new AutocompletePopupWidget
)->AsWeakPtr();
241 // On Windows use TYPE_MENU to ensure that this window uses the software
242 // compositor which avoids the UI thread blocking issue during command
243 // buffer creation. We can revert this change once http://crbug.com/125248
246 views::Widget::InitParams
params(views::Widget::InitParams::TYPE_MENU
);
247 // The menu style assumes a top most window. We don't want that in this
249 params
.keep_on_top
= false;
251 views::Widget::InitParams
params(views::Widget::InitParams::TYPE_POPUP
);
253 params
.opacity
= views::Widget::InitParams::TRANSLUCENT_WINDOW
;
254 params
.parent
= popup_parent
->GetNativeView();
255 params
.bounds
= GetPopupBounds();
256 params
.context
= popup_parent
->GetNativeWindow();
257 popup_
->Init(params
);
258 // Third-party software such as DigitalPersona identity verification can
259 // hook the underlying window creation methods and use SendMessage to
260 // synchronously change focus/activation, resulting in the popup being
261 // destroyed by the time control returns here. Bail out in this case to
262 // avoid a NULL dereference.
265 popup_
->SetVisibilityAnimationTransition(views::Widget::ANIMATE_NONE
);
266 popup_
->SetContentsView(this);
267 popup_
->StackAbove(omnibox_view_
->GetRelativeWindowForPopup());
269 // For some IMEs GetRelativeWindowForPopup triggers the omnibox to lose
270 // focus, thereby closing (and destroying) the popup.
271 // TODO(sky): this won't be needed once we close the omnibox on input
275 popup_
->ShowInactive();
277 // Animate the popup shrinking, but don't animate growing larger since that
278 // would make the popup feel less responsive.
279 start_bounds_
= GetWidget()->GetWindowBoundsInScreen();
280 if (target_bounds_
.height() < start_bounds_
.height())
281 size_animation_
.Show();
283 start_bounds_
= target_bounds_
;
284 popup_
->SetBounds(GetPopupBounds());
290 gfx::Rect
OmniboxPopupContentsView::GetTargetBounds() {
291 return target_bounds_
;
294 void OmniboxPopupContentsView::PaintUpdatesNow() {
295 // TODO(beng): remove this from the interface.
298 void OmniboxPopupContentsView::OnDragCanceled() {
299 ignore_mouse_drag_
= true;
302 ////////////////////////////////////////////////////////////////////////////////
303 // OmniboxPopupContentsView, OmniboxResultViewModel implementation:
305 bool OmniboxPopupContentsView::IsSelectedIndex(size_t index
) const {
306 return index
== model_
->selected_line();
309 bool OmniboxPopupContentsView::IsHoveredIndex(size_t index
) const {
310 return index
== model_
->hovered_line();
313 gfx::Image
OmniboxPopupContentsView::GetIconIfExtensionMatch(
314 size_t index
) const {
315 if (!HasMatchAt(index
))
317 return model_
->GetIconIfExtensionMatch(GetMatchAtIndex(index
));
320 bool OmniboxPopupContentsView::IsStarredMatch(
321 const AutocompleteMatch
& match
) const {
322 return model_
->IsStarredMatch(match
);
325 ////////////////////////////////////////////////////////////////////////////////
326 // OmniboxPopupContentsView, AnimationDelegate implementation:
328 void OmniboxPopupContentsView::AnimationProgressed(
329 const gfx::Animation
* animation
) {
330 // We should only be running the animation when the popup is already visible.
331 DCHECK(popup_
!= NULL
);
332 popup_
->SetBounds(GetPopupBounds());
335 ////////////////////////////////////////////////////////////////////////////////
336 // OmniboxPopupContentsView, views::View overrides:
338 void OmniboxPopupContentsView::Layout() {
339 // Size our children to the available content area.
342 // We need to manually schedule a paint here since we are a layered window and
343 // won't implicitly require painting until we ask for one.
347 views::View
* OmniboxPopupContentsView::GetTooltipHandlerForPoint(
348 const gfx::Point
& point
) {
352 bool OmniboxPopupContentsView::OnMousePressed(
353 const ui::MouseEvent
& event
) {
354 ignore_mouse_drag_
= false; // See comment on |ignore_mouse_drag_| in header.
355 if (event
.IsLeftMouseButton() || event
.IsMiddleMouseButton())
356 UpdateLineEvent(event
, event
.IsLeftMouseButton());
360 bool OmniboxPopupContentsView::OnMouseDragged(
361 const ui::MouseEvent
& event
) {
362 if (event
.IsLeftMouseButton() || event
.IsMiddleMouseButton())
363 UpdateLineEvent(event
, !ignore_mouse_drag_
&& event
.IsLeftMouseButton());
367 void OmniboxPopupContentsView::OnMouseReleased(
368 const ui::MouseEvent
& event
) {
369 if (ignore_mouse_drag_
) {
370 OnMouseCaptureLost();
374 if (event
.IsOnlyMiddleMouseButton() || event
.IsOnlyLeftMouseButton()) {
375 OpenSelectedLine(event
, event
.IsOnlyLeftMouseButton() ? CURRENT_TAB
:
380 void OmniboxPopupContentsView::OnMouseCaptureLost() {
381 ignore_mouse_drag_
= false;
384 void OmniboxPopupContentsView::OnMouseMoved(
385 const ui::MouseEvent
& event
) {
386 model_
->SetHoveredLine(GetIndexForPoint(event
.location()));
389 void OmniboxPopupContentsView::OnMouseEntered(
390 const ui::MouseEvent
& event
) {
391 model_
->SetHoveredLine(GetIndexForPoint(event
.location()));
394 void OmniboxPopupContentsView::OnMouseExited(
395 const ui::MouseEvent
& event
) {
396 model_
->SetHoveredLine(OmniboxPopupModel::kNoMatch
);
399 void OmniboxPopupContentsView::OnGestureEvent(ui::GestureEvent
* event
) {
400 switch (event
->type()) {
401 case ui::ET_GESTURE_TAP_DOWN
:
402 case ui::ET_GESTURE_SCROLL_BEGIN
:
403 case ui::ET_GESTURE_SCROLL_UPDATE
:
404 UpdateLineEvent(*event
, true);
406 case ui::ET_GESTURE_TAP
:
407 case ui::ET_GESTURE_SCROLL_END
:
408 OpenSelectedLine(*event
, CURRENT_TAB
);
416 ////////////////////////////////////////////////////////////////////////////////
417 // OmniboxPopupContentsView, protected:
419 int OmniboxPopupContentsView::CalculatePopupHeight() {
420 DCHECK_GE(static_cast<size_t>(child_count()), model_
->result().size());
421 int popup_height
= 0;
422 for (size_t i
= 0; i
< model_
->result().size(); ++i
)
423 popup_height
+= child_at(i
)->GetPreferredSize().height();
425 ui::ThemeProvider
* theme_provider
= location_bar_view_
->GetThemeProvider();
426 const int min_text_vertical_padding
= theme_provider
->GetDisplayProperty(
427 ThemeProperties::PROPERTY_OMNIBOX_DROPDOWN_MIN_TEXT_VERTICAL_PADDING
);
428 const int border_interior
= theme_provider
->GetDisplayProperty(
429 ThemeProperties::PROPERTY_OMNIBOX_DROPDOWN_BORDER_INTERIOR
);
431 // Add enough space on the top and bottom so it looks like there is the same
432 // amount of space between the text and the popup border as there is in the
433 // interior between each row of text.
435 // The * 2 accounts for vertical padding used at the top and bottom.
436 return popup_height
+
437 views::NonClientFrameView::kClientEdgeThickness
+ // Top border.
438 min_text_vertical_padding
* 2 + // Padding.
439 bottom_shadow_
->height() - border_interior
; // Bottom border.
442 OmniboxResultView
* OmniboxPopupContentsView::CreateResultView(
444 const gfx::FontList
& font_list
) {
445 return new OmniboxResultView(this, model_index
, location_bar_view_
,
449 ////////////////////////////////////////////////////////////////////////////////
450 // OmniboxPopupContentsView, views::View overrides, private:
452 const char* OmniboxPopupContentsView::GetClassName() const {
453 return "OmniboxPopupContentsView";
456 void OmniboxPopupContentsView::OnPaint(gfx::Canvas
* canvas
) {
458 if (ui::MaterialDesignController::IsModeMaterial()) {
459 canvas
->TileImageInt(*top_shadow_
, 0, 0, width(), top_shadow_
->height());
462 gfx::Rect(0, 0, width(),
463 views::NonClientFrameView::kClientEdgeThickness
),
464 ThemeProperties::GetDefaultColor(
465 ThemeProperties::COLOR_TOOLBAR_SEPARATOR
));
469 canvas
->TileImageInt(*bottom_shadow_
, 0, height() - bottom_shadow_
->height(),
470 width(), bottom_shadow_
->height());
473 void OmniboxPopupContentsView::PaintChildren(const ui::PaintContext
& context
) {
474 gfx::Rect contents_bounds
= GetContentsBounds();
475 ui::ThemeProvider
* theme_provider
= location_bar_view_
->GetThemeProvider();
476 const int border_interior
= theme_provider
->GetDisplayProperty(
477 ThemeProperties::PROPERTY_OMNIBOX_DROPDOWN_BORDER_INTERIOR
);
479 contents_bounds
.Inset(0, views::NonClientFrameView::kClientEdgeThickness
, 0,
480 bottom_shadow_
->height() - border_interior
);
482 ui::ClipTransformRecorder
clip_transform_recorder(context
);
483 clip_transform_recorder
.ClipRect(contents_bounds
);
485 ui::PaintRecorder
recorder(context
, size());
486 SkColor background_color
= result_view_at(0)->GetColor(
487 OmniboxResultView::NORMAL
, OmniboxResultView::BACKGROUND
);
488 recorder
.canvas()->DrawColor(background_color
);
490 View::PaintChildren(context
);
493 ////////////////////////////////////////////////////////////////////////////////
494 // OmniboxPopupContentsView, private:
496 views::View
* OmniboxPopupContentsView::TargetForRect(views::View
* root
,
497 const gfx::Rect
& rect
) {
498 CHECK_EQ(root
, this);
502 bool OmniboxPopupContentsView::HasMatchAt(size_t index
) const {
503 return index
< model_
->result().size();
506 const AutocompleteMatch
& OmniboxPopupContentsView::GetMatchAtIndex(
507 size_t index
) const {
508 return model_
->result().match_at(index
);
511 size_t OmniboxPopupContentsView::GetIndexForPoint(
512 const gfx::Point
& point
) {
513 if (!HitTestPoint(point
))
514 return OmniboxPopupModel::kNoMatch
;
516 int nb_match
= model_
->result().size();
517 DCHECK(nb_match
<= child_count());
518 for (int i
= 0; i
< nb_match
; ++i
) {
519 views::View
* child
= child_at(i
);
520 gfx::Point
point_in_child_coords(point
);
521 View::ConvertPointToTarget(this, child
, &point_in_child_coords
);
522 if (child
->visible() && child
->HitTestPoint(point_in_child_coords
))
525 return OmniboxPopupModel::kNoMatch
;
528 void OmniboxPopupContentsView::UpdateLineEvent(
529 const ui::LocatedEvent
& event
,
530 bool should_set_selected_line
) {
531 size_t index
= GetIndexForPoint(event
.location());
532 model_
->SetHoveredLine(index
);
533 if (HasMatchAt(index
) && should_set_selected_line
)
534 model_
->SetSelectedLine(index
, false, false);
537 void OmniboxPopupContentsView::OpenSelectedLine(
538 const ui::LocatedEvent
& event
,
539 WindowOpenDisposition disposition
) {
540 size_t index
= GetIndexForPoint(event
.location());
541 if (!HasMatchAt(index
))
543 omnibox_view_
->OpenMatch(model_
->result().match_at(index
), disposition
,
544 GURL(), base::string16(), index
);
547 OmniboxResultView
* OmniboxPopupContentsView::result_view_at(size_t i
) {
548 return static_cast<OmniboxResultView
*>(child_at(static_cast<int>(i
)));