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"
7 #include "chrome/browser/search/search.h"
8 #include "chrome/browser/themes/theme_properties.h"
9 #include "chrome/browser/ui/omnibox/omnibox_view.h"
10 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
11 #include "chrome/browser/ui/views/omnibox/omnibox_result_view.h"
12 #include "chrome/browser/ui/views/omnibox/touch_omnibox_popup_contents_view.h"
13 #include "grit/ui_resources.h"
14 #include "ui/base/theme_provider.h"
15 #include "ui/gfx/canvas.h"
16 #include "ui/gfx/image/image.h"
17 #include "ui/gfx/path.h"
18 #include "ui/views/controls/image_view.h"
19 #include "ui/views/widget/widget.h"
20 #include "ui/views/window/non_client_view.h"
23 #include "ui/views/corewm/window_animations.h"
26 // This is the number of pixels in the border image interior to the actual
28 const int kBorderInterior = 6;
30 class OmniboxPopupContentsView::AutocompletePopupWidget
31 : public views::Widget,
32 public base::SupportsWeakPtr<AutocompletePopupWidget> {
34 AutocompletePopupWidget() {}
35 virtual ~AutocompletePopupWidget() {}
38 DISALLOW_COPY_AND_ASSIGN(AutocompletePopupWidget);
41 ////////////////////////////////////////////////////////////////////////////////
42 // OmniboxPopupContentsView, public:
44 OmniboxPopupView* OmniboxPopupContentsView::Create(
45 const gfx::FontList& font_list,
46 OmniboxView* omnibox_view,
47 OmniboxEditModel* edit_model,
48 LocationBarView* location_bar_view) {
49 OmniboxPopupContentsView* view = NULL;
50 if (ui::GetDisplayLayout() == ui::LAYOUT_TOUCH) {
51 view = new TouchOmniboxPopupContentsView(
52 font_list, omnibox_view, edit_model, location_bar_view);
54 view = new OmniboxPopupContentsView(
55 font_list, omnibox_view, edit_model, location_bar_view);
62 OmniboxPopupContentsView::OmniboxPopupContentsView(
63 const gfx::FontList& font_list,
64 OmniboxView* omnibox_view,
65 OmniboxEditModel* edit_model,
66 LocationBarView* location_bar_view)
67 : model_(new OmniboxPopupModel(this, edit_model)),
68 omnibox_view_(omnibox_view),
69 location_bar_view_(location_bar_view),
70 font_list_(font_list),
71 ignore_mouse_drag_(false),
72 size_animation_(this),
75 outside_vertical_padding_(0) {
76 // The contents is owned by the LocationBarView.
77 set_owned_by_client();
79 ui::ThemeProvider* theme = location_bar_view_->GetThemeProvider();
80 bottom_shadow_ = theme->GetImageSkiaNamed(IDR_BUBBLE_B);
83 void OmniboxPopupContentsView::Init() {
84 // This can't be done in the constructor as at that point we aren't
85 // necessarily our final class yet, and we may have subclasses
86 // overriding CreateResultView.
87 for (size_t i = 0; i < AutocompleteResult::kMaxMatches; ++i) {
88 OmniboxResultView* result_view = CreateResultView(this, i, font_list_);
89 result_view->SetVisible(false);
90 AddChildViewAt(result_view, static_cast<int>(i));
94 OmniboxPopupContentsView::~OmniboxPopupContentsView() {
95 // We don't need to do anything with |popup_| here. The OS either has already
96 // closed the window, in which case it's been deleted, or it will soon, in
97 // which case there's nothing we need to do.
100 gfx::Rect OmniboxPopupContentsView::GetPopupBounds() const {
101 if (!size_animation_.is_animating())
102 return target_bounds_;
104 gfx::Rect current_frame_bounds = start_bounds_;
105 int total_height_delta = target_bounds_.height() - start_bounds_.height();
106 // Round |current_height_delta| instead of truncating so we won't leave single
107 // white pixels at the bottom of the popup as long when animating very small
108 // height differences.
109 int current_height_delta = static_cast<int>(
110 size_animation_.GetCurrentValue() * total_height_delta - 0.5);
111 current_frame_bounds.set_height(
112 current_frame_bounds.height() + current_height_delta);
113 return current_frame_bounds;
116 void OmniboxPopupContentsView::LayoutChildren() {
117 gfx::Rect contents_rect = GetContentsBounds();
119 contents_rect.Inset(left_margin_,
120 views::NonClientFrameView::kClientEdgeThickness +
121 outside_vertical_padding_,
122 right_margin_, outside_vertical_padding_);
123 int top = contents_rect.y();
124 for (size_t i = 0; i < AutocompleteResult::kMaxMatches; ++i) {
125 View* v = child_at(i);
127 v->SetBounds(contents_rect.x(), top, contents_rect.width(),
128 v->GetPreferredSize().height());
129 top = v->bounds().bottom();
134 ////////////////////////////////////////////////////////////////////////////////
135 // OmniboxPopupContentsView, OmniboxPopupView overrides:
137 bool OmniboxPopupContentsView::IsOpen() const {
138 return popup_ != NULL;
141 void OmniboxPopupContentsView::InvalidateLine(size_t line) {
142 OmniboxResultView* result = result_view_at(line);
143 result->Invalidate();
145 if (HasMatchAt(line) && GetMatchAtIndex(line).associated_keyword.get()) {
146 result->ShowKeyword(IsSelectedIndex(line) &&
147 model_->selected_line_state() == OmniboxPopupModel::KEYWORD);
151 void OmniboxPopupContentsView::UpdatePopupAppearance() {
152 const size_t hidden_matches = model_->result().ShouldHideTopMatch() ? 1 : 0;
153 if (model_->result().size() <= hidden_matches ||
154 omnibox_view_->IsImeShowingPopup()) {
155 // No matches or the IME is showing a popup window which may overlap
156 // the omnibox popup window. Close any existing popup.
157 if (popup_ != NULL) {
158 size_animation_.Stop();
160 // NOTE: Do NOT use CloseNow() here, as we may be deep in a callstack
161 // triggered by the popup receiving a message (e.g. LBUTTONUP), and
162 // destroying the popup would cause us to read garbage when we unwind back
164 popup_->Close(); // This will eventually delete the popup.
170 // Update the match cached by each row, in the process of doing so make sure
171 // we have enough row views.
172 const size_t result_size = model_->result().size();
173 for (size_t i = 0; i < result_size; ++i) {
174 OmniboxResultView* view = result_view_at(i);
175 view->SetMatch(GetMatchAtIndex(i));
176 view->SetVisible(i >= hidden_matches);
178 for (size_t i = result_size; i < AutocompleteResult::kMaxMatches; ++i)
179 child_at(i)->SetVisible(false);
181 gfx::Point top_left_screen_coord;
183 location_bar_view_->GetOmniboxPopupPositioningInfo(
184 &top_left_screen_coord, &width, &left_margin_, &right_margin_);
185 gfx::Rect new_target_bounds(top_left_screen_coord,
186 gfx::Size(width, CalculatePopupHeight()));
188 // If we're animating and our target height changes, reset the animation.
189 // NOTE: If we just reset blindly on _every_ update, then when the user types
190 // rapidly we could get "stuck" trying repeatedly to animate shrinking by the
191 // last few pixels to get to one visible result.
192 if (new_target_bounds.height() != target_bounds_.height())
193 size_animation_.Reset();
194 target_bounds_ = new_target_bounds;
196 if (popup_ == NULL) {
197 gfx::NativeView popup_parent =
198 location_bar_view_->GetWidget()->GetNativeView();
200 // If the popup is currently closed, we need to create it.
201 popup_ = (new AutocompletePopupWidget)->AsWeakPtr();
202 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
203 params.can_activate = false;
204 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
205 params.parent = popup_parent;
206 params.bounds = GetPopupBounds();
207 params.context = popup_parent;
208 popup_->Init(params);
209 // Third-party software such as DigitalPersona identity verification can
210 // hook the underlying window creation methods and use SendMessage to
211 // synchronously change focus/activation, resulting in the popup being
212 // destroyed by the time control returns here. Bail out in this case to
213 // avoid a NULL dereference.
216 #if defined(USE_AURA)
217 views::corewm::SetWindowVisibilityAnimationTransition(
218 popup_->GetNativeView(), views::corewm::ANIMATE_NONE);
220 popup_->SetContentsView(this);
221 popup_->StackAbove(omnibox_view_->GetRelativeWindowForPopup());
223 // For some IMEs GetRelativeWindowForPopup triggers the omnibox to lose
224 // focus, thereby closing (and destroying) the popup.
225 // TODO(sky): this won't be needed once we close the omnibox on input
229 popup_->ShowInactive();
231 // Animate the popup shrinking, but don't animate growing larger since that
232 // would make the popup feel less responsive.
233 start_bounds_ = GetWidget()->GetWindowBoundsInScreen();
234 if (target_bounds_.height() < start_bounds_.height())
235 size_animation_.Show();
237 start_bounds_ = target_bounds_;
238 popup_->SetBounds(GetPopupBounds());
244 gfx::Rect OmniboxPopupContentsView::GetTargetBounds() {
245 return target_bounds_;
248 void OmniboxPopupContentsView::PaintUpdatesNow() {
249 // TODO(beng): remove this from the interface.
252 void OmniboxPopupContentsView::OnDragCanceled() {
253 ignore_mouse_drag_ = true;
256 ////////////////////////////////////////////////////////////////////////////////
257 // OmniboxPopupContentsView, OmniboxResultViewModel implementation:
259 bool OmniboxPopupContentsView::IsSelectedIndex(size_t index) const {
260 return index == model_->selected_line();
263 bool OmniboxPopupContentsView::IsHoveredIndex(size_t index) const {
264 return index == model_->hovered_line();
267 gfx::Image OmniboxPopupContentsView::GetIconIfExtensionMatch(
268 size_t index) const {
269 if (!HasMatchAt(index))
271 return model_->GetIconIfExtensionMatch(GetMatchAtIndex(index));
274 ////////////////////////////////////////////////////////////////////////////////
275 // OmniboxPopupContentsView, AnimationDelegate implementation:
277 void OmniboxPopupContentsView::AnimationProgressed(
278 const gfx::Animation* animation) {
279 // We should only be running the animation when the popup is already visible.
280 DCHECK(popup_ != NULL);
281 popup_->SetBounds(GetPopupBounds());
284 ////////////////////////////////////////////////////////////////////////////////
285 // OmniboxPopupContentsView, views::View overrides:
287 void OmniboxPopupContentsView::Layout() {
288 // Size our children to the available content area.
291 // We need to manually schedule a paint here since we are a layered window and
292 // won't implicitly require painting until we ask for one.
296 views::View* OmniboxPopupContentsView::GetEventHandlerForRect(
297 const gfx::Rect& rect) {
301 views::View* OmniboxPopupContentsView::GetTooltipHandlerForPoint(
302 const gfx::Point& point) {
306 bool OmniboxPopupContentsView::OnMousePressed(
307 const ui::MouseEvent& event) {
308 ignore_mouse_drag_ = false; // See comment on |ignore_mouse_drag_| in header.
309 if (event.IsLeftMouseButton() || event.IsMiddleMouseButton())
310 UpdateLineEvent(event, event.IsLeftMouseButton());
314 bool OmniboxPopupContentsView::OnMouseDragged(
315 const ui::MouseEvent& event) {
316 if (event.IsLeftMouseButton() || event.IsMiddleMouseButton())
317 UpdateLineEvent(event, !ignore_mouse_drag_ && event.IsLeftMouseButton());
321 void OmniboxPopupContentsView::OnMouseReleased(
322 const ui::MouseEvent& event) {
323 if (ignore_mouse_drag_) {
324 OnMouseCaptureLost();
328 if (event.IsOnlyMiddleMouseButton() || event.IsOnlyLeftMouseButton()) {
329 OpenSelectedLine(event, event.IsOnlyLeftMouseButton() ? CURRENT_TAB :
334 void OmniboxPopupContentsView::OnMouseCaptureLost() {
335 ignore_mouse_drag_ = false;
338 void OmniboxPopupContentsView::OnMouseMoved(
339 const ui::MouseEvent& event) {
340 model_->SetHoveredLine(GetIndexForPoint(event.location()));
343 void OmniboxPopupContentsView::OnMouseEntered(
344 const ui::MouseEvent& event) {
345 model_->SetHoveredLine(GetIndexForPoint(event.location()));
348 void OmniboxPopupContentsView::OnMouseExited(
349 const ui::MouseEvent& event) {
350 model_->SetHoveredLine(OmniboxPopupModel::kNoMatch);
353 void OmniboxPopupContentsView::OnGestureEvent(ui::GestureEvent* event) {
354 switch (event->type()) {
355 case ui::ET_GESTURE_TAP_DOWN:
356 case ui::ET_GESTURE_SCROLL_BEGIN:
357 case ui::ET_GESTURE_SCROLL_UPDATE:
358 UpdateLineEvent(*event, true);
360 case ui::ET_GESTURE_TAP:
361 case ui::ET_GESTURE_SCROLL_END:
362 OpenSelectedLine(*event, CURRENT_TAB);
370 ////////////////////////////////////////////////////////////////////////////////
371 // OmniboxPopupContentsView, protected:
373 void OmniboxPopupContentsView::PaintResultViews(gfx::Canvas* canvas) {
374 canvas->DrawColor(result_view_at(0)->GetColor(
375 OmniboxResultView::NORMAL, OmniboxResultView::BACKGROUND));
376 View::PaintChildren(canvas);
379 int OmniboxPopupContentsView::CalculatePopupHeight() {
380 DCHECK_GE(static_cast<size_t>(child_count()), model_->result().size());
381 int popup_height = 0;
382 for (size_t i = model_->result().ShouldHideTopMatch() ? 1 : 0;
383 i < model_->result().size(); ++i)
384 popup_height += child_at(i)->GetPreferredSize().height();
386 // Add enough space on the top and bottom so it looks like there is the same
387 // amount of space between the text and the popup border as there is in the
388 // interior between each row of text.
390 // Discovering the exact amount of leading and padding around the font is
391 // a bit tricky and platform-specific, but this computation seems to work in
393 OmniboxResultView* result_view = result_view_at(0);
394 outside_vertical_padding_ =
395 (result_view->GetPreferredSize().height() -
396 result_view->GetTextHeight());
398 return popup_height +
399 views::NonClientFrameView::kClientEdgeThickness + // Top border.
400 outside_vertical_padding_ * 2 + // Padding.
401 bottom_shadow_->height() - kBorderInterior; // Bottom border.
404 OmniboxResultView* OmniboxPopupContentsView::CreateResultView(
405 OmniboxResultViewModel* model,
407 const gfx::FontList& font_list) {
408 return new OmniboxResultView(model, model_index, location_bar_view_,
412 ////////////////////////////////////////////////////////////////////////////////
413 // OmniboxPopupContentsView, views::View overrides, protected:
415 void OmniboxPopupContentsView::OnPaint(gfx::Canvas* canvas) {
416 gfx::Rect contents_bounds = GetContentsBounds();
417 contents_bounds.set_height(
418 contents_bounds.height() - bottom_shadow_->height() + kBorderInterior);
421 MakeContentsPath(&path, contents_bounds);
423 canvas->sk_canvas()->clipPath(path,
424 SkRegion::kIntersect_Op,
425 true /* doAntialias */);
426 PaintResultViews(canvas);
431 gfx::Rect(0, 0, width(), views::NonClientFrameView::kClientEdgeThickness),
432 ThemeProperties::GetDefaultColor(
433 ThemeProperties::COLOR_TOOLBAR_SEPARATOR));
436 canvas->TileImageInt(*bottom_shadow_, 0, height() - bottom_shadow_->height(),
437 width(), bottom_shadow_->height());
440 void OmniboxPopupContentsView::PaintChildren(gfx::Canvas* canvas) {
441 // We paint our children inside OnPaint().
444 ////////////////////////////////////////////////////////////////////////////////
445 // OmniboxPopupContentsView, private:
447 bool OmniboxPopupContentsView::HasMatchAt(size_t index) const {
448 return index < model_->result().size();
451 const AutocompleteMatch& OmniboxPopupContentsView::GetMatchAtIndex(
452 size_t index) const {
453 return model_->result().match_at(index);
456 void OmniboxPopupContentsView::MakeContentsPath(
458 const gfx::Rect& bounding_rect) {
460 rect.set(SkIntToScalar(bounding_rect.x()),
461 SkIntToScalar(bounding_rect.y()),
462 SkIntToScalar(bounding_rect.right()),
463 SkIntToScalar(bounding_rect.bottom()));
467 size_t OmniboxPopupContentsView::GetIndexForPoint(
468 const gfx::Point& point) {
469 if (!HitTestPoint(point))
470 return OmniboxPopupModel::kNoMatch;
472 int nb_match = model_->result().size();
473 DCHECK(nb_match <= child_count());
474 for (int i = 0; i < nb_match; ++i) {
475 views::View* child = child_at(i);
476 gfx::Point point_in_child_coords(point);
477 View::ConvertPointToTarget(this, child, &point_in_child_coords);
478 if (child->visible() && child->HitTestPoint(point_in_child_coords))
481 return OmniboxPopupModel::kNoMatch;
484 void OmniboxPopupContentsView::UpdateLineEvent(
485 const ui::LocatedEvent& event,
486 bool should_set_selected_line) {
487 size_t index = GetIndexForPoint(event.location());
488 model_->SetHoveredLine(index);
489 if (HasMatchAt(index) && should_set_selected_line)
490 model_->SetSelectedLine(index, false, false);
493 void OmniboxPopupContentsView::OpenSelectedLine(
494 const ui::LocatedEvent& event,
495 WindowOpenDisposition disposition) {
496 size_t index = GetIndexForPoint(event.location());
497 if (!HasMatchAt(index))
499 omnibox_view_->OpenMatch(model_->result().match_at(index), disposition,
500 GURL(), base::string16(), index);
503 OmniboxResultView* OmniboxPopupContentsView::result_view_at(size_t i) {
504 return static_cast<OmniboxResultView*>(child_at(static_cast<int>(i)));