omnibox_popup_contents_view.cc revision 0f1bc08d4cfcc34181b0b5cbf065c40f687bf740
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::SetWindowVisibilityAnimationTransition( 218 popup_->GetNativeView(), views::corewm::ANIMATE_NONE); 219#endif 220 popup_->SetContentsView(this); 221 popup_->StackAbove(omnibox_view_->GetRelativeWindowForPopup()); 222 if (!popup_.get()) { 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 226 // window showing. 227 return; 228 } 229 popup_->Show(); 230 } else { 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(); 236 else 237 start_bounds_ = target_bounds_; 238 popup_->SetBounds(GetPopupBounds()); 239 } 240 241 Layout(); 242} 243 244gfx::Rect OmniboxPopupContentsView::GetTargetBounds() { 245 return target_bounds_; 246} 247 248void OmniboxPopupContentsView::PaintUpdatesNow() { 249 // TODO(beng): remove this from the interface. 250} 251 252void OmniboxPopupContentsView::OnDragCanceled() { 253 ignore_mouse_drag_ = true; 254} 255 256//////////////////////////////////////////////////////////////////////////////// 257// OmniboxPopupContentsView, OmniboxResultViewModel implementation: 258 259bool OmniboxPopupContentsView::IsSelectedIndex(size_t index) const { 260 return index == model_->selected_line(); 261} 262 263bool OmniboxPopupContentsView::IsHoveredIndex(size_t index) const { 264 return index == model_->hovered_line(); 265} 266 267gfx::Image OmniboxPopupContentsView::GetIconIfExtensionMatch( 268 size_t index) const { 269 if (!HasMatchAt(index)) 270 return gfx::Image(); 271 return model_->GetIconIfExtensionMatch(GetMatchAtIndex(index)); 272} 273 274//////////////////////////////////////////////////////////////////////////////// 275// OmniboxPopupContentsView, AnimationDelegate implementation: 276 277void 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()); 282} 283 284//////////////////////////////////////////////////////////////////////////////// 285// OmniboxPopupContentsView, views::View overrides: 286 287void OmniboxPopupContentsView::Layout() { 288 // Size our children to the available content area. 289 LayoutChildren(); 290 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. 293 SchedulePaint(); 294} 295 296views::View* OmniboxPopupContentsView::GetEventHandlerForPoint( 297 const gfx::Point& point) { 298 return this; 299} 300 301views::View* OmniboxPopupContentsView::GetTooltipHandlerForPoint( 302 const gfx::Point& point) { 303 return NULL; 304} 305 306bool 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()); 311 return true; 312} 313 314bool OmniboxPopupContentsView::OnMouseDragged( 315 const ui::MouseEvent& event) { 316 if (event.IsLeftMouseButton() || event.IsMiddleMouseButton()) 317 UpdateLineEvent(event, !ignore_mouse_drag_ && event.IsLeftMouseButton()); 318 return true; 319} 320 321void OmniboxPopupContentsView::OnMouseReleased( 322 const ui::MouseEvent& event) { 323 if (ignore_mouse_drag_) { 324 OnMouseCaptureLost(); 325 return; 326 } 327 328 if (event.IsOnlyMiddleMouseButton() || event.IsOnlyLeftMouseButton()) { 329 OpenSelectedLine(event, event.IsOnlyLeftMouseButton() ? CURRENT_TAB : 330 NEW_BACKGROUND_TAB); 331 } 332} 333 334void OmniboxPopupContentsView::OnMouseCaptureLost() { 335 ignore_mouse_drag_ = false; 336} 337 338void OmniboxPopupContentsView::OnMouseMoved( 339 const ui::MouseEvent& event) { 340 model_->SetHoveredLine(GetIndexForPoint(event.location())); 341} 342 343void OmniboxPopupContentsView::OnMouseEntered( 344 const ui::MouseEvent& event) { 345 model_->SetHoveredLine(GetIndexForPoint(event.location())); 346} 347 348void OmniboxPopupContentsView::OnMouseExited( 349 const ui::MouseEvent& event) { 350 model_->SetHoveredLine(OmniboxPopupModel::kNoMatch); 351} 352 353void 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); 359 break; 360 case ui::ET_GESTURE_TAP: 361 case ui::ET_GESTURE_SCROLL_END: 362 OpenSelectedLine(*event, CURRENT_TAB); 363 break; 364 default: 365 return; 366 } 367 event->SetHandled(); 368} 369 370//////////////////////////////////////////////////////////////////////////////// 371// OmniboxPopupContentsView, protected: 372 373void OmniboxPopupContentsView::PaintResultViews(gfx::Canvas* canvas) { 374 canvas->DrawColor(result_view_at(0)->GetColor( 375 OmniboxResultView::NORMAL, OmniboxResultView::BACKGROUND)); 376 View::PaintChildren(canvas); 377} 378 379int 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(); 385 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. 389 // 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 392 // practice. 393 OmniboxResultView* result_view = result_view_at(0); 394 outside_vertical_padding_ = 395 (result_view->GetPreferredSize().height() - 396 result_view->GetTextHeight()); 397 398 return popup_height + 399 views::NonClientFrameView::kClientEdgeThickness + // Top border. 400 outside_vertical_padding_ * 2 + // Padding. 401 bottom_shadow_->height() - kBorderInterior; // Bottom border. 402} 403 404OmniboxResultView* OmniboxPopupContentsView::CreateResultView( 405 OmniboxResultViewModel* model, 406 int model_index, 407 const gfx::FontList& font_list) { 408 return new OmniboxResultView(model, model_index, location_bar_view_, 409 font_list); 410} 411 412//////////////////////////////////////////////////////////////////////////////// 413// OmniboxPopupContentsView, views::View overrides, protected: 414 415void OmniboxPopupContentsView::OnPaint(gfx::Canvas* canvas) { 416 gfx::Rect contents_bounds = GetContentsBounds(); 417 contents_bounds.set_height( 418 contents_bounds.height() - bottom_shadow_->height() + kBorderInterior); 419 420 gfx::Path path; 421 MakeContentsPath(&path, contents_bounds); 422 canvas->Save(); 423 canvas->sk_canvas()->clipPath(path, 424 SkRegion::kIntersect_Op, 425 true /* doAntialias */); 426 PaintResultViews(canvas); 427 canvas->Restore(); 428 429 // Top border. 430 canvas->FillRect( 431 gfx::Rect(0, 0, width(), views::NonClientFrameView::kClientEdgeThickness), 432 ThemeProperties::GetDefaultColor( 433 ThemeProperties::COLOR_TOOLBAR_SEPARATOR)); 434 435 // Bottom border. 436 canvas->TileImageInt(*bottom_shadow_, 0, height() - bottom_shadow_->height(), 437 width(), bottom_shadow_->height()); 438} 439 440void OmniboxPopupContentsView::PaintChildren(gfx::Canvas* canvas) { 441 // We paint our children inside OnPaint(). 442} 443 444//////////////////////////////////////////////////////////////////////////////// 445// OmniboxPopupContentsView, private: 446 447bool OmniboxPopupContentsView::HasMatchAt(size_t index) const { 448 return index < model_->result().size(); 449} 450 451const AutocompleteMatch& OmniboxPopupContentsView::GetMatchAtIndex( 452 size_t index) const { 453 return model_->result().match_at(index); 454} 455 456void OmniboxPopupContentsView::MakeContentsPath( 457 gfx::Path* path, 458 const gfx::Rect& bounding_rect) { 459 SkRect rect; 460 rect.set(SkIntToScalar(bounding_rect.x()), 461 SkIntToScalar(bounding_rect.y()), 462 SkIntToScalar(bounding_rect.right()), 463 SkIntToScalar(bounding_rect.bottom())); 464 path->addRect(rect); 465} 466 467size_t OmniboxPopupContentsView::GetIndexForPoint( 468 const gfx::Point& point) { 469 if (!HitTestPoint(point)) 470 return OmniboxPopupModel::kNoMatch; 471 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)) 479 return i; 480 } 481 return OmniboxPopupModel::kNoMatch; 482} 483 484void 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); 491} 492 493void OmniboxPopupContentsView::OpenSelectedLine( 494 const ui::LocatedEvent& event, 495 WindowOpenDisposition disposition) { 496 size_t index = GetIndexForPoint(event.location()); 497 if (!HasMatchAt(index)) 498 return; 499 500 // OpenMatch() may close the popup, which will clear the result set and, by 501 // extension, |match| and its contents. So copy the relevant match out to 502 // make sure it stays alive until the call completes. 503 AutocompleteMatch match = model_->result().match_at(index); 504 omnibox_view_->OpenMatch(match, disposition, GURL(), index); 505} 506 507OmniboxResultView* OmniboxPopupContentsView::result_view_at(size_t i) { 508 return static_cast<OmniboxResultView*>(child_at(static_cast<int>(i))); 509} 510