slide_out_controller.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #ifndef UI_VIEWS_ANIMATION_SLIDE_OUT_CONTROLLER_H_
  5. #define UI_VIEWS_ANIMATION_SLIDE_OUT_CONTROLLER_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/memory/weak_ptr.h"
  8. #include "ui/events/scoped_target_handler.h"
  9. #include "ui/views/view.h"
  10. #include "ui/views/views_export.h"
  11. namespace views {
  12. class SlideOutControllerDelegate;
  13. // This class contains logic to control sliding out of a layer in response to
  14. // swipes, i.e. gesture scroll events.
  15. class VIEWS_EXPORT SlideOutController : public ui::EventHandler {
  16. public:
  17. // Indicates how much the target layer is allowed to slide. See
  18. // |OnGestureEvent()| for details.
  19. enum class SlideMode {
  20. kFull, // Fully allow sliding until it's swiped out.
  21. kPartial, // Partially allow sliding until the slide amount reaches the
  22. // swipe-out threshold.
  23. kNone, // Don't allow sliding at all.
  24. };
  25. SlideOutController(ui::EventTarget* target,
  26. SlideOutControllerDelegate* delegate);
  27. SlideOutController(const SlideOutController&) = delete;
  28. SlideOutController& operator=(const SlideOutController&) = delete;
  29. ~SlideOutController() override;
  30. void set_update_opacity(bool update_opacity) {
  31. update_opacity_ = update_opacity;
  32. }
  33. void set_slide_mode(SlideMode mode) {
  34. // TODO(yoshiki): Close the slide when the slide mode sets to NO_SLIDE.
  35. mode_ = mode;
  36. }
  37. float gesture_amount() const { return gesture_amount_; }
  38. SlideMode mode() const { return mode_; }
  39. // ui::EventHandler
  40. void OnGestureEvent(ui::GestureEvent* event) override;
  41. // Enables the swipe control with specifying the width of buttons. Buttons
  42. // will appear behind the view as user slides it partially and it's kept open
  43. // after the gesture.
  44. void SetSwipeControlWidth(int swipe_control_width);
  45. float GetGestureAmount() const { return gesture_amount_; }
  46. // Moves slide back to the center position to closes the swipe control.
  47. // Effective only when swipe control is enabled by |SetSwipeControlWidth()|.
  48. void CloseSwipeControl();
  49. // Slides the view out and closes it after the animation. The sign of
  50. // |direction| indicates which way the slide occurs.
  51. void SlideOutAndClose(int direction);
  52. private:
  53. // Positions where the slided view stays after the touch released.
  54. enum class SwipeControlOpenState { kClosed, kOpenOnLeft, kOpenOnRight };
  55. // Restores the transform and opacity of the view.
  56. void RestoreVisualState();
  57. // Decides which position the slide should go back after touch is released.
  58. void CaptureControlOpenState();
  59. // Sets the opacity of the slide out layer if |update_opacity_| is true.
  60. void SetOpacityIfNecessary(float opacity);
  61. // Sets the transform matrix and performs animation if the matrix is changed.
  62. void SetTransformWithAnimationIfNecessary(const gfx::Transform& transform,
  63. base::TimeDelta animation_duration);
  64. // Called asynchronously after the slide out animation completes to inform the
  65. // delegate.
  66. void OnSlideOut();
  67. void OnAnimationsCompleted();
  68. ui::ScopedTargetHandler target_handling_;
  69. // Unowned and outlives this object.
  70. raw_ptr<SlideOutControllerDelegate> delegate_;
  71. // Cumulative scroll amount since the beginning of current slide gesture.
  72. // Includes the initial shift when swipe control was open at gesture start.
  73. float gesture_amount_ = 0.f;
  74. // Whether or not this view can be slided and/or swiped out.
  75. SlideMode mode_ = SlideMode::kFull;
  76. // Whether the swipe control is enabled. See |SetSwipeControlWidth()|.
  77. // Effective only when |mode_| is FULL or PARTIAL.
  78. bool has_swipe_control_ = false;
  79. // The horizontal position offset to for swipe control.
  80. // See |SetSwipeControlWidth()|.
  81. int swipe_control_width_ = 0;
  82. // The position where the slided view stays after the touch released.
  83. // Changed only when |mode_| is FULL or PARTIAL and |has_swipe_control_| is
  84. // true.
  85. SwipeControlOpenState control_open_state_ = SwipeControlOpenState::kClosed;
  86. // If false, it doesn't update the opacity.
  87. bool update_opacity_ = true;
  88. // Last opacity set by SetOpacityIfNecessary.
  89. float opacity_ = 1.0;
  90. base::WeakPtrFactory<SlideOutController> weak_ptr_factory_{this};
  91. };
  92. } // namespace views
  93. #endif // UI_VIEWS_ANIMATION_SLIDE_OUT_CONTROLLER_H_