| 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/search_view_controller.h" | 5 #include "chrome/browser/ui/views/search_view_controller.h" |
| 6 | 6 |
| 7 #include "chrome/browser/profiles/profile.h" |
| 8 #include "chrome/browser/search_engines/search_engine_type.h" |
| 9 #include "chrome/browser/search_engines/template_url.h" |
| 10 #include "chrome/browser/search_engines/template_url_prepopulate_data.h" |
| 11 #include "chrome/browser/search_engines/template_url_service.h" |
| 12 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 7 #include "chrome/browser/ui/search/search_model.h" | 13 #include "chrome/browser/ui/search/search_model.h" |
| 8 #include "chrome/browser/ui/search/search_tab_helper.h" | 14 #include "chrome/browser/ui/search/search_tab_helper.h" |
| 9 #include "chrome/browser/ui/search/search_types.h" | 15 #include "chrome/browser/ui/search/search_types.h" |
| 10 #include "chrome/browser/ui/search/search_ui.h" | 16 #include "chrome/browser/ui/search/search_ui.h" |
| 11 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 17 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 12 #include "chrome/browser/ui/views/frame/contents_container.h" | 18 #include "chrome/browser/ui/views/frame/contents_container.h" |
| 13 #include "chrome/browser/ui/views/location_bar/location_bar_container.h" | 19 #include "chrome/browser/ui/views/location_bar/location_bar_container.h" |
| 14 #include "chrome/browser/ui/webui/instant_ui.h" | 20 #include "chrome/browser/ui/webui/instant_ui.h" |
| 15 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
| 16 #include "grit/theme_resources.h" | 22 #include "grit/theme_resources.h" |
| 17 #include "ui/base/resource/resource_bundle.h" | 23 #include "ui/base/resource/resource_bundle.h" |
| 18 #include "ui/compositor/layer.h" | 24 #include "ui/compositor/layer.h" |
| 19 #include "ui/compositor/scoped_layer_animation_settings.h" | 25 #include "ui/compositor/scoped_layer_animation_settings.h" |
| 20 #include "ui/gfx/canvas.h" | 26 #include "ui/gfx/canvas.h" |
| 27 #include "ui/views/controls/image_view.h" |
| 28 #include "ui/views/controls/label.h" |
| 21 #include "ui/views/controls/webview/webview.h" | 29 #include "ui/views/controls/webview/webview.h" |
| 22 #include "ui/views/layout/fill_layout.h" | 30 #include "ui/views/layout/fill_layout.h" |
| 23 #include "ui/views/layout/layout_manager.h" | 31 #include "ui/views/layout/layout_manager.h" |
| 24 | 32 |
| 25 #if defined(USE_AURA) | 33 #if defined(USE_AURA) |
| 26 #include "ui/aura/window.h" | 34 #include "ui/aura/window.h" |
| 27 #endif | 35 #endif |
| 28 | 36 |
| 29 namespace { | 37 namespace { |
| 30 | 38 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 chrome::search::kResultsSeparatorColor); | 97 chrome::search::kResultsSeparatorColor); |
| 90 } | 98 } |
| 91 | 99 |
| 92 private: | 100 private: |
| 93 views::View* ntp_view_; | 101 views::View* ntp_view_; |
| 94 views::View* omnibox_popup_view_parent_; | 102 views::View* omnibox_popup_view_parent_; |
| 95 | 103 |
| 96 DISALLOW_COPY_AND_ASSIGN(SearchContainerView); | 104 DISALLOW_COPY_AND_ASSIGN(SearchContainerView); |
| 97 }; | 105 }; |
| 98 | 106 |
| 99 // FixedSizeLayoutManager ------------------------------------------------------ | |
| 100 | |
| 101 // LayoutManager that returns a specific preferred size. | |
| 102 | |
| 103 class FixedSizeLayoutManager : public views::LayoutManager { | |
| 104 public: | |
| 105 explicit FixedSizeLayoutManager(const gfx::Size& size) | |
| 106 : preferred_size_(size) { | |
| 107 } | |
| 108 virtual ~FixedSizeLayoutManager() {} | |
| 109 | |
| 110 // views::LayoutManager overrides: | |
| 111 virtual void Layout(views::View* host) OVERRIDE {} | |
| 112 virtual gfx::Size GetPreferredSize(views::View* host) OVERRIDE { | |
| 113 return preferred_size_; | |
| 114 } | |
| 115 | |
| 116 private: | |
| 117 const gfx::Size preferred_size_; | |
| 118 | |
| 119 DISALLOW_COPY_AND_ASSIGN(FixedSizeLayoutManager); | |
| 120 }; | |
| 121 | |
| 122 // NTPViewBackground ----------------------------------------------------------- | 107 // NTPViewBackground ----------------------------------------------------------- |
| 123 | 108 |
| 124 // Background for the NTP view. | 109 // Background for the NTP view. |
| 125 class NTPViewBackground : public views::Background { | 110 class NTPViewBackground : public views::Background { |
| 126 public: | 111 public: |
| 127 NTPViewBackground() {} | 112 NTPViewBackground() {} |
| 128 virtual ~NTPViewBackground() {} | 113 virtual ~NTPViewBackground() {} |
| 129 | 114 |
| 130 // views::Background overrides: | 115 // views::Background overrides: |
| 131 virtual void Paint(gfx::Canvas* canvas, views::View* view) const OVERRIDE { | 116 virtual void Paint(gfx::Canvas* canvas, views::View* view) const OVERRIDE { |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 parent()->Layout(); | 214 parent()->Layout(); |
| 230 if (child_visible_ != child->visible()) { | 215 if (child_visible_ != child->visible()) { |
| 231 child_visible_ = child->visible(); | 216 child_visible_ = child->visible(); |
| 232 search_view_controller_->PopupVisibilityChanged(); | 217 search_view_controller_->PopupVisibilityChanged(); |
| 233 } | 218 } |
| 234 } | 219 } |
| 235 | 220 |
| 236 // SearchViewController -------------------------------------------------------- | 221 // SearchViewController -------------------------------------------------------- |
| 237 | 222 |
| 238 SearchViewController::SearchViewController( | 223 SearchViewController::SearchViewController( |
| 224 content::BrowserContext* browser_context, |
| 239 ContentsContainer* contents_container) | 225 ContentsContainer* contents_container) |
| 240 : contents_container_(contents_container), | 226 : browser_context_(browser_context), |
| 227 contents_container_(contents_container), |
| 241 location_bar_container_(NULL), | 228 location_bar_container_(NULL), |
| 242 state_(STATE_NOT_VISIBLE), | 229 state_(STATE_NOT_VISIBLE), |
| 243 tab_contents_(NULL), | 230 tab_contents_(NULL), |
| 244 search_container_(NULL), | 231 search_container_(NULL), |
| 245 ntp_view_(NULL), | 232 ntp_view_(NULL), |
| 246 logo_view_(NULL), | |
| 247 content_view_(NULL), | 233 content_view_(NULL), |
| 248 omnibox_popup_view_parent_(NULL) { | 234 omnibox_popup_view_parent_(NULL) { |
| 249 omnibox_popup_view_parent_ = new OmniboxPopupViewParent(this); | 235 omnibox_popup_view_parent_ = new OmniboxPopupViewParent(this); |
| 250 } | 236 } |
| 251 | 237 |
| 252 SearchViewController::~SearchViewController() { | 238 SearchViewController::~SearchViewController() { |
| 253 if (search_model()) | 239 if (search_model()) |
| 254 search_model()->RemoveObserver(this); | 240 search_model()->RemoveObserver(this); |
| 255 | 241 |
| 256 // If the |omnibox_popup_view_| isn't parented, delete it. Otherwise it'll be | 242 // If the |omnibox_popup_view_| isn't parented, delete it. Otherwise it'll be |
| (...skipping 17 matching lines...) Expand all Loading... |
| 274 search_model()->AddObserver(this); | 260 search_model()->AddObserver(this); |
| 275 | 261 |
| 276 UpdateState(); | 262 UpdateState(); |
| 277 } | 263 } |
| 278 | 264 |
| 279 void SearchViewController::StackAtTop() { | 265 void SearchViewController::StackAtTop() { |
| 280 #if defined(USE_AURA) | 266 #if defined(USE_AURA) |
| 281 if (search_container_) { | 267 if (search_container_) { |
| 282 StackViewsLayerAtTop(search_container_); | 268 StackViewsLayerAtTop(search_container_); |
| 283 StackViewsLayerAtTop(ntp_view_); | 269 StackViewsLayerAtTop(ntp_view_); |
| 284 StackViewsLayerAtTop(logo_view_); | 270 StackViewsLayerAtTop(GetLogoView()); |
| 285 StackWebViewLayerAtTop(content_view_); | 271 StackWebViewLayerAtTop(content_view_); |
| 286 } | 272 } |
| 287 #else | 273 #else |
| 288 NOTIMPLEMENTED(); | 274 NOTIMPLEMENTED(); |
| 289 #endif | 275 #endif |
| 290 location_bar_container_->StackAtTop(); | 276 location_bar_container_->StackAtTop(); |
| 291 } | 277 } |
| 292 | 278 |
| 293 void SearchViewController::InstantReady() { | 279 void SearchViewController::InstantReady() { |
| 294 } | 280 } |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 settings.AddObserver(this); | 364 settings.AddObserver(this); |
| 379 settings.SetTransitionDuration( | 365 settings.SetTransitionDuration( |
| 380 base::TimeDelta::FromMilliseconds(180 * factor)); | 366 base::TimeDelta::FromMilliseconds(180 * factor)); |
| 381 settings.SetTweenType(ui::Tween::EASE_IN_OUT); | 367 settings.SetTweenType(ui::Tween::EASE_IN_OUT); |
| 382 gfx::Rect bounds(ntp_layer->bounds()); | 368 gfx::Rect bounds(ntp_layer->bounds()); |
| 383 bounds.set_height(1); | 369 bounds.set_height(1); |
| 384 ntp_layer->SetBounds(bounds); | 370 ntp_layer->SetBounds(bounds); |
| 385 } | 371 } |
| 386 | 372 |
| 387 { | 373 { |
| 388 ui::Layer* logo_layer = logo_view_->layer(); | 374 ui::Layer* logo_layer = GetLogoView()->layer(); |
| 389 ui::ScopedLayerAnimationSettings settings(logo_layer->GetAnimator()); | 375 ui::ScopedLayerAnimationSettings settings(logo_layer->GetAnimator()); |
| 390 settings.SetTransitionDuration( | 376 settings.SetTransitionDuration( |
| 391 base::TimeDelta::FromMilliseconds(135 * factor)); | 377 base::TimeDelta::FromMilliseconds(135 * factor)); |
| 392 settings.SetTweenType(ui::Tween::EASE_IN_OUT); | 378 settings.SetTweenType(ui::Tween::EASE_IN_OUT); |
| 393 gfx::Rect bounds(logo_layer->bounds()); | 379 gfx::Rect bounds(logo_layer->bounds()); |
| 394 bounds.set_y(bounds.y() - 100); | 380 bounds.set_y(bounds.y() - 100); |
| 395 logo_layer->SetBounds(bounds); | 381 logo_layer->SetBounds(bounds); |
| 396 logo_layer->SetOpacity(0.0f); | 382 logo_layer->SetOpacity(0.0f); |
| 397 } | 383 } |
| 398 | 384 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 411 } | 397 } |
| 412 | 398 |
| 413 void SearchViewController::CreateViews() { | 399 void SearchViewController::CreateViews() { |
| 414 DCHECK(!ntp_view_); | 400 DCHECK(!ntp_view_); |
| 415 | 401 |
| 416 ntp_view_ = new views::View; | 402 ntp_view_ = new views::View; |
| 417 ntp_view_->set_background(new NTPViewBackground); | 403 ntp_view_->set_background(new NTPViewBackground); |
| 418 ntp_view_->SetPaintToLayer(true); | 404 ntp_view_->SetPaintToLayer(true); |
| 419 ntp_view_->layer()->SetMasksToBounds(true); | 405 ntp_view_->layer()->SetMasksToBounds(true); |
| 420 | 406 |
| 421 logo_view_ = new views::View; | 407 const TemplateURL* default_provider = |
| 422 logo_view_->SetLayoutManager( | 408 TemplateURLServiceFactory::GetForProfile( |
| 423 new FixedSizeLayoutManager(gfx::Size(300, 200))); | 409 Profile::FromBrowserContext(browser_context_))-> |
| 424 logo_view_->set_background( | 410 GetDefaultSearchProvider(); |
| 425 views::Background::CreateSolidBackground(SK_ColorRED)); | 411 |
| 426 logo_view_->SetPaintToLayer(true); | 412 #if defined(GOOGLE_CHROME_BUILD) |
| 427 logo_view_->SetFillsBoundsOpaquely(false); | 413 if (default_provider && |
| 414 (TemplateURLPrepopulateData::GetEngineType(default_provider->url()) == |
| 415 SEARCH_ENGINE_GOOGLE)) { |
| 416 default_provider_logo_.reset(new views::ImageView()); |
| 417 default_provider_logo_->set_owned_by_client(); |
| 418 default_provider_logo_->SetImage(ui::ResourceBundle::GetSharedInstance(). |
| 419 GetImageSkiaNamed(IDR_GOOGLE_LOGO_LG)); |
| 420 default_provider_logo_->SetPaintToLayer(true); |
| 421 default_provider_logo_->SetFillsBoundsOpaquely(false); |
| 422 } |
| 423 #endif |
| 424 |
| 425 if (!default_provider_logo_.get()) { |
| 426 default_provider_name_.reset(new views::Label( |
| 427 default_provider ? default_provider->short_name() : string16())); |
| 428 default_provider_name_->set_owned_by_client(); |
| 429 // TODO(msw): Use a transparent background color as a workaround to support |
| 430 // using Labels' view layers via gfx::Canvas::NO_SUBPIXEL_RENDERING. |
| 431 default_provider_name_->SetBackgroundColor(0x00000000); |
| 432 default_provider_name_->set_background( |
| 433 views::Background::CreateSolidBackground(SK_ColorRED)); |
| 434 default_provider_name_->SetEnabledColor(SK_ColorRED); |
| 435 default_provider_name_->SetFont( |
| 436 default_provider_name_->font().DeriveFont(75, gfx::Font::BOLD)); |
| 437 default_provider_name_->SetPaintToLayer(true); |
| 438 default_provider_name_->SetFillsBoundsOpaquely(false); |
| 439 } |
| 428 | 440 |
| 429 // Reparent the main web contents view out of |contents_container_| and | 441 // Reparent the main web contents view out of |contents_container_| and |
| 430 // in to |ntp_view_| below. Reparent back in destructor. | 442 // in to |ntp_view_| below. Reparent back in destructor. |
| 431 content_view_ = contents_container_->active(); | 443 content_view_ = contents_container_->active(); |
| 432 DCHECK(content_view_); | 444 DCHECK(content_view_); |
| 433 contents_container_->SetActive(NULL); | 445 contents_container_->SetActive(NULL); |
| 434 | 446 |
| 447 views::View* logo_view = GetLogoView(); |
| 448 DCHECK(logo_view); |
| 435 ntp_view_->SetLayoutManager( | 449 ntp_view_->SetLayoutManager( |
| 436 new NTPViewLayoutManager(logo_view_, content_view_)); | 450 new NTPViewLayoutManager(logo_view, content_view_)); |
| 437 ntp_view_->AddChildView(logo_view_); | 451 ntp_view_->AddChildView(logo_view); |
| 438 ntp_view_->AddChildView(content_view_); | 452 ntp_view_->AddChildView(content_view_); |
| 439 | 453 |
| 440 search_container_ = | 454 search_container_ = |
| 441 new SearchContainerView(ntp_view_, omnibox_popup_view_parent_); | 455 new SearchContainerView(ntp_view_, omnibox_popup_view_parent_); |
| 442 search_container_->SetPaintToLayer(true); | 456 search_container_->SetPaintToLayer(true); |
| 443 search_container_->SetLayoutManager(new views::FillLayout); | 457 search_container_->SetLayoutManager(new views::FillLayout); |
| 444 search_container_->layer()->SetMasksToBounds(true); | 458 search_container_->layer()->SetMasksToBounds(true); |
| 445 | 459 |
| 446 contents_container_->SetOverlay(search_container_); | 460 contents_container_->SetOverlay(search_container_); |
| 447 } | 461 } |
| 448 | 462 |
| 463 views::View* SearchViewController::GetLogoView() const { |
| 464 if (default_provider_logo_.get()) |
| 465 return default_provider_logo_.get(); |
| 466 return default_provider_name_.get(); |
| 467 } |
| 468 |
| 449 void SearchViewController::DestroyViews() { | 469 void SearchViewController::DestroyViews() { |
| 450 if (!search_container_) | 470 if (!search_container_) |
| 451 return; | 471 return; |
| 452 | 472 |
| 453 // We persist the parent of the omnibox so that we don't have to inject a new | 473 // We persist the parent of the omnibox so that we don't have to inject a new |
| 454 // parent into ToolbarView. | 474 // parent into ToolbarView. |
| 455 omnibox_popup_view_parent_->parent()->RemoveChildView( | 475 omnibox_popup_view_parent_->parent()->RemoveChildView( |
| 456 omnibox_popup_view_parent_); | 476 omnibox_popup_view_parent_); |
| 457 | 477 |
| 458 // Restore control/parenting of the web_contents back to the | 478 // Restore control/parenting of the web_contents back to the |
| 459 // |main_contents_view_|. | 479 // |main_contents_view_|. |
| 460 ntp_view_->SetLayoutManager(NULL); | 480 ntp_view_->SetLayoutManager(NULL); |
| 461 ntp_view_->RemoveChildView(content_view_); | 481 ntp_view_->RemoveChildView(content_view_); |
| 462 if (content_view_->web_contents()) | 482 if (content_view_->web_contents()) |
| 463 content_view_->web_contents()->GetNativeView()->layer()->SetOpacity(1.0f); | 483 content_view_->web_contents()->GetNativeView()->layer()->SetOpacity(1.0f); |
| 464 contents_container_->SetActive(content_view_); | 484 contents_container_->SetActive(content_view_); |
| 465 contents_container_->SetOverlay(NULL); | 485 contents_container_->SetOverlay(NULL); |
| 466 | 486 |
| 467 delete search_container_; | 487 delete search_container_; |
| 468 search_container_ = NULL; | 488 search_container_ = NULL; |
| 469 ntp_view_ = NULL; | 489 ntp_view_ = NULL; |
| 470 logo_view_ = NULL; | 490 default_provider_logo_.reset(); |
| 491 default_provider_name_.reset(); |
| 471 content_view_ = NULL; | 492 content_view_ = NULL; |
| 472 | 493 |
| 473 state_ = STATE_NOT_VISIBLE; | 494 state_ = STATE_NOT_VISIBLE; |
| 474 } | 495 } |
| 475 | 496 |
| 476 void SearchViewController::PopupVisibilityChanged() { | 497 void SearchViewController::PopupVisibilityChanged() { |
| 477 // Don't do anything while animating if the child is visible. Otherwise we'll | 498 // Don't do anything while animating if the child is visible. Otherwise we'll |
| 478 // prematurely cancel the animation. | 499 // prematurely cancel the animation. |
| 479 if (state_ != STATE_NTP_ANIMATING || | 500 if (state_ != STATE_NTP_ANIMATING || |
| 480 !omnibox_popup_view_parent_->is_child_visible()) { | 501 !omnibox_popup_view_parent_->is_child_visible()) { |
| 481 UpdateState(); | 502 UpdateState(); |
| 482 } | 503 } |
| 483 } | 504 } |
| 484 | 505 |
| 485 chrome::search::SearchModel* SearchViewController::search_model() { | 506 chrome::search::SearchModel* SearchViewController::search_model() { |
| 486 return tab_contents_ ? tab_contents_->search_tab_helper()->model() : NULL; | 507 return tab_contents_ ? tab_contents_->search_tab_helper()->model() : NULL; |
| 487 } | 508 } |
| 488 | 509 |
| 489 content::WebContents* SearchViewController::web_contents() { | 510 content::WebContents* SearchViewController::web_contents() { |
| 490 return tab_contents_ ? tab_contents_->web_contents() : NULL; | 511 return tab_contents_ ? tab_contents_->web_contents() : NULL; |
| 491 } | 512 } |
| OLD | NEW |