omnibox_popup_contents_view.cc revision a36e5920737c6adbddd3e43b760e5de8431db6e0
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.
4
5#include "chrome/browser/ui/views/omnibox/omnibox_popup_contents_view.h"
6
7#include "chrome/browser/search/search.h"
8#include "chrome/browser/themes/theme_properties.h"
9#include "chrome/browser/ui/omnibox/omnibox_popup_non_view.h"
10#include "chrome/browser/ui/omnibox/omnibox_view.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 "chrome/browser/ui/views/omnibox/touch_omnibox_popup_contents_view.h"
14#include "grit/ui_resources.h"
15#include "ui/base/theme_provider.h"
16#include "ui/gfx/canvas.h"
17#include "ui/gfx/image/image.h"
18#include "ui/gfx/path.h"
19#include "ui/views/controls/image_view.h"
20#include "ui/views/widget/widget.h"
21#include "ui/views/window/non_client_view.h"
22
23#if defined(USE_AURA)
24#include "ui/views/corewm/window_animations.h"
25#endif
26
27namespace {
28
29// This is the number of pixels in the border image used to draw the bottom
30// border + drop shadow interior to the "visual" border. We lay out assuming
31// that this many pixels inside the border is "in the popup."
32const SkAlpha kGlassPopupAlpha = 240;
33const SkAlpha kOpaquePopupAlpha = 255;
34
35// This is the number of pixels in the border image interior to the actual
36// border.
37const int kBorderInterior = 6;
38
39}  // namespace
40
41class OmniboxPopupContentsView::AutocompletePopupWidget
42    : public views::Widget,
43      public base::SupportsWeakPtr<AutocompletePopupWidget> {
44 public:
45  AutocompletePopupWidget() {}
46  virtual ~AutocompletePopupWidget() {}
47
48 private:
49  DISALLOW_COPY_AND_ASSIGN(AutocompletePopupWidget);
50};
51
52////////////////////////////////////////////////////////////////////////////////
53// OmniboxPopupContentsView, public:
54
55OmniboxPopupView* OmniboxPopupContentsView::Create(
56    const gfx::FontList& font_list,
57    OmniboxView* omnibox_view,
58    OmniboxEditModel* edit_model,
59    LocationBarView* location_bar_view) {
60#if defined(HTML_INSTANT_EXTENDED_POPUP)
61  if (chrome::IsInstantExtendedAPIEnabled())
62    return new OmniboxPopupNonView(edit_model);
63#endif
64
65  OmniboxPopupContentsView* view = NULL;
66  if (ui::GetDisplayLayout() == ui::LAYOUT_TOUCH) {
67    view = new TouchOmniboxPopupContentsView(
68        font_list, omnibox_view, edit_model, location_bar_view);
69  } else {
70    view = new OmniboxPopupContentsView(
71        font_list, omnibox_view, edit_model, location_bar_view);
72  }
73
74  view->Init();
75  return view;
76}
77
78OmniboxPopupContentsView::OmniboxPopupContentsView(
79    const gfx::FontList& font_list,
80    OmniboxView* omnibox_view,
81    OmniboxEditModel* edit_model,
82    LocationBarView* location_bar_view)
83    : model_(new OmniboxPopupModel(this, edit_model)),
84      omnibox_view_(omnibox_view),
85      location_bar_view_(location_bar_view),
86      font_list_(font_list),
87      ignore_mouse_drag_(false),
88      size_animation_(this),
89      left_margin_(0),
90      right_margin_(0),
91      outside_vertical_padding_(0) {
92  // The contents is owned by the LocationBarView.
93  set_owned_by_client();
94
95  ui::ThemeProvider* theme = location_bar_view_->GetThemeProvider();
96  bottom_shadow_ = theme->GetImageSkiaNamed(IDR_BUBBLE_B);
97}
98
99void OmniboxPopupContentsView::Init() {
100  // This can't be done in the constructor as at that point we aren't
101  // necessarily our final class yet, and we may have subclasses
102  // overriding CreateResultView.
103  for (size_t i = 0; i < AutocompleteResult::kMaxMatches; ++i) {
104    OmniboxResultView* result_view = CreateResultView(this, i, font_list_);
105    result_view->SetVisible(false);
106    AddChildViewAt(result_view, static_cast<int>(i));
107  }
108}
109
110OmniboxPopupContentsView::~OmniboxPopupContentsView() {
111  // We don't need to do anything with |popup_| here.  The OS either has already
112  // closed the window, in which case it's been deleted, or it will soon, in
113  // which case there's nothing we need to do.
114}
115
116gfx::Rect OmniboxPopupContentsView::GetPopupBounds() const {
117  if (!size_animation_.is_animating())
118    return target_bounds_;
119
120  gfx::Rect current_frame_bounds = start_bounds_;
121  int total_height_delta = target_bounds_.height() - start_bounds_.height();
122  // Round |current_height_delta| instead of truncating so we won't leave single
123  // white pixels at the bottom of the popup as long when animating very small
124  // height differences.
125  int current_height_delta = static_cast<int>(
126      size_animation_.GetCurrentValue() * total_height_delta - 0.5);
127  current_frame_bounds.set_height(
128      current_frame_bounds.height() + current_height_delta);
129  return current_frame_bounds;
130}
131
132void OmniboxPopupContentsView::LayoutChildren() {
133  gfx::Rect contents_rect = GetContentsBounds();
134
135  contents_rect.Inset(left_margin_,
136                      views::NonClientFrameView::kClientEdgeThickness +
137                          outside_vertical_padding_,
138                      right_margin_, outside_vertical_padding_);
139  int top = contents_rect.y();
140  for (size_t i = 0; i < AutocompleteResult::kMaxMatches; ++i) {
141    View* v = child_at(i);
142    if (v->visible()) {
143      v->SetBounds(contents_rect.x(), top, contents_rect.width(),
144                   v->GetPreferredSize().height());
145      top = v->bounds().bottom();
146    }
147  }
148}
149
150////////////////////////////////////////////////////////////////////////////////
151// OmniboxPopupContentsView, OmniboxPopupView overrides:
152
153bool OmniboxPopupContentsView::IsOpen() const {
154  return popup_ != NULL;
155}
156
157void OmniboxPopupContentsView::InvalidateLine(size_t line) {
158  OmniboxResultView* result = result_view_at(line);
159  result->Invalidate();
160
161  if (HasMatchAt(line) && GetMatchAtIndex(line).associated_keyword.get()) {
162    result->ShowKeyword(IsSelectedIndex(line) &&
163        model_->selected_line_state() == OmniboxPopupModel::KEYWORD);
164  }
165}
166
167void OmniboxPopupContentsView::UpdatePopupAppearance() {
168  if (model_->result().empty() ||
169      omnibox_view_->IsImeShowingPopup()) {
170    // No matches or the IME is showing a popup window which may overlap
171    // the omnibox popup window.  Close any existing popup.
172    if (popup_ != NULL) {
173      size_animation_.Stop();
174
175      // NOTE: Do NOT use CloseNow() here, as we may be deep in a callstack
176      // triggered by the popup receiving a message (e.g. LBUTTONUP), and
177      // destroying the popup would cause us to read garbage when we unwind back
178      // to that level.
179      popup_->Close();  // This will eventually delete the popup.
180      popup_.reset();
181    }
182    return;
183  }
184
185  // Update the match cached by each row, in the process of doing so make sure
186  // we have enough row views.
187  const size_t result_size = model_->result().size();
188  for (size_t i = 0; i < result_size; ++i) {
189    OmniboxResultView* view = result_view_at(i);
190    view->SetMatch(GetMatchAtIndex(i));
191    view->SetVisible(true);
192  }
193  for (size_t i = result_size; i < AutocompleteResult::kMaxMatches; ++i)
194    child_at(i)->SetVisible(false);
195
196  gfx::Point top_left_screen_coord;
197  int width;
198  location_bar_view_->GetOmniboxPopupPositioningInfo(
199      &top_left_screen_coord, &width, &left_margin_, &right_margin_);
200  gfx::Rect new_target_bounds(top_left_screen_coord,
201                              gfx::Size(width, CalculatePopupHeight()));
202
203  // If we're animating and our target height changes, reset the animation.
204  // NOTE: If we just reset blindly on _every_ update, then when the user types
205  // rapidly we could get "stuck" trying repeatedly to animate shrinking by the
206  // last few pixels to get to one visible result.
207  if (new_target_bounds.height() != target_bounds_.height())
208    size_animation_.Reset();
209  target_bounds_ = new_target_bounds;
210
211  if (popup_ == NULL) {
212    gfx::NativeView popup_parent =
213        location_bar_view_->GetWidget()->GetNativeView();
214
215    // If the popup is currently closed, we need to create it.
216    popup_ = (new AutocompletePopupWidget)->AsWeakPtr();
217    views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP);
218    params.can_activate = false;
219    params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
220    params.parent = popup_parent;
221    params.bounds = GetPopupBounds();
222    params.context = popup_parent;
223    popup_->Init(params);
224#if defined(USE_AURA)
225    views::corewm::SetWindowVisibilityAnimationType(
226        popup_->GetNativeView(),
227        views::corewm::WINDOW_VISIBILITY_ANIMATION_TYPE_VERTICAL);
228#if defined(OS_CHROMEOS)
229    // No animation for autocomplete popup appearance.
230    views::corewm::SetWindowVisibilityAnimationTransition(
231        popup_->GetNativeView(), views::corewm::ANIMATE_HIDE);
232#endif
233#endif
234    popup_->SetContentsView(this);
235    popup_->StackAbove(omnibox_view_->GetRelativeWindowForPopup());
236    if (!popup_.get()) {
237      // For some IMEs GetRelativeWindowForPopup triggers the omnibox to lose
238      // focus, thereby closing (and destroying) the popup.
239      // TODO(sky): this won't be needed once we close the omnibox on input
240      // window showing.
241      return;
242    }
243    popup_->Show();
244  } else {
245    // Animate the popup shrinking, but don't animate growing larger since that
246    // would make the popup feel less responsive.
247    start_bounds_ = GetWidget()->GetWindowBoundsInScreen();
248    if (target_bounds_.height() < start_bounds_.height())
249      size_animation_.Show();
250    else
251      start_bounds_ = target_bounds_;
252    popup_->SetBounds(GetPopupBounds());
253  }
254
255  SchedulePaint();
256}
257
258gfx::Rect OmniboxPopupContentsView::GetTargetBounds() {
259  return target_bounds_;
260}
261
262void OmniboxPopupContentsView::PaintUpdatesNow() {
263  // TODO(beng): remove this from the interface.
264}
265
266void OmniboxPopupContentsView::OnDragCanceled() {
267  ignore_mouse_drag_ = true;
268}
269
270////////////////////////////////////////////////////////////////////////////////
271// OmniboxPopupContentsView, OmniboxResultViewModel implementation:
272
273bool OmniboxPopupContentsView::IsSelectedIndex(size_t index) const {
274  return index == model_->selected_line();
275}
276
277bool OmniboxPopupContentsView::IsHoveredIndex(size_t index) const {
278  return index == model_->hovered_line();
279}
280
281gfx::Image OmniboxPopupContentsView::GetIconIfExtensionMatch(
282    size_t index) const {
283  if (!HasMatchAt(index))
284    return gfx::Image();
285  return model_->GetIconIfExtensionMatch(GetMatchAtIndex(index));
286}
287
288////////////////////////////////////////////////////////////////////////////////
289// OmniboxPopupContentsView, AnimationDelegate implementation:
290
291void OmniboxPopupContentsView::AnimationProgressed(
292    const ui::Animation* animation) {
293  // We should only be running the animation when the popup is already visible.
294  DCHECK(popup_ != NULL);
295  popup_->SetBounds(GetPopupBounds());
296}
297
298////////////////////////////////////////////////////////////////////////////////
299// OmniboxPopupContentsView, views::View overrides:
300
301void OmniboxPopupContentsView::Layout() {
302  // Size our children to the available content area.
303  LayoutChildren();
304
305  // We need to manually schedule a paint here since we are a layered window and
306  // won't implicitly require painting until we ask for one.
307  SchedulePaint();
308}
309
310views::View* OmniboxPopupContentsView::GetEventHandlerForPoint(
311    const gfx::Point& point) {
312  return this;
313}
314
315views::View* OmniboxPopupContentsView::GetTooltipHandlerForPoint(
316    const gfx::Point& point) {
317  return NULL;
318}
319
320bool OmniboxPopupContentsView::OnMousePressed(
321    const ui::MouseEvent& event) {
322  ignore_mouse_drag_ = false;  // See comment on |ignore_mouse_drag_| in header.
323  if (event.IsLeftMouseButton() || event.IsMiddleMouseButton())
324    UpdateLineEvent(event, event.IsLeftMouseButton());
325  return true;
326}
327
328bool OmniboxPopupContentsView::OnMouseDragged(
329    const ui::MouseEvent& event) {
330  if (event.IsLeftMouseButton() || event.IsMiddleMouseButton())
331    UpdateLineEvent(event, !ignore_mouse_drag_ && event.IsLeftMouseButton());
332  return true;
333}
334
335void OmniboxPopupContentsView::OnMouseReleased(
336    const ui::MouseEvent& event) {
337  if (ignore_mouse_drag_) {
338    OnMouseCaptureLost();
339    return;
340  }
341
342  if (event.IsOnlyMiddleMouseButton() || event.IsOnlyLeftMouseButton()) {
343    OpenSelectedLine(event, event.IsOnlyLeftMouseButton() ? CURRENT_TAB :
344                                                            NEW_BACKGROUND_TAB);
345  }
346}
347
348void OmniboxPopupContentsView::OnMouseCaptureLost() {
349  ignore_mouse_drag_ = false;
350}
351
352void OmniboxPopupContentsView::OnMouseMoved(
353    const ui::MouseEvent& event) {
354  model_->SetHoveredLine(GetIndexForPoint(event.location()));
355}
356
357void OmniboxPopupContentsView::OnMouseEntered(
358    const ui::MouseEvent& event) {
359  model_->SetHoveredLine(GetIndexForPoint(event.location()));
360}
361
362void OmniboxPopupContentsView::OnMouseExited(
363    const ui::MouseEvent& event) {
364  model_->SetHoveredLine(OmniboxPopupModel::kNoMatch);
365}
366
367void OmniboxPopupContentsView::OnGestureEvent(ui::GestureEvent* event) {
368  switch (event->type()) {
369    case ui::ET_GESTURE_TAP_DOWN:
370    case ui::ET_GESTURE_SCROLL_BEGIN:
371    case ui::ET_GESTURE_SCROLL_UPDATE:
372      UpdateLineEvent(*event, true);
373      break;
374    case ui::ET_GESTURE_TAP:
375    case ui::ET_GESTURE_SCROLL_END:
376      OpenSelectedLine(*event, CURRENT_TAB);
377      break;
378    default:
379      return;
380  }
381  event->SetHandled();
382}
383
384////////////////////////////////////////////////////////////////////////////////
385// OmniboxPopupContentsView, protected:
386
387void OmniboxPopupContentsView::PaintResultViews(gfx::Canvas* canvas) {
388  canvas->DrawColor(result_view_at(0)->GetColor(
389      OmniboxResultView::NORMAL, OmniboxResultView::BACKGROUND));
390  View::PaintChildren(canvas);
391}
392
393int OmniboxPopupContentsView::CalculatePopupHeight() {
394  DCHECK_GE(static_cast<size_t>(child_count()), model_->result().size());
395  int popup_height = 0;
396  for (size_t i = 0; i < model_->result().size(); ++i)
397    popup_height += child_at(i)->GetPreferredSize().height();
398
399  // Add enough space on the top and bottom so it looks like there is the same
400  // amount of space between the text and the popup border as there is in the
401  // interior between each row of text.
402  //
403  // Discovering the exact amount of leading and padding around the font is
404  // a bit tricky and platform-specific, but this computation seems to work in
405  // practice.
406  OmniboxResultView* result_view = result_view_at(0);
407  outside_vertical_padding_ =
408      (result_view->GetPreferredSize().height() -
409       result_view->GetTextHeight());
410
411  return popup_height +
412         views::NonClientFrameView::kClientEdgeThickness +  // Top border.
413         outside_vertical_padding_ * 2 +                    // Padding.
414         bottom_shadow_->height() - kBorderInterior;        // Bottom border.
415}
416
417OmniboxResultView* OmniboxPopupContentsView::CreateResultView(
418    OmniboxResultViewModel* model,
419    int model_index,
420    const gfx::FontList& font_list) {
421  return new OmniboxResultView(model, model_index, location_bar_view_,
422                               font_list);
423}
424
425////////////////////////////////////////////////////////////////////////////////
426// OmniboxPopupContentsView, views::View overrides, protected:
427
428void OmniboxPopupContentsView::OnPaint(gfx::Canvas* canvas) {
429  gfx::Rect contents_bounds = GetContentsBounds();
430  contents_bounds.set_height(
431      contents_bounds.height() - bottom_shadow_->height() + kBorderInterior);
432
433  gfx::Path path;
434  MakeContentsPath(&path, contents_bounds);
435  canvas->Save();
436  canvas->sk_canvas()->clipPath(path,
437                                SkRegion::kIntersect_Op,
438                                true /* doAntialias */);
439  PaintResultViews(canvas);
440  canvas->Restore();
441
442  // Top border.
443  canvas->FillRect(
444      gfx::Rect(0, 0, width(), views::NonClientFrameView::kClientEdgeThickness),
445      ThemeProperties::GetDefaultColor(
446          ThemeProperties::COLOR_TOOLBAR_SEPARATOR));
447
448  // Bottom border.
449  canvas->TileImageInt(*bottom_shadow_, 0, height() - bottom_shadow_->height(),
450                       width(), bottom_shadow_->height());
451}
452
453void OmniboxPopupContentsView::PaintChildren(gfx::Canvas* canvas) {
454  // We paint our children inside OnPaint().
455}
456
457////////////////////////////////////////////////////////////////////////////////
458// OmniboxPopupContentsView, private:
459
460bool OmniboxPopupContentsView::HasMatchAt(size_t index) const {
461  return index < model_->result().size();
462}
463
464const AutocompleteMatch& OmniboxPopupContentsView::GetMatchAtIndex(
465    size_t index) const {
466  return model_->result().match_at(index);
467}
468
469void OmniboxPopupContentsView::MakeContentsPath(
470    gfx::Path* path,
471    const gfx::Rect& bounding_rect) {
472  SkRect rect;
473  rect.set(SkIntToScalar(bounding_rect.x()),
474           SkIntToScalar(bounding_rect.y()),
475           SkIntToScalar(bounding_rect.right()),
476           SkIntToScalar(bounding_rect.bottom()));
477  path->addRect(rect);
478}
479
480size_t OmniboxPopupContentsView::GetIndexForPoint(
481    const gfx::Point& point) {
482  if (!HitTestPoint(point))
483    return OmniboxPopupModel::kNoMatch;
484
485  int nb_match = model_->result().size();
486  DCHECK(nb_match <= child_count());
487  for (int i = 0; i < nb_match; ++i) {
488    views::View* child = child_at(i);
489    gfx::Point point_in_child_coords(point);
490    View::ConvertPointToTarget(this, child, &point_in_child_coords);
491    if (child->HitTestPoint(point_in_child_coords))
492      return i;
493  }
494  return OmniboxPopupModel::kNoMatch;
495}
496
497void OmniboxPopupContentsView::UpdateLineEvent(
498    const ui::LocatedEvent& event,
499    bool should_set_selected_line) {
500  size_t index = GetIndexForPoint(event.location());
501  model_->SetHoveredLine(index);
502  if (HasMatchAt(index) && should_set_selected_line)
503    model_->SetSelectedLine(index, false, false);
504}
505
506void OmniboxPopupContentsView::OpenSelectedLine(
507    const ui::LocatedEvent& event,
508    WindowOpenDisposition disposition) {
509  size_t index = GetIndexForPoint(event.location());
510  if (!HasMatchAt(index))
511    return;
512
513  // OpenMatch() may close the popup, which will clear the result set and, by
514  // extension, |match| and its contents.  So copy the relevant match out to
515  // make sure it stays alive until the call completes.
516  AutocompleteMatch match = model_->result().match_at(index);
517  omnibox_view_->OpenMatch(match, disposition, GURL(), index);
518}
519
520OmniboxResultView* OmniboxPopupContentsView::result_view_at(size_t i) {
521  return static_cast<OmniboxResultView*>(child_at(static_cast<int>(i)));
522}
523