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/gfx/canvas.h"
16 #include "ui/gfx/image/image.h"
17 #include "ui/gfx/path.h"
18 #include "ui/resources/grit/ui_resources.h"
19 #include "ui/views/controls/image_view.h"
20 #include "ui/views/view_targeter.h"
21 #include "ui/views/widget/widget.h"
22 #include "ui/views/window/non_client_view.h"
23 #include "ui/wm/core/window_animations.h"
25 // This is the number of pixels in the border image interior to the actual
27 const int kBorderInterior = 6;
29 class OmniboxPopupContentsView::AutocompletePopupWidget
30 : public views::Widget,
31 public base::SupportsWeakPtr<AutocompletePopupWidget> {
33 AutocompletePopupWidget() {}
34 virtual ~AutocompletePopupWidget() {}
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 outside_vertical_padding_(0) {
69 // The contents is owned by the LocationBarView.
70 set_owned_by_client();
72 ui::ThemeProvider* theme = location_bar_view_->GetThemeProvider();
73 bottom_shadow_ = theme->GetImageSkiaNamed(IDR_BUBBLE_B);
76 scoped_ptr<views::ViewTargeter>(new views::ViewTargeter(this)));
79 void OmniboxPopupContentsView::Init() {
80 // This can't be done in the constructor as at that point we aren't
81 // necessarily our final class yet, and we may have subclasses
82 // overriding CreateResultView.
83 for (size_t i = 0; i < AutocompleteResult::kMaxMatches; ++i) {
84 OmniboxResultView* result_view = CreateResultView(i, font_list_);
85 result_view->SetVisible(false);
86 AddChildViewAt(result_view, static_cast<int>(i));
90 OmniboxPopupContentsView::~OmniboxPopupContentsView() {
91 // We don't need to do anything with |popup_| here. The OS either has already
92 // closed the window, in which case it's been deleted, or it will soon, in
93 // which case there's nothing we need to do.
96 gfx::Rect OmniboxPopupContentsView::GetPopupBounds() const {
97 if (!size_animation_.is_animating())
98 return target_bounds_;
100 gfx::Rect current_frame_bounds = start_bounds_;
101 int total_height_delta = target_bounds_.height() - start_bounds_.height();
102 // Round |current_height_delta| instead of truncating so we won't leave single
103 // white pixels at the bottom of the popup as long when animating very small
104 // height differences.
105 int current_height_delta = static_cast<int>(
106 size_animation_.GetCurrentValue() * total_height_delta - 0.5);
107 current_frame_bounds.set_height(
108 current_frame_bounds.height() + current_height_delta);
109 return current_frame_bounds;
112 void OmniboxPopupContentsView::LayoutChildren() {
113 gfx::Rect contents_rect = GetContentsBounds();
115 contents_rect.Inset(left_margin_,
116 views::NonClientFrameView::kClientEdgeThickness +
117 outside_vertical_padding_,
118 right_margin_, outside_vertical_padding_);
119 int top = contents_rect.y();
120 for (size_t i = 0; i < AutocompleteResult::kMaxMatches; ++i) {
121 View* v = child_at(i);
123 v->SetBounds(contents_rect.x(), top, contents_rect.width(),
124 v->GetPreferredSize().height());
125 top = v->bounds().bottom();
130 ////////////////////////////////////////////////////////////////////////////////
131 // OmniboxPopupContentsView, OmniboxPopupView overrides:
133 bool OmniboxPopupContentsView::IsOpen() const {
134 return popup_ != NULL;
137 void OmniboxPopupContentsView::InvalidateLine(size_t line) {
138 OmniboxResultView* result = result_view_at(line);
139 result->Invalidate();
141 if (HasMatchAt(line) && GetMatchAtIndex(line).associated_keyword.get()) {
142 result->ShowKeyword(IsSelectedIndex(line) &&
143 model_->selected_line_state() == OmniboxPopupModel::KEYWORD);
147 void OmniboxPopupContentsView::UpdatePopupAppearance() {
148 const size_t hidden_matches = model_->result().ShouldHideTopMatch() ? 1 : 0;
149 if (model_->result().size() <= hidden_matches ||
150 omnibox_view_->IsImeShowingPopup()) {
151 // No matches or the IME is showing a popup window which may overlap
152 // the omnibox popup window. Close any existing popup.
153 if (popup_ != NULL) {
154 size_animation_.Stop();
156 // NOTE: Do NOT use CloseNow() here, as we may be deep in a callstack
157 // triggered by the popup receiving a message (e.g. LBUTTONUP), and
158 // destroying the popup would cause us to read garbage when we unwind back
160 popup_->Close(); // This will eventually delete the popup.
166 // Update the match cached by each row, in the process of doing so make sure
167 // we have enough row views.
168 const size_t result_size = model_->result().size();
169 max_match_contents_width_ = 0;
170 for (size_t i = 0; i < result_size; ++i) {
171 OmniboxResultView* view = result_view_at(i);
172 const AutocompleteMatch& match = GetMatchAtIndex(i);
173 view->SetMatch(match);
174 view->SetVisible(i >= hidden_matches);
175 if (match.type == AutocompleteMatchType::SEARCH_SUGGEST_INFINITE) {
176 max_match_contents_width_ = std::max(
177 max_match_contents_width_, view->GetMatchContentsWidth());
181 for (size_t i = result_size; i < AutocompleteResult::kMaxMatches; ++i)
182 child_at(i)->SetVisible(false);
184 gfx::Point top_left_screen_coord;
186 location_bar_view_->GetOmniboxPopupPositioningInfo(
187 &top_left_screen_coord, &width, &left_margin_, &right_margin_);
188 gfx::Rect new_target_bounds(top_left_screen_coord,
189 gfx::Size(width, CalculatePopupHeight()));
191 // If we're animating and our target height changes, reset the animation.
192 // NOTE: If we just reset blindly on _every_ update, then when the user types
193 // rapidly we could get "stuck" trying repeatedly to animate shrinking by the
194 // last few pixels to get to one visible result.
195 if (new_target_bounds.height() != target_bounds_.height())
196 size_animation_.Reset();
197 target_bounds_ = new_target_bounds;
199 if (popup_ == NULL) {
200 views::Widget* popup_parent = location_bar_view_->GetWidget();
202 // If the popup is currently closed, we need to create it.
203 popup_ = (new AutocompletePopupWidget)->AsWeakPtr();
204 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
205 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
206 params.parent = popup_parent->GetNativeView();
207 params.bounds = GetPopupBounds();
208 params.context = popup_parent->GetNativeWindow();
209 popup_->Init(params);
210 // Third-party software such as DigitalPersona identity verification can
211 // hook the underlying window creation methods and use SendMessage to
212 // synchronously change focus/activation, resulting in the popup being
213 // destroyed by the time control returns here. Bail out in this case to
214 // avoid a NULL dereference.
217 wm::SetWindowVisibilityAnimationTransition(
218 popup_->GetNativeView(), wm::ANIMATE_NONE);
219 popup_->SetContentsView(this);
220 popup_->StackAbove(omnibox_view_->GetRelativeWindowForPopup());
222 // For some IMEs GetRelativeWindowForPopup triggers the omnibox to lose
223 // focus, thereby closing (and destroying) the popup.
224 // TODO(sky): this won't be needed once we close the omnibox on input
228 popup_->ShowInactive();
230 // Animate the popup shrinking, but don't animate growing larger since that
231 // would make the popup feel less responsive.
232 start_bounds_ = GetWidget()->GetWindowBoundsInScreen();
233 if (target_bounds_.height() < start_bounds_.height())
234 size_animation_.Show();
236 start_bounds_ = target_bounds_;
237 popup_->SetBounds(GetPopupBounds());
243 gfx::Rect OmniboxPopupContentsView::GetTargetBounds() {
244 return target_bounds_;
247 void OmniboxPopupContentsView::PaintUpdatesNow() {
248 // TODO(beng): remove this from the interface.
251 void OmniboxPopupContentsView::OnDragCanceled() {
252 ignore_mouse_drag_ = true;
255 ////////////////////////////////////////////////////////////////////////////////
256 // OmniboxPopupContentsView, OmniboxResultViewModel implementation:
258 bool OmniboxPopupContentsView::IsSelectedIndex(size_t index) const {
259 return index == model_->selected_line();
262 bool OmniboxPopupContentsView::IsHoveredIndex(size_t index) const {
263 return index == model_->hovered_line();
266 gfx::Image OmniboxPopupContentsView::GetIconIfExtensionMatch(
267 size_t index) const {
268 if (!HasMatchAt(index))
270 return model_->GetIconIfExtensionMatch(GetMatchAtIndex(index));
273 bool OmniboxPopupContentsView::IsStarredMatch(
274 const AutocompleteMatch& match) const {
275 return model_->IsStarredMatch(match);
278 ////////////////////////////////////////////////////////////////////////////////
279 // OmniboxPopupContentsView, AnimationDelegate implementation:
281 void OmniboxPopupContentsView::AnimationProgressed(
282 const gfx::Animation* animation) {
283 // We should only be running the animation when the popup is already visible.
284 DCHECK(popup_ != NULL);
285 popup_->SetBounds(GetPopupBounds());
288 ////////////////////////////////////////////////////////////////////////////////
289 // OmniboxPopupContentsView, views::View overrides:
291 void OmniboxPopupContentsView::Layout() {
292 // Size our children to the available content area.
295 // We need to manually schedule a paint here since we are a layered window and
296 // won't implicitly require painting until we ask for one.
300 views::View* OmniboxPopupContentsView::GetTooltipHandlerForPoint(
301 const gfx::Point& point) {
305 bool OmniboxPopupContentsView::OnMousePressed(
306 const ui::MouseEvent& event) {
307 ignore_mouse_drag_ = false; // See comment on |ignore_mouse_drag_| in header.
308 if (event.IsLeftMouseButton() || event.IsMiddleMouseButton())
309 UpdateLineEvent(event, event.IsLeftMouseButton());
313 bool OmniboxPopupContentsView::OnMouseDragged(
314 const ui::MouseEvent& event) {
315 if (event.IsLeftMouseButton() || event.IsMiddleMouseButton())
316 UpdateLineEvent(event, !ignore_mouse_drag_ && event.IsLeftMouseButton());
320 void OmniboxPopupContentsView::OnMouseReleased(
321 const ui::MouseEvent& event) {
322 if (ignore_mouse_drag_) {
323 OnMouseCaptureLost();
327 if (event.IsOnlyMiddleMouseButton() || event.IsOnlyLeftMouseButton()) {
328 OpenSelectedLine(event, event.IsOnlyLeftMouseButton() ? CURRENT_TAB :
333 void OmniboxPopupContentsView::OnMouseCaptureLost() {
334 ignore_mouse_drag_ = false;
337 void OmniboxPopupContentsView::OnMouseMoved(
338 const ui::MouseEvent& event) {
339 model_->SetHoveredLine(GetIndexForPoint(event.location()));
342 void OmniboxPopupContentsView::OnMouseEntered(
343 const ui::MouseEvent& event) {
344 model_->SetHoveredLine(GetIndexForPoint(event.location()));
347 void OmniboxPopupContentsView::OnMouseExited(
348 const ui::MouseEvent& event) {
349 model_->SetHoveredLine(OmniboxPopupModel::kNoMatch);
352 void OmniboxPopupContentsView::OnGestureEvent(ui::GestureEvent* event) {
353 switch (event->type()) {
354 case ui::ET_GESTURE_TAP_DOWN:
355 case ui::ET_GESTURE_SCROLL_BEGIN:
356 case ui::ET_GESTURE_SCROLL_UPDATE:
357 UpdateLineEvent(*event, true);
359 case ui::ET_GESTURE_TAP:
360 case ui::ET_GESTURE_SCROLL_END:
361 OpenSelectedLine(*event, CURRENT_TAB);
369 ////////////////////////////////////////////////////////////////////////////////
370 // OmniboxPopupContentsView, protected:
372 void OmniboxPopupContentsView::PaintResultViews(gfx::Canvas* canvas) {
373 canvas->DrawColor(result_view_at(0)->GetColor(
374 OmniboxResultView::NORMAL, OmniboxResultView::BACKGROUND));
375 View::PaintChildren(canvas, views::CullSet());
378 int OmniboxPopupContentsView::CalculatePopupHeight() {
379 DCHECK_GE(static_cast<size_t>(child_count()), model_->result().size());
380 int popup_height = 0;
381 for (size_t i = model_->result().ShouldHideTopMatch() ? 1 : 0;
382 i < model_->result().size(); ++i)
383 popup_height += child_at(i)->GetPreferredSize().height();
385 // Add enough space on the top and bottom so it looks like there is the same
386 // amount of space between the text and the popup border as there is in the
387 // interior between each row of text.
389 // Discovering the exact amount of leading and padding around the font is
390 // a bit tricky and platform-specific, but this computation seems to work in
392 OmniboxResultView* result_view = result_view_at(0);
393 outside_vertical_padding_ =
394 (result_view->GetPreferredSize().height() -
395 result_view->GetTextHeight());
397 return popup_height +
398 views::NonClientFrameView::kClientEdgeThickness + // Top border.
399 outside_vertical_padding_ * 2 + // Padding.
400 bottom_shadow_->height() - kBorderInterior; // Bottom border.
403 OmniboxResultView* OmniboxPopupContentsView::CreateResultView(
405 const gfx::FontList& font_list) {
406 return new OmniboxResultView(this, model_index, location_bar_view_,
410 ////////////////////////////////////////////////////////////////////////////////
411 // OmniboxPopupContentsView, views::View overrides, protected:
413 void OmniboxPopupContentsView::OnPaint(gfx::Canvas* canvas) {
414 gfx::Rect contents_bounds = GetContentsBounds();
415 contents_bounds.set_height(
416 contents_bounds.height() - bottom_shadow_->height() + kBorderInterior);
419 MakeContentsPath(&path, contents_bounds);
421 canvas->sk_canvas()->clipPath(path,
422 SkRegion::kIntersect_Op,
423 true /* doAntialias */);
424 PaintResultViews(canvas);
429 gfx::Rect(0, 0, width(), views::NonClientFrameView::kClientEdgeThickness),
430 ThemeProperties::GetDefaultColor(
431 ThemeProperties::COLOR_TOOLBAR_SEPARATOR));
434 canvas->TileImageInt(*bottom_shadow_, 0, height() - bottom_shadow_->height(),
435 width(), bottom_shadow_->height());
438 void OmniboxPopupContentsView::PaintChildren(gfx::Canvas* canvas,
439 const views::CullSet& cull_set) {
440 // We paint our children inside OnPaint().
443 ////////////////////////////////////////////////////////////////////////////////
444 // OmniboxPopupContentsView, private:
446 views::View* OmniboxPopupContentsView::TargetForRect(views::View* root,
447 const gfx::Rect& rect) {
448 CHECK_EQ(root, this);
452 bool OmniboxPopupContentsView::HasMatchAt(size_t index) const {
453 return index < model_->result().size();
456 const AutocompleteMatch& OmniboxPopupContentsView::GetMatchAtIndex(
457 size_t index) const {
458 return model_->result().match_at(index);
461 void OmniboxPopupContentsView::MakeContentsPath(
463 const gfx::Rect& bounding_rect) {
465 rect.set(SkIntToScalar(bounding_rect.x()),
466 SkIntToScalar(bounding_rect.y()),
467 SkIntToScalar(bounding_rect.right()),
468 SkIntToScalar(bounding_rect.bottom()));
472 size_t OmniboxPopupContentsView::GetIndexForPoint(
473 const gfx::Point& point) {
474 if (!HitTestPoint(point))
475 return OmniboxPopupModel::kNoMatch;
477 int nb_match = model_->result().size();
478 DCHECK(nb_match <= child_count());
479 for (int i = 0; i < nb_match; ++i) {
480 views::View* child = child_at(i);
481 gfx::Point point_in_child_coords(point);
482 View::ConvertPointToTarget(this, child, &point_in_child_coords);
483 if (child->visible() && child->HitTestPoint(point_in_child_coords))
486 return OmniboxPopupModel::kNoMatch;
489 void OmniboxPopupContentsView::UpdateLineEvent(
490 const ui::LocatedEvent& event,
491 bool should_set_selected_line) {
492 size_t index = GetIndexForPoint(event.location());
493 model_->SetHoveredLine(index);
494 if (HasMatchAt(index) && should_set_selected_line)
495 model_->SetSelectedLine(index, false, false);
498 void OmniboxPopupContentsView::OpenSelectedLine(
499 const ui::LocatedEvent& event,
500 WindowOpenDisposition disposition) {
501 size_t index = GetIndexForPoint(event.location());
502 if (!HasMatchAt(index))
504 omnibox_view_->OpenMatch(model_->result().match_at(index), disposition,
505 GURL(), base::string16(), index);
508 OmniboxResultView* OmniboxPopupContentsView::result_view_at(size_t i) {
509 return static_cast<OmniboxResultView*>(child_at(static_cast<int>(i)));