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.

362 lines
12KB

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