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.

106 lines
3.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. //==============================================================================
  16. /**
  17. This object watches for mouse-events happening within a component, and if
  18. the mouse remains still for long enough, triggers an event to indicate that
  19. it has become inactive.
  20. You'd use this for situations where e.g. you want to hide the mouse-cursor
  21. when the user's not actively using the mouse.
  22. After creating an instance of this, use addListener to get callbacks when
  23. the activity status changes.
  24. @tags{GUI}
  25. */
  26. class JUCE_API MouseInactivityDetector : private Timer,
  27. private MouseListener
  28. {
  29. public:
  30. /** Creates an inactivity watcher, attached to the given component.
  31. The target component must not be deleted while this - it will be monitored
  32. for any mouse events in it or its child components.
  33. */
  34. MouseInactivityDetector (Component& target);
  35. /** Destructor. */
  36. ~MouseInactivityDetector() override;
  37. /** Sets the time for which the mouse must be still before the callback
  38. is triggered.
  39. */
  40. void setDelay (int newDelayMilliseconds) noexcept;
  41. /** Sets the number of pixels by which the cursor is allowed to drift before it is
  42. considered to be actively moved.
  43. */
  44. void setMouseMoveTolerance (int pixelsNeededToTrigger) noexcept;
  45. //==============================================================================
  46. /** Classes should implement this to receive callbacks from a MouseInactivityDetector
  47. when the mouse becomes active or inactive.
  48. */
  49. class Listener
  50. {
  51. public:
  52. virtual ~Listener() = default;
  53. /** Called when the mouse is moved or clicked for the first time
  54. after a period of inactivity. */
  55. virtual void mouseBecameActive() = 0;
  56. /** Called when the mouse hasn't been moved for the timeout period. */
  57. virtual void mouseBecameInactive() = 0;
  58. };
  59. /** Registers a listener. */
  60. void addListener (Listener* listener);
  61. /** Removes a previously-registered listener. */
  62. void removeListener (Listener* listener);
  63. private:
  64. //==============================================================================
  65. Component& targetComp;
  66. ListenerList<Listener> listenerList;
  67. Point<int> lastMousePos;
  68. int delayMs = 1500, toleranceDistance = 15;
  69. bool isActive = true;
  70. void timerCallback() override;
  71. void wakeUp (const MouseEvent&, bool alwaysWake);
  72. void setActive (bool);
  73. void mouseMove (const MouseEvent& e) override { wakeUp (e, false); }
  74. void mouseEnter (const MouseEvent& e) override { wakeUp (e, false); }
  75. void mouseExit (const MouseEvent& e) override { wakeUp (e, false); }
  76. void mouseDown (const MouseEvent& e) override { wakeUp (e, true); }
  77. void mouseDrag (const MouseEvent& e) override { wakeUp (e, true); }
  78. void mouseUp (const MouseEvent& e) override { wakeUp (e, true); }
  79. void mouseWheelMove (const MouseEvent& e, const MouseWheelDetails&) override { wakeUp (e, true); }
  80. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MouseInactivityDetector)
  81. };
  82. } // namespace juce