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 7 technical preview.
  4. Copyright (c) 2022 - 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 the technical preview this file cannot be licensed commercially.
  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. setTitle (name);
  98. setOpaque (true);
  99. if (shouldAddToDesktop)
  100. Component::addToDesktop (TopLevelWindow::getDesktopWindowStyleFlags());
  101. else
  102. setDropShadowEnabled (true);
  103. setWantsKeyboardFocus (true);
  104. setBroughtToFrontOnMouseClick (true);
  105. isCurrentlyActive = TopLevelWindowManager::getInstance()->addWindow (this);
  106. }
  107. TopLevelWindow::~TopLevelWindow()
  108. {
  109. shadower = nullptr;
  110. TopLevelWindowManager::getInstance()->removeWindow (this);
  111. }
  112. //==============================================================================
  113. void TopLevelWindow::focusOfChildComponentChanged (FocusChangeType)
  114. {
  115. auto* wm = TopLevelWindowManager::getInstance();
  116. if (hasKeyboardFocus (true))
  117. wm->checkFocus();
  118. else
  119. wm->checkFocusAsync();
  120. }
  121. void TopLevelWindow::setWindowActive (const bool isNowActive)
  122. {
  123. if (isCurrentlyActive != isNowActive)
  124. {
  125. isCurrentlyActive = isNowActive;
  126. activeWindowStatusChanged();
  127. }
  128. }
  129. void TopLevelWindow::activeWindowStatusChanged()
  130. {
  131. }
  132. bool TopLevelWindow::isUsingNativeTitleBar() const noexcept
  133. {
  134. return useNativeTitleBar && (isOnDesktop() || ! isShowing());
  135. }
  136. void TopLevelWindow::visibilityChanged()
  137. {
  138. if (isShowing())
  139. if (auto* p = getPeer())
  140. if ((p->getStyleFlags() & (ComponentPeer::windowIsTemporary
  141. | ComponentPeer::windowIgnoresKeyPresses)) == 0)
  142. toFront (true);
  143. }
  144. void TopLevelWindow::parentHierarchyChanged()
  145. {
  146. setDropShadowEnabled (useDropShadow);
  147. }
  148. int TopLevelWindow::getDesktopWindowStyleFlags() const
  149. {
  150. int styleFlags = ComponentPeer::windowAppearsOnTaskbar;
  151. if (useDropShadow) styleFlags |= ComponentPeer::windowHasDropShadow;
  152. if (useNativeTitleBar) styleFlags |= ComponentPeer::windowHasTitleBar;
  153. return styleFlags;
  154. }
  155. void TopLevelWindow::setDropShadowEnabled (const bool useShadow)
  156. {
  157. useDropShadow = useShadow;
  158. if (isOnDesktop())
  159. {
  160. shadower = nullptr;
  161. Component::addToDesktop (getDesktopWindowStyleFlags());
  162. }
  163. else
  164. {
  165. if (useShadow && isOpaque())
  166. {
  167. if (shadower == nullptr)
  168. {
  169. shadower = getLookAndFeel().createDropShadowerForComponent (*this);
  170. if (shadower != nullptr)
  171. shadower->setOwner (this);
  172. }
  173. }
  174. else
  175. {
  176. shadower = nullptr;
  177. }
  178. }
  179. }
  180. void TopLevelWindow::setUsingNativeTitleBar (const bool shouldUseNativeTitleBar)
  181. {
  182. if (useNativeTitleBar != shouldUseNativeTitleBar)
  183. {
  184. FocusRestorer focusRestorer;
  185. useNativeTitleBar = shouldUseNativeTitleBar;
  186. recreateDesktopWindow();
  187. sendLookAndFeelChange();
  188. }
  189. }
  190. void TopLevelWindow::recreateDesktopWindow()
  191. {
  192. if (isOnDesktop())
  193. {
  194. Component::addToDesktop (getDesktopWindowStyleFlags());
  195. toFront (true);
  196. }
  197. }
  198. void TopLevelWindow::addToDesktop()
  199. {
  200. shadower = nullptr;
  201. Component::addToDesktop (getDesktopWindowStyleFlags());
  202. setDropShadowEnabled (isDropShadowEnabled()); // force an update to clear away any fake shadows if necessary.
  203. }
  204. void TopLevelWindow::addToDesktop (int windowStyleFlags, void* nativeWindowToAttachTo)
  205. {
  206. /* It's not recommended to change the desktop window flags directly for a TopLevelWindow,
  207. because this class needs to make sure its layout corresponds with settings like whether
  208. it's got a native title bar or not.
  209. If you need custom flags for your window, you can override the getDesktopWindowStyleFlags()
  210. method. If you do this, it's best to call the base class's getDesktopWindowStyleFlags()
  211. method, then add or remove whatever flags are necessary from this value before returning it.
  212. */
  213. jassert ((windowStyleFlags & ~ComponentPeer::windowIsSemiTransparent)
  214. == (getDesktopWindowStyleFlags() & ~ComponentPeer::windowIsSemiTransparent));
  215. Component::addToDesktop (windowStyleFlags, nativeWindowToAttachTo);
  216. if (windowStyleFlags != getDesktopWindowStyleFlags())
  217. sendLookAndFeelChange();
  218. }
  219. std::unique_ptr<AccessibilityHandler> TopLevelWindow::createAccessibilityHandler()
  220. {
  221. return std::make_unique<AccessibilityHandler> (*this, AccessibilityRole::window);
  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. const auto scale = getDesktopScaleFactor() / Desktop::getInstance().getGlobalScaleFactor();
  235. auto targetCentre = c->localPointToGlobal (c->getLocalBounds().getCentre()) / scale;
  236. auto parentArea = getLocalArea (nullptr, 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