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