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.

359 lines
10KB

  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. /** Keeps track of the active top level window. */
  21. class TopLevelWindowManager : private Timer,
  22. private DeletedAtShutdown
  23. {
  24. public:
  25. TopLevelWindowManager() {}
  26. ~TopLevelWindowManager() override { clearSingletonInstance(); }
  27. JUCE_DECLARE_SINGLETON_SINGLETHREADED_MINIMAL (TopLevelWindowManager)
  28. void checkFocusAsync()
  29. {
  30. startTimer (10);
  31. }
  32. void checkFocus()
  33. {
  34. startTimer (jmin (1731, getTimerInterval() * 2));
  35. auto* newActive = findCurrentlyActiveWindow();
  36. if (newActive != currentActive)
  37. {
  38. currentActive = newActive;
  39. for (int i = windows.size(); --i >= 0;)
  40. if (auto* tlw = windows[i])
  41. tlw->setWindowActive (isWindowActive (tlw));
  42. Desktop::getInstance().triggerFocusCallback();
  43. }
  44. }
  45. bool addWindow (TopLevelWindow* const w)
  46. {
  47. windows.add (w);
  48. checkFocusAsync();
  49. return isWindowActive (w);
  50. }
  51. void removeWindow (TopLevelWindow* const w)
  52. {
  53. checkFocusAsync();
  54. if (currentActive == w)
  55. currentActive = nullptr;
  56. windows.removeFirstMatchingValue (w);
  57. if (windows.isEmpty())
  58. deleteInstance();
  59. }
  60. Array<TopLevelWindow*> windows;
  61. private:
  62. TopLevelWindow* currentActive = nullptr;
  63. void timerCallback() override
  64. {
  65. checkFocus();
  66. }
  67. bool isWindowActive (TopLevelWindow* const tlw) const
  68. {
  69. return (tlw == currentActive
  70. || tlw->isParentOf (currentActive)
  71. || tlw->hasKeyboardFocus (true))
  72. && tlw->isShowing();
  73. }
  74. TopLevelWindow* findCurrentlyActiveWindow() const
  75. {
  76. if (Process::isForegroundProcess())
  77. {
  78. auto* focusedComp = Component::getCurrentlyFocusedComponent();
  79. auto* w = dynamic_cast<TopLevelWindow*> (focusedComp);
  80. if (w == nullptr && focusedComp != nullptr)
  81. w = focusedComp->findParentComponentOfClass<TopLevelWindow>();
  82. if (w == nullptr)
  83. w = currentActive;
  84. if (w != nullptr && w->isShowing())
  85. return w;
  86. }
  87. return nullptr;
  88. }
  89. JUCE_DECLARE_NON_COPYABLE (TopLevelWindowManager)
  90. };
  91. JUCE_IMPLEMENT_SINGLETON (TopLevelWindowManager)
  92. void juce_checkCurrentlyFocusedTopLevelWindow();
  93. void juce_checkCurrentlyFocusedTopLevelWindow()
  94. {
  95. if (auto* wm = TopLevelWindowManager::getInstanceWithoutCreating())
  96. wm->checkFocusAsync();
  97. }
  98. //==============================================================================
  99. TopLevelWindow::TopLevelWindow (const String& name, const bool shouldAddToDesktop)
  100. : Component (name)
  101. {
  102. setTitle (name);
  103. setOpaque (true);
  104. if (shouldAddToDesktop)
  105. Component::addToDesktop (TopLevelWindow::getDesktopWindowStyleFlags());
  106. else
  107. setDropShadowEnabled (true);
  108. setWantsKeyboardFocus (true);
  109. setBroughtToFrontOnMouseClick (true);
  110. isCurrentlyActive = TopLevelWindowManager::getInstance()->addWindow (this);
  111. }
  112. TopLevelWindow::~TopLevelWindow()
  113. {
  114. shadower = nullptr;
  115. TopLevelWindowManager::getInstance()->removeWindow (this);
  116. }
  117. //==============================================================================
  118. void TopLevelWindow::focusOfChildComponentChanged (FocusChangeType)
  119. {
  120. auto* wm = TopLevelWindowManager::getInstance();
  121. if (hasKeyboardFocus (true))
  122. wm->checkFocus();
  123. else
  124. wm->checkFocusAsync();
  125. }
  126. void TopLevelWindow::setWindowActive (const bool isNowActive)
  127. {
  128. if (isCurrentlyActive != isNowActive)
  129. {
  130. isCurrentlyActive = isNowActive;
  131. activeWindowStatusChanged();
  132. }
  133. }
  134. void TopLevelWindow::activeWindowStatusChanged()
  135. {
  136. }
  137. bool TopLevelWindow::isUsingNativeTitleBar() const noexcept
  138. {
  139. return useNativeTitleBar && (isOnDesktop() || ! isShowing());
  140. }
  141. void TopLevelWindow::visibilityChanged()
  142. {
  143. if (isShowing())
  144. if (auto* p = getPeer())
  145. if ((p->getStyleFlags() & (ComponentPeer::windowIsTemporary
  146. | ComponentPeer::windowIgnoresKeyPresses)) == 0)
  147. toFront (true);
  148. }
  149. void TopLevelWindow::parentHierarchyChanged()
  150. {
  151. setDropShadowEnabled (useDropShadow);
  152. }
  153. int TopLevelWindow::getDesktopWindowStyleFlags() const
  154. {
  155. int styleFlags = ComponentPeer::windowAppearsOnTaskbar;
  156. if (useDropShadow) styleFlags |= ComponentPeer::windowHasDropShadow;
  157. if (useNativeTitleBar) styleFlags |= ComponentPeer::windowHasTitleBar;
  158. return styleFlags;
  159. }
  160. void TopLevelWindow::setDropShadowEnabled (const bool useShadow)
  161. {
  162. useDropShadow = useShadow;
  163. if (isOnDesktop())
  164. {
  165. shadower = nullptr;
  166. Component::addToDesktop (getDesktopWindowStyleFlags());
  167. }
  168. else
  169. {
  170. if (useShadow && isOpaque())
  171. {
  172. if (shadower == nullptr)
  173. {
  174. shadower = getLookAndFeel().createDropShadowerForComponent (*this);
  175. if (shadower != nullptr)
  176. shadower->setOwner (this);
  177. }
  178. }
  179. else
  180. {
  181. shadower = nullptr;
  182. }
  183. }
  184. }
  185. void TopLevelWindow::setUsingNativeTitleBar (const bool shouldUseNativeTitleBar)
  186. {
  187. if (useNativeTitleBar != shouldUseNativeTitleBar)
  188. {
  189. FocusRestorer focusRestorer;
  190. useNativeTitleBar = shouldUseNativeTitleBar;
  191. recreateDesktopWindow();
  192. sendLookAndFeelChange();
  193. }
  194. }
  195. void TopLevelWindow::recreateDesktopWindow()
  196. {
  197. if (isOnDesktop())
  198. {
  199. Component::addToDesktop (getDesktopWindowStyleFlags());
  200. toFront (true);
  201. }
  202. }
  203. void TopLevelWindow::addToDesktop()
  204. {
  205. shadower = nullptr;
  206. Component::addToDesktop (getDesktopWindowStyleFlags());
  207. setDropShadowEnabled (isDropShadowEnabled()); // force an update to clear away any fake shadows if necessary.
  208. }
  209. void TopLevelWindow::addToDesktop (int windowStyleFlags, void* nativeWindowToAttachTo)
  210. {
  211. /* It's not recommended to change the desktop window flags directly for a TopLevelWindow,
  212. because this class needs to make sure its layout corresponds with settings like whether
  213. it's got a native title bar or not.
  214. If you need custom flags for your window, you can override the getDesktopWindowStyleFlags()
  215. method. If you do this, it's best to call the base class's getDesktopWindowStyleFlags()
  216. method, then add or remove whatever flags are necessary from this value before returning it.
  217. */
  218. jassert ((windowStyleFlags & ~ComponentPeer::windowIsSemiTransparent)
  219. == (getDesktopWindowStyleFlags() & ~ComponentPeer::windowIsSemiTransparent));
  220. Component::addToDesktop (windowStyleFlags, nativeWindowToAttachTo);
  221. if (windowStyleFlags != getDesktopWindowStyleFlags())
  222. sendLookAndFeelChange();
  223. }
  224. std::unique_ptr<AccessibilityHandler> TopLevelWindow::createAccessibilityHandler()
  225. {
  226. return std::make_unique<AccessibilityHandler> (*this, AccessibilityRole::window);
  227. }
  228. //==============================================================================
  229. void TopLevelWindow::centreAroundComponent (Component* c, const int width, const int height)
  230. {
  231. if (c == nullptr)
  232. c = TopLevelWindow::getActiveTopLevelWindow();
  233. if (c == nullptr || c->getBounds().isEmpty())
  234. {
  235. centreWithSize (width, height);
  236. }
  237. else
  238. {
  239. const auto scale = getDesktopScaleFactor() / Desktop::getInstance().getGlobalScaleFactor();
  240. auto targetCentre = c->localPointToGlobal (c->getLocalBounds().getCentre()) / scale;
  241. auto parentArea = getLocalArea (nullptr, c->getParentMonitorArea());
  242. if (auto* parent = getParentComponent())
  243. {
  244. targetCentre = parent->getLocalPoint (nullptr, targetCentre);
  245. parentArea = parent->getLocalBounds();
  246. }
  247. setBounds (Rectangle<int> (targetCentre.x - width / 2,
  248. targetCentre.y - height / 2,
  249. width, height)
  250. .constrainedWithin (parentArea.reduced (12, 12)));
  251. }
  252. }
  253. //==============================================================================
  254. int TopLevelWindow::getNumTopLevelWindows() noexcept
  255. {
  256. return TopLevelWindowManager::getInstance()->windows.size();
  257. }
  258. TopLevelWindow* TopLevelWindow::getTopLevelWindow (const int index) noexcept
  259. {
  260. return TopLevelWindowManager::getInstance()->windows [index];
  261. }
  262. TopLevelWindow* TopLevelWindow::getActiveTopLevelWindow() noexcept
  263. {
  264. TopLevelWindow* best = nullptr;
  265. int bestNumTWLParents = -1;
  266. for (int i = TopLevelWindow::getNumTopLevelWindows(); --i >= 0;)
  267. {
  268. auto* tlw = TopLevelWindow::getTopLevelWindow (i);
  269. if (tlw->isActiveWindow())
  270. {
  271. int numTWLParents = 0;
  272. for (auto* c = tlw->getParentComponent(); c != nullptr; c = c->getParentComponent())
  273. if (dynamic_cast<const TopLevelWindow*> (c) != nullptr)
  274. ++numTWLParents;
  275. if (bestNumTWLParents < numTWLParents)
  276. {
  277. best = tlw;
  278. bestNumTWLParents = numTWLParents;
  279. }
  280. }
  281. }
  282. return best;
  283. }
  284. } // namespace juce