| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/autocomplete/autocomplete_popup_contents_view.
h" | 5 #include "chrome/browser/ui/views/autocomplete/autocomplete_popup_contents_view.
h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <commctrl.h> | 8 #include <commctrl.h> |
| 9 #include <dwmapi.h> | 9 #include <dwmapi.h> |
| 10 #include <objidl.h> | 10 #include <objidl.h> |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 #endif | 46 #endif |
| 47 #endif | 47 #endif |
| 48 #if defined(USE_AURA) | 48 #if defined(USE_AURA) |
| 49 #include "ash/wm/window_animations.h" | 49 #include "ash/wm/window_animations.h" |
| 50 #endif | 50 #endif |
| 51 | 51 |
| 52 namespace { | 52 namespace { |
| 53 | 53 |
| 54 const SkAlpha kGlassPopupAlpha = 240; | 54 const SkAlpha kGlassPopupAlpha = 240; |
| 55 const SkAlpha kOpaquePopupAlpha = 255; | 55 const SkAlpha kOpaquePopupAlpha = 255; |
| 56 |
| 56 // The size delta between the font used for the edit and the result rows. Passed | 57 // The size delta between the font used for the edit and the result rows. Passed |
| 57 // to gfx::Font::DeriveFont. | 58 // to gfx::Font::DeriveFont. |
| 58 #if defined(OS_CHROMEOS) | 59 #if defined(OS_CHROMEOS) |
| 59 // Don't adjust the size on Chrome OS (http://crbug.com/61433). | 60 // Don't adjust the size on Chrome OS (http://crbug.com/61433). |
| 60 const int kEditFontAdjust = 0; | 61 const int kEditFontAdjust = 0; |
| 61 #else | 62 #else |
| 62 const int kEditFontAdjust = -1; | 63 const int kEditFontAdjust = -1; |
| 63 #endif | 64 #endif |
| 64 | 65 |
| 65 // Horizontal padding between the buttons on the opt in promo. | 66 // Horizontal padding between the buttons on the opt in promo. |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 ALLOW_THIS_IN_INITIALIZER_LIST(size_animation_(this)) { | 229 ALLOW_THIS_IN_INITIALIZER_LIST(size_animation_(this)) { |
| 229 // The following little dance is required because set_border() requires a | 230 // The following little dance is required because set_border() requires a |
| 230 // pointer to a non-const object. | 231 // pointer to a non-const object. |
| 231 views::BubbleBorder* bubble_border = | 232 views::BubbleBorder* bubble_border = |
| 232 new views::BubbleBorder(views::BubbleBorder::NONE, | 233 new views::BubbleBorder(views::BubbleBorder::NONE, |
| 233 views::BubbleBorder::NO_SHADOW); | 234 views::BubbleBorder::NO_SHADOW); |
| 234 bubble_border_ = bubble_border; | 235 bubble_border_ = bubble_border; |
| 235 set_border(bubble_border); | 236 set_border(bubble_border); |
| 236 // The contents is owned by the LocationBarView. | 237 // The contents is owned by the LocationBarView. |
| 237 set_parent_owned(false); | 238 set_parent_owned(false); |
| 239 |
| 240 for (size_t i = 0; i < AutocompleteResult::kMaxMatches; ++i) { |
| 241 AutocompleteResultView* result_view = |
| 242 CreateResultView(this, i, result_font_, result_bold_font_); |
| 243 result_view->SetVisible(false); |
| 244 AddChildViewAt(result_view, static_cast<int>(i)); |
| 245 } |
| 238 } | 246 } |
| 239 | 247 |
| 240 AutocompletePopupContentsView::~AutocompletePopupContentsView() { | 248 AutocompletePopupContentsView::~AutocompletePopupContentsView() { |
| 241 // We don't need to do anything with |popup_| here. The OS either has already | 249 // We don't need to do anything with |popup_| here. The OS either has already |
| 242 // closed the window, in which case it's been deleted, or it will soon, in | 250 // closed the window, in which case it's been deleted, or it will soon, in |
| 243 // which case there's nothing we need to do. | 251 // which case there's nothing we need to do. |
| 244 } | 252 } |
| 245 | 253 |
| 246 gfx::Rect AutocompletePopupContentsView::GetPopupBounds() const { | 254 gfx::Rect AutocompletePopupContentsView::GetPopupBounds() const { |
| 247 if (!size_animation_.is_animating()) | 255 if (!size_animation_.is_animating()) |
| (...skipping 25 matching lines...) Expand all Loading... |
| 273 } | 281 } |
| 274 | 282 |
| 275 //////////////////////////////////////////////////////////////////////////////// | 283 //////////////////////////////////////////////////////////////////////////////// |
| 276 // AutocompletePopupContentsView, AutocompletePopupView overrides: | 284 // AutocompletePopupContentsView, AutocompletePopupView overrides: |
| 277 | 285 |
| 278 bool AutocompletePopupContentsView::IsOpen() const { | 286 bool AutocompletePopupContentsView::IsOpen() const { |
| 279 return (popup_ != NULL); | 287 return (popup_ != NULL); |
| 280 } | 288 } |
| 281 | 289 |
| 282 void AutocompletePopupContentsView::InvalidateLine(size_t line) { | 290 void AutocompletePopupContentsView::InvalidateLine(size_t line) { |
| 283 child_at(static_cast<int>(line))->SchedulePaint(); | 291 AutocompleteResultView* result = static_cast<AutocompleteResultView*>( |
| 292 child_at(static_cast<int>(line))); |
| 293 result->Invalidate(); |
| 294 |
| 295 if (HasMatchAt(line) && GetMatchAtIndex(line).associated_keyword.get()) { |
| 296 result->ShowKeyword(IsSelectedIndex(line) && |
| 297 model_->selected_line_state() == AutocompletePopupModel::KEYWORD); |
| 298 } |
| 284 } | 299 } |
| 285 | 300 |
| 286 void AutocompletePopupContentsView::UpdatePopupAppearance() { | 301 void AutocompletePopupContentsView::UpdatePopupAppearance() { |
| 287 if (model_->result().empty()) { | 302 if (model_->result().empty()) { |
| 288 // No matches, close any existing popup. | 303 // No matches, close any existing popup. |
| 289 if (popup_ != NULL) { | 304 if (popup_ != NULL) { |
| 290 size_animation_.Stop(); | 305 size_animation_.Stop(); |
| 306 |
| 291 // NOTE: Do NOT use CloseNow() here, as we may be deep in a callstack | 307 // NOTE: Do NOT use CloseNow() here, as we may be deep in a callstack |
| 292 // triggered by the popup receiving a message (e.g. LBUTTONUP), and | 308 // triggered by the popup receiving a message (e.g. LBUTTONUP), and |
| 293 // destroying the popup would cause us to read garbage when we unwind back | 309 // destroying the popup would cause us to read garbage when we unwind back |
| 294 // to that level. | 310 // to that level. |
| 295 popup_->Close(); // This will eventually delete the popup. | 311 popup_->Close(); // This will eventually delete the popup. |
| 296 popup_.reset(); | 312 popup_.reset(); |
| 297 } | 313 } |
| 298 return; | 314 return; |
| 299 } | 315 } |
| 300 | 316 |
| 301 // Update the match cached by each row, in the process of doing so make sure | 317 // Update the match cached by each row, in the process of doing so make sure |
| 302 // we have enough row views. | 318 // we have enough row views. |
| 303 size_t child_rv_count = child_count(); | 319 size_t child_rv_count = child_count(); |
| 304 if (opt_in_view_) { | 320 if (opt_in_view_) { |
| 305 DCHECK_GT(child_rv_count, 0u); | 321 DCHECK_GT(child_rv_count, 0u); |
| 306 child_rv_count--; | 322 child_rv_count--; |
| 307 } | 323 } |
| 308 for (size_t i = 0; i < model_->result().size(); ++i) { | 324 const size_t result_size = model_->result().size(); |
| 309 AutocompleteResultView* result_view; | 325 for (size_t i = 0; i < result_size; ++i) { |
| 310 if (i >= child_rv_count) { | 326 AutocompleteResultView* view = static_cast<AutocompleteResultView*>( |
| 311 result_view = | 327 child_at(i)); |
| 312 CreateResultView(this, i, result_font_, result_bold_font_); | 328 view->SetMatch(GetMatchAtIndex(i)); |
| 313 AddChildViewAt(result_view, static_cast<int>(i)); | 329 view->SetVisible(true); |
| 314 } else { | |
| 315 result_view = static_cast<AutocompleteResultView*>(child_at(i)); | |
| 316 result_view->SetVisible(true); | |
| 317 } | |
| 318 result_view->SetMatch(GetMatchAtIndex(i)); | |
| 319 } | 330 } |
| 320 for (size_t i = model_->result().size(); i < child_rv_count; ++i) | 331 for (size_t i = result_size; i < child_rv_count; ++i) |
| 321 child_at(i)->SetVisible(false); | 332 child_at(i)->SetVisible(false); |
| 322 | 333 |
| 323 PromoCounter* counter = profile_->GetInstantPromoCounter(); | 334 PromoCounter* counter = profile_->GetInstantPromoCounter(); |
| 324 if (!opt_in_view_ && counter && counter->ShouldShow(base::Time::Now())) { | 335 if (!opt_in_view_ && counter && counter->ShouldShow(base::Time::Now())) { |
| 325 opt_in_view_ = new InstantOptInView(this, result_bold_font_, result_font_); | 336 opt_in_view_ = new InstantOptInView(this, result_bold_font_, result_font_); |
| 326 AddChildView(opt_in_view_); | 337 AddChildView(opt_in_view_); |
| 327 } else if (opt_in_view_ && (!counter || | 338 } else if (opt_in_view_ && (!counter || |
| 328 !counter->ShouldShow(base::Time::Now()))) { | 339 !counter->ShouldShow(base::Time::Now()))) { |
| 329 delete opt_in_view_; | 340 delete opt_in_view_; |
| 330 opt_in_view_ = NULL; | 341 opt_in_view_ = NULL; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 } | 399 } |
| 389 | 400 |
| 390 void AutocompletePopupContentsView::OnDragCanceled() { | 401 void AutocompletePopupContentsView::OnDragCanceled() { |
| 391 ignore_mouse_drag_ = true; | 402 ignore_mouse_drag_ = true; |
| 392 } | 403 } |
| 393 | 404 |
| 394 //////////////////////////////////////////////////////////////////////////////// | 405 //////////////////////////////////////////////////////////////////////////////// |
| 395 // AutocompletePopupContentsView, AutocompleteResultViewModel implementation: | 406 // AutocompletePopupContentsView, AutocompleteResultViewModel implementation: |
| 396 | 407 |
| 397 bool AutocompletePopupContentsView::IsSelectedIndex(size_t index) const { | 408 bool AutocompletePopupContentsView::IsSelectedIndex(size_t index) const { |
| 398 return HasMatchAt(index) ? index == model_->selected_line() : false; | 409 return index == model_->selected_line(); |
| 399 } | 410 } |
| 400 | 411 |
| 401 bool AutocompletePopupContentsView::IsHoveredIndex(size_t index) const { | 412 bool AutocompletePopupContentsView::IsHoveredIndex(size_t index) const { |
| 402 return HasMatchAt(index) ? index == model_->hovered_line() : false; | 413 return index == model_->hovered_line(); |
| 403 } | 414 } |
| 404 | 415 |
| 405 const SkBitmap* AutocompletePopupContentsView::GetIconIfExtensionMatch( | 416 const SkBitmap* AutocompletePopupContentsView::GetIconIfExtensionMatch( |
| 406 size_t index) const { | 417 size_t index) const { |
| 407 if (!HasMatchAt(index)) | 418 if (!HasMatchAt(index)) |
| 408 return NULL; | 419 return NULL; |
| 409 return model_->GetIconIfExtensionMatch(GetMatchAtIndex(index)); | 420 return model_->GetIconIfExtensionMatch(GetMatchAtIndex(index)); |
| 410 } | 421 } |
| 411 | 422 |
| 412 //////////////////////////////////////////////////////////////////////////////// | 423 //////////////////////////////////////////////////////////////////////////////// |
| (...skipping 15 matching lines...) Expand all Loading... |
| 428 // Size our children to the available content area. | 439 // Size our children to the available content area. |
| 429 LayoutChildren(); | 440 LayoutChildren(); |
| 430 | 441 |
| 431 // We need to manually schedule a paint here since we are a layered window and | 442 // We need to manually schedule a paint here since we are a layered window and |
| 432 // won't implicitly require painting until we ask for one. | 443 // won't implicitly require painting until we ask for one. |
| 433 SchedulePaint(); | 444 SchedulePaint(); |
| 434 } | 445 } |
| 435 | 446 |
| 436 views::View* AutocompletePopupContentsView::GetEventHandlerForPoint( | 447 views::View* AutocompletePopupContentsView::GetEventHandlerForPoint( |
| 437 const gfx::Point& point) { | 448 const gfx::Point& point) { |
| 438 // If there is no opt in view, then we want all mouse events. Otherwise let | 449 // If there is no opt in view then we want all mouse events. Otherwise, let |
| 439 // any descendants of the opt-in view get mouse events. | 450 // any descendants of the opt-in view get mouse events. |
| 440 if (!opt_in_view_) | 451 if (!opt_in_view_) |
| 441 return this; | 452 return this; |
| 442 | 453 |
| 443 views::View* child = views::View::GetEventHandlerForPoint(point); | 454 views::View* child = views::View::GetEventHandlerForPoint(point); |
| 444 views::View* ancestor = child; | 455 views::View* ancestor = child; |
| 445 while (ancestor && ancestor != opt_in_view_) | 456 while (ancestor && ancestor != opt_in_view_) |
| 446 ancestor = ancestor->parent(); | 457 ancestor = ancestor->parent(); |
| 447 return ancestor ? child : this; | 458 return ancestor ? child : this; |
| 448 } | 459 } |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 639 void AutocompletePopupContentsView::OpenIndex( | 650 void AutocompletePopupContentsView::OpenIndex( |
| 640 size_t index, | 651 size_t index, |
| 641 WindowOpenDisposition disposition) { | 652 WindowOpenDisposition disposition) { |
| 642 if (!HasMatchAt(index)) | 653 if (!HasMatchAt(index)) |
| 643 return; | 654 return; |
| 644 | 655 |
| 645 // OpenMatch() may close the popup, which will clear the result set and, by | 656 // OpenMatch() may close the popup, which will clear the result set and, by |
| 646 // extension, |match| and its contents. So copy the relevant match out to | 657 // extension, |match| and its contents. So copy the relevant match out to |
| 647 // make sure it stays alive until the call completes. | 658 // make sure it stays alive until the call completes. |
| 648 AutocompleteMatch match = model_->result().match_at(index); | 659 AutocompleteMatch match = model_->result().match_at(index); |
| 649 string16 keyword; | |
| 650 const bool is_keyword_hint = model_->GetKeywordForMatch(match, &keyword); | |
| 651 omnibox_view_->OpenMatch(match, disposition, GURL(), index, | 660 omnibox_view_->OpenMatch(match, disposition, GURL(), index, |
| 652 is_keyword_hint ? string16() : keyword); | 661 match.keyword); |
| 653 } | 662 } |
| 654 | 663 |
| 655 size_t AutocompletePopupContentsView::GetIndexForPoint( | 664 size_t AutocompletePopupContentsView::GetIndexForPoint( |
| 656 const gfx::Point& point) { | 665 const gfx::Point& point) { |
| 657 if (!HitTest(point)) | 666 if (!HitTest(point)) |
| 658 return AutocompletePopupModel::kNoMatch; | 667 return AutocompletePopupModel::kNoMatch; |
| 659 | 668 |
| 660 int nb_match = model_->result().size(); | 669 int nb_match = model_->result().size(); |
| 661 DCHECK(nb_match <= child_count()); | 670 DCHECK(nb_match <= child_count()); |
| 662 for (int i = 0; i < nb_match; ++i) { | 671 for (int i = 0; i < nb_match; ++i) { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 697 opt_in_view_ = NULL; | 706 opt_in_view_ = NULL; |
| 698 PromoCounter* counter = profile_->GetInstantPromoCounter(); | 707 PromoCounter* counter = profile_->GetInstantPromoCounter(); |
| 699 DCHECK(counter); | 708 DCHECK(counter); |
| 700 counter->Hide(); | 709 counter->Hide(); |
| 701 if (opt_in) { | 710 if (opt_in) { |
| 702 browser::ShowInstantConfirmDialogIfNecessary( | 711 browser::ShowInstantConfirmDialogIfNecessary( |
| 703 location_bar_->GetWidget()->GetNativeWindow(), profile_); | 712 location_bar_->GetWidget()->GetNativeWindow(), profile_); |
| 704 } | 713 } |
| 705 UpdatePopupAppearance(); | 714 UpdatePopupAppearance(); |
| 706 } | 715 } |
| OLD | NEW |