gesture_interpreter.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2017 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 REMOTING_CLIENT_GESTURE_INTERPRETER_H_
  5. #define REMOTING_CLIENT_GESTURE_INTERPRETER_H_
  6. #include <memory>
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "remoting/client/input/touch_input_strategy.h"
  10. #include "remoting/client/ui/desktop_viewport.h"
  11. #include "remoting/client/ui/fling_animation.h"
  12. #include "remoting/proto/event.pb.h"
  13. namespace remoting {
  14. class ChromotingSession;
  15. class RendererProxy;
  16. // This is a class for interpreting a raw touch input into actions like moving
  17. // the viewport and injecting mouse clicks.
  18. class GestureInterpreter {
  19. public:
  20. enum GestureState { GESTURE_BEGAN, GESTURE_CHANGED, GESTURE_ENDED };
  21. enum InputMode {
  22. UNDEFINED_INPUT_MODE,
  23. DIRECT_INPUT_MODE,
  24. TRACKPAD_INPUT_MODE
  25. };
  26. GestureInterpreter();
  27. GestureInterpreter(const GestureInterpreter&) = delete;
  28. GestureInterpreter& operator=(const GestureInterpreter&) = delete;
  29. ~GestureInterpreter();
  30. // Sets the context for the interpreter. Both arguments are nullable. If both
  31. // are nullptr then methods below will have no effect.
  32. void SetContext(RendererProxy* renderer, ChromotingSession* input_stub);
  33. // Must be called right after the renderer is ready.
  34. void SetInputMode(InputMode mode);
  35. // Returns the current input mode.
  36. InputMode GetInputMode() const;
  37. // Coordinates of the OpenGL view surface will be used.
  38. // Called during a two-finger pinching gesture. This can happen in conjunction
  39. // with Pan().
  40. void Zoom(float pivot_x, float pivot_y, float scale, GestureState state);
  41. // Called whenever the user did a pan gesture. It can be one-finger pan, no
  42. // matter dragging in on or not, or two-finger pan in conjunction with zoom.
  43. // Two-finger pan without zoom is consider a scroll gesture.
  44. void Pan(float translation_x, float translation_y);
  45. // Called when the user did a one-finger tap.
  46. void Tap(float x, float y);
  47. void TwoFingerTap(float x, float y);
  48. void ThreeFingerTap(float x, float y);
  49. // Caller is expected to call both Pan() and Drag() when dragging is in
  50. // progress.
  51. void Drag(float x, float y, GestureState state);
  52. // Called when the user has just done a one-finger pan (no dragging or
  53. // zooming) and the pan gesture still has some final velocity.
  54. void OneFingerFling(float velocity_x, float velocity_y);
  55. // Called during a two-finger scroll (panning without zooming) gesture.
  56. void Scroll(float x, float y, float dx, float dy);
  57. // Called when the user has just done a scroll gesture and the scroll gesture
  58. // still has some final velocity.
  59. void ScrollWithVelocity(float velocity_x, float velocity_y);
  60. // Called to process one animation frame.
  61. void ProcessAnimations();
  62. void OnSurfaceSizeChanged(int width, int height);
  63. void OnDesktopSizeChanged(int width, int height);
  64. void OnSafeInsetsChanged(int left, int top, int right, int bottom);
  65. base::WeakPtr<GestureInterpreter> GetWeakPtr();
  66. private:
  67. void PanWithoutAbortAnimations(float translation_x, float translation_y);
  68. void ScrollWithoutAbortAnimations(float dx, float dy);
  69. void AbortAnimations();
  70. // Injects the mouse click event and shows the touch feedback.
  71. void InjectMouseClick(float touch_x,
  72. float touch_y,
  73. protocol::MouseEvent_MouseButton button);
  74. void InjectCursorPosition(float x, float y);
  75. void SetGestureInProgress(TouchInputStrategy::Gesture gesture,
  76. bool is_in_progress);
  77. // Starts the given feedback at (cursor_x, cursor_y) if the feedback radius
  78. // is non-zero.
  79. void StartInputFeedback(float cursor_x,
  80. float cursor_y,
  81. TouchInputStrategy::TouchFeedbackType feedback_type);
  82. InputMode input_mode_ = UNDEFINED_INPUT_MODE;
  83. std::unique_ptr<TouchInputStrategy> input_strategy_;
  84. DesktopViewport viewport_;
  85. raw_ptr<RendererProxy> renderer_ = nullptr;
  86. raw_ptr<ChromotingSession> input_stub_ = nullptr;
  87. TouchInputStrategy::Gesture gesture_in_progress_;
  88. FlingAnimation pan_animation_;
  89. FlingAnimation scroll_animation_;
  90. base::WeakPtrFactory<GestureInterpreter> weak_factory_{this};
  91. };
  92. } // namespace remoting
  93. #endif // REMOTING_CLIENT_GESTURE_INTERPRETER_H_