The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

251 lines
8.8KB

  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. extern NSMenu* createNSMenu (const PopupMenu&, const String& name, int topLevelMenuId,
  22. int topLevelIndex, bool addDelegate);
  23. class SystemTrayIconComponent::Pimpl : private Timer
  24. {
  25. public:
  26. //==============================================================================
  27. Pimpl (SystemTrayIconComponent& iconComp, const Image& im)
  28. : owner (iconComp), statusIcon (imageToNSImage (im))
  29. {
  30. static ButtonEventForwarderClass cls;
  31. eventForwarder.reset ([cls.createInstance() init]);
  32. ButtonEventForwarderClass::setOwner (eventForwarder.get(), this);
  33. configureIcon();
  34. statusItem.reset ([[[NSStatusBar systemStatusBar] statusItemWithLength: NSSquareStatusItemLength] retain]);
  35. auto button = [statusItem.get() button];
  36. button.image = statusIcon.get();
  37. button.target = eventForwarder.get();
  38. button.action = @selector (handleEvent:);
  39. #if defined (MAC_OS_X_VERSION_10_12) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_12
  40. [button sendActionOn: NSEventMaskLeftMouseDown | NSEventMaskRightMouseDown | NSEventMaskScrollWheel];
  41. #else
  42. [button sendActionOn: NSLeftMouseDownMask | NSRightMouseDownMask | NSScrollWheelMask];
  43. #endif
  44. }
  45. //==============================================================================
  46. void updateIcon (const Image& newImage)
  47. {
  48. statusIcon.reset (imageToNSImage (newImage));
  49. configureIcon();
  50. [statusItem.get() button].image = statusIcon.get();
  51. }
  52. void setHighlighted (bool shouldHighlight)
  53. {
  54. [[statusItem.get() button] setHighlighted: shouldHighlight];
  55. }
  56. void showMenu (const PopupMenu& menu)
  57. {
  58. if (NSMenu* m = createNSMenu (menu, "MenuBarItem", -2, -3, true))
  59. {
  60. setHighlighted (true);
  61. stopTimer();
  62. // There's currently no good alternative to this...
  63. #if defined __clang__ && defined (MAC_OS_X_VERSION_10_14) && MAC_OS_X_VERSION_MIN_REQUIRED <= MAC_OS_X_VERSION_10_14
  64. #define IGNORE_POPUP_DEPRECATION 1
  65. #pragma clang diagnostic push
  66. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  67. #endif
  68. [statusItem.get() popUpStatusItemMenu: m];
  69. #if IGNORE_POPUP_DEPRECATION
  70. #pragma clang diagnostic pop
  71. #endif
  72. startTimer (1);
  73. }
  74. }
  75. //==============================================================================
  76. NSStatusItem* getStatusItem()
  77. {
  78. return statusItem.get();
  79. }
  80. //==============================================================================
  81. void handleEvent()
  82. {
  83. auto e = [NSApp currentEvent];
  84. NSEventType type = [e type];
  85. const bool isLeft = (type == NSEventTypeLeftMouseDown);
  86. const bool isRight = (type == NSEventTypeRightMouseDown);
  87. if (owner.isCurrentlyBlockedByAnotherModalComponent())
  88. {
  89. if (isLeft || isRight)
  90. if (auto* current = Component::getCurrentlyModalComponent())
  91. current->inputAttemptWhenModal();
  92. }
  93. else
  94. {
  95. auto eventMods = ComponentPeer::getCurrentModifiersRealtime();
  96. if (([e modifierFlags] & NSEventModifierFlagCommand) != 0)
  97. eventMods = eventMods.withFlags (ModifierKeys::commandModifier);
  98. auto now = Time::getCurrentTime();
  99. auto mouseSource = Desktop::getInstance().getMainMouseSource();
  100. auto pressure = (float) e.pressure;
  101. if (isLeft || isRight)
  102. {
  103. owner.mouseDown ({ mouseSource, {},
  104. eventMods.withFlags (isLeft ? ModifierKeys::leftButtonModifier
  105. : ModifierKeys::rightButtonModifier),
  106. pressure,
  107. MouseInputSource::invalidOrientation, MouseInputSource::invalidRotation,
  108. MouseInputSource::invalidTiltX, MouseInputSource::invalidTiltY,
  109. &owner, &owner, now, {}, now, 1, false });
  110. owner.mouseUp ({ mouseSource, {},
  111. eventMods.withoutMouseButtons(),
  112. pressure,
  113. MouseInputSource::invalidOrientation, MouseInputSource::invalidRotation,
  114. MouseInputSource::invalidTiltX, MouseInputSource::invalidTiltY,
  115. &owner, &owner, now, {}, now, 1, false });
  116. }
  117. else if (type == NSEventTypeMouseMoved)
  118. {
  119. owner.mouseMove (MouseEvent (mouseSource, {}, eventMods, pressure,
  120. MouseInputSource::invalidOrientation, MouseInputSource::invalidRotation,
  121. MouseInputSource::invalidTiltX, MouseInputSource::invalidTiltY,
  122. &owner, &owner, now, {}, now, 1, false));
  123. }
  124. }
  125. }
  126. private:
  127. //==============================================================================
  128. void configureIcon()
  129. {
  130. [statusIcon.get() setSize: NSMakeSize (20.0f, 20.0f)];
  131. [statusIcon.get() setTemplate: true];
  132. }
  133. void timerCallback() override
  134. {
  135. stopTimer();
  136. setHighlighted (false);
  137. }
  138. //==============================================================================
  139. class ButtonEventForwarderClass : public ObjCClass<NSObject>
  140. {
  141. public:
  142. ButtonEventForwarderClass() : ObjCClass<NSObject> ("JUCEButtonEventForwarderClass_")
  143. {
  144. addIvar<Pimpl*> ("owner");
  145. addMethod (@selector (handleEvent:), handleEvent, "v@:@");
  146. registerClass();
  147. }
  148. static Pimpl* getOwner (id self) { return getIvar<Pimpl*> (self, "owner"); }
  149. static void setOwner (id self, Pimpl* owner) { object_setInstanceVariable (self, "owner", owner); }
  150. private:
  151. static void handleEvent (id self, SEL, id)
  152. {
  153. if (auto* owner = getOwner (self))
  154. owner->handleEvent();
  155. }
  156. };
  157. //==============================================================================
  158. SystemTrayIconComponent& owner;
  159. std::unique_ptr<NSStatusItem, NSObjectDeleter> statusItem;
  160. std::unique_ptr<NSObject, NSObjectDeleter> eventForwarder;
  161. std::unique_ptr<NSImage, NSObjectDeleter> statusIcon;
  162. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
  163. };
  164. //==============================================================================
  165. void SystemTrayIconComponent::setIconImage (const Image&, const Image& templateImage)
  166. {
  167. if (templateImage.isValid())
  168. {
  169. if (pimpl == nullptr)
  170. pimpl.reset (new Pimpl (*this, templateImage));
  171. else
  172. pimpl->updateIcon (templateImage);
  173. }
  174. else
  175. {
  176. pimpl.reset();
  177. }
  178. }
  179. void SystemTrayIconComponent::setIconTooltip (const String&)
  180. {
  181. // xxx not yet implemented!
  182. }
  183. void SystemTrayIconComponent::setHighlighted (bool highlight)
  184. {
  185. if (pimpl != nullptr)
  186. pimpl->setHighlighted (highlight);
  187. }
  188. void SystemTrayIconComponent::showInfoBubble (const String& /*title*/, const String& /*content*/)
  189. {
  190. // xxx Not implemented!
  191. }
  192. void SystemTrayIconComponent::hideInfoBubble()
  193. {
  194. // xxx Not implemented!
  195. }
  196. void* SystemTrayIconComponent::getNativeHandle() const
  197. {
  198. return pimpl != nullptr ? pimpl->getStatusItem() : nullptr;
  199. }
  200. void SystemTrayIconComponent::showDropdownMenu (const PopupMenu& menu)
  201. {
  202. if (pimpl != nullptr)
  203. pimpl->showMenu (menu);
  204. }
  205. } // namespace juce