The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

137 lines
3.5KB

  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::detail
  19. {
  20. /** Keeps track of the active top level window. */
  21. class TopLevelWindowManager : private Timer,
  22. private DeletedAtShutdown
  23. {
  24. public:
  25. TopLevelWindowManager() = default;
  26. ~TopLevelWindowManager() override
  27. {
  28. clearSingletonInstance();
  29. }
  30. JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL (TopLevelWindowManager)
  31. static void checkCurrentlyFocusedTopLevelWindow()
  32. {
  33. if (auto* wm = TopLevelWindowManager::getInstanceWithoutCreating())
  34. wm->checkFocusAsync();
  35. }
  36. void checkFocusAsync()
  37. {
  38. startTimer (10);
  39. }
  40. void checkFocus()
  41. {
  42. startTimer (jmin (1731, getTimerInterval() * 2));
  43. auto* newActive = findCurrentlyActiveWindow();
  44. if (newActive != currentActive)
  45. {
  46. currentActive = newActive;
  47. for (int i = windows.size(); --i >= 0;)
  48. if (auto* tlw = windows[i])
  49. tlw->setWindowActive (isWindowActive (tlw));
  50. Desktop::getInstance().triggerFocusCallback();
  51. }
  52. }
  53. bool addWindow (TopLevelWindow* const w)
  54. {
  55. windows.add (w);
  56. checkFocusAsync();
  57. return isWindowActive (w);
  58. }
  59. void removeWindow (TopLevelWindow* const w)
  60. {
  61. checkFocusAsync();
  62. if (currentActive == w)
  63. currentActive = nullptr;
  64. windows.removeFirstMatchingValue (w);
  65. if (windows.isEmpty())
  66. deleteInstance();
  67. }
  68. Array<TopLevelWindow*> windows;
  69. private:
  70. TopLevelWindow* currentActive = nullptr;
  71. void timerCallback() override
  72. {
  73. checkFocus();
  74. }
  75. bool isWindowActive (TopLevelWindow* const tlw) const
  76. {
  77. return (tlw == currentActive
  78. || tlw->isParentOf (currentActive)
  79. || tlw->hasKeyboardFocus (true))
  80. && tlw->isShowing();
  81. }
  82. TopLevelWindow* findCurrentlyActiveWindow() const
  83. {
  84. if (Process::isForegroundProcess())
  85. {
  86. auto* focusedComp = Component::getCurrentlyFocusedComponent();
  87. auto* w = dynamic_cast<TopLevelWindow*> (focusedComp);
  88. if (w == nullptr && focusedComp != nullptr)
  89. w = focusedComp->findParentComponentOfClass<TopLevelWindow>();
  90. if (w == nullptr)
  91. w = currentActive;
  92. if (w != nullptr && w->isShowing())
  93. return w;
  94. }
  95. return nullptr;
  96. }
  97. JUCE_DECLARE_NON_COPYABLE (TopLevelWindowManager)
  98. };
  99. JUCE_IMPLEMENT_SINGLETON (TopLevelWindowManager)
  100. } // namespace juce::detail