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 top_shadow_
= theme
->GetImageSkiaNamed(IDR_OMNIBOX_DROPDOWN_SHADOW_TOP
);
73 int bottom_shadow_asset
= ui::MaterialDesignController::IsModeMaterial() ?
74 IDR_OMNIBOX_DROPDOWN_SHADOW_BOTTOM
: IDR_BUBBLE_B
;
75 bottom_shadow_
= theme
->GetImageSkiaNamed(bottom_shadow_asset
);
78 scoped_ptr
<views::ViewTargeter
>(new views::ViewTargeter(this)));
81 void OmniboxPopupContentsView::Init() {
82 // This can't be done in the constructor as at that point we aren't
83 // necessarily our final class yet, and we may have subclasses
84 // overriding CreateResultView.
85 for (size_t i
= 0; i
< AutocompleteResult::kMaxMatches
; ++i
) {
86 OmniboxResultView
* result_view
= CreateResultView(i
, font_list_
);
87 result_view
->SetVisible(false);
88 AddChildViewAt(result_view
, static_cast<int>(i
));
92 OmniboxPopupContentsView::~OmniboxPopupContentsView() {
93 // We don't need to do anything with |popup_| here. The OS either has already
94 // closed the window, in which case it's been deleted, or it will soon, in
95 // which case there's nothing we need to do.
98 gfx::Rect
OmniboxPopupContentsView::GetPopupBounds() const {
99 if (!size_animation_
.is_animating())
100 return target_bounds_
;
102 gfx::Rect current_frame_bounds
= start_bounds_
;
103 int total_height_delta
= target_bounds_
.height() - start_bounds_
.height();
104 // Round |current_height_delta| instead of truncating so we won't leave single
105 // white pixels at the bottom of the popup as long when animating very small
106 // height differences.
107 int current_height_delta
= static_cast<int>(
108 size_animation_
.GetCurrentValue() * total_height_delta
- 0.5);
109 current_frame_bounds
.set_height(
110 current_frame_bounds
.height() + current_height_delta
);
111 return current_frame_bounds
;
114 void OmniboxPopupContentsView::LayoutChildren() {
115 ui::ThemeProvider
* theme_provider
= location_bar_view_
->GetThemeProvider();
116 const int min_vertical_padding
= theme_provider
->GetDisplayProperty(
117 ThemeProperties::PROPERTY_OMNIBOX_DROPDOWN_MIN_TEXT_VERTICAL_PADDING
);
119 gfx::Rect contents_rect
= GetContentsBounds();
121 0, views::NonClientFrameView::kClientEdgeThickness
+ min_vertical_padding
,
122 0, min_vertical_padding
);
124 // In the non-material dropdown, the colored/clickable regions within the
125 // dropdown are only as wide as the location bar. In the material version,
126 // these are full width, and OmniboxResultView instead insets the icons/text
127 // inside to be aligned with the location bar.
128 if (!ui::MaterialDesignController::IsModeMaterial())
129 contents_rect
.Inset(start_margin_
, 0, end_margin_
, 0);
131 int top
= contents_rect
.y();
132 for (size_t i
= 0; i
< AutocompleteResult::kMaxMatches
; ++i
) {
133 View
* v
= child_at(i
);
135 v
->SetBounds(contents_rect
.x(), top
, contents_rect
.width(),
136 v
->GetPreferredSize().height());
137 top
= v
->bounds().bottom();
142 ////////////////////////////////////////////////////////////////////////////////
143 // OmniboxPopupContentsView, OmniboxPopupView overrides:
145 bool OmniboxPopupContentsView::IsOpen() const {
146 return popup_
!= NULL
;
149 void OmniboxPopupContentsView::InvalidateLine(size_t line
) {
150 OmniboxResultView
* result
= result_view_at(line
);
151 result
->Invalidate();
153 if (HasMatchAt(line
) && GetMatchAtIndex(line
).associated_keyword
.get()) {
154 result
->ShowKeyword(IsSelectedIndex(line
) &&
155 model_
->selected_line_state() == OmniboxPopupModel::KEYWORD
);
159 void OmniboxPopupContentsView::UpdatePopupAppearance() {
160 if (model_
->result().empty() || omnibox_view_
->IsImeShowingPopup()) {
161 // No matches or the IME is showing a popup window which may overlap
162 // the omnibox popup window. Close any existing popup.
163 if (popup_
!= NULL
) {
164 size_animation_
.Stop();
166 // NOTE: Do NOT use CloseNow() here, as we may be deep in a callstack
167 // triggered by the popup receiving a message (e.g. LBUTTONUP), and
168 // destroying the popup would cause us to read garbage when we unwind back
170 popup_
->Close(); // This will eventually delete the popup.
176 // Update the match cached by each row, in the process of doing so make sure
177 // we have enough row views.
178 const size_t result_size
= model_
->result().size();
179 max_match_contents_width_
= 0;
180 for (size_t i
= 0; i
< result_size
; ++i
) {
181 OmniboxResultView
* view
= result_view_at(i
);
182 const AutocompleteMatch
& match
= GetMatchAtIndex(i
);
183 view
->SetMatch(match
);
184 view
->SetVisible(true);
185 if (match
.answer
&& !model_
->answer_bitmap().isNull()) {
186 view
->SetAnswerImage(
187 gfx::ImageSkia::CreateFrom1xBitmap(model_
->answer_bitmap()));
189 if (match
.type
== AutocompleteMatchType::SEARCH_SUGGEST_TAIL
) {
190 max_match_contents_width_
= std::max(
191 max_match_contents_width_
, view
->GetMatchContentsWidth());
195 for (size_t i
= result_size
; i
< AutocompleteResult::kMaxMatches
; ++i
)
196 child_at(i
)->SetVisible(false);
198 // In non-material mode, we want the popup to appear as if it's overlaying
199 // the top of the page content, i.e., is flush against the client edge at the
200 // bottom of the toolbar. However, if the bookmarks bar is attached, we want
201 // to draw over it (so as not to push the results below it), but that means
202 // the toolbar won't be drawing a client edge separating itself from the
203 // popup. So we unconditionally overlap the toolbar by the thickness of the
204 // client edge and draw our own edge (see OnPaint()), which fixes the
205 // attached bookmark bar case without breaking the other case.
206 int top_edge_overlap
= views::NonClientFrameView::kClientEdgeThickness
;
207 if (ui::MaterialDesignController::IsModeMaterial()) {
208 // In material mode, we cover the bookmark bar similarly, but instead of
209 // appearing below the client edge, we want the popup to appear to overlay
210 // the bottom of the toolbar. So instead of drawing a client edge atop the
211 // popup, we shift the popup to completely cover the client edge, and then
212 // draw an additional semitransparent shadow above that. So the total
213 // overlap necessary is the client edge thickness plus the shadow height.
214 top_edge_overlap
+= top_shadow_
->height();
217 gfx::Point top_left_screen_coord
;
219 location_bar_view_
->GetOmniboxPopupPositioningInfo(
220 &top_left_screen_coord
, &width
, &start_margin_
,
221 &end_margin_
, top_edge_overlap
);
222 gfx::Rect
new_target_bounds(top_left_screen_coord
,
223 gfx::Size(width
, CalculatePopupHeight()));
225 // If we're animating and our target height changes, reset the animation.
226 // NOTE: If we just reset blindly on _every_ update, then when the user types
227 // rapidly we could get "stuck" trying repeatedly to animate shrinking by the
228 // last few pixels to get to one visible result.
229 if (new_target_bounds
.height() != target_bounds_
.height())
230 size_animation_
.Reset();
231 target_bounds_
= new_target_bounds
;
233 if (popup_
== NULL
) {
234 views::Widget
* popup_parent
= location_bar_view_
->GetWidget();
236 // If the popup is currently closed, we need to create it.
237 popup_
= (new AutocompletePopupWidget
)->AsWeakPtr();
238 // On Windows use TYPE_MENU to ensure that this window uses the software
239 // compositor which avoids the UI thread blocking issue during command
240 // buffer creation. We can revert this change once http://crbug.com/125248
243 views::Widget::InitParams
params(views::Widget::InitParams::TYPE_MENU
);
244 // The menu style assumes a top most window. We don't want that in this
246 params
.keep_on_top
= false;
248 views::Widget::InitParams
params(views::Widget::InitParams::TYPE_POPUP
);
250 params
.opacity
= views::Widget::InitParams::TRANSLUCENT_WINDOW
;
251 params
.parent
= popup_parent
->GetNativeView();
252 params
.bounds
= GetPopupBounds();
253 params
.context
= popup_parent
->GetNativeWindow();
254 popup_
->Init(params
);
255 // Third-party software such as DigitalPersona identity verification can
256 // hook the underlying window creation methods and use SendMessage to
257 // synchronously change focus/activation, resulting in the popup being
258 // destroyed by the time control returns here. Bail out in this case to
259 // avoid a NULL dereference.
262 popup_
->SetVisibilityAnimationTransition(views::Widget::ANIMATE_NONE
);
263 popup_
->SetContentsView(this);
264 popup_
->StackAbove(omnibox_view_
->GetRelativeWindowForPopup());
266 // For some IMEs GetRelativeWindowForPopup triggers the omnibox to lose
267 // focus, thereby closing (and destroying) the popup.
268 // TODO(sky): this won't be needed once we close the omnibox on input
272 popup_
->ShowInactive();
274 // Animate the popup shrinking, but don't animate growing larger since that
275 // would make the popup feel less responsive.
276 start_bounds_
= GetWidget()->GetWindowBoundsInScreen();
277 if (target_bounds_
.height() < start_bounds_
.height())
278 size_animation_
.Show();
280 start_bounds_
= target_bounds_
;
281 popup_
->SetBounds(GetPopupBounds());
287 gfx::Rect
OmniboxPopupContentsView::GetTargetBounds() {
288 return target_bounds_
;
291 void OmniboxPopupContentsView::PaintUpdatesNow() {
292 // TODO(beng): remove this from the interface.
295 void OmniboxPopupContentsView::OnDragCanceled() {
296 ignore_mouse_drag_
= true;
299 ////////////////////////////////////////////////////////////////////////////////
300 // OmniboxPopupContentsView, OmniboxResultViewModel implementation:
302 bool OmniboxPopupContentsView::IsSelectedIndex(size_t index
) const {
303 return index
== model_
->selected_line();
306 bool OmniboxPopupContentsView::IsHoveredIndex(size_t index
) const {
307 return index
== model_
->hovered_line();
310 gfx::Image
OmniboxPopupContentsView::GetIconIfExtensionMatch(
311 size_t index
) const {
312 if (!HasMatchAt(index
))
314 return model_
->GetIconIfExtensionMatch(GetMatchAtIndex(index
));
317 bool OmniboxPopupContentsView::IsStarredMatch(
318 const AutocompleteMatch
& match
) const {
319 return model_
->IsStarredMatch(match
);
322 ////////////////////////////////////////////////////////////////////////////////
323 // OmniboxPopupContentsView, AnimationDelegate implementation:
325 void OmniboxPopupContentsView::AnimationProgressed(
326 const gfx::Animation
* animation
) {
327 // We should only be running the animation when the popup is already visible.
328 DCHECK(popup_
!= NULL
);
329 popup_
->SetBounds(GetPopupBounds());
332 ////////////////////////////////////////////////////////////////////////////////
333 // OmniboxPopupContentsView, views::View overrides:
335 void OmniboxPopupContentsView::Layout() {
336 // Size our children to the available content area.
339 // We need to manually schedule a paint here since we are a layered window and
340 // won't implicitly require painting until we ask for one.
344 views::View
* OmniboxPopupContentsView::GetTooltipHandlerForPoint(
345 const gfx::Point
& point
) {
349 bool OmniboxPopupContentsView::OnMousePressed(
350 const ui::MouseEvent
& event
) {
351 ignore_mouse_drag_
= false; // See comment on |ignore_mouse_drag_| in header.
352 if (event
.IsLeftMouseButton() || event
.IsMiddleMouseButton())
353 UpdateLineEvent(event
, event
.IsLeftMouseButton());
357 bool OmniboxPopupContentsView::OnMouseDragged(
358 const ui::MouseEvent
& event
) {
359 if (event
.IsLeftMouseButton() || event
.IsMiddleMouseButton())
360 UpdateLineEvent(event
, !ignore_mouse_drag_
&& event
.IsLeftMouseButton());
364 void OmniboxPopupContentsView::OnMouseReleased(
365 const ui::MouseEvent
& event
) {
366 if (ignore_mouse_drag_
) {
367 OnMouseCaptureLost();
371 if (event
.IsOnlyMiddleMouseButton() || event
.IsOnlyLeftMouseButton()) {
372 OpenSelectedLine(event
, event
.IsOnlyLeftMouseButton() ? CURRENT_TAB
:
377 void OmniboxPopupContentsView::OnMouseCaptureLost() {
378 ignore_mouse_drag_
= false;
381 void OmniboxPopupContentsView::OnMouseMoved(
382 const ui::MouseEvent
& event
) {
383 model_
->SetHoveredLine(GetIndexForPoint(event
.location()));
386 void OmniboxPopupContentsView::OnMouseEntered(
387 const ui::MouseEvent
& event
) {
388 model_
->SetHoveredLine(GetIndexForPoint(event
.location()));
391 void OmniboxPopupContentsView::OnMouseExited(
392 const ui::MouseEvent
& event
) {
393 model_
->SetHoveredLine(OmniboxPopupModel::kNoMatch
);
396 void OmniboxPopupContentsView::OnGestureEvent(ui::GestureEvent
* event
) {
397 switch (event
->type()) {
398 case ui::ET_GESTURE_TAP_DOWN
:
399 case ui::ET_GESTURE_SCROLL_BEGIN
:
400 case ui::ET_GESTURE_SCROLL_UPDATE
:
401 UpdateLineEvent(*event
, true);
403 case ui::ET_GESTURE_TAP
:
404 case ui::ET_GESTURE_SCROLL_END
:
405 OpenSelectedLine(*event
, CURRENT_TAB
);
413 ////////////////////////////////////////////////////////////////////////////////
414 // OmniboxPopupContentsView, protected:
416 int OmniboxPopupContentsView::CalculatePopupHeight() {
417 DCHECK_GE(static_cast<size_t>(child_count()), model_
->result().size());
418 int popup_height
= 0;
419 for (size_t i
= 0; i
< model_
->result().size(); ++i
)
420 popup_height
+= child_at(i
)->GetPreferredSize().height();
422 ui::ThemeProvider
* theme_provider
= location_bar_view_
->GetThemeProvider();
423 const int min_text_vertical_padding
= theme_provider
->GetDisplayProperty(
424 ThemeProperties::PROPERTY_OMNIBOX_DROPDOWN_MIN_TEXT_VERTICAL_PADDING
);
425 const int border_interior
= theme_provider
->GetDisplayProperty(
426 ThemeProperties::PROPERTY_OMNIBOX_DROPDOWN_BORDER_INTERIOR
);
428 // Add enough space on the top and bottom so it looks like there is the same
429 // amount of space between the text and the popup border as there is in the
430 // interior between each row of text.
432 // The * 2 accounts for vertical padding used at the top and bottom.
433 return popup_height
+
434 views::NonClientFrameView::kClientEdgeThickness
+ // Top border.
435 min_text_vertical_padding
* 2 + // Padding.
436 bottom_shadow_
->height() - border_interior
; // Bottom border.
439 OmniboxResultView
* OmniboxPopupContentsView::CreateResultView(
441 const gfx::FontList
& font_list
) {
442 return new OmniboxResultView(this, model_index
, location_bar_view_
,
446 ////////////////////////////////////////////////////////////////////////////////
447 // OmniboxPopupContentsView, views::View overrides, private:
449 const char* OmniboxPopupContentsView::GetClassName() const {
450 return "OmniboxPopupContentsView";
453 void OmniboxPopupContentsView::OnPaint(gfx::Canvas
* canvas
) {
455 if (ui::MaterialDesignController::IsModeMaterial()) {
456 canvas
->TileImageInt(*top_shadow_
, 0, 0, width(), top_shadow_
->height());
459 gfx::Rect(0, 0, width(),
460 views::NonClientFrameView::kClientEdgeThickness
),
461 ThemeProperties::GetDefaultColor(
462 ThemeProperties::COLOR_TOOLBAR_SEPARATOR
));
466 canvas
->TileImageInt(*bottom_shadow_
, 0, height() - bottom_shadow_
->height(),
467 width(), bottom_shadow_
->height());
470 void OmniboxPopupContentsView::PaintChildren(const ui::PaintContext
& context
) {
471 gfx::Rect contents_bounds
= GetContentsBounds();
472 ui::ThemeProvider
* theme_provider
= location_bar_view_
->GetThemeProvider();
473 const int border_interior
= theme_provider
->GetDisplayProperty(
474 ThemeProperties::PROPERTY_OMNIBOX_DROPDOWN_BORDER_INTERIOR
);
476 contents_bounds
.Inset(0, views::NonClientFrameView::kClientEdgeThickness
, 0,
477 bottom_shadow_
->height() - border_interior
);
479 ui::ClipTransformRecorder
clip_transform_recorder(context
);
480 clip_transform_recorder
.ClipRect(contents_bounds
);
482 ui::PaintRecorder
recorder(context
, size());
483 SkColor background_color
= result_view_at(0)->GetColor(
484 OmniboxResultView::NORMAL
, OmniboxResultView::BACKGROUND
);
485 recorder
.canvas()->DrawColor(background_color
);
487 View::PaintChildren(context
);
490 ////////////////////////////////////////////////////////////////////////////////
491 // OmniboxPopupContentsView, private:
493 views::View
* OmniboxPopupContentsView::TargetForRect(views::View
* root
,
494 const gfx::Rect
& rect
) {
495 CHECK_EQ(root
, this);
499 bool OmniboxPopupContentsView::HasMatchAt(size_t index
) const {
500 return index
< model_
->result().size();
503 const AutocompleteMatch
& OmniboxPopupContentsView::GetMatchAtIndex(
504 size_t index
) const {
505 return model_
->result().match_at(index
);
508 size_t OmniboxPopupContentsView::GetIndexForPoint(
509 const gfx::Point
& point
) {
510 if (!HitTestPoint(point
))
511 return OmniboxPopupModel::kNoMatch
;
513 int nb_match
= model_
->result().size();
514 DCHECK(nb_match
<= child_count());
515 for (int i
= 0; i
< nb_match
; ++i
) {
516 views::View
* child
= child_at(i
);
517 gfx::Point
point_in_child_coords(point
);
518 View::ConvertPointToTarget(this, child
, &point_in_child_coords
);
519 if (child
->visible() && child
->HitTestPoint(point_in_child_coords
))
522 return OmniboxPopupModel::kNoMatch
;
525 void OmniboxPopupContentsView::UpdateLineEvent(
526 const ui::LocatedEvent
& event
,
527 bool should_set_selected_line
) {
528 size_t index
= GetIndexForPoint(event
.location());
529 model_
->SetHoveredLine(index
);
530 if (HasMatchAt(index
) && should_set_selected_line
)
531 model_
->SetSelectedLine(index
, false, false);
534 void OmniboxPopupContentsView::OpenSelectedLine(
535 const ui::LocatedEvent
& event
,
536 WindowOpenDisposition disposition
) {
537 size_t index
= GetIndexForPoint(event
.location());
538 if (!HasMatchAt(index
))
540 omnibox_view_
->OpenMatch(model_
->result().match_at(index
), disposition
,
541 GURL(), base::string16(), index
);
544 OmniboxResultView
* OmniboxPopupContentsView::result_view_at(size_t i
) {
545 return static_cast<OmniboxResultView
*>(child_at(static_cast<int>(i
)));