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.

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