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.

352 lines
10KB

  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. /** Keeps track of the active top level window. */
  22. class TopLevelWindowManager : private Timer,
  23. private DeletedAtShutdown
  24. {
  25. public:
  26. TopLevelWindowManager() {}
  27. ~TopLevelWindowManager() { clearSingletonInstance(); }
  28. juce_DeclareSingleton_SingleThreaded_Minimal (TopLevelWindowManager)
  29. void checkFocusAsync()
  30. {
  31. startTimer (10);
  32. }
  33. void checkFocus()
  34. {
  35. startTimer (jmin (1731, getTimerInterval() * 2));
  36. auto* newActive = findCurrentlyActiveWindow();
  37. if (newActive != currentActive)
  38. {
  39. currentActive = newActive;
  40. for (int i = windows.size(); --i >= 0;)
  41. if (auto* tlw = windows[i])
  42. tlw->setWindowActive (isWindowActive (tlw));
  43. Desktop::getInstance().triggerFocusCallback();
  44. }
  45. }
  46. bool addWindow (TopLevelWindow* const w)
  47. {
  48. windows.add (w);
  49. checkFocusAsync();
  50. return isWindowActive (w);
  51. }
  52. void removeWindow (TopLevelWindow* const w)
  53. {
  54. checkFocusAsync();
  55. if (currentActive == w)
  56. currentActive = nullptr;
  57. windows.removeFirstMatchingValue (w);
  58. if (windows.isEmpty())
  59. deleteInstance();
  60. }
  61. Array<TopLevelWindow*> windows;
  62. private:
  63. TopLevelWindow* currentActive = nullptr;
  64. void timerCallback() override
  65. {
  66. checkFocus();
  67. }
  68. bool isWindowActive (TopLevelWindow* const tlw) const
  69. {
  70. return (tlw == currentActive
  71. || tlw->isParentOf (currentActive)
  72. || tlw->hasKeyboardFocus (true))
  73. && tlw->isShowing();
  74. }
  75. TopLevelWindow* findCurrentlyActiveWindow() const
  76. {
  77. if (Process::isForegroundProcess())
  78. {
  79. auto* focusedComp = Component::getCurrentlyFocusedComponent();
  80. auto* w = dynamic_cast<TopLevelWindow*> (focusedComp);
  81. if (w == nullptr && focusedComp != nullptr)
  82. w = focusedComp->findParentComponentOfClass<TopLevelWindow>();
  83. if (w == nullptr)
  84. w = currentActive;
  85. if (w != nullptr && w->isShowing())
  86. return w;
  87. }
  88. return nullptr;
  89. }
  90. JUCE_DECLARE_NON_COPYABLE (TopLevelWindowManager)
  91. };
  92. juce_ImplementSingleton_SingleThreaded (TopLevelWindowManager)
  93. void juce_checkCurrentlyFocusedTopLevelWindow();
  94. void juce_checkCurrentlyFocusedTopLevelWindow()
  95. {
  96. if (auto* wm = TopLevelWindowManager::getInstanceWithoutCreating())
  97. wm->checkFocusAsync();
  98. }
  99. //==============================================================================
  100. TopLevelWindow::TopLevelWindow (const String& name, const bool shouldAddToDesktop)
  101. : Component (name)
  102. {
  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. //==============================================================================
  225. void TopLevelWindow::centreAroundComponent (Component* c, const int width, const int height)
  226. {
  227. if (c == nullptr)
  228. c = TopLevelWindow::getActiveTopLevelWindow();
  229. if (c == nullptr || c->getBounds().isEmpty())
  230. {
  231. centreWithSize (width, height);
  232. }
  233. else
  234. {
  235. auto targetCentre = c->localPointToGlobal (c->getLocalBounds().getCentre());
  236. auto parentArea = c->getParentMonitorArea();
  237. if (auto* parent = getParentComponent())
  238. {
  239. targetCentre = parent->getLocalPoint (nullptr, targetCentre);
  240. parentArea = parent->getLocalBounds();
  241. }
  242. setBounds (Rectangle<int> (targetCentre.x - width / 2,
  243. targetCentre.y - height / 2,
  244. width, height)
  245. .constrainedWithin (parentArea.reduced (12, 12)));
  246. }
  247. }
  248. //==============================================================================
  249. int TopLevelWindow::getNumTopLevelWindows() noexcept
  250. {
  251. return TopLevelWindowManager::getInstance()->windows.size();
  252. }
  253. TopLevelWindow* TopLevelWindow::getTopLevelWindow (const int index) noexcept
  254. {
  255. return TopLevelWindowManager::getInstance()->windows [index];
  256. }
  257. TopLevelWindow* TopLevelWindow::getActiveTopLevelWindow() noexcept
  258. {
  259. TopLevelWindow* best = nullptr;
  260. int bestNumTWLParents = -1;
  261. for (int i = TopLevelWindow::getNumTopLevelWindows(); --i >= 0;)
  262. {
  263. auto* tlw = TopLevelWindow::getTopLevelWindow (i);
  264. if (tlw->isActiveWindow())
  265. {
  266. int numTWLParents = 0;
  267. for (auto* c = tlw->getParentComponent(); c != nullptr; c = c->getParentComponent())
  268. if (dynamic_cast<const TopLevelWindow*> (c) != nullptr)
  269. ++numTWLParents;
  270. if (bestNumTWLParents < numTWLParents)
  271. {
  272. best = tlw;
  273. bestNumTWLParents = numTWLParents;
  274. }
  275. }
  276. }
  277. return best;
  278. }
  279. } // namespace juce