omnibox_popup_contents_view.cc revision 8bcbed890bc3ce4d7a057a8f32cab53fa534672e
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_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" 21 22#if defined(USE_AURA) 23#include "ui/views/corewm/window_animations.h" 24#endif 25 26// This is the number of pixels in the border image interior to the actual 27// border. 28const int kBorderInterior = 6; 29 30class OmniboxPopupContentsView::AutocompletePopupWidget 31 : public views::Widget, 32 public base::SupportsWeakPtr<AutocompletePopupWidget> { 33 public: 34 AutocompletePopupWidget() {} 35 virtual ~AutocompletePopupWidget() {} 36 37 private: 38 DISALLOW_COPY_AND_ASSIGN(AutocompletePopupWidget); 39}; 40 41//////////////////////////////////////////////////////////////////////////////// 42// OmniboxPopupContentsView, public: 43 44OmniboxPopupView* 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); 53 } else { 54 view = new OmniboxPopupContentsView( 55 font_list, omnibox_view, edit_model, location_bar_view); 56 } 57 58 view->Init(); 59 return view; 60} 61 62OmniboxPopupContentsView::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), 73 left_margin_(0), 74 right_margin_(0), 75 outside_vertical_padding_(0) { 76 // The contents is owned by the LocationBarView. 77 set_owned_by_client(); 78 79 ui::ThemeProvider* theme = location_bar_view_->GetThemeProvider(); 80 bottom_shadow_ = theme->GetImageSkiaNamed(IDR_BUBBLE_B); 81} 82 83void 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)); 91 } 92} 93 94OmniboxPopupContentsView::~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. 98} 99 100gfx::Rect OmniboxPopupContentsView::GetPopupBounds() const { 101 if (!size_animation_.is_animating()) 102 return target_bounds_; 103 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; 114} 115 116void OmniboxPopupContentsView::LayoutChildren() { 117 gfx::Rect contents_rect = GetContentsBounds(); 118 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); 126 if (v->visible()) { 127 v->SetBounds(contents_rect.x(), top, contents_rect.width(), 128 v->GetPreferredSize().height()); 129 top = v->bounds().bottom(); 130 } 131 } 132} 133 134//////////////////////////////////////////////////////////////////////////////// 135// OmniboxPopupContentsView, OmniboxPopupView overrides: 136 137bool OmniboxPopupContentsView::IsOpen() const { 138 return popup_ != NULL; 139} 140 141void OmniboxPopupContentsView::InvalidateLine(size_t line) { 142 OmniboxResultView* result = result_view_at(line); 143 result->Invalidate(); 144 145 if (HasMatchAt(line) && GetMatchAtIndex(line).associated_keyword.get()) { 146 result->ShowKeyword(IsSelectedIndex(line) && 147 model_->selected_line_state() == OmniboxPopupModel::KEYWORD); 148 } 149} 150 151void 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(); 159 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 163 // to that level. 164 popup_->Close(); // This will eventually delete the popup. 165 popup_.reset(); 166 } 167 return; 168 } 169 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); 177 } 178 for (size_t i = result_size; i < AutocompleteResult::kMaxMatches; ++i) 179 child_at(i)->SetVisible(false); 180 181 gfx::Point top_left_screen_coord; 182 int width; 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())); 187 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; 195 196 if (popup_ == NULL) { 197 gfx::NativeView popup_parent = 198 location_bar_view_->GetWidget()->GetNativeView(); 199 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. 214 if (!popup_.get()) 215 return; 216#if defined(USE_AURA) 217 views::corewm::SetWindowVisibilityAnimationType( 218 popup_->GetNativeView(), 219 views::corewm::WINDOW_VISIBILITY_ANIMATION_TYPE_VERTICAL); 220 // No animation for autocomplete popup appearance. 221 views::corewm::SetWindowVisibilityAnimationTransition( 222 popup_->GetNativeView(), views::corewm::ANIMATE_HIDE); 223#endif 224 popup_->SetContentsView(this); 225 popup_->StackAbove(omnibox_view_->GetRelativeWindowForPopup()); 226 if (!popup_.get()) { 227 // For some IMEs GetRelativeWindowForPopup triggers the omnibox to lose 228 // focus, thereby closing (and destroying) the popup. 229 // TODO(sky): this won't be needed once we close the omnibox on input 230 // window showing. 231 return; 232 } 233 popup_->Show(); 234 } else { 235 // Animate the popup shrinking, but don't animate growing larger since that 236 // would make the popup feel less responsive. 237 start_bounds_ = GetWidget()->GetWindowBoundsInScreen(); 238 if (target_bounds_.height() < start_bounds_.height()) 239 size_animation_.Show(); 240 else 241 start_bounds_ = target_bounds_; 242 popup_->SetBounds(GetPopupBounds()); 243 } 244 245 Layout(); 246} 247 248gfx::Rect OmniboxPopupContentsView::GetTargetBounds() { 249 return target_bounds_; 250} 251 252void OmniboxPopupContentsView::PaintUpdatesNow() { 253 // TODO(beng): remove this from the interface. 254} 255 256void OmniboxPopupContentsView::OnDragCanceled() { 257 ignore_mouse_drag_ = true; 258} 259 260//////////////////////////////////////////////////////////////////////////////// 261// OmniboxPopupContentsView, OmniboxResultViewModel implementation: 262 263bool OmniboxPopupContentsView::IsSelectedIndex(size_t index) const { 264 return index == model_->selected_line(); 265} 266 267bool OmniboxPopupContentsView::IsHoveredIndex(size_t index) const { 268 return index == model_->hovered_line(); 269} 270 271gfx::Image OmniboxPopupContentsView::GetIconIfExtensionMatch( 272 size_t index) const { 273 if (!HasMatchAt(index)) 274 return gfx::Image(); 275 return model_->GetIconIfExtensionMatch(GetMatchAtIndex(index)); 276} 277 278//////////////////////////////////////////////////////////////////////////////// 279// OmniboxPopupContentsView, AnimationDelegate implementation: 280 281void 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()); 286} 287 288//////////////////////////////////////////////////////////////////////////////// 289// OmniboxPopupContentsView, views::View overrides: 290 291void OmniboxPopupContentsView::Layout() { 292 // Size our children to the available content area. 293 LayoutChildren(); 294 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. 297 SchedulePaint(); 298} 299 300views::View* OmniboxPopupContentsView::GetEventHandlerForPoint( 301 const gfx::Point& point) { 302 return this; 303} 304 305views::View* OmniboxPopupContentsView::GetTooltipHandlerForPoint( 306 const gfx::Point& point) { 307 return NULL; 308} 309 310bool OmniboxPopupContentsView::OnMousePressed( 311 const ui::MouseEvent& event) { 312 ignore_mouse_drag_ = false; // See comment on |ignore_mouse_drag_| in header. 313 if (event.IsLeftMouseButton() || event.IsMiddleMouseButton()) 314 UpdateLineEvent(event, event.IsLeftMouseButton()); 315 return true; 316} 317 318bool OmniboxPopupContentsView::OnMouseDragged( 319 const ui::MouseEvent& event) { 320 if (event.IsLeftMouseButton() || event.IsMiddleMouseButton()) 321 UpdateLineEvent(event, !ignore_mouse_drag_ && event.IsLeftMouseButton()); 322 return true; 323} 324 325void OmniboxPopupContentsView::OnMouseReleased( 326 const ui::MouseEvent& event) { 327 if (ignore_mouse_drag_) { 328 OnMouseCaptureLost(); 329 return; 330 } 331 332 if (event.IsOnlyMiddleMouseButton() || event.IsOnlyLeftMouseButton()) { 333 OpenSelectedLine(event, event.IsOnlyLeftMouseButton() ? CURRENT_TAB : 334 NEW_BACKGROUND_TAB); 335 } 336} 337 338void OmniboxPopupContentsView::OnMouseCaptureLost() { 339 ignore_mouse_drag_ = false; 340} 341 342void OmniboxPopupContentsView::OnMouseMoved( 343 const ui::MouseEvent& event) { 344 model_->SetHoveredLine(GetIndexForPoint(event.location())); 345} 346 347void OmniboxPopupContentsView::OnMouseEntered( 348 const ui::MouseEvent& event) { 349 model_->SetHoveredLine(GetIndexForPoint(event.location())); 350} 351 352void OmniboxPopupContentsView::OnMouseExited( 353 const ui::MouseEvent& event) { 354 model_->SetHoveredLine(OmniboxPopupModel::kNoMatch); 355} 356 357void OmniboxPopupContentsView::OnGestureEvent(ui::GestureEvent* event) { 358 switch (event->type()) { 359 case ui::ET_GESTURE_TAP_DOWN: 360 case ui::ET_GESTURE_SCROLL_BEGIN: 361 case ui::ET_GESTURE_SCROLL_UPDATE: 362 UpdateLineEvent(*event, true); 363 break; 364 case ui::ET_GESTURE_TAP: 365 case ui::ET_GESTURE_SCROLL_END: 366 OpenSelectedLine(*event, CURRENT_TAB); 367 break; 368 default: 369 return; 370 } 371 event->SetHandled(); 372} 373 374//////////////////////////////////////////////////////////////////////////////// 375// OmniboxPopupContentsView, protected: 376 377void OmniboxPopupContentsView::PaintResultViews(gfx::Canvas* canvas) { 378 canvas->DrawColor(result_view_at(0)->GetColor( 379 OmniboxResultView::NORMAL, OmniboxResultView::BACKGROUND)); 380 View::PaintChildren(canvas); 381} 382 383int OmniboxPopupContentsView::CalculatePopupHeight() { 384 DCHECK_GE(static_cast<size_t>(child_count()), model_->result().size()); 385 int popup_height = 0; 386 for (size_t i = model_->result().ShouldHideTopMatch() ? 1 : 0; 387 i < model_->result().size(); ++i) 388 popup_height += child_at(i)->GetPreferredSize().height(); 389 390 // Add enough space on the top and bottom so it looks like there is the same 391 // amount of space between the text and the popup border as there is in the 392 // interior between each row of text. 393 // 394 // Discovering the exact amount of leading and padding around the font is 395 // a bit tricky and platform-specific, but this computation seems to work in 396 // practice. 397 OmniboxResultView* result_view = result_view_at(0); 398 outside_vertical_padding_ = 399 (result_view->GetPreferredSize().height() - 400 result_view->GetTextHeight()); 401 402 return popup_height + 403 views::NonClientFrameView::kClientEdgeThickness + // Top border. 404 outside_vertical_padding_ * 2 + // Padding. 405 bottom_shadow_->height() - kBorderInterior; // Bottom border. 406} 407 408OmniboxResultView* OmniboxPopupContentsView::CreateResultView( 409 OmniboxResultViewModel* model, 410 int model_index, 411 const gfx::FontList& font_list) { 412 return new OmniboxResultView(model, model_index, location_bar_view_, 413 font_list); 414} 415 416//////////////////////////////////////////////////////////////////////////////// 417// OmniboxPopupContentsView, views::View overrides, protected: 418 419void OmniboxPopupContentsView::OnPaint(gfx::Canvas* canvas) { 420 gfx::Rect contents_bounds = GetContentsBounds(); 421 contents_bounds.set_height( 422 contents_bounds.height() - bottom_shadow_->height() + kBorderInterior); 423 424 gfx::Path path; 425 MakeContentsPath(&path, contents_bounds); 426 canvas->Save(); 427 canvas->sk_canvas()->clipPath(path, 428 SkRegion::kIntersect_Op, 429 true /* doAntialias */); 430 PaintResultViews(canvas); 431 canvas->Restore(); 432 433 // Top border. 434 canvas->FillRect( 435 gfx::Rect(0, 0, width(), views::NonClientFrameView::kClientEdgeThickness), 436 ThemeProperties::GetDefaultColor( 437 ThemeProperties::COLOR_TOOLBAR_SEPARATOR)); 438 439 // Bottom border. 440 canvas->TileImageInt(*bottom_shadow_, 0, height() - bottom_shadow_->height(), 441 width(), bottom_shadow_->height()); 442} 443 444void OmniboxPopupContentsView::PaintChildren(gfx::Canvas* canvas) { 445 // We paint our children inside OnPaint(). 446} 447 448//////////////////////////////////////////////////////////////////////////////// 449// OmniboxPopupContentsView, private: 450 451bool OmniboxPopupContentsView::HasMatchAt(size_t index) const { 452 return index < model_->result().size(); 453} 454 455const AutocompleteMatch& OmniboxPopupContentsView::GetMatchAtIndex( 456 size_t index) const { 457 return model_->result().match_at(index); 458} 459 460void OmniboxPopupContentsView::MakeContentsPath( 461 gfx::Path* path, 462 const gfx::Rect& bounding_rect) { 463 SkRect rect; 464 rect.set(SkIntToScalar(bounding_rect.x()), 465 SkIntToScalar(bounding_rect.y()), 466 SkIntToScalar(bounding_rect.right()), 467 SkIntToScalar(bounding_rect.bottom())); 468 path->addRect(rect); 469} 470 471size_t OmniboxPopupContentsView::GetIndexForPoint( 472 const gfx::Point& point) { 473 if (!HitTestPoint(point)) 474 return OmniboxPopupModel::kNoMatch; 475 476 int nb_match = model_->result().size(); 477 DCHECK(nb_match <= child_count()); 478 for (int i = 0; i < nb_match; ++i) { 479 views::View* child = child_at(i); 480 gfx::Point point_in_child_coords(point); 481 View::ConvertPointToTarget(this, child, &point_in_child_coords); 482 if (child->visible() && child->HitTestPoint(point_in_child_coords)) 483 return i; 484 } 485 return OmniboxPopupModel::kNoMatch; 486} 487 488void OmniboxPopupContentsView::UpdateLineEvent( 489 const ui::LocatedEvent& event, 490 bool should_set_selected_line) { 491 size_t index = GetIndexForPoint(event.location()); 492 model_->SetHoveredLine(index); 493 if (HasMatchAt(index) && should_set_selected_line) 494 model_->SetSelectedLine(index, false, false); 495} 496 497void OmniboxPopupContentsView::OpenSelectedLine( 498 const ui::LocatedEvent& event, 499 WindowOpenDisposition disposition) { 500 size_t index = GetIndexForPoint(event.location()); 501 if (!HasMatchAt(index)) 502 return; 503 504 // OpenMatch() may close the popup, which will clear the result set and, by 505 // extension, |match| and its contents. So copy the relevant match out to 506 // make sure it stays alive until the call completes. 507 AutocompleteMatch match = model_->result().match_at(index); 508 omnibox_view_->OpenMatch(match, disposition, GURL(), index); 509} 510 511OmniboxResultView* OmniboxPopupContentsView::result_view_at(size_t i) { 512 return static_cast<OmniboxResultView*>(child_at(static_cast<int>(i))); 513} 514