File: bubble_slide_animator.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (127 lines) | stat: -rw-r--r-- 5,179 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UI_VIEWS_ANIMATION_BUBBLE_SLIDE_ANIMATOR_H_
#define UI_VIEWS_ANIMATION_BUBBLE_SLIDE_ANIMATOR_H_

#include "base/callback_list.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "ui/gfx/animation/linear_animation.h"
#include "ui/gfx/animation/tween.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/animation/animation_delegate_views.h"
#include "ui/views/views_export.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_observer.h"

namespace views {

class BubbleDialogDelegateView;
class View;

// Animates a bubble between anchor views on demand. Must be used with
// BubbleDialogDelegateView because of its reliance on the anchoring system.
class VIEWS_EXPORT BubbleSlideAnimator : public AnimationDelegateViews,
                                         public WidgetObserver {
 public:
  // Slide complete callback is called when a slide completes and the bubble is
  // safely anchored to the new view.
  using SlideCompleteCallbackSignature = void(BubbleSlideAnimator*);
  using SlideCompleteCallback =
      base::RepeatingCallback<SlideCompleteCallbackSignature>;

  // Slide progressed callback is called for each animation frame,
  // |animation_value| will be between 0 and 1 and will scale according to the
  // |tween_type| parameter.
  using SlideProgressedCallbackSignature = void(BubbleSlideAnimator*,
                                                double animation_value);
  using SlideProgressedCallback =
      base::RepeatingCallback<SlideProgressedCallbackSignature>;

  // Constructs a new BubbleSlideAnimator associated with the specified
  // |bubble_view|, which must already have a widget. If the bubble's widget is
  // destroyed, any animations will be canceled and this animator will no longer
  // be able to be used.
  explicit BubbleSlideAnimator(BubbleDialogDelegateView* bubble_view);
  BubbleSlideAnimator(const BubbleSlideAnimator&) = delete;
  BubbleSlideAnimator& operator=(const BubbleSlideAnimator&) = delete;
  ~BubbleSlideAnimator() override;

  bool is_animating() const { return slide_animation_.is_animating(); }

  // Sets the animation duration (a default is used if not set).
  void SetSlideDuration(base::TimeDelta duration);

  View* desired_anchor_view() { return desired_anchor_view_; }
  const View* desired_anchor_view() const { return desired_anchor_view_; }

  gfx::Tween::Type tween_type() const { return tween_type_; }
  void set_tween_type(gfx::Tween::Type tween_type) { tween_type_ = tween_type; }

  // Animates to a new anchor view.
  void AnimateToAnchorView(View* desired_anchor_view);

  // Ends any ongoing animation and immediately snaps the bubble to its target
  // bounds.
  void SnapToAnchorView(View* desired_anchor_view);

  // Retargets the current animation or snaps the bubble to its correct size
  // and position if there is no current animation.
  //
  // Call if the bubble contents change size in a way that would require the
  // bubble to be resized/repositioned. If you would like a new animation to
  // always play to the new bounds, call AnimateToAnchorView() instead.
  //
  // Note: This method expects the bubble to have a valid anchor view.
  void UpdateTargetBounds();

  // Stops the animation without snapping the widget to a particular anchor
  // view.
  void StopAnimation();

  // Adds a listener for slide progressed events.
  base::CallbackListSubscription AddSlideProgressedCallback(
      SlideProgressedCallback callback);

  // Adds a listener for slide complete events.
  base::CallbackListSubscription AddSlideCompleteCallback(
      SlideCompleteCallback callback);

 private:
  // AnimationDelegateViews:
  void AnimationProgressed(const gfx::Animation* animation) override;
  void AnimationEnded(const gfx::Animation* animation) override;
  void AnimationCanceled(const gfx::Animation* animation) override;

  // WidgetObserver:
  void OnWidgetDestroying(Widget* widget) override;

  // Determines where to animate the bubble to during an animation.
  gfx::Rect CalculateTargetBounds(const View* desired_anchor_view) const;

  const raw_ptr<BubbleDialogDelegateView, DanglingUntriaged> bubble_delegate_;
  base::ScopedObservation<Widget, WidgetObserver> widget_observation_{this};
  gfx::LinearAnimation slide_animation_{this};

  // The desired anchor view, which is valid during a slide animation. When not
  // animating, this value is null.
  raw_ptr<View> desired_anchor_view_ = nullptr;

  // The tween type to use when animating. The default should be aesthetically
  // pleasing for most applications.
  gfx::Tween::Type tween_type_ = gfx::Tween::FAST_OUT_SLOW_IN;

  gfx::Rect starting_bubble_bounds_;
  gfx::Rect target_bubble_bounds_;
  base::RepeatingCallbackList<SlideProgressedCallbackSignature>
      slide_progressed_callbacks_;
  base::RepeatingCallbackList<SlideCompleteCallbackSignature>
      slide_complete_callbacks_;
};

}  // namespace views

#endif  // UI_VIEWS_ANIMATION_BUBBLE_SLIDE_ANIMATOR_H_