Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
4.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_MOUSEINACTIVITYDETECTOR_H_INCLUDED
  18. #define JUCE_MOUSEINACTIVITYDETECTOR_H_INCLUDED
  19. //==============================================================================
  20. /**
  21. This object watches for mouse-events happening within a component, and if
  22. the mouse remains still for long enough, triggers an event to indicate that
  23. it has become inactive.
  24. You'd use this for situations where e.g. you want to hide the mouse-cursor
  25. when the user's not actively using the mouse.
  26. After creating an instance of this, use addListener to get callbacks when
  27. the activity status changes.
  28. */
  29. class JUCE_API MouseInactivityDetector : private Timer,
  30. private MouseListener
  31. {
  32. public:
  33. /** Creates an inactivity watcher, attached to the given component.
  34. The target component must not be deleted while this - it will be monitored
  35. for any mouse events in it or its child components.
  36. */
  37. MouseInactivityDetector (Component& target);
  38. /** Destructor. */
  39. ~MouseInactivityDetector();
  40. /** Sets the time for which the mouse must be still before the callback
  41. is triggered.
  42. */
  43. void setDelay (int newDelayMilliseconds) noexcept;
  44. /** Sets the number of pixels by which the cursor is allowed to drift before it is
  45. considered to be actively moved.
  46. */
  47. void setMouseMoveTolerance (int pixelsNeededToTrigger) noexcept;
  48. //==============================================================================
  49. /** Classes should implement this to receive callbacks from a MouseInactivityDetector
  50. when the mouse becomes active or inactive.
  51. */
  52. class Listener
  53. {
  54. public:
  55. virtual ~Listener() {}
  56. /** Called when the mouse is moved or clicked for the first time
  57. after a period of inactivity. */
  58. virtual void mouseBecameActive() = 0;
  59. /** Called when the mouse hasn't been moved for the timeout period. */
  60. virtual void mouseBecameInactive() = 0;
  61. };
  62. /** Registers a listener. */
  63. void addListener (Listener* listener);
  64. /** Removes a previously-registered listener. */
  65. void removeListener (Listener* listener);
  66. private:
  67. //==============================================================================
  68. Component& targetComp;
  69. ListenerList<Listener> listenerList;
  70. Point<int> lastMousePos;
  71. int delayMs, toleranceDistance;
  72. bool isActive;
  73. void timerCallback() override;
  74. void wakeUp (const MouseEvent&, bool alwaysWake);
  75. void setActive (bool);
  76. void mouseMove (const MouseEvent& e) override { wakeUp (e, false); }
  77. void mouseEnter (const MouseEvent& e) override { wakeUp (e, false); }
  78. void mouseExit (const MouseEvent& e) override { wakeUp (e, false); }
  79. void mouseDown (const MouseEvent& e) override { wakeUp (e, true); }
  80. void mouseDrag (const MouseEvent& e) override { wakeUp (e, true); }
  81. void mouseUp (const MouseEvent& e) override { wakeUp (e, true); }
  82. void mouseWheelMove (const MouseEvent& e, const MouseWheelDetails&) override { wakeUp (e, true); }
  83. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MouseInactivityDetector)
  84. };
  85. #endif // JUCE_MOUSEINACTIVITYDETECTOR_H_INCLUDED