highlighter_controller.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 ASH_HIGHLIGHTER_HIGHLIGHTER_CONTROLLER_H_
  5. #define ASH_HIGHLIGHTER_HIGHLIGHTER_CONTROLLER_H_
  6. #include <memory>
  7. #include "ash/ash_export.h"
  8. #include "ash/fast_ink/fast_ink_pointer_controller.h"
  9. #include "base/callback.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/observer_list.h"
  12. #include "base/time/time.h"
  13. #include "ui/views/widget/unique_widget_ptr.h"
  14. namespace base {
  15. class OneShotTimer;
  16. }
  17. namespace gfx {
  18. class Rect;
  19. }
  20. namespace ash {
  21. class HighlighterView;
  22. // Highlighter enabled state that is notified to observers.
  23. enum class HighlighterEnabledState {
  24. // Highlighter is enabled by any ways.
  25. kEnabled,
  26. // Highlighter is disabled by user directly, for example disabling palette
  27. // tool by user actions on palette menu.
  28. kDisabledByUser,
  29. // Highlighter is disabled on metalayer session complete.
  30. kDisabledBySessionComplete,
  31. // Highlighter is disabled on metalayer session abort. An abort may occur due
  32. // to dismissal of Assistant UI or due to interruption by user via hotword.
  33. kDisabledBySessionAbort,
  34. };
  35. // Controller for the highlighter functionality, which can be enabled/disabled
  36. // by switching "Assistant" tool under Stylus panel on/off.
  37. // Enables/disables highlighter as well as receives points
  38. // and passes them off to be rendered.
  39. class ASH_EXPORT HighlighterController
  40. : public fast_ink::FastInkPointerController {
  41. public:
  42. // Interface for classes that wish to be notified with highlighter status.
  43. class Observer {
  44. public:
  45. // Called when highlighter enabled state changes.
  46. virtual void OnHighlighterEnabledChanged(HighlighterEnabledState state) {}
  47. // Called when highlighter selection is recognized.
  48. virtual void OnHighlighterSelectionRecognized(const gfx::Rect& rect) {}
  49. protected:
  50. virtual ~Observer() = default;
  51. };
  52. HighlighterController();
  53. HighlighterController(const HighlighterController&) = delete;
  54. HighlighterController& operator=(const HighlighterController&) = delete;
  55. ~HighlighterController() override;
  56. HighlighterEnabledState enabled_state() { return enabled_state_; }
  57. void AddObserver(Observer* observer);
  58. void RemoveObserver(Observer* observer);
  59. // Set the callback to exit the highlighter mode. If |require_success| is
  60. // true, the callback will be called only after a successful gesture
  61. // recognition. If |require_success| is false, the callback will be called
  62. // after the first complete gesture, regardless of the recognition result.
  63. void SetExitCallback(base::OnceClosure callback, bool require_success);
  64. // Update highlighter enabled |state| and notify observers.
  65. void UpdateEnabledState(HighlighterEnabledState enabled_state);
  66. // Aborts the current metalayer session. If no metalayer session exists,
  67. // calling this method is a no-op.
  68. void AbortSession();
  69. private:
  70. friend class HighlighterControllerTestApi;
  71. // fast_ink::FastInkPointerController:
  72. void SetEnabled(bool enabled) override;
  73. views::View* GetPointerView() const override;
  74. void CreatePointerView(base::TimeDelta presentation_delay,
  75. aura::Window* root_window) override;
  76. void UpdatePointerView(ui::TouchEvent* event) override;
  77. void DestroyPointerView() override;
  78. bool CanStartNewGesture(ui::LocatedEvent* event) override;
  79. bool ShouldProcessEvent(ui::LocatedEvent* event) override;
  80. // Performs gesture recognition, initiates appropriate visual effects,
  81. // notifies the observer if necessary.
  82. void RecognizeGesture();
  83. // Destroys |highlighter_view_widget_|, if it exists.
  84. void DestroyHighlighterView();
  85. // Destroys |result_view_widget_|, if it exists.
  86. void DestroyResultView();
  87. // Calls and clears the mode exit callback, if it is set.
  88. void CallExitCallback();
  89. // Returns the Widget contents view of the highlighter widget as
  90. // HighlighterView*.
  91. HighlighterView* GetHighlighterView();
  92. // Caches the highlighter enabled state.
  93. HighlighterEnabledState enabled_state_ =
  94. HighlighterEnabledState::kDisabledByUser;
  95. // |highlighter_view_widget_| will only hold an instance when the highlighter
  96. // is enabled and activated (pressed or dragged) and until the fade out
  97. // animation is done.
  98. views::UniqueWidgetPtr highlighter_view_widget_;
  99. // |result_view_widget_| will only hold an instance when the selection result
  100. // animation is in progress.
  101. views::UniqueWidgetPtr result_view_widget_;
  102. // Time of the session start (e.g. when the controller was enabled).
  103. base::TimeTicks session_start_;
  104. // Time of the previous gesture end, valid after the first gesture
  105. // within the session is complete.
  106. base::TimeTicks previous_gesture_end_;
  107. // Gesture counter withing a session.
  108. int gesture_counter_ = 0;
  109. // Recognized gesture counter withing a session.
  110. int recognized_gesture_counter_ = 0;
  111. // Not null while waiting for the next event to continue an interrupted
  112. // stroke.
  113. std::unique_ptr<base::OneShotTimer> interrupted_stroke_timer_;
  114. // The callback to exit the mode in the UI.
  115. base::OnceClosure exit_callback_;
  116. // If true, the mode is not exited until a valid selection is made.
  117. bool require_success_ = true;
  118. base::ObserverList<Observer>::Unchecked observers_;
  119. base::WeakPtrFactory<HighlighterController> weak_factory_{this};
  120. };
  121. } // namespace ash
  122. #endif // ASH_HIGHLIGHTER_HIGHLIGHTER_CONTROLLER_H_