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.

juce_MouseInactivityDetector.h 4.1KB

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