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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. //==============================================================================
  22. /**
  23. This object watches for mouse-events happening within a component, and if
  24. the mouse remains still for long enough, triggers an event to indicate that
  25. it has become inactive.
  26. You'd use this for situations where e.g. you want to hide the mouse-cursor
  27. when the user's not actively using the mouse.
  28. After creating an instance of this, use addListener to get callbacks when
  29. the activity status changes.
  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();
  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() {}
  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, toleranceDistance;
  74. bool isActive;
  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