omnibox_popup_contents_view.cc revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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 26namespace { 27 28// This is the number of pixels in the border image used to draw the bottom 29// border + drop shadow interior to the "visual" border. We lay out assuming 30// that this many pixels inside the border is "in the popup." 31const SkAlpha kGlassPopupAlpha = 240; 32const SkAlpha kOpaquePopupAlpha = 255; 33 34// This is the number of pixels in the border image interior to the actual 35// border. 36const int kBorderInterior = 6; 37 38} // namespace 39 40class OmniboxPopupContentsView::AutocompletePopupWidget 41 : public views::Widget, 42 public base::SupportsWeakPtr<AutocompletePopupWidget> { 43 public: 44 AutocompletePopupWidget() {} 45 virtual ~AutocompletePopupWidget() {} 46 47 private: 48 DISALLOW_COPY_AND_ASSIGN(AutocompletePopupWidget); 49}; 50 51//////////////////////////////////////////////////////////////////////////////// 52// OmniboxPopupContentsView, public: 53 54OmniboxPopupView* OmniboxPopupContentsView::Create( 55 const gfx::FontList& font_list, 56 OmniboxView* omnibox_view, 57 OmniboxEditModel* edit_model, 58 LocationBarView* location_bar_view) { 59 OmniboxPopupContentsView* view = NULL; 60 if (ui::GetDisplayLayout() == ui::LAYOUT_TOUCH) { 61 view = new TouchOmniboxPopupContentsView( 62 font_list, omnibox_view, edit_model, location_bar_view); 63 } else { 64 view = new OmniboxPopupContentsView( 65 font_list, omnibox_view, edit_model, location_bar_view); 66 } 67 68 view->Init(); 69 return view; 70} 71 72OmniboxPopupContentsView::OmniboxPopupContentsView( 73 const gfx::FontList& font_list, 74 OmniboxView* omnibox_view, 75 OmniboxEditModel* edit_model, 76 LocationBarView* location_bar_view) 77 : model_(new OmniboxPopupModel(this, edit_model)), 78 omnibox_view_(omnibox_view), 79 location_bar_view_(location_bar_view), 80 font_list_(font_list), 81 ignore_mouse_drag_(false), 82 size_animation_(this), 83 left_margin_(0), 84 right_margin_(0), 85 outside_vertical_padding_(0), 86 in_popup_init_(false) { 87 // The contents is owned by the LocationBarView. 88 set_owned_by_client(); 89 90 ui::ThemeProvider* theme = location_bar_view_->GetThemeProvider(); 91 bottom_shadow_ = theme->GetImageSkiaNamed(IDR_BUBBLE_B); 92} 93 94void OmniboxPopupContentsView::Init() { 95 // This can't be done in the constructor as at that point we aren't 96 // necessarily our final class yet, and we may have subclasses 97 // overriding CreateResultView. 98 for (size_t i = 0; i < AutocompleteResult::kMaxMatches; ++i) { 99 OmniboxResultView* result_view = CreateResultView(this, i, font_list_); 100 result_view->SetVisible(false); 101 AddChildViewAt(result_view, static_cast<int>(i)); 102 } 103} 104 105OmniboxPopupContentsView::~OmniboxPopupContentsView() { 106 // We don't need to do anything with |popup_| here. The OS either has already 107 // closed the window, in which case it's been deleted, or it will soon, in 108 // which case there's nothing we need to do. 109 CHECK(!in_popup_init_); 110} 111 112gfx::Rect OmniboxPopupContentsView::GetPopupBounds() const { 113 if (!size_animation_.is_animating()) 114 return target_bounds_; 115 116 gfx::Rect current_frame_bounds = start_bounds_; 117 int total_height_delta = target_bounds_.height() - start_bounds_.height(); 118 // Round |current_height_delta| instead of truncating so we won't leave single 119 // white pixels at the bottom of the popup as long when animating very small 120 // height differences. 121 int current_height_delta = static_cast<int>( 122 size_animation_.GetCurrentValue() * total_height_delta - 0.5); 123 current_frame_bounds.set_height( 124 current_frame_bounds.height() + current_height_delta); 125 return current_frame_bounds; 126} 127 128void OmniboxPopupContentsView::LayoutChildren() { 129 gfx::Rect contents_rect = GetContentsBounds(); 130 131 contents_rect.Inset(left_margin_, 132 views::NonClientFrameView::kClientEdgeThickness + 133 outside_vertical_padding_, 134 right_margin_, outside_vertical_padding_); 135 int top = contents_rect.y(); 136 for (size_t i = 0; i < AutocompleteResult::kMaxMatches; ++i) { 137 View* v = child_at(i); 138 if (v->visible()) { 139 v->SetBounds(contents_rect.x(), top, contents_rect.width(), 140 v->GetPreferredSize().height()); 141 top = v->bounds().bottom(); 142 } 143 } 144} 145 146//////////////////////////////////////////////////////////////////////////////// 147// OmniboxPopupContentsView, OmniboxPopupView overrides: 148 149bool OmniboxPopupContentsView::IsOpen() const { 150 return popup_ != NULL; 151} 152 153void OmniboxPopupContentsView::InvalidateLine(size_t line) { 154 OmniboxResultView* result = result_view_at(line); 155 result->Invalidate(); 156 157 if (HasMatchAt(line) && GetMatchAtIndex(line).associated_keyword.get()) { 158 result->ShowKeyword(IsSelectedIndex(line) && 159 model_->selected_line_state() == OmniboxPopupModel::KEYWORD); 160 } 161} 162 163void OmniboxPopupContentsView::UpdatePopupAppearance() { 164 const size_t hidden_matches = model_->result().ShouldHideTopMatch() ? 1 : 0; 165 if (model_->result().size() <= hidden_matches || 166 omnibox_view_->IsImeShowingPopup()) { 167 // No matches or the IME is showing a popup window which may overlap 168 // the omnibox popup window. Close any existing popup. 169 if (popup_ != NULL) { 170 CHECK(!in_popup_init_); 171 172 size_animation_.Stop(); 173 174 // NOTE: Do NOT use CloseNow() here, as we may be deep in a callstack 175 // triggered by the popup receiving a message (e.g. LBUTTONUP), and 176 // destroying the popup would cause us to read garbage when we unwind back 177 // to that level. 178 popup_->Close(); // This will eventually delete the popup. 179 popup_.reset(); 180 } 181 return; 182 } 183 184 // Update the match cached by each row, in the process of doing so make sure 185 // we have enough row views. 186 const size_t result_size = model_->result().size(); 187 for (size_t i = 0; i < result_size; ++i) { 188 OmniboxResultView* view = result_view_at(i); 189 view->SetMatch(GetMatchAtIndex(i)); 190 view->SetVisible(i >= hidden_matches); 191 } 192 for (size_t i = result_size; i < AutocompleteResult::kMaxMatches; ++i) 193 child_at(i)->SetVisible(false); 194 195 gfx::Point top_left_screen_coord; 196 int width; 197 location_bar_view_->GetOmniboxPopupPositioningInfo( 198 &top_left_screen_coord, &width, &left_margin_, &right_margin_); 199 gfx::Rect new_target_bounds(top_left_screen_coord, 200 gfx::Size(width, CalculatePopupHeight())); 201 202 // If we're animating and our target height changes, reset the animation. 203 // NOTE: If we just reset blindly on _every_ update, then when the user types 204 // rapidly we could get "stuck" trying repeatedly to animate shrinking by the 205 // last few pixels to get to one visible result. 206 if (new_target_bounds.height() != target_bounds_.height()) 207 size_animation_.Reset(); 208 target_bounds_ = new_target_bounds; 209 210 if (popup_ == NULL) { 211 gfx::NativeView popup_parent = 212 location_bar_view_->GetWidget()->GetNativeView(); 213 214 // If the popup is currently closed, we need to create it. 215 popup_ = (new AutocompletePopupWidget)->AsWeakPtr(); 216 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); 217 params.can_activate = false; 218 params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW; 219 params.parent = popup_parent; 220 params.bounds = GetPopupBounds(); 221 params.context = popup_parent; 222 in_popup_init_ = true; 223 popup_->Init(params); 224 in_popup_init_ = false; 225#if defined(USE_AURA) 226 views::corewm::SetWindowVisibilityAnimationType( 227 popup_->GetNativeView(), 228 views::corewm::WINDOW_VISIBILITY_ANIMATION_TYPE_VERTICAL); 229 // No animation for autocomplete popup appearance. 230 views::corewm::SetWindowVisibilityAnimationTransition( 231 popup_->GetNativeView(), views::corewm::ANIMATE_HIDE); 232#endif 233 popup_->SetContentsView(this); 234 popup_->StackAbove(omnibox_view_->GetRelativeWindowForPopup()); 235 if (!popup_.get()) { 236 // For some IMEs GetRelativeWindowForPopup triggers the omnibox to lose 237 // focus, thereby closing (and destroying) the popup. 238 // TODO(sky): this won't be needed once we close the omnibox on input 239 // window showing. 240 return; 241 } 242 popup_->Show(); 243 } else { 244 // Animate the popup shrinking, but don't animate growing larger since that 245 // would make the popup feel less responsive. 246 start_bounds_ = GetWidget()->GetWindowBoundsInScreen(); 247 if (target_bounds_.height() < start_bounds_.height()) 248 size_animation_.Show(); 249 else 250 start_bounds_ = target_bounds_; 251 popup_->SetBounds(GetPopupBounds()); 252 } 253 254 Layout(); 255} 256 257gfx::Rect OmniboxPopupContentsView::GetTargetBounds() { 258 return target_bounds_; 259} 260 261void OmniboxPopupContentsView::PaintUpdatesNow() { 262 // TODO(beng): remove this from the interface. 263} 264 265void OmniboxPopupContentsView::OnDragCanceled() { 266 ignore_mouse_drag_ = true; 267} 268 269//////////////////////////////////////////////////////////////////////////////// 270// OmniboxPopupContentsView, OmniboxResultViewModel implementation: 271 272bool OmniboxPopupContentsView::IsSelectedIndex(size_t index) const { 273 return index == model_->selected_line(); 274} 275 276bool OmniboxPopupContentsView::IsHoveredIndex(size_t index) const { 277 return index == model_->hovered_line(); 278} 279 280gfx::Image OmniboxPopupContentsView::GetIconIfExtensionMatch( 281 size_t index) const { 282 if (!HasMatchAt(index)) 283 return gfx::Image(); 284 return model_->GetIconIfExtensionMatch(GetMatchAtIndex(index)); 285} 286 287//////////////////////////////////////////////////////////////////////////////// 288// OmniboxPopupContentsView, AnimationDelegate implementation: 289 290void OmniboxPopupContentsView::AnimationProgressed( 291 const gfx::Animation* animation) { 292 // We should only be running the animation when the popup is already visible. 293 DCHECK(popup_ != NULL); 294 popup_->SetBounds(GetPopupBounds()); 295} 296 297//////////////////////////////////////////////////////////////////////////////// 298// OmniboxPopupContentsView, views::View overrides: 299 300void OmniboxPopupContentsView::Layout() { 301 // Size our children to the available content area. 302 LayoutChildren(); 303 304 // We need to manually schedule a paint here since we are a layered window and 305 // won't implicitly require painting until we ask for one. 306 SchedulePaint(); 307} 308 309views::View* OmniboxPopupContentsView::GetEventHandlerForPoint( 310 const gfx::Point& point) { 311 return this; 312} 313 314views::View* OmniboxPopupContentsView::GetTooltipHandlerForPoint( 315 const gfx::Point& point) { 316 return NULL; 317} 318 319bool OmniboxPopupContentsView::OnMousePressed( 320 const ui::MouseEvent& event) { 321 ignore_mouse_drag_ = false; // See comment on |ignore_mouse_drag_| in header. 322 if (event.IsLeftMouseButton() || event.IsMiddleMouseButton()) 323 UpdateLineEvent(event, event.IsLeftMouseButton()); 324 return true; 325} 326 327bool OmniboxPopupContentsView::OnMouseDragged( 328 const ui::MouseEvent& event) { 329 if (event.IsLeftMouseButton() || event.IsMiddleMouseButton()) 330 UpdateLineEvent(event, !ignore_mouse_drag_ && event.IsLeftMouseButton()); 331 return true; 332} 333 334void OmniboxPopupContentsView::OnMouseReleased( 335 const ui::MouseEvent& event) { 336 if (ignore_mouse_drag_) { 337 OnMouseCaptureLost(); 338 return; 339 } 340 341 if (event.IsOnlyMiddleMouseButton() || event.IsOnlyLeftMouseButton()) { 342 OpenSelectedLine(event, event.IsOnlyLeftMouseButton() ? CURRENT_TAB : 343 NEW_BACKGROUND_TAB); 344 } 345} 346 347void OmniboxPopupContentsView::OnMouseCaptureLost() { 348 ignore_mouse_drag_ = false; 349} 350 351void OmniboxPopupContentsView::OnMouseMoved( 352 const ui::MouseEvent& event) { 353 model_->SetHoveredLine(GetIndexForPoint(event.location())); 354} 355 356void OmniboxPopupContentsView::OnMouseEntered( 357 const ui::MouseEvent& event) { 358 model_->SetHoveredLine(GetIndexForPoint(event.location())); 359} 360 361void OmniboxPopupContentsView::OnMouseExited( 362 const ui::MouseEvent& event) { 363 model_->SetHoveredLine(OmniboxPopupModel::kNoMatch); 364} 365 366void OmniboxPopupContentsView::OnGestureEvent(ui::GestureEvent* event) { 367 switch (event->type()) { 368 case ui::ET_GESTURE_TAP_DOWN: 369 case ui::ET_GESTURE_SCROLL_BEGIN: 370 case ui::ET_GESTURE_SCROLL_UPDATE: 371 UpdateLineEvent(*event, true); 372 break; 373 case ui::ET_GESTURE_TAP: 374 case ui::ET_GESTURE_SCROLL_END: 375 OpenSelectedLine(*event, CURRENT_TAB); 376 break; 377 default: 378 return; 379 } 380 event->SetHandled(); 381} 382 383//////////////////////////////////////////////////////////////////////////////// 384// OmniboxPopupContentsView, protected: 385 386void OmniboxPopupContentsView::PaintResultViews(gfx::Canvas* canvas) { 387 canvas->DrawColor(result_view_at(0)->GetColor( 388 OmniboxResultView::NORMAL, OmniboxResultView::BACKGROUND)); 389 View::PaintChildren(canvas); 390} 391 392int OmniboxPopupContentsView::CalculatePopupHeight() { 393 DCHECK_GE(static_cast<size_t>(child_count()), model_->result().size()); 394 int popup_height = 0; 395 for (size_t i = model_->result().ShouldHideTopMatch() ? 1 : 0; 396 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->visible() && 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