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.

369 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. Desktop::Desktop()
  21. : mouseSources (new MouseInputSource::SourceList()),
  22. masterScaleFactor ((float) getDefaultMasterScale()),
  23. nativeDarkModeChangeDetectorImpl (createNativeDarkModeChangeDetectorImpl())
  24. {
  25. displays.reset (new Displays (*this));
  26. }
  27. Desktop::~Desktop()
  28. {
  29. setScreenSaverEnabled (true);
  30. animator.cancelAllAnimations (false);
  31. jassert (instance == this);
  32. instance = nullptr;
  33. // doh! If you don't delete all your windows before exiting, you're going to
  34. // be leaking memory!
  35. jassert (desktopComponents.size() == 0);
  36. }
  37. Desktop& JUCE_CALLTYPE Desktop::getInstance()
  38. {
  39. if (instance == nullptr)
  40. instance = new Desktop();
  41. return *instance;
  42. }
  43. Desktop* Desktop::instance = nullptr;
  44. //==============================================================================
  45. int Desktop::getNumComponents() const noexcept
  46. {
  47. return desktopComponents.size();
  48. }
  49. Component* Desktop::getComponent (int index) const noexcept
  50. {
  51. return desktopComponents [index];
  52. }
  53. Component* Desktop::findComponentAt (Point<int> screenPosition) const
  54. {
  55. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  56. for (int i = desktopComponents.size(); --i >= 0;)
  57. {
  58. auto* c = desktopComponents.getUnchecked(i);
  59. if (c->isVisible())
  60. {
  61. auto relative = c->getLocalPoint (nullptr, screenPosition);
  62. if (c->contains (relative))
  63. return c->getComponentAt (relative);
  64. }
  65. }
  66. return nullptr;
  67. }
  68. //==============================================================================
  69. LookAndFeel& Desktop::getDefaultLookAndFeel() noexcept
  70. {
  71. if (auto lf = currentLookAndFeel.get())
  72. return *lf;
  73. if (defaultLookAndFeel == nullptr)
  74. defaultLookAndFeel.reset (new LookAndFeel_V4());
  75. auto lf = defaultLookAndFeel.get();
  76. jassert (lf != nullptr);
  77. currentLookAndFeel = lf;
  78. return *lf;
  79. }
  80. void Desktop::setDefaultLookAndFeel (LookAndFeel* newDefaultLookAndFeel)
  81. {
  82. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  83. currentLookAndFeel = newDefaultLookAndFeel;
  84. for (int i = getNumComponents(); --i >= 0;)
  85. if (auto* c = getComponent (i))
  86. c->sendLookAndFeelChange();
  87. }
  88. //==============================================================================
  89. void Desktop::addDesktopComponent (Component* c)
  90. {
  91. jassert (c != nullptr);
  92. jassert (! desktopComponents.contains (c));
  93. desktopComponents.addIfNotAlreadyThere (c);
  94. }
  95. void Desktop::removeDesktopComponent (Component* c)
  96. {
  97. desktopComponents.removeFirstMatchingValue (c);
  98. }
  99. void Desktop::componentBroughtToFront (Component* c)
  100. {
  101. auto index = desktopComponents.indexOf (c);
  102. jassert (index >= 0);
  103. if (index >= 0)
  104. {
  105. int newIndex = -1;
  106. if (! c->isAlwaysOnTop())
  107. {
  108. newIndex = desktopComponents.size();
  109. while (newIndex > 0 && desktopComponents.getUnchecked (newIndex - 1)->isAlwaysOnTop())
  110. --newIndex;
  111. --newIndex;
  112. }
  113. desktopComponents.move (index, newIndex);
  114. }
  115. }
  116. //==============================================================================
  117. Point<int> Desktop::getMousePosition()
  118. {
  119. return getMousePositionFloat().roundToInt();
  120. }
  121. Point<float> Desktop::getMousePositionFloat()
  122. {
  123. return getInstance().getMainMouseSource().getScreenPosition();
  124. }
  125. void Desktop::setMousePosition (Point<int> newPosition)
  126. {
  127. getInstance().getMainMouseSource().setScreenPosition (newPosition.toFloat());
  128. }
  129. Point<int> Desktop::getLastMouseDownPosition()
  130. {
  131. return getInstance().getMainMouseSource().getLastMouseDownPosition().roundToInt();
  132. }
  133. int Desktop::getMouseButtonClickCounter() const noexcept { return mouseClickCounter; }
  134. int Desktop::getMouseWheelMoveCounter() const noexcept { return mouseWheelCounter; }
  135. void Desktop::incrementMouseClickCounter() noexcept { ++mouseClickCounter; }
  136. void Desktop::incrementMouseWheelCounter() noexcept { ++mouseWheelCounter; }
  137. const Array<MouseInputSource>& Desktop::getMouseSources() const noexcept { return mouseSources->sourceArray; }
  138. int Desktop::getNumMouseSources() const noexcept { return mouseSources->sources.size(); }
  139. int Desktop::getNumDraggingMouseSources() const noexcept { return mouseSources->getNumDraggingMouseSources(); }
  140. MouseInputSource* Desktop::getMouseSource (int index) const noexcept { return mouseSources->getMouseSource (index); }
  141. MouseInputSource* Desktop::getDraggingMouseSource (int index) const noexcept { return mouseSources->getDraggingMouseSource (index); }
  142. MouseInputSource Desktop::getMainMouseSource() const noexcept { return MouseInputSource (mouseSources->sources.getUnchecked(0)); }
  143. void Desktop::beginDragAutoRepeat (int interval) { mouseSources->beginDragAutoRepeat (interval); }
  144. //==============================================================================
  145. void Desktop::addFocusChangeListener (FocusChangeListener* l) { focusListeners.add (l); }
  146. void Desktop::removeFocusChangeListener (FocusChangeListener* l) { focusListeners.remove (l); }
  147. void Desktop::triggerFocusCallback() { triggerAsyncUpdate(); }
  148. void Desktop::updateFocusOutline()
  149. {
  150. if (auto* currentFocus = Component::getCurrentlyFocusedComponent())
  151. {
  152. if (currentFocus->hasFocusOutline())
  153. {
  154. focusOutline = currentFocus->getLookAndFeel().createFocusOutlineForComponent (*currentFocus);
  155. if (focusOutline != nullptr)
  156. focusOutline->setOwner (currentFocus);
  157. return;
  158. }
  159. }
  160. focusOutline = nullptr;
  161. }
  162. void Desktop::handleAsyncUpdate()
  163. {
  164. // The component may be deleted during this operation, but we'll use a SafePointer rather than a
  165. // BailOutChecker so that any remaining listeners will still get a callback (with a null pointer).
  166. focusListeners.call ([currentFocus = WeakReference<Component> { Component::getCurrentlyFocusedComponent() }] (FocusChangeListener& l)
  167. {
  168. l.globalFocusChanged (currentFocus.get());
  169. });
  170. updateFocusOutline();
  171. }
  172. //==============================================================================
  173. void Desktop::addDarkModeSettingListener (DarkModeSettingListener* l) { darkModeSettingListeners.add (l); }
  174. void Desktop::removeDarkModeSettingListener (DarkModeSettingListener* l) { darkModeSettingListeners.remove (l); }
  175. void Desktop::darkModeChanged() { darkModeSettingListeners.call ([] (DarkModeSettingListener& l) { l.darkModeSettingChanged(); }); }
  176. //==============================================================================
  177. void Desktop::resetTimer()
  178. {
  179. if (mouseListeners.size() == 0)
  180. stopTimer();
  181. else
  182. startTimer (100);
  183. lastFakeMouseMove = getMousePositionFloat();
  184. }
  185. ListenerList<MouseListener>& Desktop::getMouseListeners()
  186. {
  187. resetTimer();
  188. return mouseListeners;
  189. }
  190. void Desktop::addGlobalMouseListener (MouseListener* listener)
  191. {
  192. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  193. mouseListeners.add (listener);
  194. resetTimer();
  195. }
  196. void Desktop::removeGlobalMouseListener (MouseListener* listener)
  197. {
  198. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  199. mouseListeners.remove (listener);
  200. resetTimer();
  201. }
  202. void Desktop::timerCallback()
  203. {
  204. if (lastFakeMouseMove != getMousePositionFloat())
  205. sendMouseMove();
  206. }
  207. void Desktop::sendMouseMove()
  208. {
  209. if (! mouseListeners.isEmpty())
  210. {
  211. startTimer (20);
  212. lastFakeMouseMove = getMousePositionFloat();
  213. if (auto* target = findComponentAt (lastFakeMouseMove.roundToInt()))
  214. {
  215. Component::BailOutChecker checker (target);
  216. auto pos = target->getLocalPoint (nullptr, lastFakeMouseMove);
  217. auto now = Time::getCurrentTime();
  218. const MouseEvent me (getMainMouseSource(), pos, ModifierKeys::currentModifiers, MouseInputSource::defaultPressure,
  219. MouseInputSource::defaultOrientation, MouseInputSource::defaultRotation,
  220. MouseInputSource::defaultTiltX, MouseInputSource::defaultTiltY,
  221. target, target, now, pos, now, 0, false);
  222. if (me.mods.isAnyMouseButtonDown())
  223. mouseListeners.callChecked (checker, [&] (MouseListener& l) { l.mouseDrag (me); });
  224. else
  225. mouseListeners.callChecked (checker, [&] (MouseListener& l) { l.mouseMove (me); });
  226. }
  227. }
  228. }
  229. //==============================================================================
  230. void Desktop::setKioskModeComponent (Component* componentToUse, bool allowMenusAndBars)
  231. {
  232. if (kioskModeReentrant)
  233. return;
  234. const ScopedValueSetter<bool> setter (kioskModeReentrant, true, false);
  235. if (kioskModeComponent != componentToUse)
  236. {
  237. // agh! Don't delete or remove a component from the desktop while it's still the kiosk component!
  238. jassert (kioskModeComponent == nullptr || ComponentPeer::getPeerFor (kioskModeComponent) != nullptr);
  239. if (auto* oldKioskComp = kioskModeComponent)
  240. {
  241. kioskModeComponent = nullptr; // (to make sure that isKioskMode() returns false when resizing the old one)
  242. setKioskComponent (oldKioskComp, false, allowMenusAndBars);
  243. oldKioskComp->setBounds (kioskComponentOriginalBounds);
  244. }
  245. kioskModeComponent = componentToUse;
  246. if (kioskModeComponent != nullptr)
  247. {
  248. // Only components that are already on the desktop can be put into kiosk mode!
  249. jassert (ComponentPeer::getPeerFor (kioskModeComponent) != nullptr);
  250. kioskComponentOriginalBounds = kioskModeComponent->getBounds();
  251. setKioskComponent (kioskModeComponent, true, allowMenusAndBars);
  252. }
  253. }
  254. }
  255. //==============================================================================
  256. void Desktop::setOrientationsEnabled (int newOrientations)
  257. {
  258. if (allowedOrientations != newOrientations)
  259. {
  260. // Dodgy set of flags being passed here! Make sure you specify at least one permitted orientation.
  261. jassert (newOrientations != 0 && (newOrientations & ~allOrientations) == 0);
  262. allowedOrientations = newOrientations;
  263. allowedOrientationsChanged();
  264. }
  265. }
  266. int Desktop::getOrientationsEnabled() const noexcept
  267. {
  268. return allowedOrientations;
  269. }
  270. bool Desktop::isOrientationEnabled (DisplayOrientation orientation) const noexcept
  271. {
  272. // Make sure you only pass one valid flag in here...
  273. jassert (orientation == upright || orientation == upsideDown
  274. || orientation == rotatedClockwise || orientation == rotatedAntiClockwise);
  275. return (allowedOrientations & orientation) != 0;
  276. }
  277. void Desktop::setGlobalScaleFactor (float newScaleFactor) noexcept
  278. {
  279. JUCE_ASSERT_MESSAGE_MANAGER_IS_LOCKED
  280. if (masterScaleFactor != newScaleFactor)
  281. {
  282. masterScaleFactor = newScaleFactor;
  283. displays->refresh();
  284. }
  285. }
  286. bool Desktop::isHeadless() const noexcept
  287. {
  288. return displays->displays.isEmpty();
  289. }
  290. } // namespace juce