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.

juce_TopLevelWindow.cpp 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. setOpaque (true);
  103. if (shouldAddToDesktop)
  104. Component::addToDesktop (TopLevelWindow::getDesktopWindowStyleFlags());
  105. else
  106. setDropShadowEnabled (true);
  107. setWantsKeyboardFocus (true);
  108. setBroughtToFrontOnMouseClick (true);
  109. isCurrentlyActive = TopLevelWindowManager::getInstance()->addWindow (this);
  110. }
  111. TopLevelWindow::~TopLevelWindow()
  112. {
  113. shadower.reset();
  114. TopLevelWindowManager::getInstance()->removeWindow (this);
  115. }
  116. //==============================================================================
  117. void TopLevelWindow::focusOfChildComponentChanged (FocusChangeType)
  118. {
  119. auto* wm = TopLevelWindowManager::getInstance();
  120. if (hasKeyboardFocus (true))
  121. wm->checkFocus();
  122. else
  123. wm->checkFocusAsync();
  124. }
  125. void TopLevelWindow::setWindowActive (const bool isNowActive)
  126. {
  127. if (isCurrentlyActive != isNowActive)
  128. {
  129. isCurrentlyActive = isNowActive;
  130. activeWindowStatusChanged();
  131. }
  132. }
  133. void TopLevelWindow::activeWindowStatusChanged()
  134. {
  135. }
  136. bool TopLevelWindow::isUsingNativeTitleBar() const noexcept
  137. {
  138. return useNativeTitleBar && (isOnDesktop() || ! isShowing());
  139. }
  140. void TopLevelWindow::visibilityChanged()
  141. {
  142. if (isShowing())
  143. if (auto* p = getPeer())
  144. if ((p->getStyleFlags() & (ComponentPeer::windowIsTemporary
  145. | ComponentPeer::windowIgnoresKeyPresses)) == 0)
  146. toFront (true);
  147. }
  148. void TopLevelWindow::parentHierarchyChanged()
  149. {
  150. setDropShadowEnabled (useDropShadow);
  151. }
  152. int TopLevelWindow::getDesktopWindowStyleFlags() const
  153. {
  154. int styleFlags = ComponentPeer::windowAppearsOnTaskbar;
  155. if (useDropShadow) styleFlags |= ComponentPeer::windowHasDropShadow;
  156. if (useNativeTitleBar) styleFlags |= ComponentPeer::windowHasTitleBar;
  157. return styleFlags;
  158. }
  159. void TopLevelWindow::setDropShadowEnabled (const bool useShadow)
  160. {
  161. useDropShadow = useShadow;
  162. if (isOnDesktop())
  163. {
  164. shadower.reset();
  165. Component::addToDesktop (getDesktopWindowStyleFlags());
  166. }
  167. else
  168. {
  169. if (useShadow && isOpaque())
  170. {
  171. if (shadower == nullptr)
  172. {
  173. shadower.reset (getLookAndFeel().createDropShadowerForComponent (this));
  174. if (shadower != nullptr)
  175. shadower->setOwner (this);
  176. }
  177. }
  178. else
  179. {
  180. shadower.reset();
  181. }
  182. }
  183. }
  184. void TopLevelWindow::setUsingNativeTitleBar (const bool shouldUseNativeTitleBar)
  185. {
  186. if (useNativeTitleBar != shouldUseNativeTitleBar)
  187. {
  188. FocusRestorer focusRestorer;
  189. useNativeTitleBar = shouldUseNativeTitleBar;
  190. recreateDesktopWindow();
  191. sendLookAndFeelChange();
  192. }
  193. }
  194. void TopLevelWindow::recreateDesktopWindow()
  195. {
  196. if (isOnDesktop())
  197. {
  198. Component::addToDesktop (getDesktopWindowStyleFlags());
  199. toFront (true);
  200. }
  201. }
  202. void TopLevelWindow::addToDesktop()
  203. {
  204. shadower.reset();
  205. Component::addToDesktop (getDesktopWindowStyleFlags());
  206. setDropShadowEnabled (isDropShadowEnabled()); // force an update to clear away any fake shadows if necessary.
  207. }
  208. void TopLevelWindow::addToDesktop (int windowStyleFlags, void* nativeWindowToAttachTo)
  209. {
  210. /* It's not recommended to change the desktop window flags directly for a TopLevelWindow,
  211. because this class needs to make sure its layout corresponds with settings like whether
  212. it's got a native title bar or not.
  213. If you need custom flags for your window, you can override the getDesktopWindowStyleFlags()
  214. method. If you do this, it's best to call the base class's getDesktopWindowStyleFlags()
  215. method, then add or remove whatever flags are necessary from this value before returning it.
  216. */
  217. jassert ((windowStyleFlags & ~ComponentPeer::windowIsSemiTransparent)
  218. == (getDesktopWindowStyleFlags() & ~ComponentPeer::windowIsSemiTransparent));
  219. Component::addToDesktop (windowStyleFlags, nativeWindowToAttachTo);
  220. if (windowStyleFlags != getDesktopWindowStyleFlags())
  221. sendLookAndFeelChange();
  222. }
  223. //==============================================================================
  224. void TopLevelWindow::centreAroundComponent (Component* c, const int width, const int height)
  225. {
  226. if (c == nullptr)
  227. c = TopLevelWindow::getActiveTopLevelWindow();
  228. if (c == nullptr || c->getBounds().isEmpty())
  229. {
  230. centreWithSize (width, height);
  231. }
  232. else
  233. {
  234. auto targetCentre = c->localPointToGlobal (c->getLocalBounds().getCentre());
  235. auto parentArea = c->getParentMonitorArea();
  236. if (auto* parent = getParentComponent())
  237. {
  238. targetCentre = parent->getLocalPoint (nullptr, targetCentre);
  239. parentArea = parent->getLocalBounds();
  240. }
  241. setBounds (Rectangle<int> (targetCentre.x - width / 2,
  242. targetCentre.y - height / 2,
  243. width, height)
  244. .constrainedWithin (parentArea.reduced (12, 12)));
  245. }
  246. }
  247. //==============================================================================
  248. int TopLevelWindow::getNumTopLevelWindows() noexcept
  249. {
  250. return TopLevelWindowManager::getInstance()->windows.size();
  251. }
  252. TopLevelWindow* TopLevelWindow::getTopLevelWindow (const int index) noexcept
  253. {
  254. return TopLevelWindowManager::getInstance()->windows [index];
  255. }
  256. TopLevelWindow* TopLevelWindow::getActiveTopLevelWindow() noexcept
  257. {
  258. TopLevelWindow* best = nullptr;
  259. int bestNumTWLParents = -1;
  260. for (int i = TopLevelWindow::getNumTopLevelWindows(); --i >= 0;)
  261. {
  262. auto* tlw = TopLevelWindow::getTopLevelWindow (i);
  263. if (tlw->isActiveWindow())
  264. {
  265. int numTWLParents = 0;
  266. for (auto* c = tlw->getParentComponent(); c != nullptr; c = c->getParentComponent())
  267. if (dynamic_cast<const TopLevelWindow*> (c) != nullptr)
  268. ++numTWLParents;
  269. if (bestNumTWLParents < numTWLParents)
  270. {
  271. best = tlw;
  272. bestNumTWLParents = numTWLParents;
  273. }
  274. }
  275. }
  276. return best;
  277. }
  278. } // namespace juce