Chromium Code Reviews| 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/fullscreen_exit_bubble_views.h" | 5 #include "chrome/browser/ui/views/fullscreen_exit_bubble_views.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/app/chrome_command_ids.h" | 9 #include "chrome/app/chrome_command_ids.h" |
| 10 #include "chrome/browser/ui/fullscreen/fullscreen_controller.h" | |
| 11 #include "chrome/browser/ui/views/frame/top_container_view.h" | |
| 12 #include "chrome/common/chrome_notification_types.h" | |
| 13 #include "content/public/browser/notification_service.h" | |
| 10 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
| 11 #include "grit/generated_resources.h" | 15 #include "grit/generated_resources.h" |
| 12 #include "grit/ui_strings.h" | 16 #include "grit/ui_strings.h" |
| 13 #include "ui/base/animation/slide_animation.h" | 17 #include "ui/base/animation/slide_animation.h" |
| 14 #include "ui/base/keycodes/keyboard_codes.h" | 18 #include "ui/base/keycodes/keyboard_codes.h" |
| 15 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
| 16 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
| 17 #include "ui/gfx/canvas.h" | 21 #include "ui/gfx/canvas.h" |
| 18 #include "ui/gfx/screen.h" | 22 #include "ui/gfx/screen.h" |
| 19 #include "ui/views/bubble/bubble_border.h" | 23 #include "ui/views/bubble/bubble_border.h" |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 236 link_->SetVisible(link_visible); | 240 link_->SetVisible(link_visible); |
| 237 mouse_lock_exit_instruction_->SetVisible(!link_visible); | 241 mouse_lock_exit_instruction_->SetVisible(!link_visible); |
| 238 button_view_->SetVisible(false); | 242 button_view_->SetVisible(false); |
| 239 } | 243 } |
| 240 } | 244 } |
| 241 | 245 |
| 242 | 246 |
| 243 // FullscreenExitBubbleViews --------------------------------------------------- | 247 // FullscreenExitBubbleViews --------------------------------------------------- |
| 244 | 248 |
| 245 FullscreenExitBubbleViews::FullscreenExitBubbleViews( | 249 FullscreenExitBubbleViews::FullscreenExitBubbleViews( |
| 246 views::Widget* frame, | 250 BrowserView* browser_view, |
| 247 Browser* browser, | |
| 248 const GURL& url, | 251 const GURL& url, |
| 249 FullscreenExitBubbleType bubble_type) | 252 FullscreenExitBubbleType bubble_type) |
| 250 : FullscreenExitBubble(browser, url, bubble_type), | 253 : FullscreenExitBubble(browser_view->browser(), url, bubble_type), |
| 251 root_view_(frame->GetRootView()), | 254 browser_view_(browser_view), |
| 252 popup_(NULL), | 255 popup_(NULL), |
| 253 size_animation_(new ui::SlideAnimation(this)) { | 256 animation_(new ui::SlideAnimation(this)), |
| 254 size_animation_->Reset(1); | 257 animated_attribute_(ANIMATED_ATTRIBUTE_BOUNDS) { |
| 258 animation_->Reset(1); | |
| 255 | 259 |
| 256 // Create the contents view. | 260 // Create the contents view. |
| 257 ui::Accelerator accelerator(ui::VKEY_UNKNOWN, ui::EF_NONE); | 261 ui::Accelerator accelerator(ui::VKEY_UNKNOWN, ui::EF_NONE); |
| 258 bool got_accelerator = frame->GetAccelerator(IDC_FULLSCREEN, &accelerator); | 262 bool got_accelerator = browser_view_->GetWidget()->GetAccelerator( |
| 263 IDC_FULLSCREEN, &accelerator); | |
| 259 DCHECK(got_accelerator); | 264 DCHECK(got_accelerator); |
| 260 view_ = new FullscreenExitView( | 265 view_ = new FullscreenExitView( |
| 261 this, accelerator.GetShortcutText(), url, bubble_type_); | 266 this, accelerator.GetShortcutText(), url, bubble_type_); |
| 262 | 267 |
| 263 // TODO(yzshen): Change to use the new views bubble, BubbleDelegateView. | 268 // TODO(yzshen): Change to use the new views bubble, BubbleDelegateView. |
| 264 // Initialize the popup. | 269 // Initialize the popup. |
| 265 popup_ = new views::Widget; | 270 popup_ = new views::Widget; |
| 266 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); | 271 views::Widget::InitParams params(views::Widget::InitParams::TYPE_POPUP); |
| 267 params.transparent = true; | 272 params.transparent = true; |
| 268 params.can_activate = false; | 273 params.can_activate = false; |
| 269 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 274 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 270 params.parent = frame->GetNativeView(); | 275 params.parent = browser_view_->GetWidget()->GetNativeView(); |
| 271 params.bounds = GetPopupRect(false); | 276 params.bounds = GetPopupRect(false); |
| 272 popup_->Init(params); | 277 popup_->Init(params); |
| 273 gfx::Size size = GetPopupRect(true).size(); | 278 gfx::Size size = GetPopupRect(true).size(); |
| 274 popup_->SetContentsView(view_); | 279 popup_->SetContentsView(view_); |
| 275 // We set layout manager to NULL to prevent the widget from sizing its | 280 // We set layout manager to NULL to prevent the widget from sizing its |
| 276 // contents to the same size as itself. This prevents the widget contents from | 281 // contents to the same size as itself. This prevents the widget contents from |
| 277 // shrinking while we animate the height of the popup to give the impression | 282 // shrinking while we animate the height of the popup to give the impression |
| 278 // that it is sliding off the top of the screen. | 283 // that it is sliding off the top of the screen. |
| 279 popup_->GetRootView()->SetLayoutManager(NULL); | 284 popup_->GetRootView()->SetLayoutManager(NULL); |
| 280 view_->SetBounds(0, 0, size.width(), size.height()); | 285 view_->SetBounds(0, 0, size.width(), size.height()); |
| 281 popup_->Show(); // This does not activate the popup. | 286 popup_->Show(); // This does not activate the popup. |
| 282 | 287 |
| 283 StartWatchingMouseIfNecessary(); | 288 popup_->AddObserver(this); |
| 289 | |
| 290 registrar_.Add( | |
| 291 this, | |
| 292 chrome::NOTIFICATION_FULLSCREEN_CHANGED, | |
| 293 content::Source<FullscreenController>( | |
| 294 browser_view_->browser()->fullscreen_controller())); | |
| 295 | |
| 296 UpdateForImmersiveState(); | |
| 284 } | 297 } |
| 285 | 298 |
| 286 FullscreenExitBubbleViews::~FullscreenExitBubbleViews() { | 299 FullscreenExitBubbleViews::~FullscreenExitBubbleViews() { |
| 300 popup_->RemoveObserver(this); | |
| 301 browser_view_->immersive_mode_controller()->UnanchorWidgetFromTopContainer( | |
| 302 popup_); | |
| 303 | |
| 287 // This is tricky. We may be in an ATL message handler stack, in which case | 304 // This is tricky. We may be in an ATL message handler stack, in which case |
| 288 // the popup cannot be deleted yet. We also can't set the popup's ownership | 305 // the popup cannot be deleted yet. We also can't set the popup's ownership |
| 289 // model to NATIVE_WIDGET_OWNS_WIDGET because if the user closed the last tab | 306 // model to NATIVE_WIDGET_OWNS_WIDGET because if the user closed the last tab |
| 290 // while in fullscreen mode, Windows has already destroyed the popup HWND by | 307 // while in fullscreen mode, Windows has already destroyed the popup HWND by |
| 291 // the time we get here, and thus either the popup will already have been | 308 // the time we get here, and thus either the popup will already have been |
| 292 // deleted (if we set this in our constructor) or the popup will never get | 309 // deleted (if we set this in our constructor) or the popup will never get |
| 293 // another OnFinalMessage() call (if not, as currently). So instead, we tell | 310 // another OnFinalMessage() call (if not, as currently). So instead, we tell |
| 294 // the popup to synchronously hide, and then asynchronously close and delete | 311 // the popup to synchronously hide, and then asynchronously close and delete |
| 295 // itself. | 312 // itself. |
| 296 popup_->Close(); | 313 popup_->Close(); |
| 297 MessageLoop::current()->DeleteSoon(FROM_HERE, popup_); | 314 MessageLoop::current()->DeleteSoon(FROM_HERE, popup_); |
| 298 } | 315 } |
| 299 | 316 |
| 300 void FullscreenExitBubbleViews::UpdateContent( | 317 void FullscreenExitBubbleViews::UpdateContent( |
| 301 const GURL& url, | 318 const GURL& url, |
| 302 FullscreenExitBubbleType bubble_type) { | 319 FullscreenExitBubbleType bubble_type) { |
| 303 DCHECK_NE(FEB_TYPE_NONE, bubble_type); | 320 DCHECK_NE(FEB_TYPE_NONE, bubble_type); |
| 304 if (bubble_type_ == bubble_type && url_ == url) | 321 if (bubble_type_ == bubble_type && url_ == url) |
| 305 return; | 322 return; |
| 306 | 323 |
| 307 url_ = url; | 324 url_ = url; |
| 308 bubble_type_ = bubble_type; | 325 bubble_type_ = bubble_type; |
| 309 view_->UpdateContent(url_, bubble_type_); | 326 view_->UpdateContent(url_, bubble_type_); |
| 310 | 327 |
| 311 gfx::Size size = GetPopupRect(true).size(); | 328 gfx::Size size = GetPopupRect(true).size(); |
| 312 view_->SetSize(size); | 329 view_->SetSize(size); |
| 313 popup_->SetBounds(GetPopupRect(false)); | 330 popup_->SetBounds(GetPopupRect(false)); |
| 314 Show(); | 331 Show(); |
| 315 | 332 |
| 333 // Stop watching the mouse even if UpdateMouseWatcher() will start watching | |
| 334 // it again so that the popup with the new content is visible for at least | |
| 335 // |kInitialDelayMs|. | |
| 316 StopWatchingMouse(); | 336 StopWatchingMouse(); |
| 317 StartWatchingMouseIfNecessary(); | 337 |
| 338 UpdateMouseWatcher(); | |
| 318 } | 339 } |
| 319 | 340 |
| 320 void FullscreenExitBubbleViews::AnimationProgressed( | 341 void FullscreenExitBubbleViews::UpdateMouseWatcher() { |
| 321 const ui::Animation* animation) { | 342 bool should_watch_mouse = false; |
| 343 if (popup_->IsVisible()) | |
| 344 should_watch_mouse = !fullscreen_bubble::ShowButtonsForType(bubble_type_); | |
| 345 else | |
| 346 should_watch_mouse = CanMouseTriggerSlideIn(); | |
| 347 | |
| 348 if (should_watch_mouse == IsWatchingMouse()) | |
| 349 return; | |
| 350 | |
| 351 if (should_watch_mouse) | |
| 352 StartWatchingMouse(); | |
| 353 else | |
| 354 StopWatchingMouse(); | |
| 355 } | |
| 356 | |
| 357 void FullscreenExitBubbleViews::UpdateForImmersiveState() { | |
| 358 ImmersiveModeController* immersive_controller = | |
| 359 browser_view_->immersive_mode_controller(); | |
| 360 | |
| 361 AnimatedAttribute expected_animated_attribute = | |
| 362 immersive_controller->IsEnabled() ? | |
| 363 ANIMATED_ATTRIBUTE_OPACITY : ANIMATED_ATTRIBUTE_BOUNDS; | |
| 364 if (animated_attribute_ != expected_animated_attribute) { | |
| 365 // If an animation is currently in progress, skip to the end because | |
| 366 // switching the animated attribute midway through the animation looks | |
| 367 // weird. | |
| 368 animation_->End(); | |
| 369 | |
| 370 animated_attribute_ = expected_animated_attribute; | |
| 371 | |
| 372 // We may have finished hiding |popup_|. However, the bounds animation | |
| 373 // assumes |popup_| has the opacity when it is fully shown and the opacity | |
| 374 // animation assumes |popup_| has the bounds when |popup_| is fully shown. | |
| 375 if (animated_attribute_ == ANIMATED_ATTRIBUTE_BOUNDS) | |
| 376 popup_->SetOpacity(255); | |
| 377 else | |
| 378 UpdateBounds(); | |
| 379 } | |
| 380 | |
| 381 if (immersive_controller->IsEnabled()) { | |
| 382 // In immersive mode, anchor |popup_| to the top container. This repositions | |
| 383 // the top container so that it stays |kPopupTopPx| below the top container | |
| 384 // when the top container animates its position (top container reveals / | |
| 385 // unreveals) or the top container bounds change (eg bookmark bar is shown). | |
| 386 immersive_controller->AnchorWidgetToTopContainer(popup_, kPopupTopPx); | |
|
scheib
2013/04/10 22:23:19
I havn't fixed http://crbug.com/130098 because of
pkotwicz
2013/04/11 03:31:55
By design, AnchorWidgetToTopContainer() is a noop
| |
| 387 } else { | |
| 388 immersive_controller->UnanchorWidgetFromTopContainer(popup_); | |
| 389 } | |
| 390 | |
| 391 UpdateMouseWatcher(); | |
| 392 } | |
| 393 | |
| 394 void FullscreenExitBubbleViews::UpdateBounds() { | |
| 322 gfx::Rect popup_rect(GetPopupRect(false)); | 395 gfx::Rect popup_rect(GetPopupRect(false)); |
| 323 if (popup_rect.IsEmpty()) { | 396 if (popup_rect.IsEmpty()) { |
| 324 popup_->Hide(); | 397 popup_->Hide(); |
| 325 } else { | 398 } else { |
| 326 popup_->SetBounds(popup_rect); | 399 popup_->SetBounds(popup_rect); |
| 327 view_->SetY(popup_rect.height() - view_->height()); | 400 view_->SetY(popup_rect.height() - view_->height()); |
| 328 popup_->Show(); | 401 popup_->Show(); |
| 329 } | 402 } |
| 330 } | 403 } |
| 331 | 404 |
| 405 views::View* FullscreenExitBubbleViews::GetBrowserRootView() const { | |
| 406 return browser_view_->GetWidget()->GetRootView(); | |
| 407 } | |
| 408 | |
| 409 void FullscreenExitBubbleViews::AnimationProgressed( | |
| 410 const ui::Animation* animation) { | |
| 411 if (animated_attribute_ == ANIMATED_ATTRIBUTE_OPACITY) { | |
| 412 int opacity = animation_->CurrentValueBetween(0, 255); | |
| 413 if (opacity == 0) { | |
| 414 popup_->Hide(); | |
| 415 } else { | |
| 416 popup_->Show(); | |
| 417 popup_->SetOpacity(opacity); | |
| 418 } | |
| 419 } else { | |
| 420 UpdateBounds(); | |
| 421 } | |
| 422 } | |
| 423 | |
| 332 void FullscreenExitBubbleViews::AnimationEnded( | 424 void FullscreenExitBubbleViews::AnimationEnded( |
| 333 const ui::Animation* animation) { | 425 const ui::Animation* animation) { |
| 334 AnimationProgressed(animation); | 426 AnimationProgressed(animation); |
| 335 } | 427 } |
| 336 | 428 |
| 337 gfx::Rect FullscreenExitBubbleViews::GetPopupRect( | 429 gfx::Rect FullscreenExitBubbleViews::GetPopupRect( |
| 338 bool ignore_animation_state) const { | 430 bool ignore_animation_state) const { |
| 339 gfx::Size size(view_->GetPreferredSize()); | 431 gfx::Size size(view_->GetPreferredSize()); |
| 340 // NOTE: don't use the bounds of the root_view_. On linux changing window | 432 // NOTE: don't use the bounds of the root_view_. On linux GTK changing window |
| 341 // size is async. Instead we use the size of the screen. | 433 // size is async. Instead we use the size of the screen. |
| 342 gfx::Screen* screen = | 434 gfx::Screen* screen = |
| 343 gfx::Screen::GetScreenFor(root_view_->GetWidget()->GetNativeView()); | 435 gfx::Screen::GetScreenFor(browser_view_->GetWidget()->GetNativeView()); |
| 344 gfx::Rect screen_bounds = screen->GetDisplayNearestWindow( | 436 gfx::Rect screen_bounds = screen->GetDisplayNearestWindow( |
| 345 root_view_->GetWidget()->GetNativeView()).bounds(); | 437 browser_view_->GetWidget()->GetNativeView()).bounds(); |
| 346 gfx::Point origin(screen_bounds.x() + | 438 int x = screen_bounds.x() + (screen_bounds.width() - size.width()) / 2; |
| 347 (screen_bounds.width() - size.width()) / 2, | 439 |
| 348 kPopupTopPx + screen_bounds.y()); | 440 int top_container_bottom = screen_bounds.y(); |
| 349 if (!ignore_animation_state) { | 441 if (browser_view_->immersive_mode_controller()->IsEnabled()) { |
| 442 // Skip querying the top container height in non-immersive fullscreen | |
| 443 // because: | |
| 444 // - The top container height is always zero in non-immersive fullscreen. | |
| 445 // - Querying the top container height may return the height before entering | |
| 446 // fullscreen because layout is disabled while entering fullscreen. | |
| 447 // A visual glitch due to the delayed layout is avoided in immersive | |
| 448 // fullscreen because entering fullscreen starts with the top container | |
| 449 // revealed. When revealed, the top container has the same height as before | |
| 450 // entering fullscreen. | |
| 451 top_container_bottom = | |
| 452 browser_view_->top_container()->GetTargetBoundsInScreen().bottom(); | |
| 453 } | |
| 454 int y = top_container_bottom + kPopupTopPx; | |
| 455 | |
| 456 if (!ignore_animation_state && | |
| 457 animated_attribute_ == ANIMATED_ATTRIBUTE_BOUNDS) { | |
| 350 int total_height = size.height() + kPopupTopPx; | 458 int total_height = size.height() + kPopupTopPx; |
| 351 int popup_bottom = size_animation_->CurrentValueBetween( | 459 int popup_bottom = animation_->CurrentValueBetween(total_height, 0); |
| 352 static_cast<double>(total_height), 0.0f); | |
| 353 int y_offset = std::min(popup_bottom, kPopupTopPx); | 460 int y_offset = std::min(popup_bottom, kPopupTopPx); |
| 354 size.set_height(size.height() - popup_bottom + y_offset); | 461 size.set_height(size.height() - popup_bottom + y_offset); |
| 355 origin.set_y(origin.y() - y_offset); | 462 y -= y_offset; |
| 356 } | 463 } |
| 357 return gfx::Rect(origin, size); | 464 return gfx::Rect(gfx::Point(x, y), size); |
| 358 } | 465 } |
| 359 | 466 |
| 360 gfx::Point FullscreenExitBubbleViews::GetCursorScreenPoint() { | 467 gfx::Point FullscreenExitBubbleViews::GetCursorScreenPoint() { |
| 361 gfx::Point cursor_pos = gfx::Screen::GetScreenFor( | 468 gfx::Point cursor_pos = gfx::Screen::GetScreenFor( |
| 362 root_view_->GetWidget()->GetNativeView())->GetCursorScreenPoint(); | 469 browser_view_->GetWidget()->GetNativeView())->GetCursorScreenPoint(); |
| 363 views::View::ConvertPointToTarget(NULL, root_view_, &cursor_pos); | 470 views::View::ConvertPointToTarget(NULL, GetBrowserRootView(), &cursor_pos); |
| 364 return cursor_pos; | 471 return cursor_pos; |
| 365 } | 472 } |
| 366 | 473 |
| 367 bool FullscreenExitBubbleViews::WindowContainsPoint(gfx::Point pos) { | 474 bool FullscreenExitBubbleViews::WindowContainsPoint(gfx::Point pos) { |
| 368 return root_view_->HitTestPoint(pos); | 475 return GetBrowserRootView()->HitTestPoint(pos); |
| 369 } | 476 } |
| 370 | 477 |
| 371 bool FullscreenExitBubbleViews::IsWindowActive() { | 478 bool FullscreenExitBubbleViews::IsWindowActive() { |
| 372 return root_view_->GetWidget()->IsActive(); | 479 return browser_view_->GetWidget()->IsActive(); |
| 373 } | 480 } |
| 374 | 481 |
| 375 void FullscreenExitBubbleViews::Hide() { | 482 void FullscreenExitBubbleViews::Hide() { |
| 376 size_animation_->SetSlideDuration(kSlideOutDurationMs); | 483 animation_->SetSlideDuration(kSlideOutDurationMs); |
| 377 size_animation_->Hide(); | 484 animation_->Hide(); |
| 378 } | 485 } |
| 379 | 486 |
| 380 void FullscreenExitBubbleViews::Show() { | 487 void FullscreenExitBubbleViews::Show() { |
| 381 size_animation_->SetSlideDuration(kSlideInDurationMs); | 488 animation_->SetSlideDuration(kSlideInDurationMs); |
| 382 size_animation_->Show(); | 489 animation_->Show(); |
| 383 } | 490 } |
| 384 | 491 |
| 385 bool FullscreenExitBubbleViews::IsAnimating() { | 492 bool FullscreenExitBubbleViews::IsAnimating() { |
| 386 return size_animation_->GetCurrentValue() != 0; | 493 return animation_->is_animating(); |
| 387 } | 494 } |
| 388 | 495 |
| 389 void FullscreenExitBubbleViews::StartWatchingMouseIfNecessary() { | 496 bool FullscreenExitBubbleViews::CanMouseTriggerSlideIn() const { |
| 390 if (!fullscreen_bubble::ShowButtonsForType(bubble_type_)) | 497 return !browser_view_->immersive_mode_controller()->IsEnabled(); |
| 391 StartWatchingMouse(); | |
| 392 } | 498 } |
| 499 | |
| 500 void FullscreenExitBubbleViews::Observe( | |
| 501 int type, | |
| 502 const content::NotificationSource& source, | |
| 503 const content::NotificationDetails& details) { | |
| 504 DCHECK_EQ(chrome::NOTIFICATION_FULLSCREEN_CHANGED, type); | |
| 505 UpdateForImmersiveState(); | |
| 506 } | |
| 507 | |
| 508 void FullscreenExitBubbleViews::OnWidgetVisibilityChanged( | |
| 509 views::Widget* widget, | |
| 510 bool visible) { | |
| 511 UpdateMouseWatcher(); | |
| 512 } | |
| OLD | NEW |