app_list_bubble_search_page.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2021 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/app_list/views/app_list_bubble_search_page.h"
  5. #include <memory>
  6. #include "ash/app_list/views/productivity_launcher_search_view.h"
  7. #include "ash/bubble/bubble_constants.h"
  8. #include "base/check_op.h"
  9. #include "base/time/time.h"
  10. #include "ui/base/metadata/metadata_impl_macros.h"
  11. #include "ui/compositor/layer.h"
  12. #include "ui/compositor/layer_type.h"
  13. #include "ui/compositor/scoped_animation_duration_scale_mode.h"
  14. #include "ui/views/animation/animation_builder.h"
  15. #include "ui/views/layout/fill_layout.h"
  16. namespace ash {
  17. namespace {
  18. // The animation spec says 40 dips up over 250ms, but the opacity animation
  19. // renders the view invisible after 50ms, so animate the visible fraction.
  20. constexpr int kHideAnimationVerticalOffset = -40 * 250 / 50;
  21. // Duration for the hide animation (both transform and opacity).
  22. constexpr base::TimeDelta kHideAnimationDuration = base::Milliseconds(50);
  23. constexpr auto kSearchViewBorder =
  24. gfx::Insets::TLBR(0, 0, kBubbleCornerRadius, 0);
  25. } // namespace
  26. AppListBubbleSearchPage::AppListBubbleSearchPage(
  27. AppListViewDelegate* view_delegate,
  28. SearchResultPageDialogController* dialog_controller,
  29. SearchBoxView* search_box_view) {
  30. SetLayoutManager(std::make_unique<views::FillLayout>());
  31. search_view_ = AddChildView(std::make_unique<ProductivityLauncherSearchView>(
  32. view_delegate, dialog_controller, search_box_view));
  33. search_view_->SetBorder(views::CreateEmptyBorder(kSearchViewBorder));
  34. }
  35. AppListBubbleSearchPage::~AppListBubbleSearchPage() = default;
  36. void AppListBubbleSearchPage::AnimateShowPage() {
  37. // If skipping animations, just update visibility.
  38. if (ui::ScopedAnimationDurationScaleMode::is_zero()) {
  39. SetVisible(true);
  40. return;
  41. }
  42. // Ensure any in-progress animations have their cleanup callbacks called.
  43. // Note that this might call SetVisible(false) from the hide animation.
  44. AbortAllAnimations();
  45. // Ensure the view is visible.
  46. SetVisible(true);
  47. ui::Layer* layer = search_view_->GetPageAnimationLayer();
  48. DCHECK_EQ(layer->type(), ui::LAYER_TEXTURED);
  49. views::AnimationBuilder()
  50. .SetPreemptionStrategy(
  51. ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
  52. .Once()
  53. .SetOpacity(layer, 0.f)
  54. .At(base::Milliseconds(50))
  55. .SetDuration(base::Milliseconds(100))
  56. .SetOpacity(layer, 1.f);
  57. }
  58. void AppListBubbleSearchPage::AnimateHidePage() {
  59. // If skipping animations, just update visibility.
  60. if (ui::ScopedAnimationDurationScaleMode::is_zero()) {
  61. SetVisible(false);
  62. return;
  63. }
  64. // Update view visibility when the animation is done.
  65. auto set_visible_false = base::BindRepeating(
  66. [](base::WeakPtr<AppListBubbleSearchPage> self) {
  67. if (!self)
  68. return;
  69. self->SetVisible(false);
  70. ui::Layer* layer = self->search_view_->GetPageAnimationLayer();
  71. layer->SetOpacity(1.f);
  72. layer->SetTransform(gfx::Transform());
  73. },
  74. weak_factory_.GetWeakPtr());
  75. ui::Layer* layer = search_view_->GetPageAnimationLayer();
  76. DCHECK_EQ(layer->type(), ui::LAYER_TEXTURED);
  77. gfx::Transform translate_up;
  78. translate_up.Translate(0, kHideAnimationVerticalOffset);
  79. views::AnimationBuilder()
  80. .SetPreemptionStrategy(
  81. ui::LayerAnimator::IMMEDIATELY_ANIMATE_TO_NEW_TARGET)
  82. .OnEnded(set_visible_false)
  83. .OnAborted(set_visible_false)
  84. .Once()
  85. .SetDuration(kHideAnimationDuration)
  86. .SetOpacity(layer, 0.f)
  87. .SetTransform(layer, translate_up);
  88. }
  89. void AppListBubbleSearchPage::AbortAllAnimations() {
  90. search_view_->GetPageAnimationLayer()->GetAnimator()->AbortAllAnimations();
  91. }
  92. ui::Layer* AppListBubbleSearchPage::GetPageAnimationLayerForTest() {
  93. return search_view_->GetPageAnimationLayer();
  94. }
  95. BEGIN_METADATA(AppListBubbleSearchPage, views::View)
  96. END_METADATA
  97. } // namespace ash