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.

343 lines
9.7KB

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