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.

273 lines
9.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. namespace MouseCursorHelpers
  18. {
  19. extern NSImage* createNSImage (const Image&);
  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. Pimpl (SystemTrayIconComponent& iconComp, const Image& im)
  27. : owner (iconComp), statusItem (nil),
  28. statusIcon (MouseCursorHelpers::createNSImage (im)),
  29. view (nil), isHighlighted (false)
  30. {
  31. static SystemTrayViewClass cls;
  32. view = [cls.createInstance() init];
  33. SystemTrayViewClass::setOwner (view, this);
  34. SystemTrayViewClass::setImage (view, statusIcon);
  35. setIconSize();
  36. statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength: NSSquareStatusItemLength] retain];
  37. [statusItem setView: view];
  38. SystemTrayViewClass::frameChanged (view, SEL(), nullptr);
  39. [[NSNotificationCenter defaultCenter] addObserver: view
  40. selector: @selector (frameChanged:)
  41. name: NSWindowDidMoveNotification
  42. object: nil];
  43. }
  44. ~Pimpl()
  45. {
  46. [[NSNotificationCenter defaultCenter] removeObserver: view];
  47. [[NSStatusBar systemStatusBar] removeStatusItem: statusItem];
  48. SystemTrayViewClass::setOwner (view, nullptr);
  49. SystemTrayViewClass::setImage (view, nil);
  50. [statusItem release];
  51. [view release];
  52. [statusIcon release];
  53. }
  54. void updateIcon (const Image& newImage)
  55. {
  56. [statusIcon release];
  57. statusIcon = MouseCursorHelpers::createNSImage (newImage);
  58. setIconSize();
  59. SystemTrayViewClass::setImage (view, statusIcon);
  60. }
  61. void setHighlighted (bool shouldHighlight)
  62. {
  63. isHighlighted = shouldHighlight;
  64. [view setNeedsDisplay: true];
  65. }
  66. void handleStatusItemAction (NSEvent* e)
  67. {
  68. NSEventType type = [e type];
  69. const bool isLeft = (type == NSLeftMouseDown || type == NSLeftMouseUp);
  70. const bool isRight = (type == NSRightMouseDown || type == NSRightMouseUp);
  71. if (owner.isCurrentlyBlockedByAnotherModalComponent())
  72. {
  73. if (isLeft || isRight)
  74. if (Component* const current = Component::getCurrentlyModalComponent())
  75. current->inputAttemptWhenModal();
  76. }
  77. else
  78. {
  79. ModifierKeys eventMods (ModifierKeys::getCurrentModifiersRealtime());
  80. if (([e modifierFlags] & NSCommandKeyMask) != 0)
  81. eventMods = eventMods.withFlags (ModifierKeys::commandModifier);
  82. const Time now (Time::getCurrentTime());
  83. MouseInputSource mouseSource = Desktop::getInstance().getMainMouseSource();
  84. if (isLeft || isRight) // Only mouse up is sent by the OS, so simulate a down/up
  85. {
  86. setHighlighted (true);
  87. startTimer (150);
  88. owner.mouseDown (MouseEvent (mouseSource, Point<float>(),
  89. eventMods.withFlags (isLeft ? ModifierKeys::leftButtonModifier
  90. : ModifierKeys::rightButtonModifier),
  91. &owner, &owner, now,
  92. Point<float>(), now, 1, false));
  93. owner.mouseUp (MouseEvent (mouseSource, Point<float>(), eventMods.withoutMouseButtons(),
  94. &owner, &owner, now,
  95. Point<float>(), now, 1, false));
  96. }
  97. else if (type == NSMouseMoved)
  98. {
  99. owner.mouseMove (MouseEvent (mouseSource, Point<float>(), eventMods,
  100. &owner, &owner, now,
  101. Point<float>(), now, 1, false));
  102. }
  103. }
  104. }
  105. void showMenu (const PopupMenu& menu)
  106. {
  107. if (NSMenu* m = createNSMenu (menu, "MenuBarItem", -2, -3, true))
  108. {
  109. setHighlighted (true);
  110. stopTimer();
  111. [statusItem popUpStatusItemMenu: m];
  112. startTimer (1);
  113. }
  114. }
  115. SystemTrayIconComponent& owner;
  116. NSStatusItem* statusItem;
  117. private:
  118. NSImage* statusIcon;
  119. NSControl* view;
  120. bool isHighlighted;
  121. void setIconSize()
  122. {
  123. [statusIcon setSize: NSMakeSize (20.0f, 20.0f)];
  124. }
  125. void timerCallback() override
  126. {
  127. stopTimer();
  128. setHighlighted (false);
  129. }
  130. struct SystemTrayViewClass : public ObjCClass<NSControl>
  131. {
  132. SystemTrayViewClass() : ObjCClass<NSControl> ("JUCESystemTrayView_")
  133. {
  134. addIvar<Pimpl*> ("owner");
  135. addIvar<NSImage*> ("image");
  136. addMethod (@selector (mouseDown:), handleEventDown, "v@:@");
  137. addMethod (@selector (rightMouseDown:), handleEventDown, "v@:@");
  138. addMethod (@selector (drawRect:), drawRect, "v@:@");
  139. addMethod (@selector (frameChanged:), frameChanged, "v@:@");
  140. registerClass();
  141. }
  142. static Pimpl* getOwner (id self) { return getIvar<Pimpl*> (self, "owner"); }
  143. static NSImage* getImage (id self) { return getIvar<NSImage*> (self, "image"); }
  144. static void setOwner (id self, Pimpl* owner) { object_setInstanceVariable (self, "owner", owner); }
  145. static void setImage (id self, NSImage* image) { object_setInstanceVariable (self, "image", image); }
  146. static void frameChanged (id self, SEL, NSNotification*)
  147. {
  148. if (Pimpl* const owner = getOwner (self))
  149. {
  150. NSRect r = [[[owner->statusItem view] window] frame];
  151. NSRect sr = [[[NSScreen screens] objectAtIndex: 0] frame];
  152. r.origin.y = sr.size.height - r.origin.y - r.size.height;
  153. owner->owner.setBounds (convertToRectInt (r));
  154. }
  155. }
  156. private:
  157. static void handleEventDown (id self, SEL, NSEvent* e)
  158. {
  159. if (Pimpl* const owner = getOwner (self))
  160. owner->handleStatusItemAction (e);
  161. }
  162. static void drawRect (id self, SEL, NSRect)
  163. {
  164. NSRect bounds = [self bounds];
  165. if (Pimpl* const owner = getOwner (self))
  166. [owner->statusItem drawStatusBarBackgroundInRect: bounds
  167. withHighlight: owner->isHighlighted];
  168. if (NSImage* const im = getImage (self))
  169. {
  170. NSSize imageSize = [im size];
  171. [im drawInRect: NSMakeRect (bounds.origin.x + ((bounds.size.width - imageSize.width) / 2.0f),
  172. bounds.origin.y + ((bounds.size.height - imageSize.height) / 2.0f),
  173. imageSize.width, imageSize.height)
  174. fromRect: NSZeroRect
  175. operation: NSCompositeSourceOver
  176. fraction: 1.0f];
  177. }
  178. }
  179. };
  180. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
  181. };
  182. //==============================================================================
  183. void SystemTrayIconComponent::setIconImage (const Image& newImage)
  184. {
  185. if (newImage.isValid())
  186. {
  187. if (pimpl == nullptr)
  188. pimpl = new Pimpl (*this, newImage);
  189. else
  190. pimpl->updateIcon (newImage);
  191. }
  192. else
  193. {
  194. pimpl = nullptr;
  195. }
  196. }
  197. void SystemTrayIconComponent::setIconTooltip (const String&)
  198. {
  199. // xxx not yet implemented!
  200. }
  201. void SystemTrayIconComponent::setHighlighted (bool highlight)
  202. {
  203. if (pimpl != nullptr)
  204. pimpl->setHighlighted (highlight);
  205. }
  206. void SystemTrayIconComponent::showInfoBubble (const String& /*title*/, const String& /*content*/)
  207. {
  208. // xxx Not implemented!
  209. }
  210. void SystemTrayIconComponent::hideInfoBubble()
  211. {
  212. // xxx Not implemented!
  213. }
  214. void* SystemTrayIconComponent::getNativeHandle() const
  215. {
  216. return pimpl != nullptr ? pimpl->statusItem : nullptr;
  217. }
  218. void SystemTrayIconComponent::showDropdownMenu (const PopupMenu& menu)
  219. {
  220. if (pimpl != nullptr)
  221. pimpl->showMenu (menu);
  222. }