wallpaper_widget_controller.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #include "ash/wallpaper/wallpaper_widget_controller.h"
  5. #include <utility>
  6. #include "ash/ash_export.h"
  7. #include "ash/public/cpp/shell_window_ids.h"
  8. #include "ash/root_window_controller.h"
  9. #include "ash/shell.h"
  10. #include "ash/wallpaper/wallpaper_view.h"
  11. #include "ui/aura/window.h"
  12. #include "ui/aura/window_observer.h"
  13. #include "ui/compositor/layer.h"
  14. #include "ui/compositor/layer_animation_observer.h"
  15. #include "ui/compositor/layer_tree_owner.h"
  16. #include "ui/compositor/scoped_layer_animation_settings.h"
  17. #include "ui/gfx/animation/tween.h"
  18. #include "ui/views/widget/widget.h"
  19. #include "ui/views/widget/widget_observer.h"
  20. #include "ui/wm/core/window_util.h"
  21. namespace ash {
  22. WallpaperWidgetController::WallpaperWidgetController(aura::Window* root_window)
  23. : root_window_(root_window) {}
  24. WallpaperWidgetController::~WallpaperWidgetController() {
  25. widget_->CloseNow();
  26. }
  27. void WallpaperWidgetController::Init(bool locked) {
  28. widget_ = CreateWallpaperWidget(root_window_, wallpaper_constants::kClear,
  29. locked, &wallpaper_view_);
  30. }
  31. views::Widget* WallpaperWidgetController::GetWidget() {
  32. return widget_.get();
  33. }
  34. bool WallpaperWidgetController::IsAnimating() const {
  35. return old_layer_tree_owner_ &&
  36. old_layer_tree_owner_->root()->GetAnimator()->is_animating();
  37. }
  38. void WallpaperWidgetController::StopAnimating() {
  39. if (old_layer_tree_owner_) {
  40. old_layer_tree_owner_->root()->GetAnimator()->StopAnimating();
  41. old_layer_tree_owner_.reset();
  42. }
  43. }
  44. void WallpaperWidgetController::AddAnimationEndCallback(
  45. base::OnceClosure callback) {
  46. animation_end_callbacks_.emplace_back(std::move(callback));
  47. }
  48. bool WallpaperWidgetController::Reparent(int container) {
  49. auto* parent = GetWidget()->GetNativeWindow()->parent();
  50. auto* root_window = parent->GetRootWindow();
  51. aura::Window* new_parent = root_window->GetChildById(container);
  52. if (parent == new_parent)
  53. return false;
  54. new_parent->AddChild(GetWidget()->GetNativeWindow());
  55. return true;
  56. }
  57. bool WallpaperWidgetController::SetWallpaperBlur(
  58. float blur,
  59. const base::TimeDelta& animation_duration) {
  60. if (!widget_->GetNativeWindow())
  61. return false;
  62. StopAnimating();
  63. bool blur_changed = wallpaper_view_->blur_sigma() != blur;
  64. wallpaper_view_->set_blur_sigma(blur);
  65. // Show the widget when we have something to show.
  66. if (!widget_->IsVisible())
  67. widget_->Show();
  68. if (!animation_duration.is_zero()) {
  69. ApplyCrossFadeAnimation(animation_duration);
  70. } else {
  71. wallpaper_view_->SchedulePaint();
  72. // Since there is no actual animation scheduled, just call completed method.
  73. OnImplicitAnimationsCompleted();
  74. }
  75. return blur_changed;
  76. }
  77. float WallpaperWidgetController::GetWallpaperBlur() const {
  78. return wallpaper_view_->blur_sigma();
  79. }
  80. void WallpaperWidgetController::OnImplicitAnimationsCompleted() {
  81. StopAnimating();
  82. wallpaper_view_->SetLockShieldEnabled(
  83. wallpaper_view_->GetWidget()->GetNativeWindow()->parent()->GetId() ==
  84. kShellWindowId_LockScreenWallpaperContainer);
  85. RunAnimationEndCallbacks();
  86. }
  87. void WallpaperWidgetController::RunAnimationEndCallbacks() {
  88. std::list<base::OnceClosure> callbacks;
  89. animation_end_callbacks_.swap(callbacks);
  90. for (auto& callback : callbacks)
  91. std::move(callback).Run();
  92. }
  93. void WallpaperWidgetController::ApplyCrossFadeAnimation(
  94. base::TimeDelta duration) {
  95. DCHECK(wallpaper_view_);
  96. old_layer_tree_owner_ = ::wm::RecreateLayers(wallpaper_view_);
  97. ui::Layer* old_layer = old_layer_tree_owner_->root();
  98. ui::Layer* new_layer = wallpaper_view_->layer();
  99. DCHECK_EQ(old_layer->parent(), new_layer->parent());
  100. old_layer->parent()->StackAbove(old_layer, new_layer);
  101. old_layer->SetOpacity(1.f);
  102. new_layer->SetOpacity(1.f);
  103. // Fade out the old layer. When clearing the blur, use the opposite tween so
  104. // that the animations are mirrors of each other.
  105. const bool clearing =
  106. wallpaper_view_->blur_sigma() == wallpaper_constants::kClear;
  107. ui::ScopedLayerAnimationSettings settings(old_layer->GetAnimator());
  108. settings.SetTransitionDuration(duration);
  109. settings.SetTweenType(clearing ? gfx::Tween::EASE_IN : gfx::Tween::EASE_OUT);
  110. settings.AddObserver(this);
  111. old_layer->SetOpacity(0.f);
  112. }
  113. } // namespace ash